Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement decorators as presented at 2023-01 TC39 meeting #15405

Merged
merged 19 commits into from Feb 18, 2023
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.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
25 changes: 13 additions & 12 deletions Makefile
@@ -1,6 +1,6 @@
FLOW_COMMIT = 92bbb5e9dacb8185aa73ea343954d0434b42c40b
TEST262_COMMIT = a29788dd5d4d7664478985928e82f2a1cd7fb37d
TYPESCRIPT_COMMIT = ce85d647ef88183c019588bcf398320ce29b625a
FLOW_COMMIT = 105ad30f566f401db9cafcb49cd2831fb29e87c5
TEST262_COMMIT = 76a14bf659be962dd0efa694c4f9fc12f159f774
TYPESCRIPT_COMMIT = d87d0adcd30ac285393bf3bfbbb4d94d50c4f3c9

# Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967
export FORCE_COLOR = true
Expand Down Expand Up @@ -177,15 +177,16 @@ test-test262-update-allowlist:


new-version-checklist:
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
# @echo "!!!!!! !!!!!!"
# @echo "!!!!!! Add any message here, and UNCOMMENT THESE LINES! !!!!!!"
# @echo "!!!!!! !!!!!!"
# @echo "!!!!!! !!!!!!"
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
# @exit 1
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
@echo "!!!!!! !!!!!!"
@echo "!!!!!! Update the version to 7.21.0 in the applyDecs2301 !!!!!!"
@echo "!!!!!! helper, and in the assertVersion call for 2023-01 !!!!!!"
@echo "!!!!!! in transformer-2023-01.ts !!!!!!"
@echo "!!!!!! !!!!!!"
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
@exit 1

new-version:
$(MAKE) new-version-checklist
Expand Down
17 changes: 9 additions & 8 deletions packages/babel-generator/src/generators/classes.ts
Expand Up @@ -11,15 +11,16 @@ export function ClassDeclaration(
node: t.ClassDeclaration,
parent: t.Node,
) {
if (process.env.BABEL_8_BREAKING) {
const inExport =
isExportDefaultDeclaration(parent) || isExportNamedDeclaration(parent);

if (
!inExport ||
!this._shouldPrintDecoratorsBeforeExport(
parent as t.ExportDeclaration & { declaration: t.ClassDeclaration },
)
) {
this.printJoin(node.decorators, node);
} else {
if (
!this.format.decoratorsBeforeExport ||
(!isExportDefaultDeclaration(parent) && !isExportNamedDeclaration(parent))
) {
this.printJoin(node.decorators, node);
}
}

if (node.declare) {
Expand Down
12 changes: 12 additions & 0 deletions packages/babel-generator/src/generators/expressions.ts
Expand Up @@ -140,6 +140,18 @@ function shouldParenthesizeDecoratorExpression(
);
}

export function _shouldPrintDecoratorsBeforeExport(
this: Printer,
node: t.ExportDeclaration & { declaration: t.ClassDeclaration },
) {
if (typeof this.format.decoratorsBeforeExport === "boolean") {
return this.format.decoratorsBeforeExport;
}
return (
typeof node.start === "number" && node.start === node.declaration.start
);
}

export function Decorator(this: Printer, node: t.Decorator) {
this.token("@");
const { expression } = node;
Expand Down
32 changes: 16 additions & 16 deletions packages/babel-generator/src/generators/modules.ts
Expand Up @@ -106,18 +106,25 @@ export function ExportAllDeclaration(
this.semicolon();
}

function maybePrintDecoratorsBeforeExport(
printer: Printer,
node: t.ExportNamedDeclaration | t.ExportDefaultDeclaration,
) {
if (
isClassDeclaration(node.declaration) &&
printer._shouldPrintDecoratorsBeforeExport(
node as t.ExportNamedDeclaration & { declaration: t.ClassDeclaration },
)
) {
printer.printJoin(node.declaration.decorators, node);
}
}

export function ExportNamedDeclaration(
this: Printer,
node: t.ExportNamedDeclaration,
) {
if (!process.env.BABEL_8_BREAKING) {
if (
this.format.decoratorsBeforeExport &&
isClassDeclaration(node.declaration)
) {
this.printJoin(node.declaration.decorators, node);
}
}
maybePrintDecoratorsBeforeExport(this, node);

this.word("export");
this.space();
Expand Down Expand Up @@ -183,14 +190,7 @@ export function ExportDefaultDeclaration(
this: Printer,
node: t.ExportDefaultDeclaration,
) {
if (!process.env.BABEL_8_BREAKING) {
if (
this.format.decoratorsBeforeExport &&
isClassDeclaration(node.declaration)
) {
this.printJoin(node.declaration.decorators, node);
}
}
maybePrintDecoratorsBeforeExport(this, node);

this.word("export");
this.noIndentInnerCommentsHere();
Expand Down
7 changes: 4 additions & 3 deletions packages/babel-generator/src/index.ts
Expand Up @@ -76,7 +76,7 @@ function normalizeOptions(
};

if (!process.env.BABEL_8_BREAKING) {
format.decoratorsBeforeExport = !!opts.decoratorsBeforeExport;
format.decoratorsBeforeExport = opts.decoratorsBeforeExport;
format.jsonCompatibleStrings = opts.jsonCompatibleStrings;
}

Expand Down Expand Up @@ -200,8 +200,9 @@ export interface GeneratorOptions {
jsonCompatibleStrings?: boolean;

/**
* Set to true to enable support for experimental decorators syntax before module exports.
* Defaults to `false`.
* Set to true to enable support for experimental decorators syntax before
* module exports. If not specified, decorators will be printed in the same
* position as they were in the input source code.
* @deprecated Removed in Babel 8
*/
decoratorsBeforeExport?: boolean;
Expand Down
@@ -0,0 +1,4 @@
{
"plugins": ["decorators"],
"decoratorsBeforeExport": false
}
@@ -0,0 +1,5 @@
/* 1 */ export /* 2 */ @dec1 /* 3 */ @dec2
/* 4 */ class /* 5 */ C /* 6 */ { /* 7 */ } /* 8 */

/* A */ export /* B */ default /* C */ @dec1 /* D */ @dec2
/* E */ class /* F */ { /* G */ } /* H */
@@ -0,0 +1,5 @@
{
"BABEL_8_BREAKING": false,
"plugins": ["decorators"],
"decoratorsBeforeExport": true
}
@@ -0,0 +1,9 @@
/* 1 */@dec1
/* 3 */@dec2
/* 4 */export /* 2 */class /* 5 */C /* 6 */ {/* 7 */} /* 8 */

/* A */
@dec1
/* D */@dec2
/* E */export /* B */
default /* C */class /* F */{/* G */} /* H */
@@ -0,0 +1,5 @@
{
"BABEL_8_BREAKING": false,
"plugins": ["decorators"],
"decoratorsBeforeExport": false
}
@@ -0,0 +1,9 @@
/* 1 */export /* 4 */@dec1
/* 2 */@dec2
/* 3 */class /* 5 */C /* 6 */ {/* 7 */} /* 8 */

/* A */
export
/* D */ default /* E */@dec1
/* B */@dec2
/* C */class /* F */{/* G */} /* H */
@@ -0,0 +1,5 @@
/* 1 */ @dec1 /* 2 */ @dec2 /* 3 */
export /* 4 */ class /* 5 */ C /* 6 */ { /* 7 */ } /* 8 */

/* A */ @dec1 /* B */ @dec2 /* C */
export /* D */ default /* E */ class /* F */ { /* G */ } /* H */
@@ -0,0 +1,5 @@
{
"BABEL_8_BREAKING": false,
"plugins": ["decorators"],
"decoratorsBeforeExport": true
}
@@ -0,0 +1,2 @@
@dec export class A {}
@dec export default class {}
@@ -0,0 +1,4 @@
@dec
export class A {}
@dec
export default class {}
@@ -0,0 +1,2 @@
export @dec class A {}
export default @dec class {}
@@ -0,0 +1,4 @@
export @dec
class A {}
export default @dec
class {}
@@ -1,4 +1,3 @@
{
"plugins": [["decorators", { "decoratorsBeforeExport": false }]],
"decoratorsBeforeExport": true
"plugins": ["decorators"]
}
6 changes: 5 additions & 1 deletion packages/babel-helpers/src/helpers-generated.ts

Large diffs are not rendered by default.