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

Fix(estree): Reset export's start after decorator #15107

Merged
merged 3 commits into from Nov 1, 2022
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
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 36 additions & 26 deletions eslint/babel-eslint-tests/test/integration/eslint/verify.js
Expand Up @@ -2,6 +2,32 @@ import verifyAndAssertMessages from "../../helpers/verifyAndAssertMessages.js";
import path from "path";
import { fileURLToPath } from "url";

function verifyDecoratorsLegacyAndAssertMessages(
code,
rules,
expectedMessages,
sourceType,
) {
const overrideConfig = {
parserOptions: {
sourceType,
babelOptions: {
configFile: path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../../../babel-eslint-shared-fixtures/config/babel.config.decorators-legacy.js",
),
},
},
};
return verifyAndAssertMessages(
code,
rules,
expectedMessages,
sourceType,
overrideConfig,
);
}

describe("verify", () => {
it("arrow function support (issue #1)", () => {
verifyAndAssertMessages("describe('stuff', () => {});");
Expand Down Expand Up @@ -1073,32 +1099,6 @@ describe("verify", () => {
});

describe("decorators #72 (legacy)", () => {
function verifyDecoratorsLegacyAndAssertMessages(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This helper is moved to the top level.

code,
rules,
expectedMessages,
sourceType,
) {
const overrideConfig = {
parserOptions: {
sourceType,
babelOptions: {
configFile: path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../../../babel-eslint-shared-fixtures/config/babel.config.decorators-legacy.js",
),
},
},
};
return verifyAndAssertMessages(
code,
rules,
expectedMessages,
sourceType,
overrideConfig,
);
}

it("class declaration", () => {
verifyDecoratorsLegacyAndAssertMessages(
`
Expand Down Expand Up @@ -1247,6 +1247,16 @@ describe("verify", () => {
});
});

describe("decorators #15085 (legacy)", () => {
it("works with keyword-spacing rule", () => {
verifyDecoratorsLegacyAndAssertMessages(
"@dec export class C {}; @dec export default class {}",
{ "keyword-spacing": 1 },
[],
);
});
});

it("detects minimal no-unused-vars case #120", () => {
verifyAndAssertMessages("var unused;", { "no-unused-vars": 1 }, [
"1:5 'unused' is defined but never used. no-unused-vars",
Expand Down
22 changes: 22 additions & 0 deletions packages/babel-parser/src/plugins/estree.ts
Expand Up @@ -455,6 +455,7 @@ export default (superClass: typeof Parser) =>
unfinished: Undone<N.AnyExport>,
decorators: N.Decorator[] | null,
) {
const exportStartLoc = this.state.lastTokStartLoc;
const node = super.parseExport(unfinished, decorators);

switch (node.type) {
Expand All @@ -476,6 +477,27 @@ export default (superClass: typeof Parser) =>
delete node.specifiers;
}

// fallthrough
case "ExportDefaultDeclaration":
{
const { declaration } = node;
if (
declaration?.type === "ClassDeclaration" &&
declaration.decorators?.length > 0 &&
// decorator comes before export
declaration.start === node.start
) {
this.resetStartLocation(
node,
// For compatibility with ESLint's keyword-spacing rule, which assumes that an
// export declaration must start with export.
// https://github.com/babel/babel/issues/15085
// Here we reset export declaration's start to be the start of the export token
exportStartLoc,
);
}
}

break;
}

Expand Down
@@ -0,0 +1,2 @@
@dec export class C {}
@dec export default class {}
@@ -0,0 +1,3 @@
{
"plugins": ["estree", "decorators-legacy"]
}
@@ -0,0 +1,70 @@
{
"type": "File",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":28}},
"program": {
"type": "Program",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":28}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportNamedDeclaration",
"start":5,"end":22,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":22}},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When estree is enabled, we restore the pre-7.20 behaviour: the ExportNamedDeclaration now starts at the export token.

"specifiers": [],
"source": null,
"declaration": {
"type": "ClassDeclaration",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"id": {
"type": "Identifier",
"start":18,"end":19,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":19},"identifierName":"C"},
"name": "C"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":20,"end":22,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":22}},
"body": []
},
"decorators": [
{
"type": "Decorator",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"expression": {
"type": "Identifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4},"identifierName":"dec"},
"name": "dec"
}
}
]
}
},
{
"type": "ExportDefaultDeclaration",
"start":28,"end":51,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":28}},
"declaration": {
"type": "ClassDeclaration",
"start":23,"end":51,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":28}},
"id": null,
"superClass": null,
"body": {
"type": "ClassBody",
"start":49,"end":51,"loc":{"start":{"line":2,"column":26},"end":{"line":2,"column":28}},
"body": []
},
"decorators": [
{
"type": "Decorator",
"start":23,"end":27,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":4}},
"expression": {
"type": "Identifier",
"start":24,"end":27,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":4},"identifierName":"dec"},
"name": "dec"
}
}
]
}
}
]
}
}