question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error when using string to symbol hash key

See original GitHub issue

Metadata

  • Ruby version: ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin17]
  • @prettier/plugin-ruby version: 0.10.0

Input

{ 'a': 1 }

Current output

[error] a.rb: TypeError: Cannot read property 'type' of undefined
[error]     at propagateBreaksOnEnterFn (prettier/bin-prettier.js:12581:13)
[error]     at traverseDoc (prettier/bin-prettier.js:12439:11)
[error]     at Object.propagateBreaks (prettier/bin-prettier.js:12606:3)
[error]     at printAstToDoc (prettier/bin-prettier.js:13560:14)
[error]     at coreFormat (prettier/bin-prettier.js:13871:16)
[error]     at format (prettier/bin-prettier.js:14117:73)
[error]     at formatWithCursor (prettier/bin-prettier.js:14133:12)
[error]     at Object.formatWithCursor (prettier/bin-prettier.js:42401:15)
[error]     at format$1 (prettier/bin-prettier.js:43770:21)
[error]     at prettier/bin-prettier.js:43965:16

Expected output

{ a: 1 }
# or 'a': 1 as what's most likely is that I couldn't use a: 1 when the `a` in question, e.g. `$a`

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
deecewancommented, Apr 11, 2019

I just fixed it - got a PR coming very soon.

1reaction
deecewancommented, Apr 11, 2019

this is the dyna_symbol handler in v0.9.1

  dyna_symbol: (path, opts, print) => concat([
    ":\"",
    concat(path.call(print, "body", 0)),
    "\""
  ]),

looks like it’s trying to enforce the usage of a double-quote in that version, and in the latest version, it’s trying to maintain the quote choice that was used?

okay, digging a little deeper. There were some additions in https://github.com/prettier/plugin-ruby/commit/bf11b5e90753ca918baddee07a3c8020d8ef40d2#diff-db8f78209fc0a7d78756881ed982a3c3R245.

Here is the path.getValue() for the new test I added with the string symbol:

{
  "type": "dyna_symbol",
  "body": [
    {
      "type": "string",
      "body": [
        {
          "type": "@tstring_content",
          "body": "test",
          "start": 4,
          "end": 4
        }
      ],
      "start": 4,
      "end": 4
    }
  ],
  "start": 4,
  "end": 4
}

Here is the path.getValue() for the existing string tests:

{
  "type": "dyna_symbol",
  "body": [
    {
      "type": "xstring",
      "body": [
        {
          "type": "@tstring_content",
          "body": "abc",
          "start": 62,
          "end": 62
        },
        {
          "type": "string_embexpr",
          "body": [
            {
              "type": "stmts",
              "body": [
                {
                  "type": "vcall",
                  "body": [
                    {
                      "type": "@ident",
                      "body": "abc",
                      "start": 62,
                      "end": 62
                    }
                  ],
                  "start": 62,
                  "end": 62
                }
              ],
              "start": 62,
              "end": 62
            }
          ],
          "start": 62,
          "end": 62
        },
        {
          "type": "@tstring_content",
          "body": "abc",
          "start": 62,
          "end": 62
        }
      ],
      "start": 62,
      "end": 62,
      "quote": "\""
    }
  ],
  "start": 62,
  "end": 62
}

in one case, it’s an xstring. Inside the hash, tho, it’s just a string

Read more comments on GitHub >

github_iconTop Results From Across the Web

ruby - Hash key access via symbol not string - Stack Overflow
This means that accessing table via :key is different from 'key' . Why? How do I convert table to make it work with...
Read more >
How to access a Hash either string or symbol keys
These two examples show that you need to know the hash key's structure, whether it's a string or a symbol key, to access...
Read more >
Intention "Convert string to symbol" doesn't appear ... - YouTrack
Intention "Convert string to symbol" doesn't appear for the string keys in hash. RM-119.20. Steps to reproduce: 1. Use this code(note that hashes...
Read more >
Avoid silent failures & confusions in Ruby Hash
It's just confusing and easy to make mistakes about which type of keys (String vs Symbol) an existing hash is using if.
Read more >
Hash - Rails API
Destructively converts all keys to symbols, as long as they respond to to_sym . This includes the keys from the root hash and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found