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

Breaking: throw syntax error on incomplete variable declarations (fixes #531) #547

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
"collectCoverage": true,
"coverageReporters": [
"text-summary"
],
"collectCoverageFrom": [
Copy link
Member Author

@mysticatea mysticatea Nov 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this configuration of jest because jest threw a syntax error on new tests/fixtures/basics/solo-const.src.js file while collecting coverage.

"**/*.js",
"!**/coverage/**",
"!**/node_modules/**",
"!**/tests/**",
"!**/tools/**",
"!**/Makefile.js",
"!**/test.js"
]
}
}
43 changes: 39 additions & 4 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ const astNodeTypes = require("typescript-estree").AST_NODE_TYPES;
const traverser = require("eslint/lib/util/traverser");
const visitorKeys = require("./visitor-keys");

/**
* Create a syntax error object.
* @param {ASTNode} node The node that caused the error.
* @param {string} message The error message.
* @returns {SyntaxError} The created error.
*/
function newSyntaxError(node, message) {
const error = new SyntaxError(message);
error.index = node.range[0];
error.lineNumber = node.loc.start.line;
error.column = node.loc.start.column + 1;

return error;
}

//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
Expand All @@ -23,10 +38,30 @@ exports.parseForESLint = function parseForESLint(code, options) {
const ast = parse(code, options);
traverser.traverse(ast, {
enter: node => {
if (node.type === "DeclareFunction" || node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
if (!node.body) {
node.type = `TSEmptyBody${node.type}`;
}
switch (node.type) {
// Just for backword compatibility.
case "DeclareFunction":
if (!node.body) {
node.type = `TSEmptyBody${node.type}`;
}
break;

// Function#body cannot be null in ESTree spec.
case "FunctionExpression":
case "FunctionDeclaration":
if (!node.body) {
node.type = `TSEmptyBody${node.type}`;
}
break;

// VariableDeclaration that doesn't have any declarations is syntax error.
case "VariableDeclaration":
if (node.declarations.length === 0) {
throw newSyntaxError(node, `'${node.kind}' declarations require one or more declarator(s).`);
}
break;

// no default
}
}
});
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/basics/solo-const.src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const
2 changes: 2 additions & 0 deletions tests/lib/__snapshots__/basics.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,8 @@ Object {
}
`;

exports[`basics fixtures/solo-const.src 1`] = `"'const' declarations require one or more declarator(s)."`;

exports[`basics fixtures/typeof-expression.src 1`] = `
Object {
"body": Array [
Expand Down