|
| 1 | +#!/usr/bin/env ts-node |
| 2 | + |
| 3 | +/** |
| 4 | + * Fails the lint run when a new inline `eslint-disable` bypasses the Onyx.connect() ban. |
| 5 | + * |
| 6 | + * The ban (`rulesdir/no-onyx-connect`, shipped by eslint-config-expensify) is a normal lint rule, |
| 7 | + * so an inline disable can silence it. The ESLint CLI neither surfaces nor fails on such suppressed |
| 8 | + * violations, so this runner re-elevates them: it lints with only the ban enabled, reads the |
| 9 | + * suppressed violations off the results, and exits non-zero on any that are not grandfathered. |
| 10 | + * Because it works from ESLint's suppressed-message data, no disable directive can reach it. |
| 11 | + * |
| 12 | + * A real bypass requires a file to contain both an `Onyx.connect` reference and an `eslint-disable` |
| 13 | + * directive, so we first narrow the targets to files matching both (via git grep) and only run |
| 14 | + * ESLint on those — keeping the check fast even on a whole-repo lint. The `Onyx.connect` match |
| 15 | + * deliberately omits the `(` so it stays a superset of the AST rule (e.g. whitespace or a comment |
| 16 | + * before the paren); extra matches like `Onyx.connectWithoutView` are harmless, as the rule ignores them. |
| 17 | + */ |
| 18 | +import tsParser from '@typescript-eslint/parser'; |
| 19 | +import {ESLint} from 'eslint'; |
| 20 | +import type {Rule} from 'eslint'; |
| 21 | +import {execFileSync} from 'node:child_process'; |
| 22 | +import {createRequire} from 'node:module'; |
| 23 | +import path from 'node:path'; |
| 24 | +import {BANNED_RULE_ID, collectSuppressedBans, findNewBypasses} from './onyxConnectBypass'; |
| 25 | + |
| 26 | +const projectRoot = path.resolve(__dirname, '..'); |
| 27 | + |
| 28 | +/** The ban's rule name as registered under the `rulesdir` plugin (i.e. `BANNED_RULE_ID` without the prefix). */ |
| 29 | +const RULE_NAME = 'no-onyx-connect'; |
| 30 | + |
| 31 | +function isRuleModule(value: unknown): value is Rule.RuleModule { |
| 32 | + return typeof value === 'object' && value !== null && 'create' in value && typeof value.create === 'function'; |
| 33 | +} |
| 34 | + |
| 35 | +/** Dynamically import the shipped `no-onyx-connect` rule, which is ESM with relative imports. */ |
| 36 | +async function loadNoOnyxConnectRule(): Promise<Rule.RuleModule> { |
| 37 | + const require = createRequire(__filename); |
| 38 | + const expensifyConfigDirectory = path.dirname(require.resolve('eslint-config-expensify/package.json')); |
| 39 | + const ruleFile = path.join(expensifyConfigDirectory, 'eslint-plugin-expensify', 'no-onyx-connect.js'); |
| 40 | + const imported: unknown = await import(ruleFile); |
| 41 | + if (isRuleModule(imported)) { |
| 42 | + return imported; |
| 43 | + } |
| 44 | + if (typeof imported === 'object' && imported !== null && 'default' in imported && isRuleModule(imported.default)) { |
| 45 | + return imported.default; |
| 46 | + } |
| 47 | + throw new Error(`Could not load the no-onyx-connect rule from ${ruleFile}`); |
| 48 | +} |
| 49 | + |
| 50 | +/** Files among the lint targets that contain both an Onyx.connect() call and an eslint-disable. */ |
| 51 | +function findCandidateFiles(targets: string[]): string[] { |
| 52 | + const pathSpecs = targets.length > 0 ? targets : ['.']; |
| 53 | + try { |
| 54 | + const output = execFileSync('git', ['grep', '-lI', '-F', '--all-match', '--untracked', '--no-recurse-submodules', '-e', 'Onyx.connect', '-e', 'eslint-disable', '--', ...pathSpecs], { |
| 55 | + cwd: projectRoot, |
| 56 | + encoding: 'utf8', |
| 57 | + }); |
| 58 | + return output.split('\n').filter(Boolean); |
| 59 | + } catch (error: unknown) { |
| 60 | + // git grep exits 1 when nothing matches; anything else is a real failure. |
| 61 | + if (typeof error === 'object' && error !== null && 'status' in error && error.status === 1) { |
| 62 | + return []; |
| 63 | + } |
| 64 | + throw error; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +async function run(): Promise<void> { |
| 69 | + const candidates = findCandidateFiles(process.argv.slice(2)); |
| 70 | + if (candidates.length === 0) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const rule = await loadNoOnyxConnectRule(); |
| 75 | + const eslint = new ESLint({ |
| 76 | + cwd: projectRoot, |
| 77 | + warnIgnored: false, |
| 78 | + errorOnUnmatchedPattern: false, |
| 79 | + overrideConfigFile: true, |
| 80 | + overrideConfig: [ |
| 81 | + { |
| 82 | + files: ['**/*.{js,jsx,ts,tsx,mjs,cjs}'], |
| 83 | + languageOptions: {parser: tsParser}, |
| 84 | + plugins: {rulesdir: {rules: {[RULE_NAME]: rule}}}, |
| 85 | + rules: {[BANNED_RULE_ID]: 'error'}, |
| 86 | + }, |
| 87 | + ], |
| 88 | + }); |
| 89 | + |
| 90 | + const results = await eslint.lintFiles(candidates); |
| 91 | + const newBypasses = findNewBypasses(collectSuppressedBans(results, projectRoot)); |
| 92 | + if (newBypasses.length === 0) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + console.error('Onyx.connect() is banned and the ban cannot be bypassed with eslint-disable. Use the useOnyx() hook to read Onyx data instead.'); |
| 97 | + console.error('New bypasses found:'); |
| 98 | + for (const bypass of newBypasses) { |
| 99 | + console.error(` ${bypass.file}:${bypass.line}`); |
| 100 | + } |
| 101 | + process.exitCode = 1; |
| 102 | +} |
| 103 | + |
| 104 | +run().catch((error: unknown) => { |
| 105 | + console.error(error instanceof Error ? error.message : error); |
| 106 | + process.exitCode = 1; |
| 107 | +}); |
0 commit comments