Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

"requireSpaceAfterKeywords" should ignore object keys #83

Closed
EvHaus opened this issue Dec 2, 2013 · 1 comment · Fixed by #206
Closed

"requireSpaceAfterKeywords" should ignore object keys #83

EvHaus opened this issue Dec 2, 2013 · 1 comment · Fixed by #206
Labels

Comments

@EvHaus
Copy link

EvHaus commented Dec 2, 2013

The following rule:

"requireSpaceAfterKeywords": ['for']

Will complain about the following code:

{for: 'bar'}

I believe this is incorrect. Object keys should be ignored for "requireSpaceAfterKeywords"
@Gissues:{"order":50,"status":"done"}

@keithamus
Copy link

The problem here lies within the use of Esprima's "tokens" vs the full "body" tree object. For a simple test-case we can see:

test.js

x = { for: 'bar' };

The "for" part in the tokens array produces:

{
    "type": "Keyword",
    "value": "for",
    "range": [
        6,
        9
    ],
    "loc": {
        "start": {
            "line": 1,
            "column": 6
        },
        "end": {
            "line": 1,
            "column": 9
        }
    }
},

but in the "body" property, it produces this ouput:

"key": {
    "type": "Identifier",
    "name": "for",
    "range": [
        6,
        9
    ],
    "loc": {
        "start": {
            "line": 1,
            "column": 6
        },
        "end": {
            "line": 1,
            "column": 9
        }
    }
},

Because the tokens array (perhaps incorrectly?) puts the "type" as "Keyword", which is no different to a real for token, JSCS cannot tell it is a Keyword.

Possible Solutions

  • Move JSCS to use the "body" tree object, so it can more accurately summise what each node type is
  • Continue using the "tokens" array, but add a lookbehind in the rule, to check if the "Keyword" node is inside an object's "Punctuation" node
  • Talk to Esprima devs or raise an issue about the "tokens" array labelling object properties as "Keyword"s instead of "Identifier"s

markelog added a commit to markelog/node-jscs that referenced this issue Jan 22, 2014
Replaces type of reserved keywords if the are object keys.
See https://code.google.com/p/esprima/issues/detail?id=481 for more info

Fixes jscs-dev#83, jscs-dev#180
markelog added a commit to markelog/node-jscs that referenced this issue Jan 22, 2014
Replaces type of reserved keywords if the are object keys.
See https://code.google.com/p/esprima/issues/detail?id=481 for more info

Fixes jscs-dev#83, jscs-dev#180
markelog added a commit to markelog/node-jscs that referenced this issue Jan 22, 2014
Replaces type of reserved keywords if the are object keys.
See https://code.google.com/p/esprima/issues/detail?id=481 for more info

Fixes jscs-dev#83, jscs-dev#180
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants