Skip to content

Commit

Permalink
Analyze ClassAccessorProperty to prevent the no-undef rule (babel…
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
victorenator authored Oct 12, 2024
1 parent 8478bf9 commit 33d705e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions eslint/babel-eslint-parser/src/analyze-scope.cts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ class Referencer extends OriginalReferencer {
this._visitClassProperty(node);
}

ClassAccessorProperty(node: any) {
this._visitClassProperty(node);
}

PropertyDefinition(node: any) {
this._visitClassProperty(node);
}
Expand Down
35 changes: 35 additions & 0 deletions eslint/babel-eslint-tests/test/integration/eslint/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,41 @@ describe("verify", () => {
});
});

describe("accessor declarations", () => {
it("should not be undefined", () => {
verifyAndAssertMessages(
`
class C {
accessor d = 1;
}
`,
{ "no-undef": 1 },
);
});

it("should not be unused", () => {
verifyAndAssertMessages(
`
export class C {
accessor d = 1;
}
`,
{ "no-unused-vars": 1 },
);
});

it("no-use-before-define allows referencing the class in an accessor", () => {
verifyAndAssertMessages(
`
class C {
accessor d = C.name;
}
`,
{ "no-use-before-define": 1 },
);
});
});

describe("private methods", () => {
it("should not be undefined", () => {
verifyAndAssertMessages(
Expand Down

0 comments on commit 33d705e

Please sign in to comment.