Socket
Socket
Sign inDemoInstall

@babel/plugin-transform-typescript

Package Overview
Dependencies
61
Maintainers
4
Versions
123
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 7.20.0 to 7.20.2

13

lib/const-enum.js

@@ -7,5 +7,3 @@ "use strict";

exports.default = transpileConstEnum;
var _enum = require("./enum");
function transpileConstEnum(path, t) {

@@ -17,12 +15,8 @@ const {

let isExported = parentIsExport;
if (!isExported && t.isProgram(path.parent)) {
isExported = path.parent.body.some(stmt => t.isExportNamedDeclaration(stmt) && stmt.exportKind !== "type" && !stmt.source && stmt.specifiers.some(spec => t.isExportSpecifier(spec) && spec.exportKind !== "type" && spec.local.name === name));
}
const entries = (0, _enum.translateEnumValues)(path, t);
if (isExported) {
const obj = t.objectExpression(entries.map(([name, value]) => t.objectProperty(t.isValidIdentifier(name) ? t.identifier(name) : t.stringLiteral(name), value)));
if (path.scope.hasOwnBinding(name)) {

@@ -34,7 +28,6 @@ (parentIsExport ? path.parentPath : path).replaceWith(t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("assign")), [path.node.id, obj])));

}
return;
}
const entriesMap = new Map(entries);
const entriesMap = new Map(entries);
path.scope.path.traverse({

@@ -44,3 +37,2 @@ Scope(path) {

},
MemberExpression(path) {

@@ -51,3 +43,2 @@ if (!t.isIdentifier(path.node.object, {

let key;
if (path.node.computed) {

@@ -64,7 +55,5 @@ if (t.isStringLiteral(path.node.property)) {

}
if (!entriesMap.has(key)) return;
path.replaceWith(t.cloneNode(entriesMap.get(key)));
}
});

@@ -71,0 +60,0 @@ path.remove();

@@ -8,7 +8,4 @@ "use strict";

exports.translateEnumValues = translateEnumValues;
var _core = require("@babel/core");
var _assert = require("assert");
function transpileEnum(path, t) {

@@ -18,3 +15,2 @@ const {

} = path;
if (node.declare) {

@@ -24,6 +20,4 @@ path.remove();

}
const name = node.id.name;
const fill = enumFill(path, t, node.id);
switch (path.parent.type) {

@@ -35,3 +29,2 @@ case "BlockStatement":

path.insertAfter(fill);
if (seen(path.parentPath)) {

@@ -43,10 +36,7 @@ path.remove();

}
break;
}
default:
throw new Error(`Unexpected enum parent '${path.parent.type}`);
}
function seen(parentPath) {

@@ -56,3 +46,2 @@ if (parentPath.isExportDeclaration()) {

}
if (parentPath.getData(name)) {

@@ -66,7 +55,5 @@ return true;

}
function makeVar(id, t, kind) {
return t.variableDeclaration(kind, [t.variableDeclarator(id)]);
}
const buildEnumWrapper = (0, _core.template)(`

@@ -83,3 +70,2 @@ (function (ID) {

`);
const buildEnumMember = (isString, options) => (isString ? buildStringAssignment : buildNumericAssignment)(options);

@@ -107,3 +93,2 @@

const name = expr.node.name;
if (seen.has(name) && !expr.scope.hasOwnBinding(name)) {

@@ -114,7 +99,5 @@ expr.replaceWith(t.memberExpression(t.cloneNode(path.node.id), t.cloneNode(expr.node)));

}
const enumSelfReferenceVisitor = {
ReferencedIdentifier
};
function translateEnumValues(path, t) {

@@ -129,9 +112,6 @@ const seen = new Map();

let value;
if (initializer) {
constValue = evaluate(initializer, seen);
if (constValue !== undefined) {
seen.set(name, constValue);
if (typeof constValue === "number") {

@@ -141,3 +121,2 @@ value = t.numericLiteral(constValue);

_assert(typeof constValue === "string");
value = t.stringLiteral(constValue);

@@ -147,3 +126,2 @@ }

const initializerPath = memberPath.get("initializer");
if (initializerPath.isReferencedIdentifier()) {

@@ -162,3 +140,2 @@ ReferencedIdentifier(initializerPath, {

}
value = initializerPath.node;

@@ -178,3 +155,2 @@ seen.set(name, undefined);

}
lastName = name;

@@ -187,3 +163,2 @@ return [name, value];

return evalConstant(expr);
function evalConstant(expr) {

@@ -193,18 +168,12 @@ switch (expr.type) {

return expr.value;
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.get(expr.name);
case "TemplateLiteral":

@@ -214,3 +183,2 @@ if (expr.quasis.length === 1) {

}
default:

@@ -220,3 +188,2 @@ return undefined;

}
function evalUnaryExpression({

@@ -227,17 +194,12 @@ argument,

const value = evalConstant(argument);
if (value === undefined) {
return undefined;
}
switch (operator) {
case "+":
return value;
case "-":
return -value;
case "~":
return ~value;
default:

@@ -247,50 +209,34 @@ 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:

@@ -297,0 +243,0 @@ return undefined;

150

lib/index.js

@@ -7,17 +7,8 @@ "use strict";

exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _pluginSyntaxTypescript = require("@babel/plugin-syntax-typescript");
var _core = require("@babel/core");
var _helperCreateClassFeaturesPlugin = require("@babel/helper-create-class-features-plugin");
var _constEnum = require("./const-enum");
var _enum = require("./enum");
var _namespace = require("./namespace");
function isInType(path) {

@@ -29,9 +20,10 @@ switch (path.parent.type) {

return true;
case "TSQualifiedName":
return path.parentPath.findParent(path => path.type !== "TSQualifiedName").type !== "TSImportEqualsDeclaration";
return (
path.parentPath.findParent(path => path.type !== "TSQualifiedName").type !== "TSImportEqualsDeclaration"
);
case "ExportSpecifier":
return path.parentPath.parent.exportKind === "type";
return (
path.parentPath.parent.exportKind === "type"
);
default:

@@ -41,7 +33,5 @@ return false;

}
const GLOBAL_TYPES = new WeakMap();
const NEEDS_EXPLICIT_ESM = new WeakMap();
const PARSED_PARAMS = new WeakSet();
function isGlobalType({

@@ -55,3 +45,2 @@ scope

}
function registerGlobalType(programScope, name) {

@@ -63,6 +52,4 @@ GLOBAL_TYPES.get(programScope).add(name);

const ids = path.getBindingIdentifiers();
for (const name of Object.keys(ids)) {
const binding = path.scope.getBinding(name);
if (binding && binding.identifier === ids[name]) {

@@ -72,3 +59,2 @@ binding.scope.removeBinding(name);

}
path.opts.noScope = true;

@@ -78,4 +64,7 @@ path.remove();

}
var _default = (0, _helperPluginUtils.declare)((api, opts) => {
const {
types: t,
template
} = api;
api.assertVersion(7);

@@ -105,3 +94,2 @@ const JSX_PRAGMA_REGEX = /\*?\s*@jsx((?:Frag)?)\s+([^\s]+)/;

}
if (node.declare) {

@@ -111,3 +99,2 @@ if (node.value) {

}
if (!node.decorators) {

@@ -120,5 +107,4 @@ path.remove();

}
{
if (!allowDeclareFields && !node.decorators && !_core.types.isClassPrivateProperty(node)) {
if (!allowDeclareFields && !node.decorators && !t.isClassPrivateProperty(node)) {
path.remove();

@@ -128,7 +114,6 @@ }

} else {
if (!allowDeclareFields && !node.value && !node.decorators && !_core.types.isClassPrivateProperty(node)) {
if (!allowDeclareFields && !node.value && !node.decorators && !t.isClassPrivateProperty(node)) {
path.remove();
}
}
if (node.accessibility) node.accessibility = null;

@@ -143,3 +128,2 @@ if (node.abstract) node.abstract = null;

},
method({

@@ -152,2 +136,3 @@ node

if (node.override) node.override = null;
},

@@ -161,6 +146,4 @@

} = path;
for (const paramPath of path.get("params")) {
const param = paramPath.node;
if (param.type === "TSParameterProperty") {

@@ -171,6 +154,5 @@ const parameter = param.parameter;

let id;
if (_core.types.isIdentifier(parameter)) {
if (t.isIdentifier(parameter)) {
id = parameter;
} else if (_core.types.isAssignmentPattern(parameter) && _core.types.isIdentifier(parameter.left)) {
} else if (t.isAssignmentPattern(parameter) && t.isIdentifier(parameter.left)) {
id = parameter.left;

@@ -180,5 +162,4 @@ } else {

}
assigns.push(_core.template.statement.ast`
this.${_core.types.cloneNode(id)} = ${_core.types.cloneNode(id)}`);
assigns.push(template.statement.ast`
this.${t.cloneNode(id)} = ${t.cloneNode(id)}`);
paramPath.replaceWith(paramPath.get("parameter"));

@@ -188,6 +169,4 @@ scope.registerBinding("param", paramPath);

}
(0, _helperCreateClassFeaturesPlugin.injectInitialization)(classPath, path, assigns);
}
};

@@ -209,11 +188,8 @@ return {

const programScope = path.scope;
if (!GLOBAL_TYPES.has(programScope)) {
GLOBAL_TYPES.set(programScope, new Set());
}
if (file.ast.comments) {
for (const comment of file.ast.comments) {
const jsxMatches = JSX_PRAGMA_REGEX.exec(comment.value);
if (jsxMatches) {

@@ -228,11 +204,7 @@ if (jsxMatches[1]) {

}
let pragmaImportName = fileJsxPragma || jsxPragma;
if (pragmaImportName) {
[pragmaImportName] = pragmaImportName.split(".");
}
let pragmaFragImportName = fileJsxPragmaFrag || jsxPragmaFrag;
if (pragmaFragImportName) {

@@ -247,3 +219,2 @@ [pragmaFragImportName] = pragmaFragImportName.split(".");

}
if (stmt.node.importKind === "type") {

@@ -253,12 +224,8 @@ for (const specifier of stmt.node.specifiers) {

}
stmt.remove();
continue;
}
const importsToRemove = new Set();
const specifiersLength = stmt.node.specifiers.length;
const isAllSpecifiersElided = () => specifiersLength > 0 && specifiersLength === importsToRemove.size;
for (const specifier of stmt.node.specifiers) {

@@ -268,3 +235,2 @@ if (specifier.type === "ImportSpecifier" && specifier.importKind === "type") {

const binding = stmt.scope.getBinding(specifier.local.name);
if (binding) {

@@ -283,3 +249,2 @@ importsToRemove.add(binding.path);

}
for (const specifier of stmt.node.specifiers) {

@@ -302,3 +267,2 @@ const binding = stmt.scope.getBinding(specifier.local.name);

}
if (isAllSpecifiersElided()) {

@@ -311,10 +275,7 @@ stmt.remove();

}
continue;
}
if (stmt.isExportDeclaration()) {
stmt = stmt.get("declaration");
}
if (stmt.isVariableDeclaration({

@@ -337,11 +298,8 @@ declare: true

},
exit(path) {
if (path.node.sourceType === "module" && NEEDS_EXPLICIT_ESM.get(path.node)) {
path.pushContainer("body", _core.types.exportNamedDeclaration());
path.pushContainer("body", t.exportNamedDeclaration());
}
}
},
ExportNamedDeclaration(path, state) {

@@ -351,3 +309,2 @@ if (!NEEDS_EXPLICIT_ESM.has(state.file.ast.program)) {

}
if (path.node.exportKind === "type") {

@@ -363,13 +320,11 @@ path.remove();

if (!path.node.source && path.node.specifiers.length > 0 && path.node.specifiers.every(specifier => _core.types.isExportSpecifier(specifier) && isGlobalType(path, specifier.local.name))) {
if (!path.node.source && path.node.specifiers.length > 0 && path.node.specifiers.every(specifier => t.isExportSpecifier(specifier) && isGlobalType(path, specifier.local.name))) {
path.remove();
return;
}
NEEDS_EXPLICIT_ESM.set(state.file.ast.program, false);
},
ExportSpecifier(path) {
ExportSpecifier(path) {
const parent = path.parent;
if (!parent.source && isGlobalType(path, path.node.local.name) || path.node.exportKind === "type") {

@@ -379,3 +334,2 @@ path.remove();

},
ExportDefaultDeclaration(path, state) {

@@ -386,18 +340,14 @@ if (!NEEDS_EXPLICIT_ESM.has(state.file.ast.program)) {

if (_core.types.isIdentifier(path.node.declaration) && isGlobalType(path, path.node.declaration.name)) {
if (t.isIdentifier(path.node.declaration) && isGlobalType(path, path.node.declaration.name)) {
path.remove();
return;
}
NEEDS_EXPLICIT_ESM.set(state.file.ast.program, false);
},
TSDeclareFunction(path) {
safeRemove(path);
},
TSDeclareMethod(path) {
safeRemove(path);
},
VariableDeclaration(path) {

@@ -408,3 +358,2 @@ if (path.node.declare) {

},
VariableDeclarator({

@@ -415,7 +364,5 @@ node

},
TSIndexSignature(path) {
path.remove();
},
ClassDeclaration(path) {

@@ -425,3 +372,2 @@ const {

} = path;
if (node.declare) {

@@ -431,3 +377,2 @@ safeRemove(path);

},
Class(path) {

@@ -441,6 +386,8 @@ const {

if (node.abstract) node.abstract = null;
path.get("body.body").forEach(child => {
if (child.isClassMethod() || child.isClassPrivateMethod()) {
if (child.node.kind === "constructor") {
classMemberVisitors.constructor(child, path);
classMemberVisitors.constructor(
child, path);
} else {

@@ -454,3 +401,2 @@ classMemberVisitors.method(child);

},
Function(path) {

@@ -463,4 +409,3 @@ const {

const params = node.params;
if (params.length > 0 && _core.types.isIdentifier(params[0], {
if (params.length > 0 && t.isIdentifier(params[0], {
name: "this"

@@ -471,86 +416,68 @@ })) {

},
TSModuleDeclaration(path) {
(0, _namespace.default)(path, allowNamespaces);
},
TSInterfaceDeclaration(path) {
path.remove();
},
TSTypeAliasDeclaration(path) {
path.remove();
},
TSEnumDeclaration(path) {
if (optimizeConstEnums && path.node.const) {
(0, _constEnum.default)(path, _core.types);
(0, _constEnum.default)(path, t);
} else {
(0, _enum.default)(path, _core.types);
(0, _enum.default)(path, t);
}
},
TSImportEqualsDeclaration(path) {
if (_core.types.isTSExternalModuleReference(path.node.moduleReference)) {
if (t.isTSExternalModuleReference(path.node.moduleReference)) {
throw path.buildCodeFrameError(`\`import ${path.node.id.name} = require('${path.node.moduleReference.expression.value}')\` ` + "is not supported by @babel/plugin-transform-typescript\n" + "Please consider using " + `\`import ${path.node.id.name} from '${path.node.moduleReference.expression.value}';\` alongside ` + "Typescript's --allowSyntheticDefaultImports option.");
}
path.replaceWith(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(path.node.id, entityNameToExpr(path.node.moduleReference))]));
path.replaceWith(t.variableDeclaration("var", [t.variableDeclarator(path.node.id, entityNameToExpr(path.node.moduleReference))]));
},
TSExportAssignment(path) {
throw path.buildCodeFrameError("`export =` is not supported by @babel/plugin-transform-typescript\n" + "Please consider using `export <value>;`.");
},
TSTypeAssertion(path) {
path.replaceWith(path.node.expression);
},
[`TSAsExpression${_core.types.tsSatisfiesExpression ? "|TSSatisfiesExpression" : ""}`](path) {
[`TSAsExpression${
t.tsSatisfiesExpression ? "|TSSatisfiesExpression" : ""}`](path) {
let {
node
} = path;
do {
node = node.expression;
} while (_core.types.isTSAsExpression(node) || _core.types.isTSSatisfiesExpression != null && _core.types.isTSSatisfiesExpression(node));
} while (t.isTSAsExpression(node) || t.isTSSatisfiesExpression != null && t.isTSSatisfiesExpression(node));
path.replaceWith(node);
},
[api.types.tsInstantiationExpression ? "TSNonNullExpression|TSInstantiationExpression" : "TSNonNullExpression"](path) {
[
api.types.tsInstantiationExpression ? "TSNonNullExpression|TSInstantiationExpression" : "TSNonNullExpression"](path) {
path.replaceWith(path.node.expression);
},
CallExpression(path) {
path.node.typeParameters = null;
},
OptionalCallExpression(path) {
path.node.typeParameters = null;
},
NewExpression(path) {
path.node.typeParameters = null;
},
JSXOpeningElement(path) {
path.node.typeParameters = null;
},
TaggedTemplateExpression(path) {
path.node.typeParameters = null;
}
}
};
function entityNameToExpr(node) {
if (_core.types.isTSQualifiedName(node)) {
return _core.types.memberExpression(entityNameToExpr(node.left), node.right);
if (t.isTSQualifiedName(node)) {
return t.memberExpression(entityNameToExpr(node.left), node.right);
}
return node;
}
function visitPattern({

@@ -560,3 +487,3 @@ node

if (node.typeAnnotation) node.typeAnnotation = null;
if (_core.types.isIdentifier(node) && node.optional) node.optional = null;
if (t.isIdentifier(node) && node.optional) node.optional = null;
}

@@ -575,3 +502,2 @@

}
if (binding.identifier.name !== pragmaImportName && binding.identifier.name !== pragmaFragImportName) {

@@ -587,3 +513,2 @@ return true;

}
});

@@ -593,5 +518,4 @@ return !sourceFileHasJsx;

});
exports.default = _default;
//# sourceMappingURL=index.js.map

@@ -7,5 +7,3 @@ "use strict";

exports.default = transpileNamespace;
var _core = require("@babel/core");
function transpileNamespace(path, allowNamespaces) {

@@ -16,11 +14,8 @@ if (path.node.declare || path.node.id.type === "StringLiteral") {

}
if (!allowNamespaces) {
throw path.get("id").buildCodeFrameError("Namespace not marked type-only declare." + " Non-declarative namespaces are only supported experimentally in Babel." + " To enable and review caveats see:" + " https://babeljs.io/docs/en/babel-plugin-transform-typescript");
}
const name = path.node.id.name;
const value = handleNested(path, _core.types.cloneNode(path.node, true));
const bound = path.scope.hasOwnBinding(name);
if (path.parent.type === "ExportNamedDeclaration") {

@@ -40,7 +35,5 @@ if (!bound) {

}
function getDeclaration(name) {
return _core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.identifier(name))]);
}
function getMemberExpression(name, itemName) {

@@ -54,7 +47,5 @@ return _core.types.memberExpression(_core.types.identifier(name), _core.types.identifier(itemName));

}
const {
declarations
} = node;
if (declarations.every(declarator => _core.types.isIdentifier(declarator.id))) {

@@ -64,30 +55,21 @@ for (const declarator of declarations) {

}
return [node];
}
const bindingIdentifiers = _core.types.getBindingIdentifiers(node);
const assignments = [];
for (const idName in bindingIdentifiers) {
assignments.push(_core.types.assignmentExpression("=", getMemberExpression(name, idName), _core.types.cloneNode(bindingIdentifiers[idName])));
}
return [node, _core.types.expressionStatement(_core.types.sequenceExpression(assignments))];
}
function buildNestedAmbiendModuleError(path, node) {
throw path.hub.buildError(node, "Ambient modules cannot be nested in other modules or namespaces.", Error);
}
function handleNested(path, node, parentExport) {
const names = new Set();
const realName = node.id;
_core.types.assertIdentifier(realName);
const name = path.scope.generateUid(realName.name);
const namespaceTopLevel = _core.types.isTSModuleBlock(node.body) ? node.body.body : [_core.types.exportNamedDeclaration(node.body)];
const namespaceTopLevel = _core.types.isTSModuleBlock(node.body) ? node.body.body :
[_core.types.exportNamedDeclaration(node.body)];
for (let i = 0; i < namespaceTopLevel.length; i++) {

@@ -102,6 +84,4 @@ const subNode = namespaceTopLevel[i];

}
const transformed = handleNested(path, subNode);
const moduleName = subNode.id.name;
if (names.has(moduleName)) {

@@ -113,6 +93,4 @@ namespaceTopLevel[i] = transformed;

}
continue;
}
case "TSEnumDeclaration":

@@ -123,3 +101,2 @@ case "FunctionDeclaration":

continue;
case "VariableDeclaration":

@@ -130,9 +107,6 @@ {

}
continue;
}
default:
continue;
case "ExportNamedDeclaration":

@@ -155,3 +129,2 @@ }

}
case "VariableDeclaration":

@@ -164,3 +137,2 @@ {

}
case "TSModuleDeclaration":

@@ -171,6 +143,4 @@ {

}
const transformed = handleNested(path, subNode.declaration, _core.types.identifier(name));
const moduleName = subNode.declaration.id.name;
if (names.has(moduleName)) {

@@ -187,6 +157,4 @@ namespaceTopLevel[i] = transformed;

let fallthroughValue = _core.types.objectExpression([]);
if (parentExport) {
const memberExpr = _core.types.memberExpression(parentExport, realName);
fallthroughValue = _core.template.expression.ast`

@@ -197,3 +165,2 @@ ${_core.types.cloneNode(memberExpr)} ||

}
return _core.template.statement.ast`

@@ -200,0 +167,0 @@ (function (${_core.types.identifier(name)}) {

{
"name": "@babel/plugin-transform-typescript",
"version": "7.20.0",
"version": "7.20.2",
"description": "Transform TypeScript into ES.next",

@@ -20,4 +20,4 @@ "repository": {

"dependencies": {
"@babel/helper-create-class-features-plugin": "^7.19.0",
"@babel/helper-plugin-utils": "^7.19.0",
"@babel/helper-create-class-features-plugin": "^7.20.2",
"@babel/helper-plugin-utils": "^7.20.2",
"@babel/plugin-syntax-typescript": "^7.20.0"

@@ -29,6 +29,6 @@ },

"devDependencies": {
"@babel/core": "^7.19.6",
"@babel/core": "^7.20.2",
"@babel/helper-plugin-test-runner": "^7.18.6",
"@babel/traverse": "^7.20.0",
"@babel/types": "^7.20.0"
"@babel/traverse": "^7.20.1",
"@babel/types": "^7.20.2"
},

@@ -35,0 +35,0 @@ "homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-typescript",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc