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

Change Request: Remove deprecated context pass-through SourceCode methods #17520

Closed
1 task done
nzakas opened this issue Aug 31, 2023 · 11 comments · Fixed by #17698
Closed
1 task done

Change Request: Remove deprecated context pass-through SourceCode methods #17520

nzakas opened this issue Aug 31, 2023 · 11 comments · Fixed by #17698
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion breaking This change is backwards-incompatible core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint

Comments

@nzakas
Copy link
Member

nzakas commented Aug 31, 2023

ESLint version

HEAD

What problem do you want to solve?

We have a bunch of deprecated methods on context that pass through to SourceCode:

  • getComments()
    • getSource
  • getAllComments
  • getNodeByRangeIndex
  • getComments
  • getCommentsBefore
  • getCommentsAfter
  • getCommentsInside
  • getJSDocComment
  • getFirstToken
  • getFirstTokens
  • getLastToken
  • getLastTokens
  • getTokenAfter
  • getTokenBefore
  • getTokenByRangeStart
  • getTokens
  • getTokensAfter
  • getTokensBefore
  • getTokensBetween

What do you think is the correct solution?

I think it's time to remove these methods to clean up the codebase.

Participation

  • I am willing to submit a pull request for this change.

Additional comments

No response

@nzakas nzakas added enhancement This change enhances an existing feature of ESLint core Relates to ESLint's core APIs and features breaking This change is backwards-incompatible labels Aug 31, 2023
@mdjermanovic
Copy link
Member

Sounds good to me.

We could add deprecation warnings in RuleTester in v8.x, and then remove these methods in v9.0.0?

@nzakas
Copy link
Member Author

nzakas commented Aug 31, 2023

We don't actually have a way to do that because the context object isn't exposed outside of Linter in any way. So, I think a blog post is probably the best way to let people know this is coming.

@mdjermanovic
Copy link
Member

RuleTester already wraps create(context) here:

linter.defineRule(ruleName, Object.assign({}, rule, {
// Create a wrapper rule that freezes the `context` properties.
create(context) {
freezeDeeply(context.options);
freezeDeeply(context.settings);
freezeDeeply(context.parserOptions);
return (typeof rule === "function" ? rule : rule.create)(context);
}
}));

Perhaps we could insert an object with methods that shadow deprecated ones:

        linter.defineRule(ruleName, Object.assign({}, rule, {

            // Create a wrapper rule that freezes the `context` properties.
            create(context) {
                freezeDeeply(context.options);
                freezeDeeply(context.settings);
                freezeDeeply(context.parserOptions);

                const newContext = Object.freeze(
                    Object.create(
                        context,
                        Object.fromEntries(Object.keys(DEPRECATED_SOURCECODE_PASSTHROUGHS).map(name => [
                            name,
                            {
                                value(...args) {

                                    // emit deprecation warning here

                                    // call the original method                                   
                                    return context[name].call(this, ...args);
                                },
                                enumerable: true
                            }
                        ]))
                    )
                );

                return (typeof rule === "function" ? rule : rule.create)(newContext);
            }
        }));

@nzakas
Copy link
Member Author

nzakas commented Sep 1, 2023

Oh nice! I didn't realize we were doing that already (yet another part of the codebase I'm not familiar with). Then we can definitely do that.

@nzakas nzakas added the accepted There is consensus among the team that this change meets the criteria for inclusion label Sep 1, 2023
@nzakas
Copy link
Member Author

nzakas commented Sep 1, 2023

In looking at this, I discovered that the tests for flat-rule-tester.js don't include tests for legacy rule format or missing schemas, and I can't recall if this is intentional. Any idea @bmish?

nzakas added a commit that referenced this issue Sep 1, 2023
Emits deprecation warnings when using methods on `context` that are
deprecated.

Refs #17520
@mdjermanovic
Copy link
Member

It's intentional per #16063 (comment).

I think these warnings can also be implemented just in rule-tester.js.

@nzakas
Copy link
Member Author

nzakas commented Sep 1, 2023

In that case, the PR is ready! 🎉

mdjermanovic pushed a commit that referenced this issue Sep 2, 2023
* feat: Emit deprecation warnings in RuleTester

Emits deprecation warnings when using methods on `context` that are
deprecated.

Refs #17520

* Revert flat-rule-tester

* Fix linting error
@JoshuaKGoldberg
Copy link
Contributor

Looks like #17527 closed this out?

@mdjermanovic
Copy link
Member

Looks like #17527 closed this out?

The remaining task is to remove these methods in v9.

nzakas added a commit that referenced this issue Sep 6, 2023
* feat: Emit deprecation warnings in RuleTester

Emits deprecation warnings when using methods on `context` that are
deprecated.

Refs #17520

* Revert flat-rule-tester

* Fix linting error
@nzakas
Copy link
Member Author

nzakas commented Oct 31, 2023

Working on this.

@nzakas nzakas self-assigned this Oct 31, 2023
nzakas added a commit that referenced this issue Oct 31, 2023
@sam3k
Copy link
Contributor

sam3k commented Dec 1, 2023

2023-11-30 tsc-meeting note: This one will be ready along with the language plugins issue because it's all in the same PR

nzakas added a commit that referenced this issue Dec 11, 2023
mdjermanovic added a commit that referenced this issue Dec 20, 2023
* feat!: Remove deprecated context methods

fixes #17520

* Fix broken tests

* Remove BASE_TRAVERSAL_CONTEXT

* Remove deprecated methods from RuleTester

* Removed more methods/properties

* Fix lint errors

* Update docs/src/extend/custom-rules.md

Co-authored-by: 唯然 <weiran.zsd@outlook.com>

* Update docs/src/extend/custom-rules.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update docs/src/extend/custom-rules.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update docs/src/extend/custom-rules.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Remove unneeded deprecated notices

* Restore parserServices tests

---------

Co-authored-by: 唯然 <weiran.zsd@outlook.com>
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jun 18, 2024
@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 Jun 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion breaking This change is backwards-incompatible core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint
Projects
Archived in project
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants
@nzakas @sam3k @JoshuaKGoldberg @mdjermanovic and others