Skip to content

Commit

Permalink
Fix printing of Flow internal slot functions (#14962)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Sep 22, 2022
1 parent 40a6d42 commit 624c78d
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -272,7 +272,7 @@ jobs:
!**/node_modules/**
test-babel-8-breaking:
name: Test Babel 8 breaking changes on Node.js
name: Test Babel 8 breaking changes on
needs: build-babel8
strategy:
matrix:
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-generator/src/generators/expressions.ts
Expand Up @@ -190,12 +190,14 @@ export function OptionalCallExpression(
) {
this.print(node.callee, node);

this.print(node.typeArguments, node); // Flow
this.print(node.typeParameters, node); // TS

if (node.optional) {
this.token("?.");
}

this.print(node.typeArguments, node); // Flow

this.token("(");
this.printList(node.arguments, node);
this.token(")");
Expand Down
1 change: 1 addition & 0 deletions packages/babel-generator/src/generators/flow.ts
Expand Up @@ -332,6 +332,7 @@ export function FunctionTypeAnnotation(
if (
parent &&
(parent.type === "ObjectTypeCallProperty" ||
parent.type === "ObjectTypeInternalSlot" ||
parent.type === "DeclareFunction" ||
(parent.type === "ObjectTypeProperty" && parent.method))
) {
Expand Down
@@ -1,5 +1,6 @@
{
"BABEL_8_BREAKING": false,
"plugins": [["decorators", { "decoratorsBeforeExport": false }]],
"decoratorsBeforeExport": true
"decoratorsBeforeExport": true,
"expectedReParseError": true
}
@@ -1,5 +1,6 @@
{
"BABEL_8_BREAKING": false,
"plugins": [["decorators", { "decoratorsBeforeExport": true }]],
"decoratorsBeforeExport": false
"decoratorsBeforeExport": false,
"expectedReParseError": true
}
Expand Up @@ -8,13 +8,13 @@ interface I1 {
[[foo]]: X
}
interface I2 {
[[foo]]() => X
[[foo]](): X
}
type T1 = {
[[foo]]: X
};
type T2 = {
[[foo]]() => X
[[foo]](): X
};
type T3 = {
[[foo]]?: X
Expand Down
Expand Up @@ -16,6 +16,6 @@ new C<T>();
o.m<T>();
f < T > .0;
o?.m<T>(e);
o.m<T>?.(e);
o.m?.<T>(e);
async<T>();
f<T>?.(e);
f?.<T>(e);
@@ -1 +1,5 @@
{ "plugins": ["decorators-legacy"] }
{
"plugins": ["decorators-legacy"],
"decoratorsBeforeExport": true,
"BABEL_8_BREAKING": false
}
Expand Up @@ -32,15 +32,15 @@ class Foo {

}

export default @foo
class Foo2 {
@foo
export default class Foo2 {
bar() {
class Baz {}
}

}
export @foo
class Foo3 {
@foo
export class Foo3 {
bar() {
class Baz {}
}
Expand Down
12 changes: 9 additions & 3 deletions packages/babel-generator/test/index.js
Expand Up @@ -811,14 +811,15 @@ suites.forEach(function (testSuite) {
const actualCode = actual.code;

if (actualCode) {
const actualAst = parse(actualCode, {
const parserOpts = {
filename: actual.loc,
plugins: task.options.plugins || [],
strictMode: task.options.strictMode === false ? false : true,
sourceType: "module",
sourceMaps: !!task.sourceMap,
...task.options.parserOpts,
});
};
const actualAst = parse(actualCode, parserOpts);
const options = {
sourceFileName: path.relative(
path.dirname(fileURLToPath(import.meta.url)),
Expand All @@ -832,7 +833,7 @@ suites.forEach(function (testSuite) {
return generate(actualAst, options, actualCode);
};

const throwMsg = task.options.throws;
const throwMsg = options.throws;
if (throwMsg) {
expect(() => run()).toThrow(
throwMsg === true ? undefined : throwMsg,
Expand Down Expand Up @@ -864,6 +865,11 @@ suites.forEach(function (testSuite) {
} else {
try {
expect(result.code).toBe(expected.code);
if (!options.expectedReParseError) {
expect(() => {
parse(result.code, parserOpts);
}).not.toThrow();
}
} catch (e) {
if (!process.env.OVERWRITE) throw e;
console.log(`Updated test file: ${expected.loc}`);
Expand Down

0 comments on commit 624c78d

Please sign in to comment.