From 71b2004391af7c7c17738b276cd31a42bdceac83 Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sun, 16 Jul 2017 23:59:49 +1000 Subject: [PATCH] Cleanup unused nodes, and turn on importMeta babylon plugin (#2498) * Cleanup unused AST nodes * Add tests for JSXNamespacedName * Turn on importMeta babylon plugin * Add tests for DeclareTypeAlias * Add back TS*Keyword nodes, fix TS optional destructuring --- scripts/run-external-typescript-tests.js | 3 +- src/parser-babylon.js | 3 +- src/printer.js | 167 +++++------------- src/util.js | 1 - .../__snapshots__/jsfmt.spec.js.snap | 7 + tests/flow_type_declarations/declare_type.js | 1 + .../__snapshots__/jsfmt.spec.js.snap | 16 ++ tests/import_meta/import_meta.js | 5 + tests/import_meta/jsfmt.spec.js | 1 + .../__snapshots__/jsfmt.spec.js.snap | 12 ++ tests/jsx_namespace/jsfmt.spec.js | 1 + tests/jsx_namespace/jsx_namespaced_name.js | 3 + .../__snapshots__/jsfmt.spec.js.snap | 6 + .../typescript_destructuring/destructuring.ts | 3 + .../__snapshots__/jsfmt.spec.js.snap | 89 ++++++++++ tests/typescript_keywords/jsfmt.spec.js | 1 + tests/typescript_keywords/keywords.js | 34 ++++ 17 files changed, 231 insertions(+), 122 deletions(-) create mode 100644 tests/flow_type_declarations/declare_type.js create mode 100644 tests/import_meta/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/import_meta/import_meta.js create mode 100644 tests/import_meta/jsfmt.spec.js create mode 100644 tests/jsx_namespace/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/jsx_namespace/jsfmt.spec.js create mode 100644 tests/jsx_namespace/jsx_namespaced_name.js create mode 100644 tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript_keywords/jsfmt.spec.js create mode 100644 tests/typescript_keywords/keywords.js diff --git a/scripts/run-external-typescript-tests.js b/scripts/run-external-typescript-tests.js index 3fcccc9fd..1d4eb47ae 100644 --- a/scripts/run-external-typescript-tests.js +++ b/scripts/run-external-typescript-tests.js @@ -10,8 +10,7 @@ * If you have any ideas on how to fix this, please send a PR! */ if (process.platform !== "win32") { - console.error("Error: this script currently only works on windows."); - process.exit(1); + console.log("Warning: this script may not work on macOS."); } const fs = require("fs"); diff --git a/src/parser-babylon.js b/src/parser-babylon.js index 350929850..65a5b5353 100644 --- a/src/parser-babylon.js +++ b/src/parser-babylon.js @@ -22,7 +22,8 @@ function parse(text) { "functionBind", "functionSent", "dynamicImport", - "numericSeparator" + "numericSeparator", + "importMeta" ] }; diff --git a/src/printer.js b/src/printer.js index 4e3fd686f..06281e802 100644 --- a/src/printer.js +++ b/src/printer.js @@ -186,10 +186,7 @@ function genericPrint(path, options, printPath, args) { path.each( decoratorPath => { const decorator = decoratorPath.getValue(); - const prefix = - decorator.type === "Decorator" || decorator.type === "TSDecorator" - ? "" - : "@"; + const prefix = decorator.type === "Decorator" ? "" : "@"; decorators.push(prefix, printPath(decoratorPath), hardline); }, "declaration", @@ -272,7 +269,6 @@ function genericPrintNoParens(path, options, print, args) { return concat(parts); // Babel extension. - case "Noop": case "EmptyStatement": return ""; case "ExpressionStatement": @@ -419,8 +415,6 @@ function genericPrintNoParens(path, options, print, args) { parts.push(printBindExpressionCallee(path, options, print)); return concat(parts); - case "Path": - return join(".", n.body); case "Identifier": { const parentNode = path.getParentNode(); const isFunctionDeclarationIdentifier = @@ -591,18 +585,6 @@ function genericPrintNoParens(path, options, print, args) { } return concat(parts); - case "ModuleDeclaration": - parts.push("module", path.call(print, "id")); - - if (n.source) { - assert.ok(!n.body); - - parts.push("from", path.call(print, "source")); - } else { - parts.push(path.call(print, "body")); - } - - return join(" ", parts); case "ImportSpecifier": if (n.imported) { if (n.importKind) { @@ -639,8 +621,6 @@ function genericPrintNoParens(path, options, print, args) { } return concat(parts); - case "ExportBatchSpecifier": - return "*"; case "ImportNamespaceSpecifier": parts.push("* as "); @@ -660,7 +640,6 @@ function genericPrintNoParens(path, options, print, args) { case "TSExportAssigment": { return concat(["export = ", path.call(print, "expression"), semi]); } - case "ExportDeclaration": case "ExportDefaultDeclaration": case "ExportNamedDeclaration": return printExportDeclaration(path, options, print); @@ -1010,7 +989,8 @@ function genericPrintNoParens(path, options, print, args) { leftBrace, comments.printDanglingComments(path, options), softline, - rightBrace + rightBrace, + n.optional ? "?" : "" ]) ); } else { @@ -1026,6 +1006,7 @@ function genericPrintNoParens(path, options, print, args) { : "" ), concat([options.bracketSpacing ? line : softline, rightBrace]), + n.optional ? "?" : "", n.typeAnnotation ? ": " : "", path.call(print, "typeAnnotation") ]); @@ -1050,12 +1031,6 @@ function genericPrintNoParens(path, options, print, args) { return group(content, { shouldBreak }); } - case "PropertyPattern": - return concat([ - path.call(print, "key"), - ": ", - path.call(print, "pattern") - ]); // Babel 6 case "ObjectProperty": // Non-standard AST node type. case "Property": @@ -1095,7 +1070,6 @@ function genericPrintNoParens(path, options, print, args) { return concat(parts); // Babel 6 case "ObjectMethod": return printObjectMethod(path, options, print); - case "TSDecorator": case "Decorator": return concat(["@", path.call(print, "expression")]); case "ArrayExpression": @@ -1164,6 +1138,10 @@ function genericPrintNoParens(path, options, print, args) { ); } + if (n.optional) { + parts.push("?"); + } + if (n.typeAnnotation) { parts.push(": ", path.call(print, "typeAnnotation")); } @@ -1230,15 +1208,6 @@ function genericPrintNoParens(path, options, print, args) { return path.call(print, "value"); // Babel 6 case "DirectiveLiteral": return nodeStr(n, options); - case "ModuleSpecifier": - /* istanbul ignore if */ - if (n.local) { - throw new Error("The ESTree ModuleSpecifier type should be abstract"); - } - - // The Esprima ModuleSpecifier type is just a string-valued - // Literal identifying the imported-from module. - return nodeStr(n, options); case "UnaryExpression": parts.push(n.operator); @@ -1815,15 +1784,6 @@ function genericPrintNoParens(path, options, print, args) { requiresHardline ? hardline : "" ]); } - case "Keyword": { - return n.name; - } - case "TypeAnnotatedIdentifier": - return concat([ - path.call(print, "annotation"), - " ", - path.call(print, "identifier") - ]); case "ClassBody": if (!n.comments && n.body.length === 0) { return "{}"; @@ -1844,17 +1804,6 @@ function genericPrintNoParens(path, options, print, args) { hardline, "}" ]); - case "ClassPropertyDefinition": - parts.push("static ", path.call(print, "definition")); - - if ( - n.definition.type !== "MethodDefinition" && - n.definition.type !== "TSAbstractMethodDefinition" - ) { - parts.push(semi); - } - - return concat(parts); case "ClassProperty": case "TSAbstractClassProperty": { if (n.accessibility) { @@ -1913,13 +1862,6 @@ function genericPrintNoParens(path, options, print, args) { } return concat(parts); - case "TSHeritageClause": - return join(", ", path.map(print, "types")); - case "TSExpressionWithTypeArguments": - return concat([ - path.call(print, "expression"), - printTypeParameters(path, options, print, "typeArguments") - ]); case "TemplateElement": return join(literalline, n.value.raw.split(/\r?\n/g)); case "TemplateLiteral": { @@ -2378,8 +2320,6 @@ function genericPrintNoParens(path, options, print, args) { return concat(["typeof ", path.call(print, "argument")]); case "VoidTypeAnnotation": return "void"; - case "NullTypeAnnotation": - return "null"; case "InferredPredicate": return "%checks"; // Unhandled types below. If encountered, nodes of these types should @@ -3135,7 +3075,6 @@ function printFunctionParams(path, print, options, expandArg, printTypeParams) { "ThisTypeAnnotation", "NumberTypeAnnotation", "VoidTypeAnnotation", - "NullTypeAnnotation", "EmptyTypeAnnotation", "MixedTypeAnnotation", "BooleanTypeAnnotation", @@ -3314,57 +3253,49 @@ function printExportDeclaration(path, options, print) { } } else { if (decl.specifiers && decl.specifiers.length > 0) { - if ( - decl.specifiers.length === 1 && - decl.specifiers[0].type === "ExportBatchSpecifier" - ) { - parts.push("*"); - } else { - const specifiers = []; - const defaultSpecifiers = []; - const namespaceSpecifiers = []; + const specifiers = []; + const defaultSpecifiers = []; + const namespaceSpecifiers = []; + path.each(specifierPath => { + const specifierType = path.getValue().type; + if (specifierType === "ExportSpecifier") { + specifiers.push(print(specifierPath)); + } else if (specifierType === "ExportDefaultSpecifier") { + defaultSpecifiers.push(print(specifierPath)); + } else if (specifierType === "ExportNamespaceSpecifier") { + namespaceSpecifiers.push(concat(["* as ", print(specifierPath)])); + } + }, "specifiers"); - path.each(specifierPath => { - const specifierType = path.getValue().type; - if (specifierType === "ExportSpecifier") { - specifiers.push(print(specifierPath)); - } else if (specifierType === "ExportDefaultSpecifier") { - defaultSpecifiers.push(print(specifierPath)); - } else if (specifierType === "ExportNamespaceSpecifier") { - namespaceSpecifiers.push(concat(["* as ", print(specifierPath)])); - } - }, "specifiers"); + const isNamespaceFollowed = + namespaceSpecifiers.length !== 0 && + (specifiers.length !== 0 || defaultSpecifiers.length !== 0); + const isDefaultFollowed = + defaultSpecifiers.length !== 0 && specifiers.length !== 0; - const isNamespaceFollowed = - namespaceSpecifiers.length !== 0 && - (specifiers.length !== 0 || defaultSpecifiers.length !== 0); - const isDefaultFollowed = - defaultSpecifiers.length !== 0 && specifiers.length !== 0; - - parts.push( - decl.exportKind === "type" ? "type " : "", - concat(namespaceSpecifiers), - concat([isNamespaceFollowed ? ", " : ""]), - concat(defaultSpecifiers), - concat([isDefaultFollowed ? ", " : ""]), - specifiers.length !== 0 - ? group( - concat([ - "{", - indent( - concat([ - options.bracketSpacing ? line : softline, - join(concat([",", line]), specifiers) - ]) - ), - ifBreak(shouldPrintComma(options) ? "," : ""), - options.bracketSpacing ? line : softline, - "}" - ]) - ) - : "" - ); - } + parts.push( + decl.exportKind === "type" ? "type " : "", + concat(namespaceSpecifiers), + concat([isNamespaceFollowed ? ", " : ""]), + concat(defaultSpecifiers), + concat([isDefaultFollowed ? ", " : ""]), + specifiers.length !== 0 + ? group( + concat([ + "{", + indent( + concat([ + options.bracketSpacing ? line : softline, + join(concat([",", line]), specifiers) + ]) + ), + ifBreak(shouldPrintComma(options) ? "," : ""), + options.bracketSpacing ? line : softline, + "}" + ]) + ) + : "" + ); } else { parts.push("{}"); } diff --git a/src/util.js b/src/util.js index a30e30c72..39725ea20 100644 --- a/src/util.js +++ b/src/util.js @@ -3,7 +3,6 @@ function isExportDeclaration(node) { if (node) { switch (node.type) { - case "ExportDeclaration": case "ExportDefaultDeclaration": case "ExportDefaultSpecifier": case "DeclareExportDeclaration": diff --git a/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap b/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap index c4992d058..6311f70a7 100644 --- a/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`declare_type.js 1`] = ` +declare type A = string; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +declare type A = string; + +`; + exports[`long.js 1`] = ` export type AdamPlacementValidationSingleErrorKey = 'SOME_FANCY_TARGETS.GLOBAL_TARGET'; diff --git a/tests/flow_type_declarations/declare_type.js b/tests/flow_type_declarations/declare_type.js new file mode 100644 index 000000000..feb1ee315 --- /dev/null +++ b/tests/flow_type_declarations/declare_type.js @@ -0,0 +1 @@ +declare type A = string; diff --git a/tests/import_meta/__snapshots__/jsfmt.spec.js.snap b/tests/import_meta/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..172e08d1b --- /dev/null +++ b/tests/import_meta/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,16 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`import_meta.js 1`] = ` +const x = import.meta; +const url = import.meta.url; +import.meta; +import.meta.url; +import.meta.couldBeMutable = true; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +const x = import.meta; +const url = import.meta.url; +import.meta; +import.meta.url; +import.meta.couldBeMutable = true; + +`; diff --git a/tests/import_meta/import_meta.js b/tests/import_meta/import_meta.js new file mode 100644 index 000000000..fd4422fc6 --- /dev/null +++ b/tests/import_meta/import_meta.js @@ -0,0 +1,5 @@ +const x = import.meta; +const url = import.meta.url; +import.meta; +import.meta.url; +import.meta.couldBeMutable = true; diff --git a/tests/import_meta/jsfmt.spec.js b/tests/import_meta/jsfmt.spec.js new file mode 100644 index 000000000..f974ed6c7 --- /dev/null +++ b/tests/import_meta/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "babylon" }); diff --git a/tests/jsx_namespace/__snapshots__/jsfmt.spec.js.snap b/tests/jsx_namespace/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..e6aa0c3be --- /dev/null +++ b/tests/jsx_namespace/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,12 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`jsx_namespaced_name.js 1`] = ` +; + +1; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +; + +1; + +`; diff --git a/tests/jsx_namespace/jsfmt.spec.js b/tests/jsx_namespace/jsfmt.spec.js new file mode 100644 index 000000000..1840e9d36 --- /dev/null +++ b/tests/jsx_namespace/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, null, ["babylon"]); diff --git a/tests/jsx_namespace/jsx_namespaced_name.js b/tests/jsx_namespace/jsx_namespaced_name.js new file mode 100644 index 000000000..7f4591ee0 --- /dev/null +++ b/tests/jsx_namespace/jsx_namespaced_name.js @@ -0,0 +1,3 @@ +; + +1; diff --git a/tests/typescript_destructuring/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_destructuring/__snapshots__/jsfmt.spec.js.snap index e1ba9212c..470af5d3b 100644 --- a/tests/typescript_destructuring/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_destructuring/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,13 @@ exports[`destructuring.ts 1`] = ` ({ foo = [] } = bar); + +function f({ x }?) {} +function g([ x ]?) {} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ({ foo = [] } = bar); +function f({ x }?) {} +function g([x]?) {} + `; diff --git a/tests/typescript_destructuring/destructuring.ts b/tests/typescript_destructuring/destructuring.ts index e6b57390f..b8777b5ba 100644 --- a/tests/typescript_destructuring/destructuring.ts +++ b/tests/typescript_destructuring/destructuring.ts @@ -1 +1,4 @@ ({ foo = [] } = bar); + +function f({ x }?) {} +function g([ x ]?) {} diff --git a/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..ef7fe27d7 --- /dev/null +++ b/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,89 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`keywords.js 1`] = ` +// All of these should be an error + +module Y3 { + public module Module { + class A { s: string } + } + + // Apparently this parses :P + export private public protected static readonly async enum X { } +} + +module Y4 { + public enum Color { Blue, Red } +} + +module YY3 { + private module Module { + class A { s: string } + } +} + +module YY4 { + private enum Color { Blue, Red } +} + +module YYY3 { + static module Module { + class A { s: string } + } +} + +module YYY4 { + static enum Color { Blue, Red } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// All of these should be an error + +namespace Y3 { + public namespace Module { + class A { + s: string; + } + } + + // Apparently this parses :P + export private public protected static readonly async enum X {} +} + +namespace Y4 { + public enum Color { + Blue, + Red + } +} + +namespace YY3 { + private namespace Module { + class A { + s: string; + } + } +} + +namespace YY4 { + private enum Color { + Blue, + Red + } +} + +namespace YYY3 { + static namespace Module { + class A { + s: string; + } + } +} + +namespace YYY4 { + static enum Color { + Blue, + Red + } +} + +`; diff --git a/tests/typescript_keywords/jsfmt.spec.js b/tests/typescript_keywords/jsfmt.spec.js new file mode 100644 index 000000000..bc085c48c --- /dev/null +++ b/tests/typescript_keywords/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" }); diff --git a/tests/typescript_keywords/keywords.js b/tests/typescript_keywords/keywords.js new file mode 100644 index 000000000..6880cf1db --- /dev/null +++ b/tests/typescript_keywords/keywords.js @@ -0,0 +1,34 @@ +// All of these should be an error + +module Y3 { + public module Module { + class A { s: string } + } + + // Apparently this parses :P + export private public protected static readonly async enum X { } +} + +module Y4 { + public enum Color { Blue, Red } +} + +module YY3 { + private module Module { + class A { s: string } + } +} + +module YY4 { + private enum Color { Blue, Red } +} + +module YYY3 { + static module Module { + class A { s: string } + } +} + +module YYY4 { + static enum Color { Blue, Red } +}