Add no-top-level-side-effects rule#2912
Open
pannous wants to merge 4 commits intosindresorhus:mainfrom
Open
Conversation
Disallows side-effecting expression statements (function calls, `new` expressions, tagged template literals) at the top level of ES modules, enabling safe tree-shaking. Files with a hashbang or without any exports are skipped. Assignment expressions are explicitly allowed. Closes sindresorhus#1661
… dogfooding - Commit missing AVA snapshots (were untracked — caused all test CI failures) - Add blank line before list in docs to fix MD032 markdownlint error - Refactor GlobalReferenceTracker to use static class fields instead of Object.assign() at module top level (fixes run-rules-on-codebase failure)
…Tracker The @stylistic/lines-between-class-members rule disallows blank lines between class members. The blank line between static properties and private fields caused a CI lint error.
Owner
|
I couple of things:
|
814b622 to
0e023e3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the
no-top-level-side-effectsrule proposed in #1661.Modules with top-level side effects cannot be safely tree-shaken. Webpack calls these "pure modules" — a module is considered side-effect-free when it doesn't execute observable operations at the top level.
What is flagged
foo(),console.log(),foo.bar()foo?.()newexpressions:new Foo()html`…`(function() {})(),(() => {})()awaitwrapping the above:await fetch(url)What is allowed
const x = foo()module.exports = …,x = foo()'use strict'Exceptions (entire file is skipped)
#!/usr/bin/env node) — treated as scriptsTest plan
package.jstests pass (rule registered, has docs, in configs)