Skip to content

Commit a118848

Browse files
authored
fix: do babel transformations for script, not module MONGOSH-986 (#1103)
Tell babel that we are targeting script mode, not ES module mode. (Note that the babel docs indicate that `script` should be the default – it seems that that is not entirely accurate.) This fixes the linked bug because unlike ES modules, script mode does not automatically also enable strict mode. This also fixes a minor bug in the wrap-as-iife babel plugin, where if the input was a single string literal, escaped characters would have remained escaped (e.g., entering `'\x41'` in mongosh would have returned `\x41`, not 'A'), by keeping the existing information about the raw source instead of generating an entirely new string literal in that case.
1 parent 2f4d3ec commit a118848

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

packages/async-rewriter2/src/async-writer-babel.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ describe('AsyncWriter', () => {
120120
it('runs code in sloppy mode by default', () => {
121121
expect(runTranspiledCode('delete Object.prototype')).to.equal(false);
122122
});
123+
124+
it('parses code in sloppy mode by default', () => {
125+
expect(runTranspiledCode('"<\\101>"')).to.equal('<A>');
126+
expect(runTranspiledCode('"x" + "<\\101>"')).to.equal('x<A>');
127+
});
128+
129+
it('parses code in strict mode if strict mode is explicitly enabled', () => {
130+
expect(() => runTranspiledCode('"use strict"; "<\\101>"')).to.throw(SyntaxError);
131+
});
123132
});
124133

125134
context('scoping', () => {

packages/async-rewriter2/src/async-writer-babel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export default class AsyncWriter {
3232
code: true,
3333
configFile: false,
3434
babelrc: false,
35-
compact: code.length > 10_000
35+
compact: code.length > 10_000,
36+
sourceType: 'script'
3637
})?.code as string;
3738
}
3839

packages/async-rewriter2/src/stages/wrap-as-iife.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export default ({ types: t }: { types: typeof BabelTypes }): babel.PluginObj<Wra
135135
path.node.body.length === 0) {
136136
path.replaceWith(t.program([
137137
t.expressionStatement(
138-
t.stringLiteral(path.node.directives[0].value.value))
138+
{ ...path.node.directives[0].value, type: 'StringLiteral' })
139139
]));
140140
}
141141
},

packages/cli-repl/src/js-multiline-to-singleline.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export function makeMultilineJSIntoSingleLine(src: string): string {
5757
compact: false,
5858
code: true,
5959
comments: true,
60-
plugins: [lineCommentToBlockComment]
60+
plugins: [lineCommentToBlockComment],
61+
sourceType: 'script'
6162
})?.code ?? src;
6263
} catch {
6364
// The src might still be invalid, e.g. because a recoverable error was not fixed

packages/cli-repl/test/e2e.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ describe('e2e', function() {
136136
const err = await shell.executeLine('parcelRequire');
137137
expect(err).to.match(/ReferenceError: parcelRequire is not defined/);
138138
});
139+
it('parses code in sloppy mode by default (single line)', async() => {
140+
const result = await shell.executeLine('"<\\101>"');
141+
expect(result).to.match(/<A>/);
142+
});
143+
it('parses code in sloppy mode by default (multiline)', async() => {
144+
const result = await shell.executeLine('"a"+\n"<\\101>"');
145+
expect(result).to.match(/a<A>/);
146+
});
139147
});
140148
describe('set db', () => {
141149
for (const { mode, dbname, dbnameUri } of [

0 commit comments

Comments
 (0)