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

Breaking: Decorator ESTree compliance, always convert (fixes #250) #293

Merged
merged 1 commit into from
May 28, 2017
Merged
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: 8 additions & 1 deletion Makefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const NODE_MODULES = "./node_modules/",
MAKEFILE = "./Makefile.js",
/* eslint-disable no-use-before-define */
JS_FILES = `${find("lib/").filter(fileType("js")).join(" ")} parser.js`,
TEST_FILES = find("tests/lib/").filter(fileType("js")).join(" ");
TEST_FILES = find("tests/lib/").filter(fileType("js")).join(" "),
TOOLS_FILES = find("tools/").filter(fileType("js")).join(" ");
/* eslint-enable no-use-before-define */

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -88,6 +89,12 @@ target.lint = function() {
errors++;
}

echo("Validating JavaScript tools files");
lastReturn = nodeCLI.exec("eslint", TOOLS_FILES);
if (lastReturn.code !== 0) {
errors++;
}

if (errors) {
exit(1);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/ast-node-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = {
ContinueStatement: "ContinueStatement",
DebuggerStatement: "DebuggerStatement",
DeclareFunction: "DeclareFunction",
Decorator: "Decorator",
DoWhileStatement: "DoWhileStatement",
EmptyStatement: "EmptyStatement",
ExperimentalRestProperty: "ExperimentalRestProperty",
Expand Down Expand Up @@ -106,6 +107,7 @@ module.exports = {
TSConstructorType: "TSConstructorType",
TSConstructSignature: "TSConstructSignature",
TSDeclareKeyword: "TSDeclareKeyword",
TSEnumDeclaration: "TSEnumDeclaration",
TSIndexSignature: "TSIndexSignature",
TSInterfaceBody: "TSInterfaceBody",
TSInterfaceDeclaration: "TSInterfaceDeclaration",
Expand Down
37 changes: 25 additions & 12 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ module.exports = function convert(config) {
return classImplementsNode;
}

/**
* Converts an array of TSNode decorators into an array of ESTreeNode decorators
* @param {TSNode[]} decorators An array of TSNode decorators to be converted
* @returns {ESTreeNode[]} an array of converted ESTreeNode decorators
*/
function convertDecorators(decorators) {
if (!decorators || !decorators.length) {
return [];
}
return decorators.map(decorator => {
const expression = convertChild(decorator.expression);
return {
type: AST_NODE_TYPES.Decorator,
range: [decorator.getStart(), decorator.end],
loc: nodeUtils.getLoc(decorator, ast),
expression
};
});
}

/**
* For nodes that are copied directly from the TypeScript AST into
* ESTree mostly as-is. The only difference is the addition of a type
Expand Down Expand Up @@ -232,6 +252,11 @@ module.exports = function convert(config) {
result.typeParameters = (node.typeParameters)
? convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters)
: null;
} else if (key === "decorators") {
const decorators = convertDecorators(node.decorators);
if (decorators && decorators.length) {
result.decorators = decorators;
}
} else {
if (Array.isArray(node[key])) {
result[key] = node[key].map(convertChild);
Expand Down Expand Up @@ -276,18 +301,6 @@ module.exports = function convert(config) {
return tagNameToken;
}

/**
* Converts an array of TSNode decorators into an array of ESTreeNode decorators
* @param {TSNode[]} decorators An array of TSNode decorators to be converted
* @returns {ESTreeNode[]} an array of converted ESTreeNode decorators
*/
function convertDecorators(decorators) {
if (!decorators || !decorators.length) {
return [];
}
return decorators.map(decorator => convertChild(decorator.expression));
}

/**
* The core of the conervsion logic:
* Identify and convert each relevant TypeScript SyntaxKind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,26 +203,26 @@ module.exports = {
"accessibility": null,
"decorators": [
{
"type": "CallExpression",
"type": "Decorator",
"range": [
19,
18,
38
],
"loc": {
"start": {
"line": 2,
"column": 5
"column": 4
},
"end": {
"line": 2,
"column": 24
}
},
"callee": {
"type": "Identifier",
"expression": {
"type": "CallExpression",
"range": [
19,
31
38
],
"loc": {
"start": {
Expand All @@ -231,32 +231,49 @@ module.exports = {
},
"end": {
"line": 2,
"column": 17
"column": 24
}
},
"name": "configurable"
},
"arguments": [
{
"type": "Literal",
"callee": {
"type": "Identifier",
"range": [
32,
37
19,
31
],
"loc": {
"start": {
"line": 2,
"column": 18
"column": 5
},
"end": {
"line": 2,
"column": 23
"column": 17
}
},
"value": false,
"raw": "false"
}
]
"name": "configurable"
},
"arguments": [
{
"type": "Literal",
"range": [
32,
37
],
"loc": {
"start": {
"line": 2,
"column": 18
},
"end": {
"line": 2,
"column": 23
}
},
"value": false,
"raw": "false"
}
]
}
}
]
}
Expand Down Expand Up @@ -644,4 +661,4 @@ module.exports = {
}
}
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -203,26 +203,26 @@ module.exports = {
"accessibility": null,
"decorators": [
{
"type": "CallExpression",
"type": "Decorator",
"range": [
19,
18,
37
],
"loc": {
"start": {
"line": 2,
"column": 5
"column": 4
},
"end": {
"line": 2,
"column": 23
}
},
"callee": {
"type": "Identifier",
"expression": {
"type": "CallExpression",
"range": [
19,
22
37
],
"loc": {
"start": {
Expand All @@ -231,50 +231,50 @@ module.exports = {
},
"end": {
"line": 2,
"column": 8
"column": 23
}
},
"name": "foo"
},
"arguments": [
{
"type": "ObjectExpression",
"callee": {
"type": "Identifier",
"range": [
23,
36
19,
22
],
"loc": {
"start": {
"line": 2,
"column": 9
"column": 5
},
"end": {
"line": 2,
"column": 22
"column": 8
}
},
"properties": [
{
"type": "Property",
"range": [
25,
34
],
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 20
}
"name": "foo"
},
"arguments": [
{
"type": "ObjectExpression",
"range": [
23,
36
],
"loc": {
"start": {
"line": 2,
"column": 9
},
"key": {
"type": "Identifier",
"end": {
"line": 2,
"column": 22
}
},
"properties": [
{
"type": "Property",
"range": [
25,
28
34
],
"loc": {
"start": {
Expand All @@ -283,38 +283,55 @@ module.exports = {
},
"end": {
"line": 2,
"column": 14
"column": 20
}
},
"name": "baz"
},
"value": {
"type": "Literal",
"range": [
30,
34
],
"loc": {
"start": {
"line": 2,
"column": 16
"key": {
"type": "Identifier",
"range": [
25,
28
],
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 14
}
},
"end": {
"line": 2,
"column": 20
}
"name": "baz"
},
"value": true,
"raw": "true"
},
"computed": false,
"method": false,
"shorthand": false,
"kind": "init"
}
]
}
]
"value": {
"type": "Literal",
"range": [
30,
34
],
"loc": {
"start": {
"line": 2,
"column": 16
},
"end": {
"line": 2,
"column": 20
}
},
"value": true,
"raw": "true"
},
"computed": false,
"method": false,
"shorthand": false,
"kind": "init"
}
]
}
]
}
}
]
}
Expand Down
Loading