Skip to content

Commit 33d705e

Browse files
authored
Analyze ClassAccessorProperty to prevent the no-undef rule (#16884)
* Analyze `ClassAccessorProperty` to prevent the `no-undef` rule a.js: ```js class A { accessor b; } ``` Current and expected behavior: ```bash npx eslint a.js ``` ``` a.js 2:11 error 'b' is not defined no-undef ✖ 1 problem (1 error, 0 warnings) ``` * add class accessor tests * fix typo in article
1 parent 8478bf9 commit 33d705e

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

eslint/babel-eslint-parser/src/analyze-scope.cts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ class Referencer extends OriginalReferencer {
187187
this._visitClassProperty(node);
188188
}
189189

190+
ClassAccessorProperty(node: any) {
191+
this._visitClassProperty(node);
192+
}
193+
190194
PropertyDefinition(node: any) {
191195
this._visitClassProperty(node);
192196
}

eslint/babel-eslint-tests/test/integration/eslint/verify.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,41 @@ describe("verify", () => {
18371837
});
18381838
});
18391839

1840+
describe("accessor declarations", () => {
1841+
it("should not be undefined", () => {
1842+
verifyAndAssertMessages(
1843+
`
1844+
class C {
1845+
accessor d = 1;
1846+
}
1847+
`,
1848+
{ "no-undef": 1 },
1849+
);
1850+
});
1851+
1852+
it("should not be unused", () => {
1853+
verifyAndAssertMessages(
1854+
`
1855+
export class C {
1856+
accessor d = 1;
1857+
}
1858+
`,
1859+
{ "no-unused-vars": 1 },
1860+
);
1861+
});
1862+
1863+
it("no-use-before-define allows referencing the class in an accessor", () => {
1864+
verifyAndAssertMessages(
1865+
`
1866+
class C {
1867+
accessor d = C.name;
1868+
}
1869+
`,
1870+
{ "no-use-before-define": 1 },
1871+
);
1872+
});
1873+
});
1874+
18401875
describe("private methods", () => {
18411876
it("should not be undefined", () => {
18421877
verifyAndAssertMessages(

0 commit comments

Comments
 (0)