From e37a5eb5eb81680dbc5017d44c8c325351f7c72b Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 7 Aug 2017 08:45:52 -0700 Subject: [PATCH] Add babel-plugin-syntax-typescript, babel-plugin-transform-typescript, and babel-preset-typescript (#5899) * Add babel-plugin-syntax-typescript and babel-plugin-transform-typescript * Add babel-preset-typescript * Remove unnecessary handler for JSXOpeningElement * Use `t.isFoo(node)` instead of `node.type === "Foo"` * Clean up parameter property assignment generation * Don't use function for `isSuperCall` * slice -> shift * Calculate sourceFileHasJsx only if necessary * Remove `export =` support * remove some syntax readme newlines [skip ci] --- .../babel-plugin-syntax-typescript/.npmignore | 2 + .../babel-plugin-syntax-typescript/README.md | 33 +++ .../package.json | 14 + .../src/index.js | 7 + .../.npmignore | 3 + .../README.md | 54 ++++ .../package.json | 18 ++ .../src/enum.js | 206 ++++++++++++++ .../src/index.js | 255 ++++++++++++++++++ .../fixtures/cast/as-expression/actual.js | 1 + .../fixtures/cast/as-expression/expected.js | 1 + .../cast/non-null-assertion/actual.js | 1 + .../cast/non-null-assertion/expected.js | 1 + .../fixtures/cast/type-assertion/actual.js | 1 + .../fixtures/cast/type-assertion/expected.js | 1 + .../test/fixtures/class/head/actual.js | 1 + .../test/fixtures/class/head/expected.js | 1 + .../fixtures/class/index-signature/actual.js | 3 + .../class/index-signature/expected.js | 1 + .../test/fixtures/class/methods/actual.js | 4 + .../test/fixtures/class/methods/expected.js | 4 + .../parameter-properties-with-super/actual.js | 5 + .../expected.js | 7 + .../class/parameter-properties/actual.js | 3 + .../class/parameter-properties/expected.js | 8 + .../test/fixtures/class/properties/actual.js | 4 + .../fixtures/class/properties/expected.js | 3 + .../fixtures/declarations/erased/actual.js | 10 + .../fixtures/declarations/erased/expected.js | 1 + .../test/fixtures/enum/const/actual.js | 1 + .../test/fixtures/enum/const/options.json | 1 + .../fixtures/enum/constant-folding/actual.js | 10 + .../enum/constant-folding/expected.js | 12 + .../test/fixtures/enum/export/actual.js | 6 + .../test/fixtures/enum/export/expected.js | 9 + .../test/fixtures/enum/inferred/actual.js | 4 + .../test/fixtures/enum/inferred/expected.js | 6 + .../enum/non-foldable-constant/actual.js | 4 + .../enum/non-foldable-constant/options.json | 3 + .../test/fixtures/enum/non-scoped/actual.js | 5 + .../test/fixtures/enum/non-scoped/expected.js | 10 + .../test/fixtures/enum/scoped/actual.js | 4 + .../test/fixtures/enum/scoped/expected.js | 6 + .../test/fixtures/exports/export=/actual.js | 1 + .../fixtures/exports/export=/options.json | 3 + .../fixtures/function/overloads/actual.js | 3 + .../fixtures/function/overloads/expected.js | 1 + .../fixtures/function/parameters/actual.js | 2 + .../fixtures/function/parameters/expected.js | 3 + .../function/this-parameter/actual.js | 7 + .../function/this-parameter/expected.js | 11 + .../elide-no-import-specifiers/actual.js | 2 + .../elide-no-import-specifiers/expected.js | 2 + .../imports/elide-react-no-2/actual.js | 3 + .../imports/elide-react-no-2/expected.js | 3 + .../imports/elide-react-no-2/options.json | 3 + .../fixtures/imports/elide-react-no/actual.js | 3 + .../imports/elide-react-no/expected.js | 3 + .../imports/elide-react-no/options.json | 3 + .../fixtures/imports/elide-react/actual.js | 2 + .../fixtures/imports/elide-react/expected.js | 1 + .../fixtures/imports/elide-typeof/actual.js | 2 + .../fixtures/imports/elide-typeof/expected.js | 1 + .../imports/elision-locations/actual.js | 6 + .../imports/elision-locations/expected.js | 6 + .../imports/elision-qualifiedname/actual.js | 2 + .../imports/elision-qualifiedname/expected.js | 1 + .../fixtures/imports/elision-rename/actual.js | 2 + .../imports/elision-rename/expected.js | 1 + .../test/fixtures/imports/elision/actual.js | 7 + .../test/fixtures/imports/elision/expected.js | 6 + .../test/fixtures/imports/import=/actual.js | 2 + .../fixtures/imports/import=/options.json | 3 + .../test/fixtures/namespace/fails/actual.js | 1 + .../fixtures/namespace/fails/options.json | 3 + .../test/fixtures/options.json | 3 + .../fixtures/type-arguments/call/actual.js | 1 + .../fixtures/type-arguments/call/expected.js | 1 + .../fixtures/type-arguments/new/actual.js | 1 + .../fixtures/type-arguments/new/expected.js | 1 + .../types-erased/actual.js | 3 + .../types-erased/expected.js | 6 + .../test/index.js | 2 + packages/babel-preset-typescript/README.md | 54 ++++ packages/babel-preset-typescript/package.json | 16 ++ packages/babel-preset-typescript/src/index.js | 8 + 86 files changed, 929 insertions(+) create mode 100644 packages/babel-plugin-syntax-typescript/.npmignore create mode 100644 packages/babel-plugin-syntax-typescript/README.md create mode 100644 packages/babel-plugin-syntax-typescript/package.json create mode 100644 packages/babel-plugin-syntax-typescript/src/index.js create mode 100644 packages/babel-plugin-transform-typescript/.npmignore create mode 100644 packages/babel-plugin-transform-typescript/README.md create mode 100644 packages/babel-plugin-transform-typescript/package.json create mode 100644 packages/babel-plugin-transform-typescript/src/enum.js create mode 100644 packages/babel-plugin-transform-typescript/src/index.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/cast/as-expression/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/cast/as-expression/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/cast/non-null-assertion/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/cast/non-null-assertion/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/cast/type-assertion/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/cast/type-assertion/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/head/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/head/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/index-signature/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/index-signature/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/methods/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/methods/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-super/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-super/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/properties/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/class/properties/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/declarations/erased/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/declarations/erased/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/const/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/const/options.json create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/export/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/export/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/inferred/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/inferred/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/options.json create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/non-scoped/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/non-scoped/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/scoped/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/scoped/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/exports/export=/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/exports/export=/options.json create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/function/overloads/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/function/overloads/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/function/parameters/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/function/parameters/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-no-import-specifiers/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-no-import-specifiers/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-typeof/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-typeof/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-locations/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-locations/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-qualifiedname/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-qualifiedname/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-rename/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-rename/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elision/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/elision/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/options.json create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/namespace/fails/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/namespace/fails/options.json create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/options.json create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/call/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/call/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/new/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/new/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/variable-declaration/types-erased/actual.js create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/variable-declaration/types-erased/expected.js create mode 100644 packages/babel-plugin-transform-typescript/test/index.js create mode 100644 packages/babel-preset-typescript/README.md create mode 100644 packages/babel-preset-typescript/package.json create mode 100644 packages/babel-preset-typescript/src/index.js diff --git a/packages/babel-plugin-syntax-typescript/.npmignore b/packages/babel-plugin-syntax-typescript/.npmignore new file mode 100644 index 0000000000..cd3ca40881 --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/.npmignore @@ -0,0 +1,2 @@ +node_modules +src diff --git a/packages/babel-plugin-syntax-typescript/README.md b/packages/babel-plugin-syntax-typescript/README.md new file mode 100644 index 0000000000..93622fe720 --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/README.md @@ -0,0 +1,33 @@ +# babel-plugin-syntax-typescript + +## Installation + +```sh +npm install --save-dev babel-plugin-syntax-typescript +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +```json +{ + "plugins": ["syntax-typescript"] +} +``` + +### Via CLI + +```sh +babel --plugins syntax-typescript script.js +``` + +### Via Node API + +```javascript +require("babel-core").transform("code", { + plugins: ["syntax-typescript"] +}); +``` diff --git a/packages/babel-plugin-syntax-typescript/package.json b/packages/babel-plugin-syntax-typescript/package.json new file mode 100644 index 0000000000..74d94c2885 --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/package.json @@ -0,0 +1,14 @@ +{ + "name": "babel-plugin-syntax-typescript", + "version": "7.0.0-alpha.17", + "description": "Allow parsing of TypeScript syntax", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-typescript", + "license": "MIT", + "main": "lib/index.js", + "keywords": [ + "babel-plugin", + "typescript" + ], + "dependencies": {}, + "devDependencies": {} +} \ No newline at end of file diff --git a/packages/babel-plugin-syntax-typescript/src/index.js b/packages/babel-plugin-syntax-typescript/src/index.js new file mode 100644 index 0000000000..1a87cfcef5 --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/src/index.js @@ -0,0 +1,7 @@ +export default function() { + return { + manipulateOptions(opts, parserOpts) { + parserOpts.plugins.push("typescript"); + }, + }; +} diff --git a/packages/babel-plugin-transform-typescript/.npmignore b/packages/babel-plugin-transform-typescript/.npmignore new file mode 100644 index 0000000000..fabb1e7371 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/.npmignore @@ -0,0 +1,3 @@ +.gitignore +src +test diff --git a/packages/babel-plugin-transform-typescript/README.md b/packages/babel-plugin-transform-typescript/README.md new file mode 100644 index 0000000000..1e572ab568 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/README.md @@ -0,0 +1,54 @@ +# babel-plugin-transform-typescript + +> Transform [TypeScript](https://github.com/Microsoft/TypeScript) into ES.next. + +Does not type-check its input. For that, you will need to install and set up TypeScript. + +Does not support `namespace`s or `const enum`s because those require type information to transpile. +Also does not support `export =` and `import =`, because those cannot be transpiled to ES.next. + +## Example + +**In** + +```javascript +const x: number = 0; +``` + +**Out** + +```javascript +const x = 0; +``` + +## Installation + +```sh +npm install --save-dev babel-plugin-transform-typescript +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +```json +{ + "plugins": ["transform-typescript"] +} +``` + +### Via CLI + +```sh +babel --plugins transform-typescript script.js +``` + +### Via Node API + +```javascript +require("babel-core").transform("code", { + plugins: ["transform-typescript"] +}); +``` diff --git a/packages/babel-plugin-transform-typescript/package.json b/packages/babel-plugin-transform-typescript/package.json new file mode 100644 index 0000000000..c33f5bc027 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/package.json @@ -0,0 +1,18 @@ +{ + "name": "babel-plugin-transform-typescript", + "version": "7.0.0-alpha.17", + "description": "Transform TypeScript into ES.next", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-typescript", + "license": "MIT", + "main": "lib/index.js", + "keywords": [ + "babel-plugin", + "typescript" + ], + "dependencies": { + "babel-plugin-syntax-typescript": "7.0.0-alpha.17" + }, + "devDependencies": { + "babel-helper-plugin-test-runner": "7.0.0-alpha.17" + } +} diff --git a/packages/babel-plugin-transform-typescript/src/enum.js b/packages/babel-plugin-transform-typescript/src/enum.js new file mode 100644 index 0000000000..0dd3d1e927 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/src/enum.js @@ -0,0 +1,206 @@ +export default function transpileEnum(path, t) { + const { node } = path; + if (node.declare) { + path.remove(); + return; + } + + if (node.const) { + throw path.buildCodeFrameError("'const' enums are not supported."); + } + + const name = node.id.name; + const fill = enumFill(path, t, node.id); + + switch (path.parent.type) { + case "BlockStatement": + case "Program": { + const isGlobal = t.isProgram(path.parent); // && !path.parent.body.some(t.isModuleDeclaration); + if (seen(path.parentPath)) { + path.replaceWith(fill); + } else { + path.replaceWithMultiple([ + makeVar(node.id, t, isGlobal ? "var" : "let"), + fill, + ]); + } + break; + } + + case "ExportNamedDeclaration": { + path.parentPath.insertAfter(fill); + if (seen(path.parentPath.parentPath)) { + path.remove(); + } else { + path.replaceWith(makeVar(node.id, t, "let")); + } + break; + } + + default: + throw new Error(`Unexpected enum parent '${path.parent.type}`); + } + + function seen(parentPath: Path) { + if (parentPath.getData(name)) { + return true; + } else { + parentPath.setData(name, true); + return false; + } + } +} + +function makeVar(id, t, kind): VariableDeclaration { + return t.variableDeclaration(kind, [t.variableDeclarator(id)]); +} + +/** + * Generates the statement that fills in the variable declared by the enum. + * `(function (E) { ... assignments ... })(E || (E = {}));` + */ +function enumFill(path, t, id) { + const x = translateEnumValues(path, t); + const assignments = x.map(([memberName, memberValue]) => { + const inner = t.assignmentExpression( + "=", + t.memberExpression(id, t.stringLiteral(memberName), /*computed*/ true), + memberValue, + ); + const outer = t.assignmentExpression( + "=", + t.memberExpression(id, inner, /*computed*/ true), + t.stringLiteral(memberName), + ); + return t.expressionStatement(outer); + }); + + // E || (E = {}) + const callArg = t.logicalExpression( + "||", + id, + t.assignmentExpression("=", id, t.objectExpression([])), + ); + const body = t.blockStatement(assignments); + const callee = t.functionExpression(null, [id], body); + return t.expressionStatement(t.callExpression(callee, [callArg])); +} + +/** + * Maps the name of an enum member to its value. + * We keep track of the previous enum members so you can write code like: + * enum E { + * X = 1 << 0, + * Y = 1 << 1, + * Z = X | Y, + * } + */ +type PreviousEnumMembers = { [name: string]: number | typeof undefined }; + +function translateEnumValues(path, t) { + const seen: PreviousEnumMembers = Object.create(null); + // Start at -1 so the first enum member is its increment, 0. + let prev: number | typeof undefined = -1; + return path.node.members.map(member => { + const name = t.isIdentifier(member.id) ? member.id.name : member.id.value; + const initializer = member.initializer; + let value: Expression; + if (initializer) { + const constValue = evaluate(initializer, seen); + if (constValue !== undefined) { + value = t.numericLiteral(constValue); + prev = constValue; + } else { + value = initializer; + prev = undefined; + } + } else { + if (prev !== undefined) { + prev++; + value = t.numericLiteral(prev); + } else { + throw path.buildCodeFrameError("Enum member must have initializer."); + } + } + + return [name, value]; + }); +} + +// Based on the TypeScript repository's `evalConstant` in `checker.ts`. +function evaluate(expr, seen: PreviousEnumMembers) { + return evalConstant(expr); + + function evalConstant(expr) { + switch (expr.type) { + case "UnaryExpression": + return evalUnaryExpression(expr); + case "BinaryExpression": + return evalBinaryExpression(expr); + case "NumericLiteral": + return expr.value; + case "ParenthesizedExpression": + return evalConstant(expr.expression); + case "Identifier": + return seen[expr.name]; + default: + return undefined; + } + } + + function evalUnaryExpression({ argument, operator }) { + const value = evalConstant(argument); + if (value === undefined) { + return undefined; + } + + switch (operator) { + case "+": + return value; + case "-": + return -value; + case "~": + return ~value; + default: + return undefined; + } + } + + function evalBinaryExpression(expr) { + const left = evalConstant(expr.left); + if (left === undefined) { + return undefined; + } + const right = evalConstant(expr.right); + if (right === undefined) { + return undefined; + } + + switch (expr.operator) { + case "|": + return left | right; + case "&": + return left & right; + case ">>": + return left >> right; + case ">>>": + return left >>> right; + case "<<": + return left << right; + case "^": + return left ^ right; + case "*": + return left * right; + case "/": + return left / right; + case "+": + return left + right; + case "-": + return left - right; + case "%": + return left % right; + default: + return undefined; + } + } +} diff --git a/packages/babel-plugin-transform-typescript/src/index.js b/packages/babel-plugin-transform-typescript/src/index.js new file mode 100644 index 0000000000..942fd2dae3 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/src/index.js @@ -0,0 +1,255 @@ +import syntaxTypeScript from "babel-plugin-syntax-typescript"; + +import transpileEnum from "./enum"; + +function isInType(path) { + switch (path.parent.type) { + case "TSTypeReference": + case "TSQualifiedName": + case "TSExpressionWithTypeArguments": + case "TSTypeQuery": + return true; + default: + return false; + } +} + +interface State { + programPath: any, +} + +export default function({ types: t }) { + return { + inherits: syntaxTypeScript, + visitor: { + //"Pattern" alias doesn't include Identifier or RestElement. + Pattern: visitPattern, + Identifier: visitPattern, + RestElement: visitPattern, + + Program(path, state: State) { + state.programPath = path; + }, + + ImportDeclaration(path, state: State) { + // Note: this will allow both `import { } from "m"` and `import "m";`. + // In TypeScript, the former would be elided. + if (path.node.specifiers.length === 0) { + return; + } + + let allElided = true; + const importsToRemove: Path[] = []; + + for (const specifier of path.node.specifiers) { + const binding = path.scope.getBinding(specifier.local.name); + if (isImportTypeOnly(binding, state.programPath)) { + importsToRemove.push(binding.path); + } else { + allElided = false; + } + } + + if (allElided) { + path.remove(); + } else { + for (const importPath of importsToRemove) { + importPath.remove(); + } + } + }, + + TSDeclareFunction(path) { + path.remove(); + }, + + TSDeclareMethod(path) { + path.remove(); + }, + + VariableDeclaration(path) { + if (path.node.declare) path.remove(); + }, + + ClassMethod(path) { + const { node } = path; + + if (node.accessibility) node.accessibility = null; + if (node.abstract) node.abstract = null; + if (node.optional) node.optional = null; + + if (node.kind !== "constructor") { + return; + } + + // Collect parameter properties + const parameterProperties = []; + for (const param of node.params) { + if (param.type === "TSParameterProperty") { + parameterProperties.push(param.parameter); + } + } + + if (!parameterProperties.length) { + return; + } + + const assigns = parameterProperties.map(p => { + let name; + if (t.isIdentifier(p)) { + name = p.name; + } else if (t.isAssignmentPattern(p) && t.isIdentifier(p.left)) { + name = p.left.name; + } else { + throw path.buildCodeFrameError( + "Parameter properties can not be destructuring patterns.", + ); + } + + const id = t.identifier(name); + const thisDotName = t.memberExpression(t.thisExpression(), id); + const assign = t.assignmentExpression("=", thisDotName, id); + return t.expressionStatement(assign); + }); + + const statements = node.body.body; + + const first = statements[0]; + const startsWithSuperCall = + first !== undefined && + t.isExpressionStatement(first) && + t.isCallExpression(first.expression) && + t.isSuper(first.expression.callee); + + // Make sure to put parameter properties *after* the `super` call. + // TypeScript will enforce that a 'super()' call is the first statement + // when there are parameter properties. + node.body.body = startsWithSuperCall + ? [first, ...assigns, ...statements.slice(1)] + : [...assigns, ...statements]; + + // Rest handled by Function visitor + }, + + TSParameterProperty(path) { + path.replaceWith(path.node.parameter); + }, + + ClassProperty(path) { + const { node } = path; + if (!node.value) { + path.remove(); + return; + } + + if (node.accessibility) node.accessibility = null; + if (node.abstract) node.abstract = null; + if (node.optional) node.optional = null; + if (node.typeAnnotation) node.typeAnnotation = null; + }, + + TSIndexSignature(path) { + path.remove(); + }, + + ClassDeclaration(path) { + const { node } = path; + if (node.declare) { + path.remove(); + return; + } + if (node.abstract) node.abstract = null; + }, + + Class({ node }) { + if (node.typeParameters) node.typeParameters = null; + if (node.superTypeParameters) node.superTypeParameters = null; + if (node.implements) node.implements = null; + }, + + Function({ node }) { + if (node.typeParameters) node.typeParameters = null; + if (node.returnType) node.returnType = null; + + const p0 = node.params[0]; + if (p0 && t.isIdentifier(p0) && p0.name === "this") { + node.params.shift(); + } + }, + + TSModuleDeclaration(path) { + if (!path.node.declare && path.node.id.type !== "StringLiteral") { + throw path.buildCodeFrameError("Namespaces are not supported."); + } + path.remove(); + }, + + TSInterfaceDeclaration(path) { + path.remove(); + }, + + TSTypeAliasDeclaration(path) { + path.remove(); + }, + + TSEnumDeclaration(path) { + transpileEnum(path, t); + }, + + TSImportEqualsDeclaration(path) { + throw path.buildCodeFrameError("`import =` is not supported."); + }, + + TSExportAssignment(path) { + throw path.buildCodeFrameError("`export =` is not supported."); + }, + + TSTypeAssertion(path) { + path.replaceWith(path.node.expression); + }, + + TSAsExpression(path) { + path.replaceWith(path.node.expression); + }, + + TSNonNullExpression(path) { + path.replaceWith(path.node.expression); + }, + + CallExpression(path) { + path.node.typeParameters = null; + }, + + NewExpression(path) { + path.node.typeParameters = null; + }, + }, + }; + + function visitPattern({ node }) { + if (node.typeAnnotation) node.typeAnnotation = null; + if (t.isIdentifier(node) && node.optional) node.optional = null; + // 'access' and 'readonly' are only for parameter properties, so constructor visitor will handle them. + } + + function isImportTypeOnly(binding, programPath) { + for (const path of binding.referencePaths) { + if (!isInType(path)) { + return false; + } + } + + if (binding.identifier.name != "React") { + return true; + } + + // "React" is referenced as a value if there are any JSX elements in the code. + let sourceFileHasJsx = false; + programPath.traverse({ + JSXElement() { + sourceFileHasJsx = true; + }, + }); + return !sourceFileHasJsx; + } +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/cast/as-expression/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/cast/as-expression/actual.js new file mode 100644 index 0000000000..a782b10ab1 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/cast/as-expression/actual.js @@ -0,0 +1 @@ +x as T; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/cast/as-expression/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/cast/as-expression/expected.js new file mode 100644 index 0000000000..3f043d7f81 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/cast/as-expression/expected.js @@ -0,0 +1 @@ +x; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/cast/non-null-assertion/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/cast/non-null-assertion/actual.js new file mode 100644 index 0000000000..703cb104ad --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/cast/non-null-assertion/actual.js @@ -0,0 +1 @@ +x!; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/cast/non-null-assertion/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/cast/non-null-assertion/expected.js new file mode 100644 index 0000000000..3f043d7f81 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/cast/non-null-assertion/expected.js @@ -0,0 +1 @@ +x; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/cast/type-assertion/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/cast/type-assertion/actual.js new file mode 100644 index 0000000000..35a8b2e127 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/cast/type-assertion/actual.js @@ -0,0 +1 @@ + x; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/cast/type-assertion/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/cast/type-assertion/expected.js new file mode 100644 index 0000000000..3f043d7f81 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/cast/type-assertion/expected.js @@ -0,0 +1 @@ +x; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/head/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/head/actual.js new file mode 100644 index 0000000000..e643f6ec40 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/head/actual.js @@ -0,0 +1 @@ +abstract class C extends D implements I {} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/head/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/head/expected.js new file mode 100644 index 0000000000..3155433d5a --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/head/expected.js @@ -0,0 +1 @@ +class C extends D {} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/index-signature/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/index-signature/actual.js new file mode 100644 index 0000000000..311fee8a03 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/index-signature/actual.js @@ -0,0 +1,3 @@ +class C { + [s: string]: number; +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/index-signature/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/index-signature/expected.js new file mode 100644 index 0000000000..f9822136b3 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/index-signature/expected.js @@ -0,0 +1 @@ +class C {} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/methods/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/methods/actual.js new file mode 100644 index 0000000000..35f255f2b2 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/methods/actual.js @@ -0,0 +1,4 @@ +class C { + m(): void; + public m(x?: number, ...y: number[]): void {} +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/methods/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/methods/expected.js new file mode 100644 index 0000000000..332ab44167 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/methods/expected.js @@ -0,0 +1,4 @@ +class C { + m(x, ...y) {} + +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-super/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-super/actual.js new file mode 100644 index 0000000000..c81a1da870 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-super/actual.js @@ -0,0 +1,5 @@ +class C extends Object { + constructor(public x) { + super(); + } +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-super/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-super/expected.js new file mode 100644 index 0000000000..cdfd579b4b --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-super/expected.js @@ -0,0 +1,7 @@ +class C extends Object { + constructor(x) { + super(); + this.x = x; + } + +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties/actual.js new file mode 100644 index 0000000000..d28a070374 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties/actual.js @@ -0,0 +1,3 @@ +class C { + constructor(public x, public y = 0, public z: number = 0) {} +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties/expected.js new file mode 100644 index 0000000000..e559d614a3 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties/expected.js @@ -0,0 +1,8 @@ +class C { + constructor(x, y = 0, z = 0) { + this.x = x; + this.y = y; + this.z = z; + } + +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/properties/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/properties/actual.js new file mode 100644 index 0000000000..36c4f340ca --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/properties/actual.js @@ -0,0 +1,4 @@ +class C { + public a?: number; + private b: number = 0; +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/properties/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/properties/expected.js new file mode 100644 index 0000000000..3f3bcf41b5 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/properties/expected.js @@ -0,0 +1,3 @@ +class C { + b = 0; +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/declarations/erased/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/declarations/erased/actual.js new file mode 100644 index 0000000000..a8fbeee28e --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/declarations/erased/actual.js @@ -0,0 +1,10 @@ +declare const x: number; +declare function f(): void; +declare class C {} +declare enum E {} +declare module "m" {} +declare module M {} +declare namespace N {} +export interface I {} +export type T = number; +export class C2 {} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/declarations/erased/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/declarations/erased/expected.js new file mode 100644 index 0000000000..2cb72714d1 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/declarations/erased/expected.js @@ -0,0 +1 @@ +export class C2 {} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/const/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/const/actual.js new file mode 100644 index 0000000000..c8a36474cb --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/const/actual.js @@ -0,0 +1 @@ +const enum E {} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/const/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/enum/const/options.json new file mode 100644 index 0000000000..e64a9bc9fd --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/const/options.json @@ -0,0 +1 @@ +{ "throws": "'const' enums are not supported." } \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/actual.js new file mode 100644 index 0000000000..3a30334e22 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/actual.js @@ -0,0 +1,10 @@ +enum E { + a, + b = 2 + 3, + c = 2 - 3, + d = 2 * 3, + e = 2 / 3, + f = -1, + g = 1 + 2 - 3 * 4 / -5, + h, +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/expected.js new file mode 100644 index 0000000000..1dbfbeb560 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/expected.js @@ -0,0 +1,12 @@ +var E; + +(function (E) { + E[E["a"] = 0] = "a"; + E[E["b"] = 5] = "b"; + E[E["c"] = -1] = "c"; + E[E["d"] = 6] = "d"; + E[E["e"] = 0.6666666666666666] = "e"; + E[E["f"] = -1] = "f"; + E[E["g"] = 5.4] = "g"; + E[E["h"] = 6.4] = "h"; +})(E || (E = {})); \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/export/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/export/actual.js new file mode 100644 index 0000000000..c0aaa08c46 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/export/actual.js @@ -0,0 +1,6 @@ +export enum E { + A = 1 +} +export enum E { + B = 2 +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/export/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/export/expected.js new file mode 100644 index 0000000000..f1164ae68a --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/export/expected.js @@ -0,0 +1,9 @@ +export let E; + +(function (E) { + E[E["A"] = 1] = "A"; +})(E || (E = {})); + +(function (E) { + E[E["B"] = 2] = "B"; +})(E || (E = {})); \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/inferred/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/inferred/actual.js new file mode 100644 index 0000000000..7be34f5756 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/inferred/actual.js @@ -0,0 +1,4 @@ +enum E { + x, + y, +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/inferred/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/inferred/expected.js new file mode 100644 index 0000000000..064529c6be --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/inferred/expected.js @@ -0,0 +1,6 @@ +var E; + +(function (E) { + E[E["x"] = 0] = "x"; + E[E["y"] = 1] = "y"; +})(E || (E = {})); \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/actual.js new file mode 100644 index 0000000000..968c8d6d49 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/actual.js @@ -0,0 +1,4 @@ +enum E { + a = Math.sin(1), + b, +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/options.json new file mode 100644 index 0000000000..32e95fb26a --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-foldable-constant/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Enum member must have initializer." +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-scoped/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-scoped/actual.js new file mode 100644 index 0000000000..1c87e06107 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-scoped/actual.js @@ -0,0 +1,5 @@ +enum E { + x = 1, + "y" = 2, +} +enum E { z = 3 } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-scoped/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-scoped/expected.js new file mode 100644 index 0000000000..90525de811 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-scoped/expected.js @@ -0,0 +1,10 @@ +var E; + +(function (E) { + E[E["x"] = 1] = "x"; + E[E["y"] = 2] = "y"; +})(E || (E = {})); + +(function (E) { + E[E["z"] = 3] = "z"; +})(E || (E = {})); diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/scoped/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/scoped/actual.js new file mode 100644 index 0000000000..d8664c6032 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/scoped/actual.js @@ -0,0 +1,4 @@ +{ + // Uses 'let' within a scope + enum E {} +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/scoped/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/scoped/expected.js new file mode 100644 index 0000000000..cacbe4aaea --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/scoped/expected.js @@ -0,0 +1,6 @@ +{ + // Uses 'let' within a scope + let E; + + (function (E) {})(E || (E = {})); +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export=/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export=/actual.js new file mode 100644 index 0000000000..fbedae645e --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export=/actual.js @@ -0,0 +1 @@ +export = 0; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export=/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export=/options.json new file mode 100644 index 0000000000..c29eb672a9 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export=/options.json @@ -0,0 +1,3 @@ +{ + "throws": "`export =` is not supported." +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/function/overloads/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/function/overloads/actual.js new file mode 100644 index 0000000000..e9cda03879 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/function/overloads/actual.js @@ -0,0 +1,3 @@ +function f(): void; +function f(): void { +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/function/overloads/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/function/overloads/expected.js new file mode 100644 index 0000000000..9aa2d1819a --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/function/overloads/expected.js @@ -0,0 +1 @@ +function f() {} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/function/parameters/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/function/parameters/actual.js new file mode 100644 index 0000000000..8b8fe0038d --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/function/parameters/actual.js @@ -0,0 +1,2 @@ +function f(x?: T, ...y: T[]): T {} +function g(x: number = 0): number {} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/function/parameters/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/function/parameters/expected.js new file mode 100644 index 0000000000..cec636bc5b --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/function/parameters/expected.js @@ -0,0 +1,3 @@ +function f(x, ...y) {} + +function g(x = 0) {} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/actual.js new file mode 100644 index 0000000000..420e204814 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/actual.js @@ -0,0 +1,7 @@ +function f(this: {}) {} +const o = { + m(this: {}) {} +}; +class C { + m(this: {}) {} +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/expected.js new file mode 100644 index 0000000000..3729cfe4a0 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/expected.js @@ -0,0 +1,11 @@ +function f() {} + +const o = { + m() {} + +}; + +class C { + m() {} + +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-no-import-specifiers/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-no-import-specifiers/actual.js new file mode 100644 index 0000000000..f05f7a69aa --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-no-import-specifiers/actual.js @@ -0,0 +1,2 @@ +import {} from "a"; +import "b"; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-no-import-specifiers/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-no-import-specifiers/expected.js new file mode 100644 index 0000000000..bd40fab745 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-no-import-specifiers/expected.js @@ -0,0 +1,2 @@ +import "a"; +import "b"; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/actual.js new file mode 100644 index 0000000000..12738d77dd --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/actual.js @@ -0,0 +1,3 @@ +// Don't elide React if a JSX element appears somewhere. +import * as React from "react"; +
; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/expected.js new file mode 100644 index 0000000000..d4abed09de --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/expected.js @@ -0,0 +1,3 @@ +// Don't elide React if a JSX element appears somewhere. +import * as React from "react"; +
; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json new file mode 100644 index 0000000000..fa715c3ccb --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["syntax-jsx"] +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/actual.js new file mode 100644 index 0000000000..b0cc898bb3 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/actual.js @@ -0,0 +1,3 @@ +// Don't elide React if a JSX element appears somewhere. +import * as React from "react"; +
; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/expected.js new file mode 100644 index 0000000000..a39e9a6dc2 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/expected.js @@ -0,0 +1,3 @@ +// Don't elide React if a JSX element appears somewhere. +import * as React from "react"; +
; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json new file mode 100644 index 0000000000..fa715c3ccb --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["syntax-jsx"] +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react/actual.js new file mode 100644 index 0000000000..ad37a2b189 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react/actual.js @@ -0,0 +1,2 @@ +import * as React from "react"; +const x: React.T = 0; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react/expected.js new file mode 100644 index 0000000000..c4ad81e932 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react/expected.js @@ -0,0 +1 @@ +const x = 0; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-typeof/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-typeof/actual.js new file mode 100644 index 0000000000..d8fad37d6e --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-typeof/actual.js @@ -0,0 +1,2 @@ +import { A } from "mod"; +const x: typeof A = 0; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-typeof/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-typeof/expected.js new file mode 100644 index 0000000000..c4ad81e932 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-typeof/expected.js @@ -0,0 +1 @@ +const x = 0; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-locations/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-locations/actual.js new file mode 100644 index 0000000000..37ca2c9fdf --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-locations/actual.js @@ -0,0 +1,6 @@ +import { A, B, C, D, E, F, G, H } from "m"; + +class Class extends A implements C {} +interface Iface extends E {} +const x: G = 0; +const y: H.T = 0; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-locations/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-locations/expected.js new file mode 100644 index 0000000000..d2c8144a99 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-locations/expected.js @@ -0,0 +1,6 @@ +import { A } from "m"; + +class Class extends A {} + +const x = 0; +const y = 0; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-qualifiedname/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-qualifiedname/actual.js new file mode 100644 index 0000000000..2081810ac3 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-qualifiedname/actual.js @@ -0,0 +1,2 @@ +import * as A from "lib"; +const x: A.B = 0; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-qualifiedname/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-qualifiedname/expected.js new file mode 100644 index 0000000000..c4ad81e932 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-qualifiedname/expected.js @@ -0,0 +1 @@ +const x = 0; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-rename/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-rename/actual.js new file mode 100644 index 0000000000..129292f774 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-rename/actual.js @@ -0,0 +1,2 @@ +import { A as B } from "lib"; +const x: B = 0; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-rename/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-rename/expected.js new file mode 100644 index 0000000000..c4ad81e932 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision-rename/expected.js @@ -0,0 +1 @@ +const x = 0; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision/actual.js new file mode 100644 index 0000000000..3584b160d8 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision/actual.js @@ -0,0 +1,7 @@ +import A, { B, Used } from "lib"; +import Used2, { C } from "lib"; +import * as D from "lib"; +import * as Used3 from "lib"; +const x: A = Used; +const y: B = Used2; +const z: C | D = Used3; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision/expected.js new file mode 100644 index 0000000000..5a65b6e456 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elision/expected.js @@ -0,0 +1,6 @@ +import { Used } from "lib"; +import Used2 from "lib"; +import * as Used3 from "lib"; +const x = Used; +const y = Used2; +const z = Used3; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/actual.js new file mode 100644 index 0000000000..dc8b049669 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/actual.js @@ -0,0 +1,2 @@ +import lib = require("lib"); +lib(); diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/options.json new file mode 100644 index 0000000000..52205fd8d3 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/options.json @@ -0,0 +1,3 @@ +{ + "throws": "`import =` is not supported." +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/namespace/fails/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/fails/actual.js new file mode 100644 index 0000000000..4db486e32f --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/fails/actual.js @@ -0,0 +1 @@ +namespace N {} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/namespace/fails/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/fails/options.json new file mode 100644 index 0000000000..8449a74304 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/fails/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Namespaces are not supported." +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/options.json new file mode 100644 index 0000000000..5c79172a60 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-typescript"] +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/call/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/call/actual.js new file mode 100644 index 0000000000..8472372782 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/call/actual.js @@ -0,0 +1 @@ +f(); diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/call/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/call/expected.js new file mode 100644 index 0000000000..f1996bcd70 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/call/expected.js @@ -0,0 +1 @@ +f(); \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/new/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/new/actual.js new file mode 100644 index 0000000000..4a1e3a084a --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/new/actual.js @@ -0,0 +1 @@ +new C(); diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/new/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/new/expected.js new file mode 100644 index 0000000000..2ea584da0d --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/type-arguments/new/expected.js @@ -0,0 +1 @@ +new C(); \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/variable-declaration/types-erased/actual.js b/packages/babel-plugin-transform-typescript/test/fixtures/variable-declaration/types-erased/actual.js new file mode 100644 index 0000000000..e79aa5371c --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/variable-declaration/types-erased/actual.js @@ -0,0 +1,3 @@ +const n: number = 0; +const [a, b]: any = 0; +const { x, y }: any = 0; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/variable-declaration/types-erased/expected.js b/packages/babel-plugin-transform-typescript/test/fixtures/variable-declaration/types-erased/expected.js new file mode 100644 index 0000000000..2f67412265 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/variable-declaration/types-erased/expected.js @@ -0,0 +1,6 @@ +const n = 0; +const [a, b] = 0; +const { + x, + y +} = 0; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/index.js b/packages/babel-plugin-transform-typescript/test/index.js new file mode 100644 index 0000000000..18cb06a8b6 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/index.js @@ -0,0 +1,2 @@ +import runner from "babel-helper-plugin-test-runner"; +runner(__dirname); diff --git a/packages/babel-preset-typescript/README.md b/packages/babel-preset-typescript/README.md new file mode 100644 index 0000000000..1244a5bbf6 --- /dev/null +++ b/packages/babel-preset-typescript/README.md @@ -0,0 +1,54 @@ +# babel-preset-typescript + +> Babel preset for TypeScript. + +This preset includes the following plugins: + +- [transform-typescript](https://babeljs.io/docs/plugins/transform-typescript/) +- [syntax-object-rest-spread](https://babeljs.io/docs/plugins/syntax-object-rest-spread/) + +## Example + +**In** + +```javascript +const x: number = 0; +``` + +**Out** + +```javascript +const x = 0; +``` + +## Installation + +```sh +npm install --save-dev babel-preset-typescript +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +```json +{ + "presets": ["typescript"] +} +``` + +### Via CLI + +```sh +babel --presets typescript script.ts +``` + +### Via Node API + +```javascript +require("babel-core").transform("code", { + presets: ["typescript"] +}); +``` diff --git a/packages/babel-preset-typescript/package.json b/packages/babel-preset-typescript/package.json new file mode 100644 index 0000000000..7565baf76a --- /dev/null +++ b/packages/babel-preset-typescript/package.json @@ -0,0 +1,16 @@ +{ + "name": "babel-preset-typescript", + "version": "7.0.0-alpha.17", + "description": "Babel preset for TypeScript.", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-preset-typescript", + "license": "MIT", + "main": "lib/index.js", + "keywords": [ + "babel-preset", + "typescript" + ], + "dependencies": { + "babel-plugin-transform-typescript": "7.0.0-alpha.17", + "babel-plugin-syntax-object-rest-spread": "7.0.0-alpha.17" + } +} diff --git a/packages/babel-preset-typescript/src/index.js b/packages/babel-preset-typescript/src/index.js new file mode 100644 index 0000000000..5ddd896df8 --- /dev/null +++ b/packages/babel-preset-typescript/src/index.js @@ -0,0 +1,8 @@ +import transformTypeScript from "babel-plugin-transform-typescript"; +import syntaxObjectRestSpread from "babel-plugin-syntax-object-rest-spread"; + +export default function() { + return { + plugins: [transformTypeScript, syntaxObjectRestSpread], + }; +}