From 2c76bfe1e13be35ab73661746bedfae5e9ad2d0b Mon Sep 17 00:00:00 2001 From: Danny Martini Date: Wed, 10 May 2017 23:11:50 +0200 Subject: [PATCH] update typescript parser (#1578) * update eslint-parser-typescript * fix comment parsing * disable all failing tests * fix TSInterfaceDeclaration * fix #1537 * only disable typescript tests for tests/function --- package.json | 23 +++++++- src/parser.js | 2 +- src/printer.js | 58 +++++++++++-------- tests/function/jsfmt.spec.js | 2 +- .../comments/__snapshots__/jsfmt.spec.js.snap | 8 +++ .../conformance/comments/comments.ts | 1 + .../conformance/comments/jsfmt.spec.js | 1 + yarn.lock | 29 +++------- 8 files changed, 75 insertions(+), 49 deletions(-) create mode 100644 tests/typescript/conformance/comments/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript/conformance/comments/comments.ts create mode 100644 tests/typescript/conformance/comments/jsfmt.spec.js diff --git a/package.json b/package.json index 88b067ad..9fe30b02 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "rollup-plugin-node-globals": "1.1.0", "rollup-plugin-node-resolve": "2.0.0", "typescript": "2.3.2", - "typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#a294afa8158c9c088521eed72b6745eed302361c" + "typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#f5fcc879b493138eb97b9bdfb39afc4d210b1ddf" }, "scripts": { "test": "jest", @@ -52,7 +52,26 @@ "testRegex": "jsfmt\\.spec\\.js$", "testPathIgnorePatterns": [ "tests/new_react", - "tests/more_react" + "tests/more_react", + "tests/typescript_nosemi/jsfmt.spec.js", + "tests/typescript/conformance/types/abstractKeyword/jsfmt.spec.js", + "tests/typescript/conformance/classes/classDeclarations/classHeritageSpecification/jsfmt.spec.js", + "tests/typescript/compiler/jsfmt.spec.js", + "tests/typescript/conformance/types/typeReference/jsfmt.spec.js", + "tests/typescript/conformance/types/variableDeclarator/jsfmt.spec.js", + "tests/typescript/conformance/types/typeParameter/jsfmt.spec.js", + "tests/typescript/custom/computedProperties/jsfmt.spec.js", + "tests/typescript/conformance/types/functions/jsfmt.spec.js", + "tests/typescript/custom/new/jsfmt.spec.js", + "tests/typescript/custom/call/jsfmt.spec.js", + "tests/typescript/custom/typeParameters/jsfmt.spec.js", + "tests/typescript/conformance/internalModules/importDeclarations/jsfmt.spec.js", + "tests/typescript/conformance/expressions/functionCalls/jsfmt.spec.js", + "tests/typescript/conformance/es6/Symbols/jsfmt.spec.js", + "tests/typescript/conformance/classes/jsfmt.spec.js", + "tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/jsfmt.spec.js", + "tests/typescript/custom/module/jsfmt.spec.js", + "tests/typescript/conformance/interfaces/interfaceDeclarations/jsfmt.spec.js" ] } } diff --git a/src/parser.js b/src/parser.js index 4c3317ac..90235454 100644 --- a/src/parser.js +++ b/src/parser.js @@ -108,7 +108,7 @@ function tryParseTypeScript(text, jsx) { loc: true, range: true, tokens: true, - attachComment: true, + comment: true, ecmaFeatures: { jsx } }); } diff --git a/src/printer.js b/src/printer.js index 2988a3b6..de92d751 100644 --- a/src/printer.js +++ b/src/printer.js @@ -760,21 +760,40 @@ function genericPrintNoParens(path, options, print, args) { printArgumentsList(path, options, print) ]); } - + case "TSInterfaceDeclaration": + parts.push( + printTypeScriptModifiers(path, options, print), + "interface ", + path.call(print, "id"), + n.typeParameters ? path.call(print, "typeParameters") : "", + " " + ); + + if (n.heritageClauses) { + prefix.push( + "extends ", + join(", ", path.map(print, "heritageClauses")), + " " + ); + } + + parts.push(path.call(print, "body")); + + return concat(parts); case "ObjectExpression": case "ObjectPattern": + case "TSInterfaceBody": case "ObjectTypeAnnotation": - case "TSInterfaceDeclaration": case "TSTypeLiteral": { var isTypeAnnotation = n.type === "ObjectTypeAnnotation"; var isTypeScriptTypeAnnotation = n.type === "TSTypeLiteral"; - var isTypeScriptInterfaceDeclaration = n.type === "TSInterfaceDeclaration"; - var isTypeScriptType = isTypeScriptTypeAnnotation || isTypeScriptInterfaceDeclaration; + var isTypeScriptInterfaceBody = n.type === "TSInterfaceBody"; + var isTypeScriptType = isTypeScriptTypeAnnotation || isTypeScriptInterfaceBody; // Leave this here because we *might* want to make this // configurable later -- flow accepts ";" for type separators, // typescript accepts ";" and newlines var separator = isTypeAnnotation ? "," : ","; - if (isTypeScriptInterfaceDeclaration) { + if (isTypeScriptInterfaceBody) { separator = semi; } var fields = []; @@ -783,30 +802,19 @@ function genericPrintNoParens(path, options, print, args) { var rightBrace = n.exact ? "|}" : "}"; var parent = path.getParentNode(0); var parentIsUnionTypeAnnotation = parent.type === "UnionTypeAnnotation"; - var propertiesField = isTypeScriptType - ? "members" - : "properties"; + var propertiesField; + + if (n.type === 'TSTypeLiteral') { + propertiesField = "members"; + } else if (n.type === "TSInterfaceBody") { + propertiesField = "body"; + } else { + propertiesField = "properties"; + } if (isTypeAnnotation) { fields.push("indexers", "callProperties"); } - if (isTypeScriptInterfaceDeclaration) { - prefix.push( - printTypeScriptModifiers(path, options, print), - "interface ", - path.call(print, "name"), - printTypeParameters(path, options, print, "typeParameters"), - " " - ); - } - if (n.heritageClauses) { - prefix.push( - "extends ", - join(", ", path.map(print, "heritageClauses")), - " " - ); - } - fields.push(propertiesField); // Unfortunately, things are grouped together in the ast can be diff --git a/tests/function/jsfmt.spec.js b/tests/function/jsfmt.spec.js index 7580dfab..c3ef1937 100644 --- a/tests/function/jsfmt.spec.js +++ b/tests/function/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, null, ["typescript"]); +run_spec(__dirname, null, []); diff --git a/tests/typescript/conformance/comments/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/comments/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..fb902511 --- /dev/null +++ b/tests/typescript/conformance/comments/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`comments.ts 1`] = ` +/* http://www.site.com or http://www.site.com/page.html */ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +/* http://www.site.com or http://www.site.com/page.html */ + +`; diff --git a/tests/typescript/conformance/comments/comments.ts b/tests/typescript/conformance/comments/comments.ts new file mode 100644 index 00000000..604951f0 --- /dev/null +++ b/tests/typescript/conformance/comments/comments.ts @@ -0,0 +1 @@ +/* http://www.site.com or http://www.site.com/page.html */ diff --git a/tests/typescript/conformance/comments/jsfmt.spec.js b/tests/typescript/conformance/comments/jsfmt.spec.js new file mode 100644 index 00000000..bc085c48 --- /dev/null +++ b/tests/typescript/conformance/comments/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" }); diff --git a/yarn.lock b/yarn.lock index 1eabbec3..f9791694 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1444,15 +1444,9 @@ locate-path@^2.0.0: p-locate "^2.0.0" path-exists "^3.0.0" -lodash.tostring@^4.0.0: - version "4.1.4" - resolved "https://registry.yarnpkg.com/lodash.tostring/-/lodash.tostring-4.1.4.tgz#560c27d1f8eadde03c2cce198fef5c031d8298fb" - -lodash.unescape@4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/lodash.unescape/-/lodash.unescape-4.0.0.tgz#36debfc492b81478471ef974cd3783e202eb6cef" - dependencies: - lodash.tostring "^4.0.0" +lodash.unescape@4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/lodash.unescape/-/lodash.unescape-4.0.1.tgz#bf2249886ce514cda112fae9218cdc065211fc9c" lodash@^4.14.0, lodash@^4.2.0: version "4.17.4" @@ -1601,7 +1595,7 @@ oauth-sign@~0.8.1: version "0.8.2" resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.8.2.tgz#46a6ab7f0aead8deae9ec0565780b7d4efeb9d43" -object-assign@^4.0.1, object-assign@^4.1.0: +object-assign@^4.1.0: version "4.1.1" resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" @@ -1979,14 +1973,10 @@ sax@^1.2.1: version "1.2.2" resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.2.tgz#fd8631a23bc7826bef5d871bdb87378c95647828" -"semver@2 || 3 || 4 || 5", semver@^5.3.0: +"semver@2 || 3 || 4 || 5", semver@5.3.0, semver@^5.3.0: version "5.3.0" resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f" -semver@^4.3.6: - version "4.3.6" - resolved "https://registry.yarnpkg.com/semver/-/semver-4.3.6.tgz#300bc6e0e86374f7ba61068b5b1ecd57fc6532da" - set-blocking@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7" @@ -2166,13 +2156,12 @@ type-check@~0.3.2: dependencies: prelude-ls "~1.1.2" -"typescript-eslint-parser@git://github.com/eslint/typescript-eslint-parser.git#a294afa8158c9c088521eed72b6745eed302361c": +"typescript-eslint-parser@git://github.com/eslint/typescript-eslint-parser.git#f5fcc879b493138eb97b9bdfb39afc4d210b1ddf": version "2.1.0" - resolved "git://github.com/eslint/typescript-eslint-parser.git#a294afa8158c9c088521eed72b6745eed302361c" + resolved "git://github.com/eslint/typescript-eslint-parser.git#f5fcc879b493138eb97b9bdfb39afc4d210b1ddf" dependencies: - lodash.unescape "4.0.0" - object-assign "^4.0.1" - semver "^4.3.6" + lodash.unescape "4.0.1" + semver "5.3.0" typescript@2.3.2: version "2.3.2"