Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computed properties failing #2120

Closed
sscotth opened this issue Mar 21, 2015 · 7 comments
Closed

Computed properties failing #2120

sscotth opened this issue Mar 21, 2015 · 7 comments
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@sscotth
Copy link

sscotth commented Mar 21, 2015

// ecmaFeatures:
//   objectLiteralComputedProperties:  true  # enable computed object literal property names
var suffix = " name";

var person = {
    ["first" + suffix]: "Nicholas",
    ["last" + suffix]: "Zakas"
};

console.log(person["first name"]);      // "Nicholas"
console.log(person["last name"]);       // "Zakas"
/Users/scott/.nvm/versions/io.js/v1.6.1/lib/node_modules/eslint/lib/rules/key-spacing.js:38
    return (key.type === "Identifier" ? key.name : key.raw).length;
                                                           ^
TypeError: Cannot read property 'length' of undefined
    at getKeyWidth (/Users/scott/.nvm/versions/io.js/v1.6.1/lib/node_modules/eslint/lib/rules/key-spacing.js:38:60)
    at Array.map (native)
    at verifyAlignment (/Users/scott/.nvm/versions/io.js/v1.6.1/lib/node_modules/eslint/lib/rules/key-spacing.js:158:33)
    at /Users/scott/.nvm/versions/io.js/v1.6.1/lib/node_modules/eslint/lib/rules/key-spacing.js:201:21
    at Array.forEach (native)
    at EventEmitter.ObjectExpression (/Users/scott/.nvm/versions/io.js/v1.6.1/lib/node_modules/eslint/lib/rules/key-spacing.js:200:26)
    at emitOne (events.js:82:20)
    at EventEmitter.emit (events.js:166:7)
    at Controller.controller.traverse.enter (/Users/scott/.nvm/versions/io.js/v1.6.1/lib/node_modules/eslint/lib/eslint.js:735:25)
    at Controller.__execute (/Users/scott/.nvm/versions/io.js/v1.6.1/lib/node_modules/eslint/node_modules/estraverse/estraverse.js:393:31)
@michaelficarra
Copy link
Member

This is in the key-spacing rule. As a workaround, you should disable that rule.

@nzakas nzakas added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Mar 21, 2015
@nzakas
Copy link
Member

nzakas commented Mar 21, 2015

We will get this fixed soon.

@gyandeeps
Copy link
Member

I think it gives this error on iojs only.
On node 0.12.0 with eslint 0.17.1, it doesnt throw any error but I think this rule doesnt work for objectLiteralComputedProperties property of ecmaFeatures.

.eslintrc

{
    "env": {
        "node": true
    },
    "ecmaFeatures": {
        "objectLiteralComputedProperties": true
    },
    "rules": {
        "key-spacing": [2, {
            "beforeColon": false,
            "afterColon": true
        }]
    }
}

app.js

var suffix = " name";

var person = {
    ["first" + suffix] : "Nicholas",
    ["last" + suffix]: "Zakas"
};

console.log(person["first name"]);      // "Nicholas"
console.log(person["last name"]);       // "Zakas"

I would expect 1 error under this scenario.

UPADTE:

It throws error on node 0.12.0 also but only when "align": "colon" property is set inside the rule.

So this tells us that there is potentially 2 problems.

@nzakas
Copy link
Member

nzakas commented Mar 23, 2015

@gyandeeps what makes you think this is related to Node version? The problem is that we're expecting an identifier or string.

@gyandeeps
Copy link
Member

@nzakas Yup, it throws error regardless of the node version. I did update my comment.
I think we have 2 potential problem (take the code in this issue as example):

  • When we use the rule with "align": "colon" option then it throws the error as mentioned in this issue.
  • Use this rule but without the option "align": "colon" and then i would expect 1 error (currently it doesn't give an error message). For this use the setup as described below.

.eslintrc

{
    "env": {
        "node": true
    },
    "ecmaFeatures": {
        "objectLiteralComputedProperties": true
    },
    "rules": {
        "key-spacing": [2, {
            "beforeColon": false,
            "afterColon": true
        }]
    }
}

app.js

var suffix = " name";

var person = {
    ["first" + suffix] : "Nicholas",
    ["last" + suffix]: "Zakas"
};

console.log(person["first name"]);      // "Nicholas"
console.log(person["last name"]);       // "Zakas"

I would expect 1 error under this scenario.

@nzakas
Copy link
Member

nzakas commented Mar 23, 2015

Yeah, we're just not handling computed properties at all, so everything is haywire.

@btmills
Copy link
Member

btmills commented Mar 26, 2015

Working on this.

nzakas added a commit that referenced this issue Mar 27, 2015
Fix: key-spacing crash on computed properties (fixes #2120)
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

5 participants