Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/async-rewriter2/src/async-writer-babel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ describe('AsyncWriter', () => {
});
});

context('use strict', () => {
it('parses a plain "use strict"; as a string literal', () => {
expect(runTranspiledCode('"use strict"')).to.equal('use strict');
expect(runTranspiledCode('"use strict";')).to.equal('use strict');
expect(runTranspiledCode("'use strict'")).to.equal('use strict');
expect(runTranspiledCode("'use strict';")).to.equal('use strict');
});

it('runs code that starts with "use strict"', () => {
expect(runTranspiledCode("'use strict'; 144 + 233;")).to.equal(377);
});

it('fails to run invalid strict-mode code', () => {
try {
runTranspiledCode("'use strict'; delete Object.prototype");
expect.fail('missed exception');
} catch (err) {
expect(err.name).to.equal('TypeError');
}
});

it('runs code in sloppy mode by default', () => {
expect(runTranspiledCode('delete Object.prototype')).to.equal(false);
});
});

context('scoping', () => {
it('adds functions to the global scope as expected', () => {
const f = runTranspiledCode('function f() {}');
Expand Down
26 changes: 15 additions & 11 deletions packages/async-rewriter2/src/stages/wrap-as-iife.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ export default ({ types: t }: { types: typeof BabelTypes }): babel.PluginObj<Wra
Program: {
enter(path) {
// If the body of the program consists of a single string literal,
// we want to intepret it as such and not as a a directive (that is,
// we want to intepret it as such and not as a directive (that is,
// a "use strict"-like thing).
if (path.node.directives.length === 1 &&
path.node.directives[0].value.type === 'DirectiveLiteral') {
path.node.directives[0].value.type === 'DirectiveLiteral' &&
path.node.body.length === 0) {
path.replaceWith(t.program([
t.expressionStatement(
t.stringLiteral(path.node.directives[0].value.value))
Expand All @@ -147,15 +148,18 @@ export default ({ types: t }: { types: typeof BabelTypes }): babel.PluginObj<Wra
this.completionRecordId = path.scope.generateUidIdentifier('cr');
this.movedStatements.unshift(
t.variableDeclaration('var', [t.variableDeclarator(this.completionRecordId)]));
path.replaceWith(t.program([
...this.variables.map(
v => t.variableDeclaration('var', [t.variableDeclarator(t.identifier(v))])),
...this.functionDeclarations,
t.expressionStatement(t.callExpression(
t.arrowFunctionExpression(
[],
t.blockStatement(this.movedStatements)
), []))]));
path.replaceWith(t.program(
[
...this.variables.map(
v => t.variableDeclaration('var', [t.variableDeclarator(t.identifier(v))])),
...this.functionDeclarations,
t.expressionStatement(t.callExpression(
t.arrowFunctionExpression(
[],
t.blockStatement(this.movedStatements)
), []))
],
path.node.directives));
}
},
BlockStatement: {
Expand Down