From 503de722540dca3cc5ef9084d5b120ba7c974c58 Mon Sep 17 00:00:00 2001 From: Ika Date: Tue, 24 Apr 2018 10:56:13 +0800 Subject: [PATCH 1/3] New: support TaggedTemplateExpression typeArguments (#469) --- lib/convert.js | 3 + package.json | 2 +- tests/ast-alignment/fixtures-to-test.js | 10 +- ...-template-expression-type-arguments.src.ts | 1 + tests/lib/__snapshots__/typescript.js.snap | 281 ++++++++++++++++++ 5 files changed, 295 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/typescript/expressions/tagged-template-expression-type-arguments.src.ts diff --git a/lib/convert.js b/lib/convert.js index 225ae37..ab67985 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -1237,6 +1237,9 @@ module.exports = function convert(config) { case SyntaxKind.TaggedTemplateExpression: Object.assign(result, { type: AST_NODE_TYPES.TaggedTemplateExpression, + typeParameters: (node.typeArguments) + ? convertTypeArgumentsToTypeParameters(node.typeArguments) + : undefined, tag: convertChild(node.tag), quasi: convertChild(node.template) }); diff --git a/package.json b/package.json index 406e1d9..1d0ff3d 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "npm-license": "0.3.3", "shelljs": "0.8.1", "shelljs-nodecli": "0.1.1", - "typescript": "~2.8.1" + "typescript": "2.9.0-dev.20180421" }, "keywords": [ "ast", diff --git a/tests/ast-alignment/fixtures-to-test.js b/tests/ast-alignment/fixtures-to-test.js index a1554d3..777cc07 100644 --- a/tests/ast-alignment/fixtures-to-test.js +++ b/tests/ast-alignment/fixtures-to-test.js @@ -439,7 +439,15 @@ let fixturePatternConfigsToTest = [ createFixturePatternConfigFor("typescript/decorators/parameter-decorators", { fileType: "ts" }), createFixturePatternConfigFor("typescript/decorators/property-decorators", { fileType: "ts" }), - createFixturePatternConfigFor("typescript/expressions", { fileType: "ts" }), + createFixturePatternConfigFor("typescript/expressions", { + fileType: "ts", + ignore: [ + /** + * currently babylon not supported + */ + "tagged-template-expression-type-arguments" + ] + }), createFixturePatternConfigFor("typescript/errorRecovery", { fileType: "ts", diff --git a/tests/fixtures/typescript/expressions/tagged-template-expression-type-arguments.src.ts b/tests/fixtures/typescript/expressions/tagged-template-expression-type-arguments.src.ts new file mode 100644 index 0000000..aff1105 --- /dev/null +++ b/tests/fixtures/typescript/expressions/tagged-template-expression-type-arguments.src.ts @@ -0,0 +1 @@ +foo`baz`; \ No newline at end of file diff --git a/tests/lib/__snapshots__/typescript.js.snap b/tests/lib/__snapshots__/typescript.js.snap index c6ddec7..ce46ff0 100644 --- a/tests/lib/__snapshots__/typescript.js.snap +++ b/tests/lib/__snapshots__/typescript.js.snap @@ -63681,6 +63681,287 @@ Object { } `; +exports[`typescript fixtures/expressions/tagged-template-expression-type-arguments.src 1`] = ` +Object { + "body": Array [ + Object { + "expression": Object { + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "quasi": Object { + "expressions": Array [], + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 8, + "line": 1, + }, + }, + "quasis": Array [ + Object { + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 8, + "line": 1, + }, + }, + "range": Array [ + 8, + 13, + ], + "tail": true, + "type": "TemplateElement", + "value": Object { + "cooked": "baz", + "raw": "baz", + }, + }, + ], + "range": Array [ + 8, + 13, + ], + "type": "TemplateLiteral", + }, + "range": Array [ + 0, + 13, + ], + "tag": Object { + "loc": Object { + "end": Object { + "column": 3, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "name": "foo", + "range": Array [ + 0, + 3, + ], + "type": "Identifier", + }, + "type": "TaggedTemplateExpression", + "typeParameters": Object { + "loc": Object { + "end": Object { + "column": 8, + "line": 1, + }, + "start": Object { + "column": 3, + "line": 1, + }, + }, + "params": Array [ + Object { + "loc": Object { + "end": Object { + "column": 7, + "line": 1, + }, + "start": Object { + "column": 4, + "line": 1, + }, + }, + "range": Array [ + 4, + 7, + ], + "type": "TSTypeReference", + "typeName": Object { + "loc": Object { + "end": Object { + "column": 7, + "line": 1, + }, + "start": Object { + "column": 4, + "line": 1, + }, + }, + "name": "bar", + "range": Array [ + 4, + 7, + ], + "type": "Identifier", + }, + }, + ], + "range": Array [ + 3, + 8, + ], + "type": "TSTypeParameterInstantiation", + }, + }, + "loc": Object { + "end": Object { + "column": 14, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 14, + ], + "type": "ExpressionStatement", + }, + ], + "loc": Object { + "end": Object { + "column": 14, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 14, + ], + "sourceType": "script", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 3, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 3, + ], + "type": "Identifier", + "value": "foo", + }, + Object { + "loc": Object { + "end": Object { + "column": 4, + "line": 1, + }, + "start": Object { + "column": 3, + "line": 1, + }, + }, + "range": Array [ + 3, + 4, + ], + "type": "Punctuator", + "value": "<", + }, + Object { + "loc": Object { + "end": Object { + "column": 7, + "line": 1, + }, + "start": Object { + "column": 4, + "line": 1, + }, + }, + "range": Array [ + 4, + 7, + ], + "type": "Identifier", + "value": "bar", + }, + Object { + "loc": Object { + "end": Object { + "column": 8, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "range": Array [ + 7, + 8, + ], + "type": "Punctuator", + "value": ">", + }, + Object { + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 8, + "line": 1, + }, + }, + "range": Array [ + 8, + 13, + ], + "type": "Template", + "value": "\`baz\`", + }, + Object { + "loc": Object { + "end": Object { + "column": 14, + "line": 1, + }, + "start": Object { + "column": 13, + "line": 1, + }, + }, + "range": Array [ + 13, + 14, + ], + "type": "Punctuator", + "value": ";", + }, + ], + "type": "Program", +} +`; + exports[`typescript fixtures/namespaces-and-modules/ambient-module-declaration-with-import.src 1`] = ` Object { "body": Array [ From 2960b002746c01fb9cb15bb5f4c1e7e925c6519a Mon Sep 17 00:00:00 2001 From: Ika Date: Tue, 24 Apr 2018 11:07:09 +0800 Subject: [PATCH 2/3] New: support generic JSX element (fixes #462) (#461) --- lib/convert.js | 3 + tests/ast-alignment/fixtures-to-test.js | 6 +- .../fixtures/tsx/generic-jsx-element.src.tsx | 1 + tests/lib/__snapshots__/tsx.js.snap | 422 ++++++++++++++++++ 4 files changed, 431 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/tsx/generic-jsx-element.src.tsx diff --git a/lib/convert.js b/lib/convert.js index ab67985..96e8343 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -1859,6 +1859,9 @@ module.exports = function convert(config) { case SyntaxKind.JsxOpeningElement: Object.assign(result, { type: AST_NODE_TYPES.JSXOpeningElement, + typeParameters: (node.typeArguments) + ? convertTypeArgumentsToTypeParameters(node.typeArguments) + : undefined, selfClosing: false, name: convertTypeScriptJSXTagNameToESTreeName(node.tagName), attributes: node.attributes.properties.map(convertChild) diff --git a/tests/ast-alignment/fixtures-to-test.js b/tests/ast-alignment/fixtures-to-test.js index 777cc07..bef1c79 100644 --- a/tests/ast-alignment/fixtures-to-test.js +++ b/tests/ast-alignment/fixtures-to-test.js @@ -324,7 +324,11 @@ let fixturePatternConfigsToTest = [ /** * AST difference */ - "react-typed-props" + "react-typed-props", + /** + * currently babylon not supported + */ + "generic-jsx-element" ] }), diff --git a/tests/fixtures/tsx/generic-jsx-element.src.tsx b/tests/fixtures/tsx/generic-jsx-element.src.tsx new file mode 100644 index 0000000..7a62016 --- /dev/null +++ b/tests/fixtures/tsx/generic-jsx-element.src.tsx @@ -0,0 +1 @@ + data={12} /> diff --git a/tests/lib/__snapshots__/tsx.js.snap b/tests/lib/__snapshots__/tsx.js.snap index a18c507..d78b679 100644 --- a/tests/lib/__snapshots__/tsx.js.snap +++ b/tests/lib/__snapshots__/tsx.js.snap @@ -1,5 +1,427 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`TSX fixtures/generic-jsx-element.src 1`] = ` +Object { + "body": Array [ + Object { + "expression": Object { + "children": Array [], + "closingElement": null, + "loc": Object { + "end": Object { + "column": 33, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "openingElement": Object { + "attributes": Array [ + Object { + "loc": Object { + "end": Object { + "column": 30, + "line": 1, + }, + "start": Object { + "column": 21, + "line": 1, + }, + }, + "name": Object { + "loc": Object { + "end": Object { + "column": 25, + "line": 1, + }, + "start": Object { + "column": 21, + "line": 1, + }, + }, + "name": "data", + "range": Array [ + 21, + 25, + ], + "type": "JSXIdentifier", + }, + "range": Array [ + 21, + 30, + ], + "type": "JSXAttribute", + "value": Object { + "expression": Object { + "loc": Object { + "end": Object { + "column": 29, + "line": 1, + }, + "start": Object { + "column": 27, + "line": 1, + }, + }, + "range": Array [ + 27, + 29, + ], + "raw": "12", + "type": "Literal", + "value": 12, + }, + "loc": Object { + "end": Object { + "column": 30, + "line": 1, + }, + "start": Object { + "column": 26, + "line": 1, + }, + }, + "range": Array [ + 26, + 30, + ], + "type": "JSXExpressionContainer", + }, + }, + ], + "loc": Object { + "end": Object { + "column": 33, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "name": Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "name": "MyComponent", + "range": Array [ + 1, + 12, + ], + "type": "JSXIdentifier", + }, + "range": Array [ + 0, + 33, + ], + "selfClosing": true, + "type": "JSXOpeningElement", + "typeParameters": Object { + "loc": Object { + "end": Object { + "column": 20, + "line": 1, + }, + "start": Object { + "column": 12, + "line": 1, + }, + }, + "params": Array [ + Object { + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 13, + "line": 1, + }, + }, + "range": Array [ + 13, + 19, + ], + "type": "TSNumberKeyword", + }, + ], + "range": Array [ + 12, + 20, + ], + "type": "TSTypeParameterInstantiation", + }, + }, + "range": Array [ + 0, + 33, + ], + "type": "JSXElement", + }, + "loc": Object { + "end": Object { + "column": 33, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 33, + ], + "type": "ExpressionStatement", + }, + ], + "loc": Object { + "end": Object { + "column": 0, + "line": 2, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 34, + ], + "sourceType": "script", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 1, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 1, + ], + "type": "Punctuator", + "value": "<", + }, + Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "range": Array [ + 1, + 12, + ], + "type": "JSXIdentifier", + "value": "MyComponent", + }, + Object { + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 12, + "line": 1, + }, + }, + "range": Array [ + 12, + 13, + ], + "type": "Punctuator", + "value": "<", + }, + Object { + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 13, + "line": 1, + }, + }, + "range": Array [ + 13, + 19, + ], + "type": "Identifier", + "value": "number", + }, + Object { + "loc": Object { + "end": Object { + "column": 20, + "line": 1, + }, + "start": Object { + "column": 19, + "line": 1, + }, + }, + "range": Array [ + 19, + 20, + ], + "type": "Punctuator", + "value": ">", + }, + Object { + "loc": Object { + "end": Object { + "column": 25, + "line": 1, + }, + "start": Object { + "column": 21, + "line": 1, + }, + }, + "range": Array [ + 21, + 25, + ], + "type": "JSXIdentifier", + "value": "data", + }, + Object { + "loc": Object { + "end": Object { + "column": 26, + "line": 1, + }, + "start": Object { + "column": 25, + "line": 1, + }, + }, + "range": Array [ + 25, + 26, + ], + "type": "Punctuator", + "value": "=", + }, + Object { + "loc": Object { + "end": Object { + "column": 27, + "line": 1, + }, + "start": Object { + "column": 26, + "line": 1, + }, + }, + "range": Array [ + 26, + 27, + ], + "type": "Punctuator", + "value": "{", + }, + Object { + "loc": Object { + "end": Object { + "column": 29, + "line": 1, + }, + "start": Object { + "column": 27, + "line": 1, + }, + }, + "range": Array [ + 27, + 29, + ], + "type": "Numeric", + "value": "12", + }, + Object { + "loc": Object { + "end": Object { + "column": 30, + "line": 1, + }, + "start": Object { + "column": 29, + "line": 1, + }, + }, + "range": Array [ + 29, + 30, + ], + "type": "Punctuator", + "value": "}", + }, + Object { + "loc": Object { + "end": Object { + "column": 32, + "line": 1, + }, + "start": Object { + "column": 31, + "line": 1, + }, + }, + "range": Array [ + 31, + 32, + ], + "type": "Punctuator", + "value": "/", + }, + Object { + "loc": Object { + "end": Object { + "column": 33, + "line": 1, + }, + "start": Object { + "column": 32, + "line": 1, + }, + }, + "range": Array [ + 32, + 33, + ], + "type": "Punctuator", + "value": ">", + }, + ], + "type": "Program", +} +`; + exports[`TSX fixtures/react-typed-props.src 1`] = ` Object { "body": Array [ From 9237b4ff6884d105829051d3c0c814ef3f56890e Mon Sep 17 00:00:00 2001 From: James Henry Date: Sun, 3 Jun 2018 21:52:29 -0400 Subject: [PATCH 3/3] Chore: Set latest TS version --- README.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2014732..484da02 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A parser that converts TypeScript source code into an [ESTree](https://github.co We will always endeavor to support the latest stable version of TypeScript. -The version of TypeScript currently supported by this parser is `~2.8.1`. This is reflected in the `devDependency` requirement within the package.json file, and it is what the tests will be run against. We have an open `peerDependency` requirement in order to allow for experimentation on newer/beta versions of TypeScript. +The version of TypeScript currently supported by this parser is `~2.9.1`. This is reflected in the `devDependency` requirement within the package.json file, and it is what the tests will be run against. We have an open `peerDependency` requirement in order to allow for experimentation on newer/beta versions of TypeScript. If you use a non-supported version of TypeScript, the parser will log a warning to the console. diff --git a/package.json b/package.json index 974b24d..111f40d 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "npm-license": "0.3.3", "shelljs": "0.8.1", "shelljs-nodecli": "0.1.1", - "typescript": "2.9.0-dev.20180421" + "typescript": "~2.9.1" }, "keywords": [ "ast",