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 destructuringmaster
parent
b7fa01af17
commit
71b2004391
|
@ -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");
|
||||
|
|
|
@ -22,7 +22,8 @@ function parse(text) {
|
|||
"functionBind",
|
||||
"functionSent",
|
||||
"dynamicImport",
|
||||
"numericSeparator"
|
||||
"numericSeparator",
|
||||
"importMeta"
|
||||
]
|
||||
};
|
||||
|
||||
|
|
167
src/printer.js
167
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("{}");
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
function isExportDeclaration(node) {
|
||||
if (node) {
|
||||
switch (node.type) {
|
||||
case "ExportDeclaration":
|
||||
case "ExportDefaultDeclaration":
|
||||
case "ExportDefaultSpecifier":
|
||||
case "DeclareExportDeclaration":
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
declare type A = string;
|
|
@ -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;
|
||||
|
||||
`;
|
|
@ -0,0 +1,5 @@
|
|||
const x = import.meta;
|
||||
const url = import.meta.url;
|
||||
import.meta;
|
||||
import.meta.url;
|
||||
import.meta.couldBeMutable = true;
|
|
@ -0,0 +1 @@
|
|||
run_spec(__dirname, { parser: "babylon" });
|
|
@ -0,0 +1,12 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`jsx_namespaced_name.js 1`] = `
|
||||
<jsx:namespacedname />;
|
||||
|
||||
<jsx:namespacedname>1</jsx:namespacedname>;
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
<jsx:namespacedname />;
|
||||
|
||||
<jsx:namespacedname>1</jsx:namespacedname>;
|
||||
|
||||
`;
|
|
@ -0,0 +1 @@
|
|||
run_spec(__dirname, null, ["babylon"]);
|
|
@ -0,0 +1,3 @@
|
|||
<jsx:namespacedname />;
|
||||
|
||||
<jsx:namespacedname>1</jsx:namespacedname>;
|
|
@ -2,7 +2,13 @@
|
|||
|
||||
exports[`destructuring.ts 1`] = `
|
||||
({ foo = [] } = bar);
|
||||
|
||||
function f({ x }?) {}
|
||||
function g([ x ]?) {}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
({ foo = [] } = bar);
|
||||
|
||||
function f({ x }?) {}
|
||||
function g([x]?) {}
|
||||
|
||||
`;
|
||||
|
|
|
@ -1 +1,4 @@
|
|||
({ foo = [] } = bar);
|
||||
|
||||
function f({ x }?) {}
|
||||
function g([ x ]?) {}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
`;
|
|
@ -0,0 +1 @@
|
|||
run_spec(__dirname, { parser: "typescript" });
|
|
@ -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 }
|
||||
}
|
Loading…
Reference in New Issue