|
| 1 | +/* |
| 2 | + * SonarQube JavaScript Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +import { rule } from './index.js'; |
| 18 | +import { rules } from '../external/unicorn.js'; |
| 19 | +import { DefaultParserRuleTester } from '../../../../tests/jsts/tools/testers/rule-tester.js'; |
| 20 | +import { describe, it } from 'node:test'; |
| 21 | + |
| 22 | +const upstreamRule = rules['prefer-structured-clone']; |
| 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('S7784 upstream sentinel', () => { |
| 28 | + it('upstream prefer-structured-clone raises on JSON.stringify-nested patterns that decorator suppresses', () => { |
| 29 | + const ruleTester = new DefaultParserRuleTester(); |
| 30 | + ruleTester.run('prefer-structured-clone', upstreamRule, { |
| 31 | + valid: [], |
| 32 | + invalid: [ |
| 33 | + // direct argument to JSON.stringify() — suppressed by decorator, raised by upstream |
| 34 | + { code: `const s = JSON.stringify(JSON.parse(JSON.stringify(data)));`, errors: 1 }, |
| 35 | + // nested in object literal inside JSON.stringify() — suppressed by decorator, raised by upstream |
| 36 | + { |
| 37 | + code: `const s = JSON.stringify({ items: JSON.parse(JSON.stringify(self.items)) });`, |
| 38 | + errors: 1, |
| 39 | + }, |
| 40 | + // nested in array literal inside JSON.stringify() — suppressed by decorator, raised by upstream |
| 41 | + { code: `const s = JSON.stringify([JSON.parse(JSON.stringify(items[0]))]);`, errors: 1 }, |
| 42 | + // spread inside JSON.stringify() — suppressed by decorator, raised by upstream |
| 43 | + { code: `const s = JSON.stringify({ ...JSON.parse(JSON.stringify(config)) });`, errors: 1 }, |
| 44 | + // computed property key, value position inside JSON.stringify() — suppressed by decorator, raised by upstream |
| 45 | + { |
| 46 | + code: `const s = JSON.stringify({ [key]: JSON.parse(JSON.stringify(model)) });`, |
| 47 | + errors: 1, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('S7784', () => { |
| 55 | + it('S7784', () => { |
| 56 | + const ruleTester = new DefaultParserRuleTester(); |
| 57 | + ruleTester.run('prefer-structured-clone', rule, { |
| 58 | + valid: [ |
| 59 | + { |
| 60 | + // direct argument to JSON.stringify(): serialization normalization, not deep clone |
| 61 | + code: `const s = JSON.stringify(JSON.parse(JSON.stringify(data)));`, |
| 62 | + }, |
| 63 | + { |
| 64 | + // nested in object literal inside JSON.stringify() for API request body |
| 65 | + code: ` |
| 66 | +fetch('/api/save', { |
| 67 | + method: 'POST', |
| 68 | + body: JSON.stringify({ |
| 69 | + items: JSON.parse(JSON.stringify(self.items)), |
| 70 | + blocks: JSON.parse(JSON.stringify(self.blocks)), |
| 71 | + }), |
| 72 | +}); |
| 73 | + `, |
| 74 | + }, |
| 75 | + { |
| 76 | + // nested in array literal inside JSON.stringify() |
| 77 | + code: `const s = JSON.stringify([JSON.parse(JSON.stringify(items[0]))]);`, |
| 78 | + }, |
| 79 | + { |
| 80 | + // spread inside JSON.stringify() |
| 81 | + code: `const s = JSON.stringify({ ...JSON.parse(JSON.stringify(config)) });`, |
| 82 | + }, |
| 83 | + { |
| 84 | + // computed property key, value position inside JSON.stringify() |
| 85 | + code: `const s = JSON.stringify({ [key]: JSON.parse(JSON.stringify(model)) });`, |
| 86 | + }, |
| 87 | + ], |
| 88 | + invalid: [ |
| 89 | + { |
| 90 | + // genuine deep clone: not inside JSON.stringify() |
| 91 | + code: `const clone = JSON.parse(JSON.stringify(original));`, |
| 92 | + errors: 1, |
| 93 | + }, |
| 94 | + { |
| 95 | + // deep clone in object literal not passed to JSON.stringify() |
| 96 | + code: `const obj = { copy: JSON.parse(JSON.stringify(state)) };`, |
| 97 | + errors: 1, |
| 98 | + }, |
| 99 | + ], |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments