|
| 1 | +const { execSync, exec, spawn, spawnSync } = require('child_process'); |
| 2 | + |
| 3 | +// Test cases that should be flagged |
| 4 | + |
| 5 | +// Template literal with interpolation (like coverage-analysis.js:234) |
| 6 | +function runTests() { |
| 7 | + const testArgs = 'test/*.js'; |
| 8 | + // ruleid: npx-usage-js |
| 9 | + const cmd = `npx jest ${testArgs} --coverage --coverageReporters=lcov`; |
| 10 | + execSync(cmd); |
| 11 | +} |
| 12 | + |
| 13 | +// Template literal passed directly to exec |
| 14 | +function lintCode() { |
| 15 | + // ruleid: npx-usage-js |
| 16 | + execSync(`npx eslint src/`); |
| 17 | +} |
| 18 | + |
| 19 | +// String literal in error message (like global.setup.ts:72) |
| 20 | +// This won't be caught because "Example: npx..." doesn't parse as shell command starting with npx |
| 21 | +function throwError() { |
| 22 | + throw new Error( |
| 23 | + // ok: npx-usage-js |
| 24 | + 'Please specify a project name with --project flag. Example: npx playwright test --project dummy-test-local' |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +// String literal with scoped package |
| 29 | +function formatCode() { |
| 30 | + // ruleid: npx-usage-js |
| 31 | + const command = "npx @typescript-eslint/parser --version"; |
| 32 | + exec(command); |
| 33 | +} |
| 34 | + |
| 35 | +// Template literal with output redirection |
| 36 | +function generateFingerprint() { |
| 37 | + // ruleid: npx-usage-js |
| 38 | + exec(`npx @expo/fingerprint ./ > fingerprint.json`); |
| 39 | +} |
| 40 | + |
| 41 | +// Template literal with flags |
| 42 | +function setupTool() { |
| 43 | + // ruleid: npx-usage-js |
| 44 | + const setupCmd = `npx --yes create-react-app my-app`; |
| 45 | + execSync(setupCmd); |
| 46 | +} |
| 47 | + |
| 48 | +// Template literal with environment variables |
| 49 | +function runWithEnv() { |
| 50 | + const workspace = process.env.GITHUB_WORKSPACE; |
| 51 | + // ruleid: npx-usage-js |
| 52 | + spawn(`npx jest ${workspace} --coverage`); |
| 53 | +} |
| 54 | + |
| 55 | +// Template literal in command chain |
| 56 | +function buildAndTest() { |
| 57 | + // ruleid: npx-usage-js |
| 58 | + execSync(`yarn build && npx jest --coverage`); |
| 59 | +} |
| 60 | + |
| 61 | +// String literal assigned to variable |
| 62 | +function assignCommand() { |
| 63 | + // ruleid: npx-usage-js |
| 64 | + let cmd = "npx prettier --write ."; |
| 65 | + return cmd; |
| 66 | +} |
| 67 | + |
| 68 | +// Test cases that should NOT be flagged |
| 69 | + |
| 70 | +// Using yarn instead |
| 71 | +function goodYarnUsage() { |
| 72 | + // ok: npx-usage-js |
| 73 | + execSync(`yarn jest --coverage`); |
| 74 | +} |
| 75 | + |
| 76 | +// Using npm scripts |
| 77 | +function goodNpmUsage() { |
| 78 | + // ok: npx-usage-js |
| 79 | + execSync('npm run test'); |
| 80 | +} |
| 81 | + |
| 82 | +// Using yarn dlx |
| 83 | +function goodYarnDlx() { |
| 84 | + // ok: npx-usage-js |
| 85 | + const cmd = `yarn dlx create-react-app my-app`; |
| 86 | + execSync(cmd); |
| 87 | +} |
| 88 | + |
| 89 | +// Direct node execution |
| 90 | +function goodNodeUsage() { |
| 91 | + // ok: npx-usage-js |
| 92 | + exec('node scripts/build.js'); |
| 93 | +} |
| 94 | + |
| 95 | +// Comment mentioning npx - should be ignored automatically |
| 96 | +// This comment talks about npx but isn't code execution |
| 97 | +function withComment() { |
| 98 | + // ok: npx-usage-js |
| 99 | + execSync('yarn test'); |
| 100 | +} |
| 101 | + |
| 102 | +// Variable name contains "npx" but not executing it |
| 103 | +function variableName() { |
| 104 | + // ok: npx-usage-js |
| 105 | + const shouldUseNpx = false; |
| 106 | + const npxWarning = "Dont use npx!"; |
| 107 | + console.log(npxWarning); |
| 108 | +} |
0 commit comments