Skip to content

Commit 98c3bb9

Browse files
authored
Improve module expression parsing/printing (#14980)
* fix: several ModuleExpression printing edgecases * fix: end module expression's program before } * fix: expect braceL after module * refactor: introduce ensureNoLineTerminator * use this.ensureNoLineTerminator * update source-map fixtures * address review comments * consume eof when end is eof
1 parent 5729ce9 commit 98c3bb9

29 files changed

Lines changed: 80 additions & 33 deletions

File tree

packages/babel-generator/src/generators/expressions.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,19 @@ export function V8IntrinsicIdentifier(
351351

352352
export function ModuleExpression(this: Printer, node: t.ModuleExpression) {
353353
this.word("module");
354-
this.space();
354+
// ensure no line terminator between `module` and `{`
355+
this.ensureNoLineTerminator(() => {
356+
this.printInnerComments(node);
357+
this.space();
358+
});
355359
this.token("{");
356-
if (node.body.body.length === 0) {
357-
this.token("}");
358-
} else {
360+
this.indent();
361+
const { body } = node;
362+
if (body.body.length || body.directives.length) {
359363
this.newline();
360-
this.printSequence(node.body.body, node, { indent: true });
361-
362-
this.sourceWithOffset("end", node.loc, 0, -1);
363-
364-
this.rightBrace();
365364
}
365+
this.print(body, node);
366+
this.dedent();
367+
this.sourceWithOffset("end", node.loc, 0, -1);
368+
this.rightBrace();
366369
}

packages/babel-generator/src/printer.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,13 @@ class Printer {
495495
return this._indentRepeat * this._indent;
496496
}
497497

498+
ensureNoLineTerminator(fn: () => void) {
499+
const { _noLineTerminator } = this;
500+
this._noLineTerminator = true;
501+
fn();
502+
this._noLineTerminator = _noLineTerminator;
503+
}
504+
498505
printTerminatorless(node: t.Node, parent: t.Node, isLabel: boolean) {
499506
/**
500507
* Set some state that will be modified if a newline has been inserted before any
@@ -512,9 +519,9 @@ class Printer {
512519
* `undefined` will be returned and not `foo` due to the terminator.
513520
*/
514521
if (isLabel) {
515-
this._noLineTerminator = true;
516-
this.print(node, parent);
517-
this._noLineTerminator = false;
522+
this.ensureNoLineTerminator(() => {
523+
this.print(node, parent);
524+
});
518525
} else {
519526
const terminatorState = {
520527
printed: false,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module /* 1 */ { /* 2 */ }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["moduleBlocks"]
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module /* 1 */ {
2+
/* 2 */
3+
};

packages/babel-generator/test/fixtures/sourcemaps/ModuleExpression/source-map.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"sourcesContent": [
1212
"const m = module { export const foo = \"foo\" };\nmodule {\n foo;\n bar;\n};\nfoo(module {});"
1313
],
14-
"mappings": "AAAA,MAAMA,CAAC,GAAG;EAAS,OAAO,MAAMC,GAAG,GAAG,KAAK;AAAC,CAAC;AAC7C;EACEA,GAAG;EACHC,GAAG;AACL,CAAC;AACDD,GAAG,CAAC,SAAS,CAAC"
14+
"mappings": "AAAA,MAAMA,CAAC,GAAG;EAAS,OAAO,MAAMC,GAAG,GAAG,KAAK;AAAC,CAAC;AAC7C;EACEA,GAAG;EACHC,GAAG;AACL,CAAC;AACDD,GAAG,CAAC,QAAQ,CAAC,CAAC"
1515
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module {
2+
"hide source";
3+
secret;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"plugins": ["moduleBlocks"],
3+
"sourceType": "module"
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module {
2+
"hide source";
3+
4+
secret;
5+
};

packages/babel-parser/src/parser/expression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3233,7 +3233,7 @@ export default abstract class ExpressionParser extends LValParser {
32333233
this.expectPlugin("moduleBlocks");
32343234
const node = this.startNode<N.ModuleExpression>();
32353235
this.next(); // eat "module"
3236-
this.eat(tt.braceL);
3236+
this.expect(tt.braceL);
32373237

32383238
const revertScopes = this.initializeScopes(/** inModule */ true);
32393239
this.enterInitialScopes();

0 commit comments

Comments
 (0)