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

Docs: context migration suggestions fail on earlier v8 versions #559

Closed
1 task
MikeMcC399 opened this issue Apr 24, 2024 · 3 comments · Fixed by #560
Closed
1 task

Docs: context migration suggestions fail on earlier v8 versions #559

MikeMcC399 opened this issue Apr 24, 2024 · 3 comments · Fixed by #560
Assignees
Labels
accepted documentation Improvements or additions to documentation

Comments

@MikeMcC399
Copy link

Docs page(s)

At least the following call transitions are problematic where the API call was not available on earlier ESLint v8.x releases

Other calls added around the same time are probably also affected.

What documentation issue do you want to solve?

Code examples using
const { sourceCode } = context; cause an error running in Jest for ESLint < v8.37.0.

TypeError: Cannot read properties of undefined (reading 'getScope')

What do you think is the correct solution?

Please check if this should be changed to
const { sourceCode = {} } = context;

Participation

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

Additional comments

Steps to reproduce

Ubuntu 22.04.4 LTS, Node.js 20.12.2 using ESLint 8.57.0 as base vesion.

Original state

git clone https://github.com/MikeMcC399/eslint-plugin-cypress
cd eslint-plugin-cypress
git switch show/api-deprecation
npm ci
npx jest tests/lib/rules/no-unnecessary-waiting.js --silent

Note deprecation warning

$ npx jest tests/lib/rules/no-unnecessary-waiting.js --silent
(node:24892) DeprecationWarning: "no-unnecessary-waiting" rule is using `context.getScope()`, which is deprecated and will be removed in ESLint v9. Please use `sourceCode.getScope()` instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
PASS tests/lib/rules/no-unnecessary-waiting.js

Recommended update

Using recommended code from blog Preparing your custom rules for ESLint v9.0.0 > From context to SourceCode

git switch show/convert-to-sourcecode-Rev1
npm install [email protected]
npx jest tests/lib/rules/no-unnecessary-waiting.js

Note failure (log truncated):

$ npx jest tests/lib/rules/no-unnecessary-waiting.js --silent
FAIL tests/lib/rules/no-unnecessary-waiting.js
  ● no-unnecessary-waiting › valid › cy.wait("@someRequest")

    TypeError: Cannot read properties of undefined (reading 'getScope')
    Occurred while linting <input>:1
    Rule: "no-unnecessary-waiting"

      20 |       CallExpression (node) {
      21 |         if (isCallingCyWait(node)) {
    > 22 |           const scope = sourceCode.getScope
         |                                    ^
      23 |             ? sourceCode.getScope(node)
      24 |             : context.getScope()
      25 |
...

Modified update

Use suggested change const { sourceCode = {} } = context; in branch show/convert-to-sourcecode-Rev2
Continue to use [email protected]

git switch show/convert-to-sourcecode-Rev2
npx jest tests/lib/rules/no-unnecessary-waiting.js --silent

Note success with no deprecation message:

$ npx jest tests/lib/rules/no-unnecessary-waiting.js --silent
PASS tests/lib/rules/no-unnecessary-waiting.js

Revert to [email protected]

git restore .
npm ci
npx jest tests/lib/rules/no-unnecessary-waiting.js --silent

Note also success with no deprecation message.

@MikeMcC399 MikeMcC399 added the documentation Improvements or additions to documentation label Apr 24, 2024
@nzakas nzakas transferred this issue from eslint/eslint Apr 25, 2024
@nzakas nzakas self-assigned this Apr 25, 2024
@nzakas
Copy link
Member

nzakas commented Apr 25, 2024

Thanks. I'm working on this.

@MikeMcC399
Copy link
Author

MikeMcC399 commented Apr 26, 2024

@nzakas

Many thanks for the update to the blog Preparing your custom rules for ESLint v9.0.0 > From context to SourceCode!

I ran into a snag when implementing this, since the repo I'm updating had the following in its config:

  "parserOptions": {
    "ecmaVersion": 2018
  }

I needed to update to "ecmaVersion": 2020 because the Nullish coalescing operator (??) was not available in previous versions.

@MikeMcC399
Copy link
Author

@nzakas

Thanks once again for the assistance!

cypress-io/[email protected] has successfully used your new recommendations!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted documentation Improvements or additions to documentation
Projects
Status: Complete
Development

Successfully merging a pull request may close this issue.

2 participants