Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint/babel-eslint-shared-fixtures/config/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
plugins: [
"@babel/plugin-syntax-export-default-from",
"@babel/plugin-transform-class-properties",
["@babel/plugin-proposal-decorators", { version: "2023-05" }],
["@babel/plugin-proposal-decorators", { version: "2023-11" }],
"@babel/plugin-transform-private-methods",
"@babel/plugin-proposal-do-expressions",
"@babel/plugin-transform-explicit-resource-management",
Expand Down
3 changes: 0 additions & 3 deletions packages/babel-generator/src/generators/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ 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
);
Expand Down
4 changes: 0 additions & 4 deletions packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ export type Format = {
* Changes what token is used for pipe bodies’ topic references.
*/
topicToken?: GeneratorOptions["topicToken"];
/**
* @deprecated Removed in Babel 8
*/
decoratorsBeforeExport?: boolean;
/**
* The import attributes syntax style:
* - "with" : `import { a } from "b" with { type: "json" };`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class C {
@dec(0)
@globalThis.dec(0)
@(globalThis["dec"])(0)
@((globalThis["dec"])(0))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this expected?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, now you cannot have a parenthesized expression followed by call arguments. The whole thing needs to be in the parentheses.

p
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ class C extends class {} {
p;
}

class ShouldAddParens {
// NOTE: This is not actually valid syntax in newer versions of the proposal
@(decs[three])()
p;
}

class WillPreserveParens {
@(decs)
@(decs.one)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"plugins": [["decorators", { "decoratorsBeforeExport": false }]],
"decoratorsBeforeExport": true,
"plugins": ["decorators"],
"parserOpts": { "createParenthesizedExpressions": true }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ class C extends class {} {
@decs.two()
p;
}
class ShouldAddParens {
// NOTE: This is not actually valid syntax in newer versions of the proposal
@((decs[three])())
p;
}
class WillPreserveParens {
@(decs)
@(decs.one)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ class C extends class {} {
p;
}

class ShouldAddParens {
@(decs[three])()
p;
}

class ShouldRemoveParens {
@(decs)
@(decs.one)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ class C extends class {} {
@decs.two()
p;
}
class ShouldAddParens {
@(decs[three]())
p;
}
class ShouldRemoveParens {
@decs
@decs.one
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"retainLines": true,
"plugins": [["decorators", { "decoratorsBeforeExport": true }]]
"plugins": ["decorators"]
}
3 changes: 0 additions & 3 deletions packages/babel-generator/test/preserve-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ const FAILURES = [
// or about an old decorators version
"comments/decorators-after-export-to-before/input.js",
"comments/decorators-before-export-to-after/input.js",
"decorators/decorator-call-expression/input.js",
"decorators/decorator-parenthesized-expression/input.js",
"decorators/decorator-parenthesized-expression-createParenthesizedExpression/input.js",
"decoratorsBeforeExport/false-to-true/input.js",
"decoratorsBeforeExport/true-to-false/input.js",

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,7 @@ type ClassElementCanHaveComputedKeys =
| t.ClassProperty
| t.ClassAccessorProperty;

// TODO(Babel 8): Only keep 2023-11
export type DecoratorVersionKind =
| "2023-11"
| "2023-05"
| "2023-01"
| "2022-03"
| "2021-12";
export type DecoratorVersionKind = "2023-11";

function incrementId(id: number[], idx = id.length - 1): void {
// If index is -1, id needs an additional character, unshift A
Expand Down Expand Up @@ -214,10 +208,8 @@ function addProxyAccessorsFor(
targetKey: t.PrivateName,
isComputed: boolean,
isStatic: boolean,
version: DecoratorVersionKind,
): void {
const thisArg =
version === "2023-11" && isStatic ? className : t.thisExpression();
const thisArg = isStatic ? className : t.thisExpression();

const getterBody = t.blockStatement([
t.returnStatement(
Expand Down Expand Up @@ -272,22 +264,7 @@ function addProxyAccessorsFor(

function extractProxyAccessorsFor(
targetKey: t.PrivateName,
version: DecoratorVersionKind,
): (t.FunctionExpression | t.ArrowFunctionExpression)[] {
if (version !== "2023-11" && version !== "2023-05" && version !== "2023-01") {
return [
template.expression.ast`
function () {
return this.${t.cloneNode(targetKey)};
}
` as t.FunctionExpression,
template.expression.ast`
function (value) {
this.${t.cloneNode(targetKey)} = value;
}
` as t.FunctionExpression,
];
}
return [
template.expression.ast`
o => o.${t.cloneNode(targetKey)}
Expand Down Expand Up @@ -604,7 +581,6 @@ const METHOD = 2;
const GETTER = 3;
const SETTER = 4;

const STATIC_OLD_VERSION = 5; // Before 2023-05
const STATIC = 8; // 1 << 3
const DECORATORS_HAVE_THIS = 16; // 1 << 4

Expand Down Expand Up @@ -684,19 +660,17 @@ type GenerateDecorationListResult = {
*
* @param {t.Decorator[]} decorators
* @param {((t.Expression | undefined)[])} decoratorsThis decorator this values
* @param {DecoratorVersionKind} version
* @returns {GenerateDecorationListResult}
*/
function generateDecorationList(
decorators: t.Decorator[],
decoratorsThis: (t.Expression | undefined)[],
version: DecoratorVersionKind,
): GenerateDecorationListResult {
const decsCount = decorators.length;
const haveOneThis = decoratorsThis.some(Boolean);
const decs: t.Expression[] = [];
for (let i = 0; i < decsCount; i++) {
if (version === "2023-11" && haveOneThis) {
if (haveOneThis) {
decs.push(
decoratorsThis[i] || t.unaryExpression("void", t.numericLiteral(0)),
);
Expand All @@ -709,13 +683,12 @@ function generateDecorationList(

function generateDecorationExprs(
decorationInfo: DecoratorInfo[],
version: DecoratorVersionKind,
): t.ArrayExpression {
return t.arrayExpression(
decorationInfo.map(el => {
let flag = el.kind;
if (el.isStatic) {
flag += version === "2023-11" ? STATIC : STATIC_OLD_VERSION;
flag += STATIC;
}
if (el.decoratorsHaveThis) flag += DECORATORS_HAVE_THIS;

Expand Down Expand Up @@ -746,7 +719,6 @@ function extractElementLocalAssignments(decorationInfo: DecoratorInfo[]) {
}

function addCallAccessorsFor(
version: DecoratorVersionKind,
element: NodePath,
key: t.PrivateName,
getId: t.Identifier,
Expand Down Expand Up @@ -1011,7 +983,6 @@ function checkPrivateMethodUpdateError(
* - If className is an Identifier, it is the reference to the name derived from NamedEvaluation
* - If className is a StringLiteral, it is derived from NamedEvaluation on literal computed keys
* @param propertyVisitor The visitor that should be applied on property prior to the transform.
* @param version The decorator version.
* @returns The transformed class path or undefined if there are no decorators.
*/
function transformClass(
Expand All @@ -1021,7 +992,6 @@ function transformClass(
ignoreFunctionLength: boolean,
className: string | t.Identifier | t.StringLiteral | undefined,
propertyVisitor: Visitor<PluginPass>,
version: DecoratorVersionKind,
): NodePath | undefined {
const body = path.get("body.body");

Expand Down Expand Up @@ -1105,9 +1075,7 @@ function transformClass(
element as NodePath<t.ClassAccessorProperty>,
state,
);
if (version === "2023-11") {
break;
}
break;
/* fallthrough */
default:
if (elementNode.static) {
Expand Down Expand Up @@ -1163,7 +1131,6 @@ function transformClass(
newId,
computed,
isStatic,
version,
);
}

Expand Down Expand Up @@ -1210,7 +1177,7 @@ function transformClass(
for (const decorator of decorators) {
const { expression } = decorator;
let object;
if (version === "2023-11" && t.isMemberExpression(expression)) {
if (t.isMemberExpression(expression)) {
if (t.isSuper(expression.object)) {
object = t.thisExpression();
} else if (scopeParent.isStatic(expression.object)) {
Expand Down Expand Up @@ -1253,7 +1220,6 @@ function transformClass(
const { haveThis, decs } = generateDecorationList(
classDecorators,
decoratorsThis,
version,
);
classDecorationsFlag = haveThis ? 1 : 0;
classDecorations = decs;
Expand Down Expand Up @@ -1359,7 +1325,6 @@ function transformClass(
const { decs, haveThis } = generateDecorationList(
decorators,
decoratorsThis,
version,
);
decoratorsHaveThis = haveThis;
decoratorsArray = decs.length === 1 ? decs[0] : t.arrayExpression(decs);
Expand Down Expand Up @@ -1457,12 +1422,12 @@ function transformClass(
const [newPath] = element.replaceWith(newField);

if (isPrivate) {
privateMethods = extractProxyAccessorsFor(newId, version);
privateMethods = extractProxyAccessorsFor(newId);

const getId = generateLetUidIdentifier(scopeParent, `get_${name}`);
const setId = generateLetUidIdentifier(scopeParent, `set_${name}`);

addCallAccessorsFor(version, newPath, key, getId, setId, isStatic);
addCallAccessorsFor(newPath, key, getId, setId, isStatic);

locals = [newFieldInitId, getId, setId];
} else {
Expand All @@ -1477,7 +1442,6 @@ function transformClass(
newId,
isComputed,
isStatic,
version,
);
locals = [newFieldInitId];
}
Expand All @@ -1495,7 +1459,7 @@ function transformClass(
locals = [initId];

if (isPrivate) {
privateMethods = extractProxyAccessorsFor(key, version);
privateMethods = extractProxyAccessorsFor(key);
}
} else if (isPrivate) {
const callId = generateLetUidIdentifier(scopeParent, `call_${name}`);
Expand Down Expand Up @@ -1596,7 +1560,7 @@ function transformClass(
staticFieldInitializerExpressions = [];
}

if (hasDecorators && version === "2023-11") {
if (hasDecorators) {
if (kind === FIELD || kind === ACCESSOR) {
const initExtraId = generateLetUidIdentifier(
scopeParent,
Expand Down Expand Up @@ -1680,10 +1644,7 @@ function transformClass(
const sortedElementDecoratorInfo =
toSortedDecoratorInfo(elementDecoratorInfo);

const elementDecorations = generateDecorationExprs(
elementDecoratorInfo,
version,
);
const elementDecorations = generateDecorationExprs(elementDecoratorInfo);

const elementLocals: t.Identifier[] = extractElementLocalAssignments(
sortedElementDecoratorInfo,
Expand Down Expand Up @@ -2347,7 +2308,6 @@ export default function (
ignoreFunctionLength,
className,
namedEvaluationVisitor,
version,
);
if (newPath) {
VISITED.add(newPath);
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-parser/ast/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ interface ImportDeclaration <: Node {
importKind: null | "type" | "typeof" | "value";
specifiers: [ ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier ];
source: StringLiteral;
assertions?: [ ImportAttribute ];
attribtues?: [ ImportAttribute ];
}
```

Expand Down Expand Up @@ -1332,7 +1332,7 @@ interface ExportNamedDeclaration <: ExportDeclaration {
declaration: Declaration | null;
specifiers: [ ExportSpecifier | ExportNamespaceSpecifier ];
source: StringLiteral | null;
assertions?: [ ImportAttribute ];
attributes?: [ ImportAttribute ];
}
```

Expand Down Expand Up @@ -1392,7 +1392,7 @@ An export default declaration, e.g., `export default function () {};` or `export
interface ExportAllDeclaration <: ExportDeclaration {
type: "ExportAllDeclaration";
source: StringLiteral;
assertions?: [ ImportAttribute ];
attributes?: [ ImportAttribute ];
}
```

Expand Down
Loading