Repository navigation
ci(core): Guard the "sideEffects": false contract with a lint check #6836
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
Open
antonis
wants to merge
2
commits into
main
Choose a base branch
from
antonis/ci-guard-side-effects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const ts = require('typescript'); | ||
|
|
||
| // Guards the `"sideEffects": false` contract in package.json: no module that ships | ||
| // in the bundle may run code at import time. A bundler is free to drop such a module | ||
| // when its exports are unused, so an import-time side effect would silently vanish | ||
| // from a consumer's build. This fails if one is introduced. | ||
| // | ||
| // Allowlist approach: only pure declarations are permitted at module top level; | ||
| // every other statement is flagged, since anything else can execute at import time | ||
| // (an `if`/`for`/`try`/block wrapping a call, a bare expression, an IIFE, a global | ||
| // write). A side-effect import (`import 'x'` or `import {} from 'x'`, i.e. one that | ||
| // binds nothing) is flagged too. Declarations are fine, including `const x = f()`: | ||
| // they are module-local and ride with the module only when it is kept. | ||
| // | ||
| // Excluded: `tools/` (Node-only build tools, never bundled into an app) and `vendor/` | ||
| // (audited third-party code reviewed when it is vendored in). The guard's job is to | ||
| // stop first-party SDK code from gaining an import-time side effect. | ||
|
|
||
| const ROOT = path.resolve(__dirname, '..'); | ||
| const SRC = path.join(ROOT, 'src', 'js'); | ||
| const EXCLUDE_DIRS = [path.join(SRC, 'tools'), path.join(SRC, 'vendor')]; | ||
|
|
||
| // Top-level statement kinds that are pure declarations (no import-time execution). | ||
| const ALLOWED_KINDS = new Set([ | ||
| ts.SyntaxKind.FunctionDeclaration, | ||
| ts.SyntaxKind.ClassDeclaration, | ||
| ts.SyntaxKind.InterfaceDeclaration, | ||
| ts.SyntaxKind.TypeAliasDeclaration, | ||
| ts.SyntaxKind.EnumDeclaration, | ||
| ts.SyntaxKind.ModuleDeclaration, // namespace / declare module | ||
| ts.SyntaxKind.VariableStatement, // const/let/var — incl. `const x = f()` | ||
| ts.SyntaxKind.ExportDeclaration, // export { ... } / export * from | ||
| ts.SyntaxKind.ExportAssignment, // export default ... / export = | ||
| ts.SyntaxKind.ImportEqualsDeclaration, | ||
| ts.SyntaxKind.EmptyStatement, | ||
| ]); | ||
|
|
||
| // An import binds nothing (pure side-effect import) when it has no clause, or a clause | ||
| // with neither a default name nor namespace nor any named binding. Type-only imports | ||
| // are erased at compile time, so they never run. | ||
| function isSideEffectImport(node) { | ||
| const clause = node.importClause; | ||
| if (clause && clause.isTypeOnly) { | ||
| return false; | ||
| } | ||
| if (!clause) { | ||
| return true; | ||
| } | ||
| if (clause.name) { | ||
| return false; | ||
| } | ||
| const bindings = clause.namedBindings; | ||
| if (!bindings) { | ||
| return true; | ||
| } | ||
| return ts.isNamedImports(bindings) && bindings.elements.length === 0; | ||
| } | ||
|
|
||
| function walk(dir, out) { | ||
| for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { | ||
| const p = path.join(dir, entry.name); | ||
| if (entry.isDirectory()) { | ||
| if (EXCLUDE_DIRS.some(d => p === d || p.startsWith(d + path.sep))) { | ||
| continue; | ||
| } | ||
| walk(p, out); | ||
| } else if (/\.tsx?$/.test(entry.name) && !/\.(test|spec)\.tsx?$/.test(entry.name)) { | ||
| out.push(p); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| const findings = []; | ||
| for (const file of walk(SRC, [])) { | ||
| const text = fs.readFileSync(file, 'utf8'); | ||
| const sourceFile = ts.createSourceFile(file, text, ts.ScriptTarget.ES2018, true, ts.ScriptKind.TSX); | ||
| for (const stmt of sourceFile.statements) { | ||
| const lineOf = () => sourceFile.getLineAndCharacterOfPosition(stmt.getStart(sourceFile)).line + 1; | ||
| if (ts.isImportDeclaration(stmt)) { | ||
| if (isSideEffectImport(stmt)) { | ||
| findings.push({ file, line: lineOf(), kind: 'side-effect import', code: stmt.getText(sourceFile) }); | ||
| } | ||
| } else if (!ALLOWED_KINDS.has(stmt.kind)) { | ||
| findings.push({ file, line: lineOf(), kind: 'top-level executed statement', code: stmt.getText(sourceFile).slice(0, 100) }); | ||
| } | ||
| } | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const rel = f => path.relative(ROOT, f); | ||
| if (findings.length) { | ||
| console.error(`\n✖ import-time side effects found (${findings.length}) — these break the "sideEffects": false contract:\n`); | ||
| for (const f of findings) { | ||
| console.error(` ${rel(f.file)}:${f.line} [${f.kind}] ${f.code.replace(/\n/g, ' ')}`); | ||
| } | ||
| console.error('\nMove the side effect into a function called by init()/wrap()/an integration factory.\n'); | ||
| process.exit(1); | ||
| } | ||
| console.log('✓ no import-time side effects in src/js (tools/, vendor/ excluded) — "sideEffects": false is safe'); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.