Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 81761d5

Browse files
committed
Breaking: throw syntax error on solo const keyword (fixes #531)
1 parent a9d932a commit 81761d5

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@
6363
"collectCoverage": true,
6464
"coverageReporters": [
6565
"text-summary"
66+
],
67+
"collectCoverageFrom": [
68+
"**/*.js",
69+
"!**/coverage/**",
70+
"!**/node_modules/**",
71+
"!**/tests/**",
72+
"!**/tools/**",
73+
"!**/Makefile.js",
74+
"!**/test.js"
6675
]
6776
}
6877
}

parser.js

+39-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ const astNodeTypes = require("typescript-estree").AST_NODE_TYPES;
1313
const traverser = require("eslint/lib/util/traverser");
1414
const visitorKeys = require("./visitor-keys");
1515

16+
/**
17+
* Create a syntax error object.
18+
* @param {ASTNode} node The node that caused the error.
19+
* @param {string} message The error message.
20+
* @returns {SyntaxError} The created error.
21+
*/
22+
function newSyntaxError(node, message) {
23+
const error = new SyntaxError(message);
24+
error.index = node.range[0];
25+
error.lineNumber = node.loc.start.line;
26+
error.column = node.loc.start.column + 1;
27+
28+
return error;
29+
}
30+
1631
//------------------------------------------------------------------------------
1732
// Public
1833
//------------------------------------------------------------------------------
@@ -23,10 +38,30 @@ exports.parseForESLint = function parseForESLint(code, options) {
2338
const ast = parse(code, options);
2439
traverser.traverse(ast, {
2540
enter: node => {
26-
if (node.type === "DeclareFunction" || node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
27-
if (!node.body) {
28-
node.type = `TSEmptyBody${node.type}`;
29-
}
41+
switch (node.type) {
42+
// Just for backword compatibility.
43+
case "DeclareFunction":
44+
if (!node.body) {
45+
node.type = `TSEmptyBody${node.type}`;
46+
}
47+
break;
48+
49+
// Function#body cannot be null in ESTree spec.
50+
case "FunctionExpression":
51+
case "FunctionDeclaration":
52+
if (!node.body) {
53+
node.type = `TSEmptyBody${node.type}`;
54+
}
55+
break;
56+
57+
// VariableDeclaration that doesn't have any declarations is syntax error.
58+
case "VariableDeclaration":
59+
if (node.declarations.length === 0) {
60+
throw newSyntaxError(node, `'${node.kind}' declarations require one or more declarator(s).`);
61+
}
62+
break;
63+
64+
// no default
3065
}
3166
}
3267
});
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const

tests/lib/__snapshots__/basics.js.snap

+2
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,8 @@ Object {
21622162
}
21632163
`;
21642164

2165+
exports[`basics fixtures/solo-const.src 1`] = `"'const' declarations require one or more declarator(s)."`;
2166+
21652167
exports[`basics fixtures/typeof-expression.src 1`] = `
21662168
Object {
21672169
"body": Array [

0 commit comments

Comments
 (0)