Skip to content

Commit 624c78d

Browse files
authored
Fix printing of Flow internal slot functions (#14962)
1 parent 40a6d42 commit 624c78d

10 files changed

Lines changed: 31 additions & 16 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ jobs:
272272
!**/node_modules/**
273273
274274
test-babel-8-breaking:
275-
name: Test Babel 8 breaking changes on Node.js
275+
name: Test Babel 8 breaking changes on
276276
needs: build-babel8
277277
strategy:
278278
matrix:

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,14 @@ export function OptionalCallExpression(
190190
) {
191191
this.print(node.callee, node);
192192

193-
this.print(node.typeArguments, node); // Flow
194193
this.print(node.typeParameters, node); // TS
195194

196195
if (node.optional) {
197196
this.token("?.");
198197
}
198+
199+
this.print(node.typeArguments, node); // Flow
200+
199201
this.token("(");
200202
this.printList(node.arguments, node);
201203
this.token(")");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ export function FunctionTypeAnnotation(
332332
if (
333333
parent &&
334334
(parent.type === "ObjectTypeCallProperty" ||
335+
parent.type === "ObjectTypeInternalSlot" ||
335336
parent.type === "DeclareFunction" ||
336337
(parent.type === "ObjectTypeProperty" && parent.method))
337338
) {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"BABEL_8_BREAKING": false,
33
"plugins": [["decorators", { "decoratorsBeforeExport": false }]],
4-
"decoratorsBeforeExport": true
4+
"decoratorsBeforeExport": true,
5+
"expectedReParseError": true
56
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"BABEL_8_BREAKING": false,
33
"plugins": [["decorators", { "decoratorsBeforeExport": true }]],
4-
"decoratorsBeforeExport": false
4+
"decoratorsBeforeExport": false,
5+
"expectedReParseError": true
56
}

packages/babel-generator/test/fixtures/flow/internal-slot/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ interface I1 {
88
[[foo]]: X
99
}
1010
interface I2 {
11-
[[foo]]() => X
11+
[[foo]](): X
1212
}
1313
type T1 = {
1414
[[foo]]: X
1515
};
1616
type T2 = {
17-
[[foo]]() => X
17+
[[foo]](): X
1818
};
1919
type T3 = {
2020
[[foo]]?: X

packages/babel-generator/test/fixtures/flow/typeapp-call/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ new C<T>();
1616
o.m<T>();
1717
f < T > .0;
1818
o?.m<T>(e);
19-
o.m<T>?.(e);
19+
o.m?.<T>(e);
2020
async<T>();
21-
f<T>?.(e);
21+
f?.<T>(e);
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
{ "plugins": ["decorators-legacy"] }
1+
{
2+
"plugins": ["decorators-legacy"],
3+
"decoratorsBeforeExport": true,
4+
"BABEL_8_BREAKING": false
5+
}

packages/babel-generator/test/fixtures/types/Decorator/output.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ class Foo {
3232

3333
}
3434

35-
export default @foo
36-
class Foo2 {
35+
@foo
36+
export default class Foo2 {
3737
bar() {
3838
class Baz {}
3939
}
4040

4141
}
42-
export @foo
43-
class Foo3 {
42+
@foo
43+
export class Foo3 {
4444
bar() {
4545
class Baz {}
4646
}

packages/babel-generator/test/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,14 +811,15 @@ suites.forEach(function (testSuite) {
811811
const actualCode = actual.code;
812812

813813
if (actualCode) {
814-
const actualAst = parse(actualCode, {
814+
const parserOpts = {
815815
filename: actual.loc,
816816
plugins: task.options.plugins || [],
817817
strictMode: task.options.strictMode === false ? false : true,
818818
sourceType: "module",
819819
sourceMaps: !!task.sourceMap,
820820
...task.options.parserOpts,
821-
});
821+
};
822+
const actualAst = parse(actualCode, parserOpts);
822823
const options = {
823824
sourceFileName: path.relative(
824825
path.dirname(fileURLToPath(import.meta.url)),
@@ -832,7 +833,7 @@ suites.forEach(function (testSuite) {
832833
return generate(actualAst, options, actualCode);
833834
};
834835

835-
const throwMsg = task.options.throws;
836+
const throwMsg = options.throws;
836837
if (throwMsg) {
837838
expect(() => run()).toThrow(
838839
throwMsg === true ? undefined : throwMsg,
@@ -864,6 +865,11 @@ suites.forEach(function (testSuite) {
864865
} else {
865866
try {
866867
expect(result.code).toBe(expected.code);
868+
if (!options.expectedReParseError) {
869+
expect(() => {
870+
parse(result.code, parserOpts);
871+
}).not.toThrow();
872+
}
867873
} catch (e) {
868874
if (!process.env.OVERWRITE) throw e;
869875
console.log(`Updated test file: ${expected.loc}`);

0 commit comments

Comments
 (0)