@babel/plugin-transform-typescript
Advanced tools
Comparing version 7.0.0-beta.42 to 7.0.0-beta.43
"use strict"; | ||
exports.__esModule = true; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = transpileEnum; | ||
var _assert = _interopRequireDefault(require("assert")); | ||
function _assert() { | ||
const data = _interopRequireDefault(require("assert")); | ||
var _core = require("@babel/core"); | ||
_assert = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _core() { | ||
const data = require("@babel/core"); | ||
_core = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function transpileEnum(path, t) { | ||
var node = path.node; | ||
const { | ||
node | ||
} = path; | ||
@@ -24,4 +44,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); | ||
@@ -38,3 +58,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")); | ||
@@ -47,3 +67,3 @@ } | ||
default: | ||
throw new Error("Unexpected enum parent '" + path.parent.type); | ||
throw new Error(`Unexpected enum parent '${path.parent.type}`); | ||
} | ||
@@ -69,21 +89,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({ | ||
@@ -96,11 +118,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); | ||
@@ -114,3 +136,3 @@ if (constValue !== undefined) { | ||
} else { | ||
(0, _assert.default)(typeof constValue === "string"); | ||
(0, _assert().default)(typeof constValue === "string"); | ||
value = t.stringLiteral(constValue); | ||
@@ -166,6 +188,7 @@ prev = undefined; | ||
function evalUnaryExpression(_ref2) { | ||
var argument = _ref2.argument, | ||
operator = _ref2.operator; | ||
var value = evalConstant(argument); | ||
function evalUnaryExpression({ | ||
argument, | ||
operator | ||
}) { | ||
const value = evalConstant(argument); | ||
@@ -192,3 +215,3 @@ if (value === undefined) { | ||
function evalBinaryExpression(expr) { | ||
var left = evalConstant(expr.left); | ||
const left = evalConstant(expr.left); | ||
@@ -199,3 +222,3 @@ if (left === undefined) { | ||
var right = evalConstant(expr.right); | ||
const right = evalConstant(expr.right); | ||
@@ -202,0 +225,0 @@ if (right === undefined) { |
231
lib/index.js
"use strict"; | ||
exports.__esModule = true; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _helperPluginUtils = require("@babel/helper-plugin-utils"); | ||
function _helperPluginUtils() { | ||
const data = require("@babel/helper-plugin-utils"); | ||
var _pluginSyntaxTypescript = _interopRequireDefault(require("@babel/plugin-syntax-typescript")); | ||
_helperPluginUtils = function () { | ||
return data; | ||
}; | ||
var _core = require("@babel/core"); | ||
return data; | ||
} | ||
function _pluginSyntaxTypescript() { | ||
const data = _interopRequireDefault(require("@babel/plugin-syntax-typescript")); | ||
_pluginSyntaxTypescript = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _core() { | ||
const data = require("@babel/core"); | ||
_core = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
var _enum = _interopRequireDefault(require("./enum")); | ||
@@ -29,6 +55,6 @@ | ||
var _default = (0, _helperPluginUtils.declare)(function (api) { | ||
var _default = (0, _helperPluginUtils().declare)(api => { | ||
api.assertVersion(7); | ||
return { | ||
inherits: _pluginSyntaxTypescript.default, | ||
inherits: _pluginSyntaxTypescript().default, | ||
visitor: { | ||
@@ -38,6 +64,8 @@ Pattern: visitPattern, | ||
RestElement: visitPattern, | ||
Program: function Program(path, state) { | ||
Program(path, state) { | ||
state.programPath = path; | ||
}, | ||
ImportDeclaration: function ImportDeclaration(path, state) { | ||
ImportDeclaration(path, state) { | ||
if (path.node.specifiers.length === 0) { | ||
@@ -47,20 +75,8 @@ return; | ||
var allElided = true; | ||
var importsToRemove = []; | ||
let allElided = true; | ||
const importsToRemove = []; | ||
for (var _iterator = path.node.specifiers, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
for (const specifier of path.node.specifiers) { | ||
const binding = path.scope.getBinding(specifier.local.name); | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var _specifier = _ref; | ||
var binding = path.scope.getBinding(_specifier.local.name); | ||
if (binding && isImportTypeOnly(binding, state.programPath)) { | ||
@@ -76,4 +92,3 @@ importsToRemove.push(binding.path); | ||
} else { | ||
for (var _i2 = 0; _i2 < importsToRemove.length; _i2++) { | ||
var importPath = importsToRemove[_i2]; | ||
for (const importPath of importsToRemove) { | ||
importPath.remove(); | ||
@@ -83,17 +98,25 @@ } | ||
}, | ||
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; | ||
if (node.accessibility) node.accessibility = null; | ||
@@ -107,21 +130,8 @@ 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]();;) { | ||
var _ref3; | ||
if (_isArray2) { | ||
if (_i3 >= _iterator2.length) break; | ||
_ref3 = _iterator2[_i3++]; | ||
} else { | ||
_i3 = _iterator2.next(); | ||
if (_i3.done) break; | ||
_ref3 = _i3.value; | ||
for (const param of node.params) { | ||
if (param.type === "TSParameterProperty") { | ||
parameterProperties.push(param.parameter); | ||
} | ||
var _param = _ref3; | ||
if (_param.type === "TSParameterProperty") { | ||
parameterProperties.push(_param.parameter); | ||
} | ||
} | ||
@@ -133,8 +143,8 @@ | ||
var assigns = parameterProperties.map(function (p) { | ||
var name; | ||
const assigns = parameterProperties.map(p => { | ||
let name; | ||
if (_core.types.isIdentifier(p)) { | ||
if (_core().types.isIdentifier(p)) { | ||
name = p.name; | ||
} else if (_core.types.isAssignmentPattern(p) && _core.types.isIdentifier(p.left)) { | ||
} else if (_core().types.isAssignmentPattern(p) && _core().types.isIdentifier(p.left)) { | ||
name = p.left.name; | ||
@@ -145,19 +155,23 @@ } else { | ||
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); | ||
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; | ||
if (!node.value) { | ||
@@ -175,8 +189,12 @@ path.remove(); | ||
}, | ||
TSIndexSignature: function TSIndexSignature(path) { | ||
TSIndexSignature(path) { | ||
path.remove(); | ||
}, | ||
ClassDeclaration: function ClassDeclaration(path) { | ||
var node = path.node; | ||
ClassDeclaration(path) { | ||
const { | ||
node | ||
} = path; | ||
if (node.declare) { | ||
@@ -189,4 +207,6 @@ path.remove(); | ||
}, | ||
Class: function Class(_ref4) { | ||
var node = _ref4.node; | ||
Class({ | ||
node | ||
}) { | ||
if (node.typeParameters) node.typeParameters = null; | ||
@@ -196,13 +216,16 @@ 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]; | ||
if (p0 && _core.types.isIdentifier(p0) && p0.name === "this") { | ||
if (p0 && _core().types.isIdentifier(p0) && p0.name === "this") { | ||
node.params.shift(); | ||
} | ||
}, | ||
TSModuleDeclaration: function TSModuleDeclaration(path) { | ||
TSModuleDeclaration(path) { | ||
if (!path.node.declare && path.node.id.type !== "StringLiteral") { | ||
@@ -214,57 +237,56 @@ 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) { | ||
(0, _enum.default)(path, _core.types); | ||
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; | ||
if (_core.types.isIdentifier(node) && node.optional) node.optional = null; | ||
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; | ||
if (_isArray3) { | ||
if (_i4 >= _iterator3.length) break; | ||
_ref7 = _iterator3[_i4++]; | ||
} else { | ||
_i4 = _iterator3.next(); | ||
if (_i4.done) break; | ||
_ref7 = _i4.value; | ||
} | ||
var _path = _ref7; | ||
if (!isInType(_path)) { | ||
for (const path of binding.referencePaths) { | ||
if (!isInType(path)) { | ||
return false; | ||
@@ -278,7 +300,8 @@ } | ||
var sourceFileHasJsx = false; | ||
let sourceFileHasJsx = false; | ||
programPath.traverse({ | ||
JSXElement: function JSXElement() { | ||
JSXElement() { | ||
sourceFileHasJsx = true; | ||
} | ||
}); | ||
@@ -285,0 +308,0 @@ return !sourceFileHasJsx; |
{ | ||
"name": "@babel/plugin-transform-typescript", | ||
"version": "7.0.0-beta.42", | ||
"version": "7.0.0-beta.43", | ||
"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.42", | ||
"@babel/plugin-syntax-typescript": "7.0.0-beta.42" | ||
"@babel/helper-plugin-utils": "7.0.0-beta.43", | ||
"@babel/plugin-syntax-typescript": "7.0.0-beta.43" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "7.0.0-beta.42" | ||
"@babel/core": "7.0.0-beta.43" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "7.0.0-beta.42", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.42" | ||
"@babel/core": "7.0.0-beta.43", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.43" | ||
} | ||
} |
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
432
14990
+ Added@babel/code-frame@7.0.0-beta.43(transitive)
+ Added@babel/core@7.0.0-beta.43(transitive)
+ Added@babel/generator@7.0.0-beta.43(transitive)
+ Added@babel/helper-function-name@7.0.0-beta.43(transitive)
+ Added@babel/helper-get-function-arity@7.0.0-beta.43(transitive)
+ Added@babel/helper-plugin-utils@7.0.0-beta.43(transitive)
+ Added@babel/helper-split-export-declaration@7.0.0-beta.43(transitive)
+ Added@babel/helpers@7.0.0-beta.43(transitive)
+ Added@babel/highlight@7.0.0-beta.43(transitive)
+ Added@babel/plugin-syntax-typescript@7.0.0-beta.43(transitive)
+ Added@babel/template@7.0.0-beta.43(transitive)
+ Added@babel/traverse@7.0.0-beta.43(transitive)
+ Added@babel/types@7.0.0-beta.43(transitive)
+ Addedbabylon@7.0.0-beta.43(transitive)
- Removed@babel/code-frame@7.0.0-beta.42(transitive)
- Removed@babel/core@7.0.0-beta.42(transitive)
- Removed@babel/generator@7.0.0-beta.42(transitive)
- Removed@babel/helper-function-name@7.0.0-beta.42(transitive)
- Removed@babel/helper-get-function-arity@7.0.0-beta.42(transitive)
- Removed@babel/helper-plugin-utils@7.0.0-beta.42(transitive)
- Removed@babel/helper-split-export-declaration@7.0.0-beta.42(transitive)
- Removed@babel/helpers@7.0.0-beta.42(transitive)
- Removed@babel/highlight@7.0.0-beta.42(transitive)
- Removed@babel/plugin-syntax-typescript@7.0.0-beta.42(transitive)
- Removed@babel/template@7.0.0-beta.42(transitive)
- Removed@babel/traverse@7.0.0-beta.42(transitive)
- Removed@babel/types@7.0.0-beta.42(transitive)
- Removedbabylon@7.0.0-beta.42(transitive)