Skip to content

Commit

Permalink
Fix: Handle number literals correctly in `no-whitespace-before-proper…
Browse files Browse the repository at this point in the history
…ty` (#7185)
  • Loading branch information
not-an-aardvark authored and nzakas committed Sep 20, 2016
1 parent 462a3f7 commit 8e1fee1
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/ast-utils.js
Expand Up @@ -715,5 +715,27 @@ module.exports = {
}

return directives;
},


/**
* Determines whether this node is a decimal integer literal. If a node is a decimal integer literal, a dot added
after the node will be parsed as a decimal point, rather than a property-access dot.
* @param {ASTNode} node - The node to check.
* @returns {boolean} `true` if this node is a decimal integer.
* @example
*
* 5 // true
* 5. // false
* 5.0 // false
* 05 // false
* 0x5 // false
* 0b101 // false
* 0o5 // false
* 5e0 // false
* '5' // false
*/
isDecimalInteger(node) {
return node.type === "Literal" && typeof node.value === "number" && /^(0|[1-9]\d*)$/.test(node.raw);
}
};
6 changes: 6 additions & 0 deletions lib/rules/no-whitespace-before-property.js
Expand Up @@ -62,6 +62,12 @@ module.exports = {
propName: sourceCode.getText(node.property)
},
fix(fixer) {
if (!node.computed && astUtils.isDecimalInteger(node.object)) {

// If the object is a number literal, fixing it to something like 5.toString() would cause a SyntaxError.
// Don't fix this case.
return null;
}
return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], replacementText);
}
});
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/ast-utils.js
Expand Up @@ -646,4 +646,24 @@ describe("ast-utils", function() {
assert.equal(result[1].expression.value, "use asm");
});
});

describe("isDecimalInteger", function() {
const expectedResults = {
5: true,
0: true,
"5.": false,
"5.0": false,
"05": false,
"0x5": false,
"5e0": false,
"5e-0": false,
"'5'": false
};

Object.keys(expectedResults).forEach(key => {
it(`should return ${expectedResults[key]} for ${key}`, function() {
assert.strictEqual(astUtils.isDecimalInteger(espree.parse(key).body[0].expression), expectedResults[key]);
});
});
});
});
48 changes: 47 additions & 1 deletion tests/lib/rules/no-whitespace-before-property.js
Expand Up @@ -98,7 +98,8 @@ ruleTester.run("no-whitespace-before-property", rule, {
"foo[0][[('baz')]]",
"foo[bar.baz('qux')]",
"foo[(bar.baz() + 0) + qux]",
"foo['bar ' + 1 + ' baz']"
"foo['bar ' + 1 + ' baz']",
"5['toExponential']()"
],

invalid: [
Expand Down Expand Up @@ -507,6 +508,51 @@ ruleTester.run("no-whitespace-before-property", rule, {
code: "foo ['bar ' + 1 + ' baz']",
output: "foo['bar ' + 1 + ' baz']",
errors: ["Unexpected whitespace before property 'bar ' + 1 + ' baz'."]
},
{
code: "5 .toExponential()",
output: "5 .toExponential()", // This case is not fixed; can't be sure whether 5..toExponential or (5).toExponential is preferred
errors: ["Unexpected whitespace before property toExponential."]
},
{
code: "5 .toExponential()",
output: "5 .toExponential()", // Not fixed
errors: ["Unexpected whitespace before property toExponential."]
},
{
code: "5. .toExponential()",
output: "5..toExponential()",
errors: ["Unexpected whitespace before property toExponential."]
},
{
code: "5.0 .toExponential()",
output: "5.0.toExponential()",
errors: ["Unexpected whitespace before property toExponential."]
},
{
code: "0x5 .toExponential()",
output: "0x5.toExponential()",
errors: ["Unexpected whitespace before property toExponential."]
},
{
code: "5e0 .toExponential()",
output: "5e0.toExponential()",
errors: ["Unexpected whitespace before property toExponential."]
},
{
code: "5e-0 .toExponential()",
output: "5e-0.toExponential()",
errors: ["Unexpected whitespace before property toExponential."]
},
{
code: "5 ['toExponential']()",
output: "5['toExponential']()",
errors: ["Unexpected whitespace before property 'toExponential'."]
},
{
code: "05 .toExponential()",
output: "05.toExponential()",
errors: ["Unexpected whitespace before property toExponential."]
}
]
});

0 comments on commit 8e1fee1

Please sign in to comment.