From 882b712c5f94ad03d6452942b6848265455904b7 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sun, 28 May 2017 13:18:29 -0700 Subject: [PATCH] Update typescript-eslint (#1787) It improves a few things. --- bin/prettier.js | 2 +- package.json | 2 +- src/fast-path.js | 2 + src/printer.js | 67 ++++++++++++++----- .../__snapshots__/jsfmt.spec.js.snap | 30 +++++++++ tests/typescript_class/constructor.ts | 7 ++ tests/typescript_class/optional.ts | 3 + yarn.lock | 11 ++- 8 files changed, 102 insertions(+), 22 deletions(-) create mode 100644 tests/typescript_class/constructor.ts create mode 100644 tests/typescript_class/optional.ts diff --git a/bin/prettier.js b/bin/prettier.js index b8d2278f7..458d985f4 100755 --- a/bin/prettier.js +++ b/bin/prettier.js @@ -219,7 +219,7 @@ function handleError(filename, e) { // If validation fails for one file, it will fail for all of them. process.exit(1); } else { - console.error(filename + ":", e); + console.error(filename + ":", e.stack); } // Don't exit the process if one file failed diff --git a/package.json b/package.json index 662daa6a3..d5c4efc81 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,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#992f1fa940c5679c8a509059665146f717da58cb" + "typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#2d09fb183e36a3b4089509c67cd256cd5f8871b0" }, "scripts": { "test": "jest", diff --git a/src/fast-path.js b/src/fast-path.js index 4740fba84..6dbce5e6e 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -435,6 +435,8 @@ FastPath.prototype.needsParens = function() { return false; } else if (parent.type === "ExpressionStatement") { return node.left.type === "ObjectPattern"; + } else if (parent.type === "TSPropertySignature" && parent.key === node) { + return false; } else if (parent.type === "AssignmentExpression") { return false; } else if ( diff --git a/src/printer.js b/src/printer.js index 4a2a248fb..9fba943b7 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2220,21 +2220,30 @@ function genericPrintNoParens(path, options, print, args) { case "TSArrayType": return concat([path.call(print, "elementType"), "[]"]); case "TSPropertySignature": { - const computed = n.name.type !== "Identifier" && !isLiteral(n.name); + if (n.accessibility) { + parts.push(n.accessibility + " "); + } + if (n.export) { + parts.push("export "); + } + if (n.static) { + parts.push("static "); + } + if (n.readonly) { + parts.push("readonly "); + } - parts.push(printTypeScriptModifiers(path, options, print)); - - if (computed) { + if (n.computed) { parts.push("["); } - parts.push(path.call(print, "name")); + parts.push(path.call(print, "key")); - if (computed) { + if (n.computed) { parts.push("]"); } - if (n.questionToken && !n.name.optional) { + if (n.optional) { parts.push("?"); } @@ -2254,6 +2263,12 @@ function genericPrintNoParens(path, options, print, args) { if (n.accessibility) { parts.push(n.accessibility + " "); } + if (n.export) { + parts.push("export "); + } + if (n.static) { + parts.push("static "); + } if (n.isReadonly) { parts.push("readonly "); } @@ -2270,16 +2285,29 @@ function genericPrintNoParens(path, options, print, args) { return concat(["typeof ", path.call(print, "exprName")]); case "TSParenthesizedType": return concat(["(", path.call(print, "typeAnnotation"), ")"]); - case "TSIndexSignature": + case "TSIndexSignature": { + let printedParams = []; + if (n.params) { + printedParams = path.map(print, "params"); + } + if (n.parameters) { + printedParams = path.map(print, "parameters"); + } + return concat([ - printTypeScriptModifiers(path, options, print), + n.accessibility ? concat([n.accessibility, " "]) : "", + n.export ? "export " : "", + n.static ? "static " : "", + n.readonly ? "readonly " : "", "[", + path.call(print, "index"), // This should only contain a single element, however TypeScript parses // it using parseDelimitedList that uses commas as delimiter. - join(", ", path.map(print, "parameters")), + join(", ", printedParams), "]: ", path.call(print, "typeAnnotation") ]); + } case "TSTypePredicate": return concat([ path.call(print, "parameterName"), @@ -2311,7 +2339,10 @@ function genericPrintNoParens(path, options, print, args) { parts.push(printTypeParameters(path, options, print, "typeParameters")); } - parts.push("(", join(", ", path.map(print, "parameters")), ")"); + const params = n.params + ? path.map(print, "params") + : path.map(print, "parameters"); + parts.push("(", join(", ", params), ")"); if (n.typeAnnotation) { parts.push(isType ? " => " : ": ", path.call(print, "typeAnnotation")); } @@ -2353,8 +2384,8 @@ function genericPrintNoParens(path, options, print, args) { return concat(parts); case "TSMethodSignature": parts.push( - path.call(print, "name"), - n.questionToken && !n.name.optional ? "?" : "", + path.call(print, "key"), + n.optional ? "?" : "", printFunctionTypeParameters(path, options, print), printFunctionParams(path, print, options) ); @@ -3059,12 +3090,12 @@ function printFunctionTypeParameters(path, options, print) { function printFunctionParams(path, print, options, expandArg) { const fun = path.getValue(); - const paramsField = fun.type === "TSFunctionType" || - fun.type === "TSMethodSignature" - ? "parameters" - : "params"; + const paramsField = fun.parameters ? "parameters" : "params"; - const printed = path.map(print, paramsField); + let printed = []; + if (fun[paramsField]) { + printed = path.map(print, paramsField); + } if (fun.defaults) { path.each(defExprPath => { diff --git a/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap index 667ab947e..a69134043 100644 --- a/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,24 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`constructor.ts 1`] = ` +class foo { + constructor(static a: number) {} +} + +class foo { + constructor(export a: number) {} +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class foo { + constructor(static a: number) {} +} + +class foo { + constructor(export a: number) {} +} + +`; + exports[`dunder.ts 1`] = ` // eslint/typescript-eslint-parser#296 class F<__T> {} @@ -34,3 +53,14 @@ class X { } `; + +exports[`optional.ts 1`] = ` +class X { + private foo? = undefined; +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class X { + private foo? = undefined; +} + +`; diff --git a/tests/typescript_class/constructor.ts b/tests/typescript_class/constructor.ts new file mode 100644 index 000000000..49cebcef6 --- /dev/null +++ b/tests/typescript_class/constructor.ts @@ -0,0 +1,7 @@ +class foo { + constructor(static a: number) {} +} + +class foo { + constructor(export a: number) {} +} diff --git a/tests/typescript_class/optional.ts b/tests/typescript_class/optional.ts new file mode 100644 index 000000000..4bf5fd92b --- /dev/null +++ b/tests/typescript_class/optional.ts @@ -0,0 +1,3 @@ +class X { + private foo? = undefined; +} diff --git a/yarn.lock b/yarn.lock index ad8aa9abf..846faeacf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2808,9 +2808,16 @@ typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" -"typescript-eslint-parser@git://github.com/eslint/typescript-eslint-parser.git#992f1fa940c5679c8a509059665146f717da58cb": +"typescript-eslint-parser@git://github.com/eslint/typescript-eslint-parser.git#2d09fb183e36a3b4089509c67cd256cd5f8871b0": version "3.0.0" - resolved "git://github.com/eslint/typescript-eslint-parser.git#992f1fa940c5679c8a509059665146f717da58cb" + resolved "git://github.com/eslint/typescript-eslint-parser.git#2d09fb183e36a3b4089509c67cd256cd5f8871b0" + dependencies: + lodash.unescape "4.0.1" + semver "5.3.0" + +"typescript-eslint-parser@git://github.com/eslint/typescript-eslint-parser.git#a3610632a8bd1ee9ad4179ed01a01b7739143bf5": + version "3.0.0" + resolved "git://github.com/eslint/typescript-eslint-parser.git#a3610632a8bd1ee9ad4179ed01a01b7739143bf5" dependencies: lodash.unescape "4.0.1" semver "5.3.0"