@ts-liveserver/ts-transpiler
Advanced tools
Comparing version 0.0.15 to 0.0.16
@@ -22,2 +22,6 @@ import CommonJsTransformer from '../../src/transformers/CommonJsTransformer' | ||
}) | ||
it('Should convert require without reference in sub-scope', async () => { | ||
const input = 'if(true) { require("hello.js"); }' | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
}) | ||
}) | ||
@@ -88,2 +92,6 @@ describe('Forwards', () => { | ||
}) | ||
it('Should export one default', async () => { | ||
const input = 'exports.default = "hello"' | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
}) | ||
it('Should export nested expression', async () => { | ||
@@ -90,0 +98,0 @@ const input = ` |
@@ -292,7 +292,13 @@ "use strict"; | ||
inRootScope === false) { | ||
const newIdentifierName = this.generateUniqueName(); | ||
newStatements.push(typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([ | ||
typescript_1.default.factory.createVariableDeclaration(newIdentifierName, undefined, undefined, node), | ||
], typescript_1.default.NodeFlags.Let))); | ||
return typescript_1.default.factory.createIdentifier(newIdentifierName); | ||
if (node.parent && typescript_1.default.isExpressionStatement(node.parent)) { | ||
newStatements.push(typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(node.expression, undefined, node.arguments))); | ||
return typescript_1.default.factory.createVoidExpression(typescript_1.default.factory.createNumericLiteral('0')); | ||
} | ||
else { | ||
const newIdentifierName = this.generateUniqueName(); | ||
newStatements.push(typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([ | ||
typescript_1.default.factory.createVariableDeclaration(newIdentifierName, undefined, undefined, node), | ||
], typescript_1.default.NodeFlags.Let))); | ||
return typescript_1.default.factory.createIdentifier(newIdentifierName); | ||
} | ||
} | ||
@@ -299,0 +305,0 @@ return typescript_1.default.visitEachChild(node, visit, this.context); |
{ | ||
"name": "@ts-liveserver/ts-transpiler", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -459,4 +459,9 @@ import TypeScript from 'typescript' | ||
): TypeScript.SourceFile { | ||
const newStatements: TypeScript.VariableStatement[] = [] | ||
const visit = (node: TypeScript.Node): TypeScript.Node => { | ||
const newStatements: ( | ||
| TypeScript.VariableStatement | ||
| TypeScript.ExpressionStatement | ||
)[] = [] | ||
const visit = ( | ||
node: TypeScript.Node, | ||
): TypeScript.Node | TypeScript.Node[] | undefined => { | ||
const inRootScope = | ||
@@ -475,20 +480,35 @@ node?.parent?.parent?.parent?.parent && | ||
) { | ||
const newIdentifierName = this.generateUniqueName() | ||
newStatements.push( | ||
TypeScript.factory.createVariableStatement( | ||
undefined, | ||
TypeScript.factory.createVariableDeclarationList( | ||
[ | ||
TypeScript.factory.createVariableDeclaration( | ||
newIdentifierName, | ||
undefined, | ||
undefined, | ||
node, | ||
), | ||
], | ||
TypeScript.NodeFlags.Let, | ||
if (node.parent && TypeScript.isExpressionStatement(node.parent)) { | ||
newStatements.push( | ||
TypeScript.factory.createExpressionStatement( | ||
TypeScript.factory.createCallExpression( | ||
node.expression, | ||
undefined, | ||
node.arguments, | ||
), | ||
), | ||
), | ||
) | ||
return TypeScript.factory.createIdentifier(newIdentifierName) | ||
) | ||
return TypeScript.factory.createVoidExpression( | ||
TypeScript.factory.createNumericLiteral('0'), | ||
) | ||
} else { | ||
const newIdentifierName = this.generateUniqueName() | ||
newStatements.push( | ||
TypeScript.factory.createVariableStatement( | ||
undefined, | ||
TypeScript.factory.createVariableDeclarationList( | ||
[ | ||
TypeScript.factory.createVariableDeclaration( | ||
newIdentifierName, | ||
undefined, | ||
undefined, | ||
node, | ||
), | ||
], | ||
TypeScript.NodeFlags.Let, | ||
), | ||
), | ||
) | ||
return TypeScript.factory.createIdentifier(newIdentifierName) | ||
} | ||
} | ||
@@ -495,0 +515,0 @@ return TypeScript.visitEachChild(node, visit, this.context) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
146994
2688