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

Commit 8744577

Browse files
authored
Breaking: Decorator ESTree compliance, always convert (fixes #250) (#293)
1 parent dd6404a commit 8744577

25 files changed

+1032
-402
lines changed

Makefile.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const NODE_MODULES = "./node_modules/",
3939
MAKEFILE = "./Makefile.js",
4040
/* eslint-disable no-use-before-define */
4141
JS_FILES = `${find("lib/").filter(fileType("js")).join(" ")} parser.js`,
42-
TEST_FILES = find("tests/lib/").filter(fileType("js")).join(" ");
42+
TEST_FILES = find("tests/lib/").filter(fileType("js")).join(" "),
43+
TOOLS_FILES = find("tools/").filter(fileType("js")).join(" ");
4344
/* eslint-enable no-use-before-define */
4445

4546
//------------------------------------------------------------------------------
@@ -88,6 +89,12 @@ target.lint = function() {
8889
errors++;
8990
}
9091

92+
echo("Validating JavaScript tools files");
93+
lastReturn = nodeCLI.exec("eslint", TOOLS_FILES);
94+
if (lastReturn.code !== 0) {
95+
errors++;
96+
}
97+
9198
if (errors) {
9299
exit(1);
93100
}

lib/ast-node-types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = {
3838
ContinueStatement: "ContinueStatement",
3939
DebuggerStatement: "DebuggerStatement",
4040
DeclareFunction: "DeclareFunction",
41+
Decorator: "Decorator",
4142
DoWhileStatement: "DoWhileStatement",
4243
EmptyStatement: "EmptyStatement",
4344
ExperimentalRestProperty: "ExperimentalRestProperty",
@@ -106,6 +107,7 @@ module.exports = {
106107
TSConstructorType: "TSConstructorType",
107108
TSConstructSignature: "TSConstructSignature",
108109
TSDeclareKeyword: "TSDeclareKeyword",
110+
TSEnumDeclaration: "TSEnumDeclaration",
109111
TSIndexSignature: "TSIndexSignature",
110112
TSInterfaceBody: "TSInterfaceBody",
111113
TSInterfaceDeclaration: "TSInterfaceDeclaration",

lib/convert.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,26 @@ module.exports = function convert(config) {
202202
return classImplementsNode;
203203
}
204204

205+
/**
206+
* Converts an array of TSNode decorators into an array of ESTreeNode decorators
207+
* @param {TSNode[]} decorators An array of TSNode decorators to be converted
208+
* @returns {ESTreeNode[]} an array of converted ESTreeNode decorators
209+
*/
210+
function convertDecorators(decorators) {
211+
if (!decorators || !decorators.length) {
212+
return [];
213+
}
214+
return decorators.map(decorator => {
215+
const expression = convertChild(decorator.expression);
216+
return {
217+
type: AST_NODE_TYPES.Decorator,
218+
range: [decorator.getStart(), decorator.end],
219+
loc: nodeUtils.getLoc(decorator, ast),
220+
expression
221+
};
222+
});
223+
}
224+
205225
/**
206226
* For nodes that are copied directly from the TypeScript AST into
207227
* ESTree mostly as-is. The only difference is the addition of a type
@@ -232,6 +252,11 @@ module.exports = function convert(config) {
232252
result.typeParameters = (node.typeParameters)
233253
? convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters)
234254
: null;
255+
} else if (key === "decorators") {
256+
const decorators = convertDecorators(node.decorators);
257+
if (decorators && decorators.length) {
258+
result.decorators = decorators;
259+
}
235260
} else {
236261
if (Array.isArray(node[key])) {
237262
result[key] = node[key].map(convertChild);
@@ -276,18 +301,6 @@ module.exports = function convert(config) {
276301
return tagNameToken;
277302
}
278303

279-
/**
280-
* Converts an array of TSNode decorators into an array of ESTreeNode decorators
281-
* @param {TSNode[]} decorators An array of TSNode decorators to be converted
282-
* @returns {ESTreeNode[]} an array of converted ESTreeNode decorators
283-
*/
284-
function convertDecorators(decorators) {
285-
if (!decorators || !decorators.length) {
286-
return [];
287-
}
288-
return decorators.map(decorator => convertChild(decorator.expression));
289-
}
290-
291304
/**
292305
* The core of the conervsion logic:
293306
* Identify and convert each relevant TypeScript SyntaxKind

tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-factory-instance-member.result.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,26 +203,26 @@ module.exports = {
203203
"accessibility": null,
204204
"decorators": [
205205
{
206-
"type": "CallExpression",
206+
"type": "Decorator",
207207
"range": [
208-
19,
208+
18,
209209
38
210210
],
211211
"loc": {
212212
"start": {
213213
"line": 2,
214-
"column": 5
214+
"column": 4
215215
},
216216
"end": {
217217
"line": 2,
218218
"column": 24
219219
}
220220
},
221-
"callee": {
222-
"type": "Identifier",
221+
"expression": {
222+
"type": "CallExpression",
223223
"range": [
224224
19,
225-
31
225+
38
226226
],
227227
"loc": {
228228
"start": {
@@ -231,32 +231,49 @@ module.exports = {
231231
},
232232
"end": {
233233
"line": 2,
234-
"column": 17
234+
"column": 24
235235
}
236236
},
237-
"name": "configurable"
238-
},
239-
"arguments": [
240-
{
241-
"type": "Literal",
237+
"callee": {
238+
"type": "Identifier",
242239
"range": [
243-
32,
244-
37
240+
19,
241+
31
245242
],
246243
"loc": {
247244
"start": {
248245
"line": 2,
249-
"column": 18
246+
"column": 5
250247
},
251248
"end": {
252249
"line": 2,
253-
"column": 23
250+
"column": 17
254251
}
255252
},
256-
"value": false,
257-
"raw": "false"
258-
}
259-
]
253+
"name": "configurable"
254+
},
255+
"arguments": [
256+
{
257+
"type": "Literal",
258+
"range": [
259+
32,
260+
37
261+
],
262+
"loc": {
263+
"start": {
264+
"line": 2,
265+
"column": 18
266+
},
267+
"end": {
268+
"line": 2,
269+
"column": 23
270+
}
271+
},
272+
"value": false,
273+
"raw": "false"
274+
}
275+
]
276+
}
260277
}
261278
]
262279
}
@@ -644,4 +661,4 @@ module.exports = {
644661
}
645662
}
646663
]
647-
};
664+
};

tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-factory-static-member.result.js

Lines changed: 80 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -203,26 +203,26 @@ module.exports = {
203203
"accessibility": null,
204204
"decorators": [
205205
{
206-
"type": "CallExpression",
206+
"type": "Decorator",
207207
"range": [
208-
19,
208+
18,
209209
37
210210
],
211211
"loc": {
212212
"start": {
213213
"line": 2,
214-
"column": 5
214+
"column": 4
215215
},
216216
"end": {
217217
"line": 2,
218218
"column": 23
219219
}
220220
},
221-
"callee": {
222-
"type": "Identifier",
221+
"expression": {
222+
"type": "CallExpression",
223223
"range": [
224224
19,
225-
22
225+
37
226226
],
227227
"loc": {
228228
"start": {
@@ -231,50 +231,50 @@ module.exports = {
231231
},
232232
"end": {
233233
"line": 2,
234-
"column": 8
234+
"column": 23
235235
}
236236
},
237-
"name": "foo"
238-
},
239-
"arguments": [
240-
{
241-
"type": "ObjectExpression",
237+
"callee": {
238+
"type": "Identifier",
242239
"range": [
243-
23,
244-
36
240+
19,
241+
22
245242
],
246243
"loc": {
247244
"start": {
248245
"line": 2,
249-
"column": 9
246+
"column": 5
250247
},
251248
"end": {
252249
"line": 2,
253-
"column": 22
250+
"column": 8
254251
}
255252
},
256-
"properties": [
257-
{
258-
"type": "Property",
259-
"range": [
260-
25,
261-
34
262-
],
263-
"loc": {
264-
"start": {
265-
"line": 2,
266-
"column": 11
267-
},
268-
"end": {
269-
"line": 2,
270-
"column": 20
271-
}
253+
"name": "foo"
254+
},
255+
"arguments": [
256+
{
257+
"type": "ObjectExpression",
258+
"range": [
259+
23,
260+
36
261+
],
262+
"loc": {
263+
"start": {
264+
"line": 2,
265+
"column": 9
272266
},
273-
"key": {
274-
"type": "Identifier",
267+
"end": {
268+
"line": 2,
269+
"column": 22
270+
}
271+
},
272+
"properties": [
273+
{
274+
"type": "Property",
275275
"range": [
276276
25,
277-
28
277+
34
278278
],
279279
"loc": {
280280
"start": {
@@ -283,38 +283,55 @@ module.exports = {
283283
},
284284
"end": {
285285
"line": 2,
286-
"column": 14
286+
"column": 20
287287
}
288288
},
289-
"name": "baz"
290-
},
291-
"value": {
292-
"type": "Literal",
293-
"range": [
294-
30,
295-
34
296-
],
297-
"loc": {
298-
"start": {
299-
"line": 2,
300-
"column": 16
289+
"key": {
290+
"type": "Identifier",
291+
"range": [
292+
25,
293+
28
294+
],
295+
"loc": {
296+
"start": {
297+
"line": 2,
298+
"column": 11
299+
},
300+
"end": {
301+
"line": 2,
302+
"column": 14
303+
}
301304
},
302-
"end": {
303-
"line": 2,
304-
"column": 20
305-
}
305+
"name": "baz"
306306
},
307-
"value": true,
308-
"raw": "true"
309-
},
310-
"computed": false,
311-
"method": false,
312-
"shorthand": false,
313-
"kind": "init"
314-
}
315-
]
316-
}
317-
]
307+
"value": {
308+
"type": "Literal",
309+
"range": [
310+
30,
311+
34
312+
],
313+
"loc": {
314+
"start": {
315+
"line": 2,
316+
"column": 16
317+
},
318+
"end": {
319+
"line": 2,
320+
"column": 20
321+
}
322+
},
323+
"value": true,
324+
"raw": "true"
325+
},
326+
"computed": false,
327+
"method": false,
328+
"shorthand": false,
329+
"kind": "init"
330+
}
331+
]
332+
}
333+
]
334+
}
318335
}
319336
]
320337
}

0 commit comments

Comments
 (0)