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: Correctly consume RuleTester statics #15507

Merged
merged 1 commit into from
Jan 14, 2022
Merged

fix: Correctly consume RuleTester statics #15507

merged 1 commit into from
Jan 14, 2022

Conversation

bradzacher
Copy link
Contributor

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

Tell us about your environment:

  • ESLint Version: latest
  • Node Version: 16
  • npm Version: 8

What parser (default, @babel/eslint-parser, @typescript-eslint/parser, etc.) are you using? espree

Please show your full configuration:
N/A

What did you do? Please include the actual source code causing the issue.

const { RuleTester } = require("eslint")

class MyCustomRuleTesterSubclass extends RuleTester {}

const rule = (context) => ({});
const ruleTester = new MyCustomRuleTesterSubclass ();

MyCustomRuleTesterSubclass.it = (text, callback) => {
  console.log("MyCustomRuleTesterSubclass has been customized!")
}

ruleTester.run("repro", rule, {
  valid: ["var foo = 0;"],
  invalid: [],
});

runkit repl

What did you expect to happen?

This test should log out MyCustomRuleTesterSubclass has been customized!

What actually happened? Please include the actual, raw output from ESLint.

No console log.

If you instead set the it directly on RuleTester, it works.

This bug is caused because RuleTester directly references itself within its instance method instead of using this.constructor to reference the correct static context:

RuleTester.describe(ruleName, () => {
RuleTester.describe("valid", () => {
test.valid.forEach(valid => {
RuleTester[valid.only ? "itOnly" : "it"](
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
() => {
testValidTemplate(valid);
}
);
});
});
RuleTester.describe("invalid", () => {
test.invalid.forEach(invalid => {
RuleTester[invalid.only ? "itOnly" : "it"](
sanitize(invalid.name || invalid.code),
() => {
testInvalidTemplate(invalid);
}
);
});
});
});

What changes did you make? (Give an overview)

  • Convert RuleTester to use this.constructor instead of RuleTester to access static members.
  • Add tests to prevent regressions

Is there anything you'd like reviewers to focus on?

This was reported by a user of @typescript-eslint.
Fixes: typescript-eslint/typescript-eslint#4422

@eslint-github-bot eslint-github-bot bot added the triage An ESLint team member will look at this issue soon label Jan 11, 2022
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 11, 2022

CLA Signed

The committers are authorized under a signed CLA.

@bradzacher bradzacher changed the title fix: correctly consume RuleTester statics so that subclasses can work correctly fix: Correctly consume RuleTester statics so that subclasses can work correctly Jan 11, 2022
@eslint-github-bot
Copy link

Hi @bradzacher!, thanks for the Pull Request

The first commit message isn't properly formatted. We ask that you update the message to match this format, as we use it to generate changelogs and automate releases.

  • The length of the commit message must be less than or equal to 72

To Fix: You can fix this problem by running git commit --amend, editing your commit message, and then running git push -f to update this pull request.

Read more about contributing to ESLint here

@bradzacher bradzacher changed the title fix: Correctly consume RuleTester statics so that subclasses can work correctly fix: Correctly consume RuleTester statics Jan 11, 2022
@eslint-github-bot
Copy link

Hi @bradzacher!, thanks for the Pull Request

The first commit message isn't properly formatted. We ask that you update the message to match this format, as we use it to generate changelogs and automate releases.

  • The length of the commit message must be less than or equal to 72

To Fix: You can fix this problem by running git commit --amend, editing your commit message, and then running git push -f to update this pull request.

Read more about contributing to ESLint here

@eslint-github-bot eslint-github-bot bot added the bug ESLint is working incorrectly label Jan 11, 2022
@eslint eslint deleted a comment from eslint-github-bot bot Jan 11, 2022
@eslint eslint deleted a comment from eslint-github-bot bot Jan 11, 2022
Copy link
Member

@aladdin-add aladdin-add left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@aladdin-add aladdin-add added core Relates to ESLint's core APIs and features evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Jan 11, 2022
Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for fixing this.

@nzakas nzakas merged commit 02d6426 into eslint:main Jan 14, 2022
@bradzacher bradzacher deleted the ruletester-statics branch January 14, 2022 00:48
yudai-nkt added a commit to yudai-nkt/eslint-plugin-uvu that referenced this pull request Jan 22, 2022
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jul 14, 2022
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jul 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TSESLint.RuleTester doesn't accept customization of its describe and it.
3 participants