New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@ts-liveserver/ts-transpiler

Package Overview
Dependencies
Maintainers
2
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ts-liveserver/ts-transpiler - npm Package Compare versions

Comparing version 0.0.12 to 0.0.13

10

__tests__/transformers/CommonJsTransformer.test.ts

@@ -87,2 +87,12 @@ import CommonJsTransformer from '../../src/transformers/CommonJsTransformer'

})
it('Should export nested expression', async () => {
const input = `
(exports = function (key) {
return key;
})('versions', []).push({
version: '3.6.5',
});
`
expect(await transformWithPlugin(input)).toMatchSnapshot()
})
})

@@ -89,0 +99,0 @@ describe('Should read from internal export', () => {

5

dist/transformers/CommonJsTransformer.d.ts

@@ -13,3 +13,4 @@ import TypeScript from 'typescript';

private convertToEsmImport;
private convertToEsmExport;
private convertForwards;
private oldconvertToEsmExport;
private stripModule;

@@ -20,2 +21,4 @@ private stripWildcardExports;

private addModuleToScope;
private getAllExportedNames;
private convertToEsmExport;
}

145

dist/transformers/CommonJsTransformer.js

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

const typescript_1 = __importDefault(require("typescript"));
const KEYNAME_EXPORTS = 'exports';
var KEYNAME;
(function (KEYNAME) {
KEYNAME["exports"] = "exports";
KEYNAME["module"] = "module";
})(KEYNAME || (KEYNAME = {}));
/*

@@ -24,8 +28,18 @@ Transpile CommonJS to ES6 module

const withoutDefineProperty = this.convertDefinePropery(withoutModule);
const withoutWildcardExports = this.stripWildcardExports(withoutDefineProperty);
const requireInTopScope = this.requireTopScope(withoutWildcardExports);
const esmExport = this.convertToEsmExport(requireInTopScope);
const withEsmImport = this.convertToEsmImport(esmExport);
const withSyntheticDefaultExport = this.createSyntheticDefaultExport(withEsmImport);
return this.addModuleToScope(withSyntheticDefaultExport);
const convertedForwards = this.convertForwards(withoutDefineProperty);
/*
const withoutWildcardExports = this.stripWildcardExports(
withoutDefineProperty,
)
const requireInTopScope = this.requireTopScope(withoutWildcardExports)
const esmExport = this.convertToEsmExport(requireInTopScope)
const withEsmImport = this.convertToEsmImport(esmExport)
const withSyntheticDefaultExport = this.createSyntheticDefaultExport(
withEsmImport,
)
return this.addModuleToScope(withSyntheticDefaultExport)
*/
const requireInTopScope = this.requireTopScope(convertedForwards);
const withEsmExport = this.convertToEsmExport(requireInTopScope);
return this.convertToEsmImport(withEsmExport);
}

@@ -45,2 +59,5 @@ transformBundle() {

convertDefinePropery(sourceFile) {
if (!/defineProperty/gm.test(sourceFile.text)) {
return sourceFile;
}
const visit = (node) => {

@@ -58,6 +75,6 @@ if (typescript_1.default.isCallExpression(node) &&

if (typescript_1.default.isIdentifier(firstArgument) &&
firstArgument.text === KEYNAME_EXPORTS &&
firstArgument.text === KEYNAME.exports &&
typescript_1.default.isStringLiteral(secondArgument)) {
if (typescript_1.default.isIdentifier(thirdArgument)) {
return typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME_EXPORTS), typescript_1.default.factory.createIdentifier(secondArgument.text)), typescript_1.default.SyntaxKind.EqualsToken, thirdArgument);
return typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME.exports), typescript_1.default.factory.createIdentifier(secondArgument.text)), typescript_1.default.SyntaxKind.EqualsToken, thirdArgument);
}

@@ -78,3 +95,3 @@ else if (typescript_1.default.isObjectLiteralExpression(thirdArgument)) {

firstStatement.expression) {
return typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME_EXPORTS), typescript_1.default.factory.createIdentifier(secondArgument.text)), typescript_1.default.SyntaxKind.EqualsToken, firstStatement.expression);
return typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME.exports), typescript_1.default.factory.createIdentifier(secondArgument.text)), typescript_1.default.SyntaxKind.EqualsToken, firstStatement.expression);
}

@@ -90,3 +107,3 @@ }

propertyValue.initializer) {
return typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME_EXPORTS), typescript_1.default.factory.createIdentifier(secondArgument.text)), typescript_1.default.SyntaxKind.EqualsToken, propertyValue.initializer);
return typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME.exports), typescript_1.default.factory.createIdentifier(secondArgument.text)), typescript_1.default.SyntaxKind.EqualsToken, propertyValue.initializer);
}

@@ -153,4 +170,28 @@ }

}
// exports = require('hello.js') -> export * from 'hello.js'
convertForwards(sourceFile) {
const visit = (node) => {
if (typescript_1.default.isExpressionStatement(node) &&
typescript_1.default.isBinaryExpression(node.expression) &&
typescript_1.default.isIdentifier(node.expression.left) &&
node.expression.left.text === KEYNAME.exports &&
node.expression.operatorToken.kind ===
typescript_1.default.SyntaxKind.EqualsToken &&
typescript_1.default.isCallExpression(node.expression.right) &&
typescript_1.default.isIdentifier(node.expression.right.expression) &&
node.expression.right.arguments.length === 1) {
const argument = node.expression.right.arguments[0];
if (typescript_1.default.isStringLiteralLike(argument)) {
return [
node,
typescript_1.default.factory.createExportDeclaration(undefined, undefined, false, undefined, typescript_1.default.factory.createStringLiteral(argument.text)),
];
}
}
return node;
};
return typescript_1.default.visitEachChild(sourceFile, visit, this.context);
}
// exports.hello = something -> export { something as hello }
convertToEsmExport(sourceFile) {
oldconvertToEsmExport(sourceFile) {
const newTopStatements = [];

@@ -163,3 +204,3 @@ const newBottomStatements = [];

typescript_1.default.isIdentifier(node.expression) &&
node.expression.text === KEYNAME_EXPORTS) {
node.expression.text === KEYNAME.exports) {
if (node.name.text in this.exportsVariableMap) {

@@ -186,11 +227,2 @@ return typescript_1.default.factory.createIdentifier(this.exportsVariableMap[node.name.text]);

}
/*
return TypeScript.factory.createExpressionStatement(
TypeScript.factory.createBinaryExpression(
identifier,
node.expression.operatorToken,
node.expression.right,
),
)
*/
}

@@ -212,4 +244,4 @@ return typescript_1.default.visitEachChild(node, visit, this.context);

typescript_1.default.isIdentifier(node.name) &&
node.expression.text === 'module' &&
node.name.text === KEYNAME_EXPORTS) {
node.expression.text === KEYNAME.module &&
node.name.text === KEYNAME.exports) {
return typescript_1.default.factory.createIdentifier(node.name.text);

@@ -231,3 +263,3 @@ }

typescript_1.default.isIdentifier(node.expression.left) &&
node.expression.left.text === KEYNAME_EXPORTS) {
node.expression.left.text === KEYNAME.exports) {
// exports = { a: 'a' }

@@ -240,3 +272,3 @@ if (typescript_1.default.isObjectLiteralExpression(node.expression.right)) {

typescript_1.default.isIdentifier(property.name)) {
expressions.push(typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME_EXPORTS), typescript_1.default.factory.createIdentifier(property.name.text)), node.expression.operatorToken, typescript_1.default.isShorthandPropertyAssignment(property)
expressions.push(typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME.exports), typescript_1.default.factory.createIdentifier(property.name.text)), node.expression.operatorToken, typescript_1.default.isShorthandPropertyAssignment(property)
? property.name

@@ -250,3 +282,3 @@ : property.initializer)));

else {
return typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME_EXPORTS), typescript_1.default.factory.createIdentifier('default')), node.expression.operatorToken, node.expression.right));
return typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME.exports), typescript_1.default.factory.createIdentifier('default')), node.expression.operatorToken, node.expression.right));
}

@@ -329,8 +361,65 @@ }

])),
], typescript_1.default.NodeFlags.Const)),
], typescript_1.default.NodeFlags.Let)),
...sourceFile.statements,
]);
}
getAllExportedNames(sourceFile) {
const exportedNames = new Set();
const visit = (node) => {
if (typescript_1.default.isBinaryExpression(node) &&
node.operatorToken.kind === typescript_1.default.SyntaxKind.EqualsToken) {
// exports.name = something
if (typescript_1.default.isPropertyAccessExpression(node.left) &&
typescript_1.default.isIdentifier(node.left.expression) &&
node.left.expression.text === KEYNAME.exports &&
typescript_1.default.isIdentifier(node.left.name)) {
exportedNames.add(node.left.name.text);
}
// exports = something
else if (typescript_1.default.isIdentifier(node.left) &&
node.left.text === KEYNAME.exports) {
// exports = { a: b }
if (typescript_1.default.isObjectLiteralExpression(node.right)) {
for (const property of node.right.properties) {
if (property.name && typescript_1.default.isIdentifier(property.name)) {
exportedNames.add(property.name.text);
}
}
}
}
}
return typescript_1.default.visitEachChild(node, visit, this.context);
};
typescript_1.default.visitEachChild(sourceFile, visit, this.context);
return Array.from(exportedNames);
}
convertToEsmExport(sourceFile) {
const exportedNames = this.getAllExportedNames(sourceFile);
const moduleExportStatement = typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([
typescript_1.default.factory.createVariableDeclaration(KEYNAME.exports, undefined, undefined, typescript_1.default.factory.createObjectLiteralExpression()),
typescript_1.default.factory.createVariableDeclaration(KEYNAME.module, undefined, undefined, typescript_1.default.factory.createObjectLiteralExpression([
typescript_1.default.factory.createPropertyAssignment(KEYNAME.exports, typescript_1.default.factory.createIdentifier(KEYNAME.exports)),
])),
], typescript_1.default.NodeFlags.Let));
const exportDefaultStatement = typescript_1.default.factory.createExportAssignment(undefined, undefined, undefined, typescript_1.default.factory.createIdentifier(KEYNAME.exports));
const statements = [
moduleExportStatement,
...sourceFile.statements,
exportDefaultStatement,
];
if (exportedNames.length) {
const variableDeclarations = [];
const exportSpecifiers = [];
for (const name of exportedNames) {
const identifier = typescript_1.default.factory.createIdentifier(this.generateUniqueName());
exportSpecifiers.push(typescript_1.default.factory.createExportSpecifier(identifier, name));
variableDeclarations.push(typescript_1.default.factory.createVariableDeclaration(identifier, undefined, undefined, typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME.exports), typescript_1.default.factory.createIdentifier(name))));
}
const exportDeclaration = typescript_1.default.factory.createExportDeclaration(undefined, undefined, false, typescript_1.default.factory.createNamedExports(exportSpecifiers));
statements.push(typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList(variableDeclarations, typescript_1.default.NodeFlags.Const)), exportDeclaration);
}
return typescript_1.default.factory.updateSourceFile(sourceFile, statements);
}
}
exports.default = CommonJsTransformer;
//# sourceMappingURL=CommonJsTransformer.js.map

@@ -35,4 +35,2 @@ "use strict";

if (nodeResolve) {
// eslint-disable-next-line no-console
console.error(nodeResolve);
return nodeResolve;

@@ -39,0 +37,0 @@ }

{
"name": "@ts-liveserver/ts-transpiler",
"version": "0.0.12",
"version": "0.0.13",
"main": "dist/index.js",

@@ -19,4 +19,4 @@ "scripts": {

"dependencies": {
"typescript": "^4.1.2"
"typescript": "~4.0.0"
}
}
import TypeScript from 'typescript'
const KEYNAME_EXPORTS = 'exports'
enum KEYNAME {
exports = 'exports',
module = 'module',
}
/*

@@ -23,2 +26,4 @@ Transpile CommonJS to ES6 module

const withoutDefineProperty = this.convertDefinePropery(withoutModule)
const convertedForwards = this.convertForwards(withoutDefineProperty)
/*
const withoutWildcardExports = this.stripWildcardExports(

@@ -32,4 +37,8 @@ withoutDefineProperty,

withEsmImport,
)
return this.addModuleToScope(withSyntheticDefaultExport)
)
return this.addModuleToScope(withSyntheticDefaultExport)
*/
const requireInTopScope = this.requireTopScope(convertedForwards)
const withEsmExport = this.convertToEsmExport(requireInTopScope)
return this.convertToEsmImport(withEsmExport)
}

@@ -51,2 +60,5 @@ public transformBundle(): TypeScript.Bundle {

): TypeScript.SourceFile {
if (!/defineProperty/gm.test(sourceFile.text)) {
return sourceFile
}
const visit = (node: TypeScript.Node): TypeScript.Node => {

@@ -67,3 +79,3 @@ if (

TypeScript.isIdentifier(firstArgument) &&
firstArgument.text === KEYNAME_EXPORTS &&
firstArgument.text === KEYNAME.exports &&
TypeScript.isStringLiteral(secondArgument)

@@ -74,3 +86,3 @@ ) {

TypeScript.factory.createPropertyAccessExpression(
TypeScript.factory.createIdentifier(KEYNAME_EXPORTS),
TypeScript.factory.createIdentifier(KEYNAME.exports),
TypeScript.factory.createIdentifier(secondArgument.text),

@@ -105,3 +117,3 @@ ),

TypeScript.factory.createPropertyAccessExpression(
TypeScript.factory.createIdentifier(KEYNAME_EXPORTS),
TypeScript.factory.createIdentifier(KEYNAME.exports),
TypeScript.factory.createIdentifier(secondArgument.text),

@@ -129,3 +141,3 @@ ),

TypeScript.factory.createPropertyAccessExpression(
TypeScript.factory.createIdentifier(KEYNAME_EXPORTS),
TypeScript.factory.createIdentifier(KEYNAME.exports),
TypeScript.factory.createIdentifier(secondArgument.text),

@@ -251,4 +263,40 @@ ),

}
// exports = require('hello.js') -> export * from 'hello.js'
private convertForwards(
sourceFile: TypeScript.SourceFile,
): TypeScript.SourceFile {
const visit = (
node: TypeScript.Node,
): TypeScript.Node | TypeScript.Node[] => {
if (
TypeScript.isExpressionStatement(node) &&
TypeScript.isBinaryExpression(node.expression) &&
TypeScript.isIdentifier(node.expression.left) &&
node.expression.left.text === KEYNAME.exports &&
node.expression.operatorToken.kind ===
TypeScript.SyntaxKind.EqualsToken &&
TypeScript.isCallExpression(node.expression.right) &&
TypeScript.isIdentifier(node.expression.right.expression) &&
node.expression.right.arguments.length === 1
) {
const argument = node.expression.right.arguments[0]
if (TypeScript.isStringLiteralLike(argument)) {
return [
node,
TypeScript.factory.createExportDeclaration(
undefined,
undefined,
false,
undefined,
TypeScript.factory.createStringLiteral(argument.text),
),
]
}
}
return node
}
return TypeScript.visitEachChild(sourceFile, visit, this.context)
}
// exports.hello = something -> export { something as hello }
private convertToEsmExport(
private oldconvertToEsmExport(
sourceFile: TypeScript.SourceFile,

@@ -269,3 +317,3 @@ ): TypeScript.SourceFile {

TypeScript.isIdentifier(node.expression) &&
node.expression.text === KEYNAME_EXPORTS
node.expression.text === KEYNAME.exports
) {

@@ -325,11 +373,2 @@ if (node.name.text in this.exportsVariableMap) {

}
/*
return TypeScript.factory.createExpressionStatement(
TypeScript.factory.createBinaryExpression(
identifier,
node.expression.operatorToken,
node.expression.right,
),
)
*/
}

@@ -354,4 +393,4 @@ return TypeScript.visitEachChild(node, visit, this.context)

TypeScript.isIdentifier(node.name) &&
node.expression.text === 'module' &&
node.name.text === KEYNAME_EXPORTS
node.expression.text === KEYNAME.module &&
node.name.text === KEYNAME.exports
) {

@@ -379,3 +418,3 @@ return TypeScript.factory.createIdentifier(node.name.text)

TypeScript.isIdentifier(node.expression.left) &&
node.expression.left.text === KEYNAME_EXPORTS
node.expression.left.text === KEYNAME.exports
) {

@@ -395,3 +434,3 @@ // exports = { a: 'a' }

TypeScript.factory.createPropertyAccessExpression(
TypeScript.factory.createIdentifier(KEYNAME_EXPORTS),
TypeScript.factory.createIdentifier(KEYNAME.exports),
TypeScript.factory.createIdentifier(property.name.text),

@@ -415,3 +454,3 @@ ),

TypeScript.factory.createPropertyAccessExpression(
TypeScript.factory.createIdentifier(KEYNAME_EXPORTS),
TypeScript.factory.createIdentifier(KEYNAME.exports),
TypeScript.factory.createIdentifier('default'),

@@ -568,3 +607,3 @@ ),

],
TypeScript.NodeFlags.Const,
TypeScript.NodeFlags.Let,
),

@@ -575,2 +614,121 @@ ),

}
private getAllExportedNames(sourceFile: TypeScript.SourceFile): string[] {
const exportedNames: Set<string> = new Set()
const visit = (
node: TypeScript.Node,
): TypeScript.Node | TypeScript.Node[] => {
if (
TypeScript.isBinaryExpression(node) &&
node.operatorToken.kind === TypeScript.SyntaxKind.EqualsToken
) {
// exports.name = something
if (
TypeScript.isPropertyAccessExpression(node.left) &&
TypeScript.isIdentifier(node.left.expression) &&
node.left.expression.text === KEYNAME.exports &&
TypeScript.isIdentifier(node.left.name)
) {
exportedNames.add(node.left.name.text)
}
// exports = something
else if (
TypeScript.isIdentifier(node.left) &&
node.left.text === KEYNAME.exports
) {
// exports = { a: b }
if (TypeScript.isObjectLiteralExpression(node.right)) {
for (const property of node.right.properties) {
if (property.name && TypeScript.isIdentifier(property.name)) {
exportedNames.add(property.name.text)
}
}
}
}
}
return TypeScript.visitEachChild(node, visit, this.context)
}
TypeScript.visitEachChild(sourceFile, visit, this.context)
return Array.from(exportedNames)
}
private convertToEsmExport(
sourceFile: TypeScript.SourceFile,
): TypeScript.SourceFile {
const exportedNames = this.getAllExportedNames(sourceFile)
const moduleExportStatement = TypeScript.factory.createVariableStatement(
undefined,
TypeScript.factory.createVariableDeclarationList(
[
TypeScript.factory.createVariableDeclaration(
KEYNAME.exports,
undefined,
undefined,
TypeScript.factory.createObjectLiteralExpression(),
),
TypeScript.factory.createVariableDeclaration(
KEYNAME.module,
undefined,
undefined,
TypeScript.factory.createObjectLiteralExpression([
TypeScript.factory.createPropertyAssignment(
KEYNAME.exports,
TypeScript.factory.createIdentifier(KEYNAME.exports),
),
]),
),
],
TypeScript.NodeFlags.Let,
),
)
const exportDefaultStatement: TypeScript.ExportAssignment = TypeScript.factory.createExportAssignment(
undefined,
undefined,
undefined,
TypeScript.factory.createIdentifier(KEYNAME.exports),
)
const statements = [
moduleExportStatement,
...sourceFile.statements,
exportDefaultStatement,
]
if (exportedNames.length) {
const variableDeclarations: TypeScript.VariableDeclaration[] = []
const exportSpecifiers: TypeScript.ExportSpecifier[] = []
for (const name of exportedNames) {
const identifier = TypeScript.factory.createIdentifier(
this.generateUniqueName(),
)
exportSpecifiers.push(
TypeScript.factory.createExportSpecifier(identifier, name),
)
variableDeclarations.push(
TypeScript.factory.createVariableDeclaration(
identifier,
undefined,
undefined,
TypeScript.factory.createPropertyAccessExpression(
TypeScript.factory.createIdentifier(KEYNAME.exports),
TypeScript.factory.createIdentifier(name),
),
),
)
}
const exportDeclaration: TypeScript.ExportDeclaration = TypeScript.factory.createExportDeclaration(
undefined,
undefined,
false,
TypeScript.factory.createNamedExports(exportSpecifiers),
)
statements.push(
TypeScript.factory.createVariableStatement(
undefined,
TypeScript.factory.createVariableDeclarationList(
variableDeclarations,
TypeScript.NodeFlags.Const,
),
),
exportDeclaration,
)
}
return TypeScript.factory.updateSourceFile(sourceFile, statements)
}
}

@@ -104,3 +104,2 @@ import TypeScript from 'typescript'

).resolveRelativeDependency(node.text),
// this.resolveDependencyName(node.getSourceFile().fileName, node.text),
)

@@ -107,0 +106,0 @@ }

@@ -46,4 +46,2 @@ import TypeScript from 'typescript'

if (nodeResolve) {
// eslint-disable-next-line no-console
console.error(nodeResolve)
return nodeResolve

@@ -50,0 +48,0 @@ }

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc