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

Update to ESLint 7 #1000

Closed
samreid opened this issue Jan 1, 2021 · 17 comments
Closed

Update to ESLint 7 #1000

samreid opened this issue Jan 1, 2021 · 17 comments

Comments

@samreid
Copy link
Member

samreid commented Jan 1, 2021

As described in phetsims/phet-info#150 (comment)

Upgrade to ESLint 7 (we are currently on 6, which is missing some style rules).

The migration guide is here: https://eslint.org/docs/user-guide/migrating-to-7.0.0

@samreid samreid self-assigned this Jan 1, 2021
@samreid
Copy link
Member Author

samreid commented Jan 2, 2021

I noticed weddell is already using eslint 7.

@samreid
Copy link
Member Author

samreid commented Jan 2, 2021

ESLint 7 has a new default rule that prevents setters from returning a value. I considered turning the rule off for compatibility, but it seems better to eliminate these returns, so I'll commit that.

samreid added a commit to phetsims/scenery that referenced this issue Jan 2, 2021
samreid added a commit to phetsims/scenery-phet that referenced this issue Jan 2, 2021
@samreid
Copy link
Member Author

samreid commented Jan 3, 2021

Following the migration guide linked above, I adapted our grunt lint to use ESLint 7. The most significant changes were:

  • Use new ESLint instead of CLIEngine
  • lintFiles is now async, so I converted the whole function to async
  • I added a command line parameter --fix which will run the ESLint fixes

I ran the following tests on the new version:

  • Create a lint error in fourier and grunt lint in fourier.
  • Create a lint error in fourier and grunt lint-everything from perennial.
  • Test caching for lint-everything (see below)
  • Test caching for one repo (see below)
  • Make sure cache files appear where desired
  • Make sure our custom rules are respected
  • Make sure the .eslintignore file is respected. I tried disrupting one of each kind of path and saw that it had the desired behavior.
  • Test precommit hooks. There was a problem that process.exit was called too soon in the puppeteer part. I had to eliminate that to give the lint time to run. I also labeled the function with a version string so that we can choose to only run the linting with compatible SHAs.
~/apache-document-root/main/perennial$ time grunt lint-everything --disable-eslint-cache
Running "lint-everything" task
(node:29823) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)

Done.

real	0m50.599s
user	1m0.679s
sys	0m3.901s
~/apache-document-root/main/perennial$ time grunt lint-everything
Running "lint-everything" task
(node:29835) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)

Done.

real	0m52.462s
user	1m2.254s
sys	0m3.950s
~/apache-document-root/main/perennial$ time grunt lint-everything
Running "lint-everything" task
(node:29838) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)

Done.

real	0m3.875s
user	0m2.793s
sys	0m0.893s
~/apache-document-root/main/perennial$ 

############

~/apache-document-root/main/fourier-making-waves$ time grunt lint --disable-eslint-cache
Running "lint" task

Done.

real	0m4.200s
user	0m3.016s
sys	0m0.713s
~/apache-document-root/main/fourier-making-waves$ time grunt lint
Running "lint" task

Done.

real	0m2.097s
user	0m2.437s
sys	0m0.358s
~/apache-document-root/main/fourier-making-waves$ 
~/apache-document-root/main/fourier-making-waves$ time grunt lint
Running "lint" task

Done.

real	0m1.498s
user	0m1.443s
sys	0m0.312s

~/apache-document-root/main/fourier-making-waves$ time grunt lint --disable-eslint-cache
Running "lint" task

Done.

real	0m3.812s
user	0m2.896s
sys	0m0.592s
~/apache-document-root/main/fourier-making-waves$ time grunt lint
Running "lint" task

Done.

real	0m2.633s
user	0m2.564s
sys	0m0.409s
~/apache-document-root/main/fourier-making-waves$ time grunt lint
Running "lint" task

Done.

real	0m1.468s
user	0m1.410s
sys	0m0.309s

Everything worked as desired, so I'll take the following steps:

  • Commit to master
  • Notify slack, since a npm update will be necessary in chipper
  • Notify the google group for the same reason
  • Assign to @ariel-phet to request review. The other developers with commits on lint.js are primarily: @jonathanolson @zepumph

samreid added a commit to phetsims/binder that referenced this issue Jan 3, 2021
samreid added a commit to phetsims/perennial that referenced this issue Jan 3, 2021
samreid added a commit that referenced this issue Jan 3, 2021
@samreid
Copy link
Member Author

samreid commented Jan 3, 2021

While I was working on this, ESLint 7.17.0 was released, with an autofix for formatting ternary operators. I'm inclined to update to this now so we only need to notify clients and PhET developers to npm update once.

samreid added a commit that referenced this issue Jan 3, 2021
@samreid
Copy link
Member Author

samreid commented Jan 3, 2021

I posted a message in slack and in the google group:

Chipper has updated from ESLint 6 to 7, details are in #1000. Please npm update in chipper when you pull changes.

The last step is:

Assign to @ariel-phet to request review. The other developers with commits on lint.js are primarily: @jonathanolson @zepumph

@samreid
Copy link
Member Author

samreid commented Jan 3, 2021

CT discovered an issue when there are no files to lint (in phet-io-wrapper-classroom-activity), I corrected it in the commit.

@samreid
Copy link
Member Author

samreid commented Jan 3, 2021

On second thought, it makes sense for me to add a --files option before review, so I'll reassign to me until that's done.

@samreid
Copy link
Member Author

samreid commented Jan 3, 2021

I added support for --patterns=... in #1001 and I think that should be included in the review. Summarizing what should be reviewed:

  • lint.js, including its options and async parts and chipperAPIVersion
  • usages of lint including dynamically loaded parts, like in perennial, in the precommit hook, etc.

Questions:

  • I noticed that perennial gruntfile has 2 ways of invoking lint, one is via execute and one is via await. Should both use same strategy? If so, which strategy?

@ariel-phet
Copy link
Contributor

Assigning to @jonathanolson, since we use this tool quite a bit, I am recommending high priority.

@jonathanolson
Copy link
Contributor

I'm trying to understand the let failed = true and other logic in hook-pre-commit.js seems

results.forEach( result => {
  let failed = false;
  if ( result.errorCount > 0 || result.warningCount > 0 ) {
    console.error( `lint failed in ${repo}`, result.filePath, result );
    failed = true;
  }
  if ( failed ) {
    process.exit( 1 );
  }
} );

could be simplified to:

for ( const result of results ) {
  if ( result.errorCount > 0 || result.warningCount > 0 ) {
    console.error( `lint failed in ${repo}`, result.filePath, result );
    process.exit( 1 );
  }
}

Also: preferring for-of loops in async functions, so that it will work nicely if we add awaits INSIDE the loop.

@jonathanolson
Copy link
Contributor

  const sum = ( a, b ) => a + b;
  const count = numbers => numbers.length === 0 ? 0 : numbers.reduce( sum );

I believe can be reduced to:

  const count = numbers.reduce( ( a, b ) => a + b, 0 );

or, since we have lodash:

const count = _.sum( numbers );

@jonathanolson
Copy link
Contributor

The comment here seems incorrect. I presume it's more "no JS code that we WANT to be linted exists here"?

const EXCLUDE_PATTERNS = [ // patterns that have no code and lint should not be attempted

@jonathanolson
Copy link
Contributor

Looks good to me! Can you take a look at the potential improvements above?

@samreid
Copy link
Member Author

samreid commented Jan 8, 2021

I'm trying to understand the let failed = true and other logic in hook-pre-commit.js seems

The intent was that it would report all failing repos instead of just the first one.

@jonathanolson
Copy link
Contributor

Sounds good, then the failed and if ( failed ) should be moved out, since it looks like it would process-exit on the first failure immediately.

samreid added a commit that referenced this issue Jan 8, 2021
samreid added a commit that referenced this issue Jan 8, 2021
samreid added a commit to phetsims/perennial that referenced this issue Jan 8, 2021
@samreid
Copy link
Member Author

samreid commented Jan 8, 2021

Great recommendations, I addressed each above (and improved output formatting), ready for re-review.

@samreid samreid assigned jonathanolson and unassigned samreid Jan 8, 2021
@jonathanolson
Copy link
Contributor

Looks great, thanks!

zepumph pushed a commit to phetsims/scenery that referenced this issue Dec 15, 2021
zepumph pushed a commit to phetsims/perennial that referenced this issue Jul 21, 2022
zepumph pushed a commit to phetsims/perennial that referenced this issue Jul 21, 2022
zepumph pushed a commit to phetsims/perennial that referenced this issue Jul 21, 2022
zepumph pushed a commit to phetsims/perennial that referenced this issue Jul 21, 2022
samreid added a commit that referenced this issue Aug 1, 2022
samreid added a commit to phetsims/perennial that referenced this issue Oct 17, 2024
samreid added a commit to phetsims/perennial that referenced this issue Oct 17, 2024
samreid added a commit to phetsims/perennial that referenced this issue Oct 17, 2024
samreid added a commit to phetsims/perennial that referenced this issue Oct 17, 2024
zepumph pushed a commit to phetsims/perennial that referenced this issue Oct 22, 2024
zepumph pushed a commit to phetsims/perennial that referenced this issue Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants