@@ -13,6 +13,21 @@ const astNodeTypes = require("typescript-estree").AST_NODE_TYPES;
13
13
const traverser = require ( "eslint/lib/util/traverser" ) ;
14
14
const visitorKeys = require ( "./visitor-keys" ) ;
15
15
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
+
16
31
//------------------------------------------------------------------------------
17
32
// Public
18
33
//------------------------------------------------------------------------------
@@ -23,10 +38,30 @@ exports.parseForESLint = function parseForESLint(code, options) {
23
38
const ast = parse ( code , options ) ;
24
39
traverser . traverse ( ast , {
25
40
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
30
65
}
31
66
}
32
67
} ) ;
0 commit comments