@babel/plugin-transform-typescript
Advanced tools
Comparing version 7.0.0-beta.46 to 7.0.0-beta.47
@@ -9,3 +9,3 @@ "use strict"; | ||
function _assert() { | ||
var data = _interopRequireDefault(require("assert")); | ||
const data = _interopRequireDefault(require("assert")); | ||
@@ -20,3 +20,3 @@ _assert = function _assert() { | ||
function _core() { | ||
var data = require("@babel/core"); | ||
const data = require("@babel/core"); | ||
@@ -33,3 +33,3 @@ _core = function _core() { | ||
function transpileEnum(path, t) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -45,4 +45,4 @@ if (node.declare) { | ||
var name = node.id.name; | ||
var fill = enumFill(path, t, node.id); | ||
const name = node.id.name; | ||
const fill = enumFill(path, t, node.id); | ||
@@ -59,3 +59,3 @@ switch (path.parent.type) { | ||
} else { | ||
var isGlobal = t.isProgram(path.parent); | ||
const isGlobal = t.isProgram(path.parent); | ||
path.replaceWith(makeVar(node.id, t, isGlobal ? "var" : "let")); | ||
@@ -68,3 +68,3 @@ } | ||
default: | ||
throw new Error("Unexpected enum parent '" + path.parent.type); | ||
throw new Error(`Unexpected enum parent '${path.parent.type}`); | ||
} | ||
@@ -90,21 +90,23 @@ | ||
var buildEnumWrapper = (0, _core().template)("\n (function (ID) {\n ASSIGNMENTS;\n })(ID || (ID = {}));\n"); | ||
var buildStringAssignment = (0, _core().template)("\n ENUM[\"NAME\"] = VALUE;\n"); | ||
var buildNumericAssignment = (0, _core().template)("\n ENUM[ENUM[\"NAME\"] = VALUE] = \"NAME\";\n"); | ||
const buildEnumWrapper = (0, _core().template)(` | ||
(function (ID) { | ||
ASSIGNMENTS; | ||
})(ID || (ID = {})); | ||
`); | ||
const buildStringAssignment = (0, _core().template)(` | ||
ENUM["NAME"] = VALUE; | ||
`); | ||
const buildNumericAssignment = (0, _core().template)(` | ||
ENUM[ENUM["NAME"] = VALUE] = "NAME"; | ||
`); | ||
var buildEnumMember = function buildEnumMember(isString, options) { | ||
return (isString ? buildStringAssignment : buildNumericAssignment)(options); | ||
}; | ||
const buildEnumMember = (isString, options) => (isString ? buildStringAssignment : buildNumericAssignment)(options); | ||
function enumFill(path, t, id) { | ||
var x = translateEnumValues(path, t); | ||
var assignments = x.map(function (_ref) { | ||
var memberName = _ref[0], | ||
memberValue = _ref[1]; | ||
return buildEnumMember(t.isStringLiteral(memberValue), { | ||
ENUM: t.cloneNode(id), | ||
NAME: memberName, | ||
VALUE: memberValue | ||
}); | ||
}); | ||
const x = translateEnumValues(path, t); | ||
const assignments = x.map(([memberName, memberValue]) => buildEnumMember(t.isStringLiteral(memberValue), { | ||
ENUM: t.cloneNode(id), | ||
NAME: memberName, | ||
VALUE: memberValue | ||
})); | ||
return buildEnumWrapper({ | ||
@@ -117,11 +119,11 @@ ID: t.cloneNode(id), | ||
function translateEnumValues(path, t) { | ||
var seen = Object.create(null); | ||
var prev = -1; | ||
return path.node.members.map(function (member) { | ||
var name = t.isIdentifier(member.id) ? member.id.name : member.id.value; | ||
var initializer = member.initializer; | ||
var value; | ||
const seen = Object.create(null); | ||
let prev = -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; | ||
if (initializer) { | ||
var constValue = evaluate(initializer, seen); | ||
const constValue = evaluate(initializer, seen); | ||
@@ -186,6 +188,7 @@ if (constValue !== undefined) { | ||
function evalUnaryExpression(_ref2) { | ||
var argument = _ref2.argument, | ||
operator = _ref2.operator; | ||
var value = evalConstant(argument); | ||
function evalUnaryExpression({ | ||
argument, | ||
operator | ||
}) { | ||
const value = evalConstant(argument); | ||
@@ -212,3 +215,3 @@ if (value === undefined) { | ||
function evalBinaryExpression(expr) { | ||
var left = evalConstant(expr.left); | ||
const left = evalConstant(expr.left); | ||
@@ -219,3 +222,3 @@ if (left === undefined) { | ||
var right = evalConstant(expr.right); | ||
const right = evalConstant(expr.right); | ||
@@ -222,0 +225,0 @@ if (right === undefined) { |
221
lib/index.js
@@ -9,3 +9,3 @@ "use strict"; | ||
function _helperPluginUtils() { | ||
var data = require("@babel/helper-plugin-utils"); | ||
const data = require("@babel/helper-plugin-utils"); | ||
@@ -20,3 +20,3 @@ _helperPluginUtils = function _helperPluginUtils() { | ||
function _pluginSyntaxTypescript() { | ||
var data = _interopRequireDefault(require("@babel/plugin-syntax-typescript")); | ||
const data = _interopRequireDefault(require("@babel/plugin-syntax-typescript")); | ||
@@ -31,3 +31,3 @@ _pluginSyntaxTypescript = function _pluginSyntaxTypescript() { | ||
function _core() { | ||
var data = require("@babel/core"); | ||
const data = require("@babel/core"); | ||
@@ -58,3 +58,5 @@ _core = function _core() { | ||
var _default = (0, _helperPluginUtils().declare)(function (api) { | ||
var _default = (0, _helperPluginUtils().declare)((api, { | ||
jsxPragma = "React" | ||
}) => { | ||
api.assertVersion(7); | ||
@@ -67,14 +69,7 @@ return { | ||
RestElement: visitPattern, | ||
Program: function Program(path, state) { | ||
Program(path, state) { | ||
state.programPath = path; | ||
}, | ||
ImportDeclaration: function ImportDeclaration(path, state) { | ||
if (path.node.specifiers.length === 0) { | ||
return; | ||
} | ||
var allElided = true; | ||
var importsToRemove = []; | ||
for (var _iterator = path.node.specifiers, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
for (var _iterator = path.get("body"), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
@@ -91,36 +86,66 @@ | ||
var specifier = _ref; | ||
var binding = path.scope.getBinding(specifier.local.name); | ||
const stmt = _ref; | ||
if (binding && isImportTypeOnly(binding, state.programPath)) { | ||
importsToRemove.push(binding.path); | ||
} else { | ||
allElided = false; | ||
} | ||
} | ||
if (_core().types.isImportDeclaration(stmt)) { | ||
if (stmt.node.specifiers.length === 0) { | ||
return; | ||
} | ||
if (allElided) { | ||
path.remove(); | ||
} else { | ||
for (var _i2 = 0; _i2 < importsToRemove.length; _i2++) { | ||
var importPath = importsToRemove[_i2]; | ||
importPath.remove(); | ||
let allElided = true; | ||
const importsToRemove = []; | ||
for (var _iterator2 = stmt.node.specifiers, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref2 = _i2.value; | ||
} | ||
const specifier = _ref2; | ||
const binding = stmt.scope.getBinding(specifier.local.name); | ||
if (binding && isImportTypeOnly(binding, state.programPath)) { | ||
importsToRemove.push(binding.path); | ||
} else { | ||
allElided = false; | ||
} | ||
} | ||
if (allElided) { | ||
stmt.remove(); | ||
} else { | ||
for (var _i3 = 0; _i3 < importsToRemove.length; _i3++) { | ||
const importPath = importsToRemove[_i3]; | ||
importPath.remove(); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
TSDeclareFunction: function TSDeclareFunction(path) { | ||
TSDeclareFunction(path) { | ||
path.remove(); | ||
}, | ||
TSDeclareMethod: function TSDeclareMethod(path) { | ||
TSDeclareMethod(path) { | ||
path.remove(); | ||
}, | ||
VariableDeclaration: function VariableDeclaration(path) { | ||
VariableDeclaration(path) { | ||
if (path.node.declare) path.remove(); | ||
}, | ||
VariableDeclarator: function VariableDeclarator(_ref2) { | ||
var node = _ref2.node; | ||
VariableDeclarator({ | ||
node | ||
}) { | ||
if (node.definite) node.definite = null; | ||
}, | ||
ClassMethod: function ClassMethod(path) { | ||
var node = path.node; | ||
ClassMethod(path) { | ||
const node = path.node; | ||
if (node.accessibility) node.accessibility = null; | ||
@@ -134,17 +159,17 @@ if (node.abstract) node.abstract = null; | ||
var parameterProperties = []; | ||
const parameterProperties = []; | ||
for (var _iterator2 = node.params, _isArray2 = Array.isArray(_iterator2), _i3 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
for (var _iterator3 = node.params, _isArray3 = Array.isArray(_iterator3), _i4 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { | ||
var _ref3; | ||
if (_isArray2) { | ||
if (_i3 >= _iterator2.length) break; | ||
_ref3 = _iterator2[_i3++]; | ||
if (_isArray3) { | ||
if (_i4 >= _iterator3.length) break; | ||
_ref3 = _iterator3[_i4++]; | ||
} else { | ||
_i3 = _iterator2.next(); | ||
if (_i3.done) break; | ||
_ref3 = _i3.value; | ||
_i4 = _iterator3.next(); | ||
if (_i4.done) break; | ||
_ref3 = _i4.value; | ||
} | ||
var param = _ref3; | ||
const param = _ref3; | ||
@@ -160,4 +185,4 @@ if (param.type === "TSParameterProperty") { | ||
var assigns = parameterProperties.map(function (p) { | ||
var name; | ||
const assigns = parameterProperties.map(p => { | ||
let name; | ||
@@ -172,19 +197,21 @@ if (_core().types.isIdentifier(p)) { | ||
var assign = _core().types.assignmentExpression("=", _core().types.memberExpression(_core().types.thisExpression(), _core().types.identifier(name)), _core().types.identifier(name)); | ||
const assign = _core().types.assignmentExpression("=", _core().types.memberExpression(_core().types.thisExpression(), _core().types.identifier(name)), _core().types.identifier(name)); | ||
return _core().types.expressionStatement(assign); | ||
}); | ||
var statements = node.body.body; | ||
var first = statements[0]; | ||
const statements = node.body.body; | ||
const first = statements[0]; | ||
var startsWithSuperCall = first !== undefined && _core().types.isExpressionStatement(first) && _core().types.isCallExpression(first.expression) && _core().types.isSuper(first.expression.callee); | ||
const startsWithSuperCall = first !== undefined && _core().types.isExpressionStatement(first) && _core().types.isCallExpression(first.expression) && _core().types.isSuper(first.expression.callee); | ||
node.body.body = startsWithSuperCall ? [first].concat(assigns, statements.slice(1)) : assigns.concat(statements); | ||
node.body.body = startsWithSuperCall ? [first, ...assigns, ...statements.slice(1)] : [...assigns, ...statements]; | ||
}, | ||
TSParameterProperty: function TSParameterProperty(path) { | ||
TSParameterProperty(path) { | ||
path.replaceWith(path.node.parameter); | ||
}, | ||
ClassProperty: function ClassProperty(path) { | ||
var node = path.node; | ||
ClassProperty(path) { | ||
const node = path.node; | ||
if (!node.value) { | ||
@@ -202,8 +229,10 @@ path.remove(); | ||
}, | ||
TSIndexSignature: function TSIndexSignature(path) { | ||
TSIndexSignature(path) { | ||
path.remove(); | ||
}, | ||
ClassDeclaration: function ClassDeclaration(path) { | ||
var node = path.node; | ||
ClassDeclaration(path) { | ||
const node = path.node; | ||
if (node.declare) { | ||
@@ -216,4 +245,6 @@ path.remove(); | ||
}, | ||
Class: function Class(_ref4) { | ||
var node = _ref4.node; | ||
Class({ | ||
node | ||
}) { | ||
if (node.typeParameters) node.typeParameters = null; | ||
@@ -223,7 +254,9 @@ if (node.superTypeParameters) node.superTypeParameters = null; | ||
}, | ||
Function: function Function(_ref5) { | ||
var node = _ref5.node; | ||
Function({ | ||
node | ||
}) { | ||
if (node.typeParameters) node.typeParameters = null; | ||
if (node.returnType) node.returnType = null; | ||
var p0 = node.params[0]; | ||
const p0 = node.params[0]; | ||
@@ -234,3 +267,4 @@ if (p0 && _core().types.isIdentifier(p0) && p0.name === "this") { | ||
}, | ||
TSModuleDeclaration: function TSModuleDeclaration(path) { | ||
TSModuleDeclaration(path) { | ||
if (!path.node.declare && path.node.id.type !== "StringLiteral") { | ||
@@ -242,37 +276,49 @@ throw path.buildCodeFrameError("Namespaces are not supported."); | ||
}, | ||
TSInterfaceDeclaration: function TSInterfaceDeclaration(path) { | ||
TSInterfaceDeclaration(path) { | ||
path.remove(); | ||
}, | ||
TSTypeAliasDeclaration: function TSTypeAliasDeclaration(path) { | ||
TSTypeAliasDeclaration(path) { | ||
path.remove(); | ||
}, | ||
TSEnumDeclaration: function TSEnumDeclaration(path) { | ||
TSEnumDeclaration(path) { | ||
(0, _enum.default)(path, _core().types); | ||
}, | ||
TSImportEqualsDeclaration: function TSImportEqualsDeclaration(path) { | ||
TSImportEqualsDeclaration(path) { | ||
throw path.buildCodeFrameError("`import =` is not supported by @babel/plugin-transform-typescript\n" + "Please consider using " + "`import <moduleName> from '<moduleName>';` alongside " + "Typescript's --allowSyntheticDefaultImports option."); | ||
}, | ||
TSExportAssignment: function TSExportAssignment(path) { | ||
TSExportAssignment(path) { | ||
throw path.buildCodeFrameError("`export =` is not supported by @babel/plugin-transform-typescript\n" + "Please consider using `export <value>;`."); | ||
}, | ||
TSTypeAssertion: function TSTypeAssertion(path) { | ||
TSTypeAssertion(path) { | ||
path.replaceWith(path.node.expression); | ||
}, | ||
TSAsExpression: function TSAsExpression(path) { | ||
TSAsExpression(path) { | ||
path.replaceWith(path.node.expression); | ||
}, | ||
TSNonNullExpression: function TSNonNullExpression(path) { | ||
TSNonNullExpression(path) { | ||
path.replaceWith(path.node.expression); | ||
}, | ||
CallExpression: function CallExpression(path) { | ||
CallExpression(path) { | ||
path.node.typeParameters = null; | ||
}, | ||
NewExpression: function NewExpression(path) { | ||
NewExpression(path) { | ||
path.node.typeParameters = null; | ||
} | ||
} | ||
}; | ||
function visitPattern(_ref6) { | ||
var node = _ref6.node; | ||
function visitPattern({ | ||
node | ||
}) { | ||
if (node.typeAnnotation) node.typeAnnotation = null; | ||
@@ -283,15 +329,15 @@ if (_core().types.isIdentifier(node) && node.optional) node.optional = null; | ||
function isImportTypeOnly(binding, programPath) { | ||
for (var _iterator3 = binding.referencePaths, _isArray3 = Array.isArray(_iterator3), _i4 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { | ||
var _ref7; | ||
for (var _iterator4 = binding.referencePaths, _isArray4 = Array.isArray(_iterator4), _i5 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) { | ||
var _ref4; | ||
if (_isArray3) { | ||
if (_i4 >= _iterator3.length) break; | ||
_ref7 = _iterator3[_i4++]; | ||
if (_isArray4) { | ||
if (_i5 >= _iterator4.length) break; | ||
_ref4 = _iterator4[_i5++]; | ||
} else { | ||
_i4 = _iterator3.next(); | ||
if (_i4.done) break; | ||
_ref7 = _i4.value; | ||
_i5 = _iterator4.next(); | ||
if (_i5.done) break; | ||
_ref4 = _i5.value; | ||
} | ||
var path = _ref7; | ||
const path = _ref4; | ||
@@ -303,11 +349,12 @@ if (!isInType(path)) { | ||
if (binding.identifier.name != "React") { | ||
if (binding.identifier.name !== jsxPragma) { | ||
return true; | ||
} | ||
var sourceFileHasJsx = false; | ||
let sourceFileHasJsx = false; | ||
programPath.traverse({ | ||
JSXElement: function JSXElement() { | ||
JSXElement() { | ||
sourceFileHasJsx = true; | ||
} | ||
}); | ||
@@ -314,0 +361,0 @@ return !sourceFileHasJsx; |
{ | ||
"name": "@babel/plugin-transform-typescript", | ||
"version": "7.0.0-beta.46", | ||
"version": "7.0.0-beta.47", | ||
"description": "Transform TypeScript into ES.next", | ||
@@ -13,12 +13,12 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-typescript", | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "7.0.0-beta.46", | ||
"@babel/plugin-syntax-typescript": "7.0.0-beta.46" | ||
"@babel/helper-plugin-utils": "7.0.0-beta.47", | ||
"@babel/plugin-syntax-typescript": "7.0.0-beta.47" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "7.0.0-beta.46" | ||
"@babel/core": "7.0.0-beta.47" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "7.0.0-beta.46", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.46" | ||
"@babel/core": "7.0.0-beta.47", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.47" | ||
} | ||
} |
@@ -59,3 +59,12 @@ # @babel/plugin-transform-typescript | ||
``` | ||
## Options | ||
### `jsxPragma` | ||
`string` | ||
Replace the function used when compiling JSX expressions. | ||
This is so that we know that the import is not a type import, and should not be removed | ||
[const_enum]: https://www.typescriptlang.org/docs/handbook/enums.html#const-enums | ||
@@ -62,0 +71,0 @@ [namespace]: https://www.typescriptlang.org/docs/handbook/namespaces.html |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
17108
469
73
0
+ Added@babel/code-frame@7.0.0-beta.47(transitive)
+ Added@babel/core@7.0.0-beta.47(transitive)
+ Added@babel/generator@7.0.0-beta.47(transitive)
+ Added@babel/helper-function-name@7.0.0-beta.47(transitive)
+ Added@babel/helper-get-function-arity@7.0.0-beta.47(transitive)
+ Added@babel/helper-plugin-utils@7.0.0-beta.47(transitive)
+ Added@babel/helper-split-export-declaration@7.0.0-beta.47(transitive)
+ Added@babel/helpers@7.0.0-beta.47(transitive)
+ Added@babel/highlight@7.0.0-beta.47(transitive)
+ Added@babel/plugin-syntax-typescript@7.0.0-beta.47(transitive)
+ Added@babel/template@7.0.0-beta.47(transitive)
+ Added@babel/traverse@7.0.0-beta.47(transitive)
+ Added@babel/types@7.0.0-beta.47(transitive)
+ Addedbabylon@7.0.0-beta.47(transitive)
- Removed@babel/code-frame@7.0.0-beta.46(transitive)
- Removed@babel/core@7.0.0-beta.46(transitive)
- Removed@babel/generator@7.0.0-beta.46(transitive)
- Removed@babel/helper-function-name@7.0.0-beta.46(transitive)
- Removed@babel/helper-get-function-arity@7.0.0-beta.46(transitive)
- Removed@babel/helper-plugin-utils@7.0.0-beta.46(transitive)
- Removed@babel/helper-split-export-declaration@7.0.0-beta.46(transitive)
- Removed@babel/helpers@7.0.0-beta.46(transitive)
- Removed@babel/highlight@7.0.0-beta.46(transitive)
- Removed@babel/plugin-syntax-typescript@7.0.0-beta.46(transitive)
- Removed@babel/template@7.0.0-beta.46(transitive)
- Removed@babel/traverse@7.0.0-beta.46(transitive)
- Removed@babel/types@7.0.0-beta.46(transitive)
- Removedbabylon@7.0.0-beta.46(transitive)