|
15 | 15 | * along with this program; if not, see https://sonarsource.com/license/ssal/ |
16 | 16 | */ |
17 | 17 | import { rule } from './index.js'; |
| 18 | +import { rules as tsEslintRules } from '../external/typescript-eslint/index.js'; |
18 | 19 | import { NoTypeCheckingRuleTester } from '../../../../tests/jsts/tools/testers/rule-tester.js'; |
19 | 20 | import { describe, it } from 'node:test'; |
20 | 21 |
|
21 | 22 | const ruleTester = new NoTypeCheckingRuleTester(); |
22 | 23 |
|
| 24 | +// Sentinel: verify that the upstream ESLint rule still raises on the patterns our decorator fixes. |
| 25 | +// If this test starts failing (i.e., the upstream rule no longer reports these patterns), |
| 26 | +// it signals that the decorator can be safely removed. |
| 27 | +describe('S905 upstream sentinel', () => { |
| 28 | + it('upstream no-unused-expressions raises on comma operator sequences with side effects that decorator suppresses', () => { |
| 29 | + const upstreamRule = tsEslintRules['no-unused-expressions']; |
| 30 | + ruleTester.run('no-unused-expressions', upstreamRule, { |
| 31 | + valid: [], |
| 32 | + invalid: [ |
| 33 | + { code: `a(), b();`, errors: 1 }, // sequence of calls — suppressed by decorator, raised by upstream |
| 34 | + { code: `++i, ++j;`, errors: 1 }, // sequence of updates — suppressed by decorator, raised by upstream |
| 35 | + { code: `x = 1, f();`, errors: 1 }, // assignment then call — suppressed by decorator, raised by upstream |
| 36 | + ], |
| 37 | + }); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
23 | 41 | describe('S905', () => { |
24 | 42 | it('S905', () => { |
25 | 43 | ruleTester.run('Disallow unused expressions', rule, { |
@@ -87,6 +105,54 @@ describe('S905', () => { |
87 | 105 | }); |
88 | 106 | `, |
89 | 107 | }, |
| 108 | + // comma operator sequencing multiple function calls — all operands are calls with side effects |
| 109 | + { |
| 110 | + code: ` |
| 111 | + for (let i = 0; i < items.length; i++) { |
| 112 | + output.push(items[i]), log.push(i); |
| 113 | + } |
| 114 | + `, |
| 115 | + }, |
| 116 | + // comma operator sequencing increments — all operands are updates with side effects |
| 117 | + { |
| 118 | + code: ` |
| 119 | + while (index < items.length) { |
| 120 | + ++index, ++total; |
| 121 | + } |
| 122 | + `, |
| 123 | + }, |
| 124 | + // assignment then call — both operands have side effects |
| 125 | + { |
| 126 | + code: ` |
| 127 | + while (lexer.pos < lexer.source.length) { |
| 128 | + escaped = true, advance(); |
| 129 | + } |
| 130 | + `, |
| 131 | + }, |
| 132 | + // call then multiple increments — all operands have side effects |
| 133 | + { |
| 134 | + code: ` |
| 135 | + while (i < j) { |
| 136 | + swap(array, i, j), ++i, --j; |
| 137 | + } |
| 138 | + `, |
| 139 | + }, |
| 140 | + // ternary where alternate branch is a comma-operator sequence of assignments |
| 141 | + { |
| 142 | + code: `condition ? (state.x = 0) : (state.y = 1, state.z = 2);`, |
| 143 | + }, |
| 144 | + // logical expression where right side is a comma-operator sequence of calls |
| 145 | + { |
| 146 | + code: `condition && (f(), g());`, |
| 147 | + }, |
| 148 | + // nested ternary (UMD-style) where all branches are assignments or calls |
| 149 | + { |
| 150 | + code: `cond1 ? module.exports = factory() : cond2 ? define(factory) : (g = this, g.lib = factory());`, |
| 151 | + }, |
| 152 | + // sequence of test-framework calls separated by commas |
| 153 | + { |
| 154 | + code: `it('first test', function() { assert(true); }), it('second test', function() { assert(true); });`, |
| 155 | + }, |
90 | 156 | ], |
91 | 157 | invalid: [ |
92 | 158 | { |
@@ -152,6 +218,11 @@ describe('S905', () => { |
152 | 218 | }, |
153 | 219 | ], |
154 | 220 | }, |
| 221 | + // sequence where one element has no side effect — still flagged |
| 222 | + { |
| 223 | + code: `a + b, c();`, |
| 224 | + errors: 1, |
| 225 | + }, |
155 | 226 | ], |
156 | 227 | }); |
157 | 228 | }); |
|
0 commit comments