|
| 1 | +import { RuleTester } from "eslint" |
| 2 | +import rule from "../../../lib/rules/prefer-named-replacement" |
| 3 | + |
| 4 | +const tester = new RuleTester({ |
| 5 | + parserOptions: { |
| 6 | + ecmaVersion: 2020, |
| 7 | + sourceType: "module", |
| 8 | + }, |
| 9 | +}) |
| 10 | + |
| 11 | +tester.run("prefer-named-replacement", rule as any, { |
| 12 | + valid: [ |
| 13 | + `"str".replace(/regexp/, "foo")`, |
| 14 | + `"str".replace(/a(b)c/, "_$1_")`, |
| 15 | + `"str".replaceAll(/a(b)c/, "_$1_")`, |
| 16 | + `"str".replace(/a(?<foo>b)c/, "_$<foo>_")`, |
| 17 | + `"str".replaceAll(/a(?<foo>b)c/, "_$<foo>_")`, |
| 18 | + `"str".replace(/a(?<foo>b)c/, "_$0_")`, |
| 19 | + `"str".replace(/(a)(?<foo>b)c/, "_$1_")`, |
| 20 | + `"str".replace(/a(b)c/, "_$2_")`, |
| 21 | + `unknown.replace(/a(?<foo>b)c/, "_$1_")`, |
| 22 | + `unknown.replaceAll(/a(?<foo>b)c/, "_$1_")`, |
| 23 | + ], |
| 24 | + invalid: [ |
| 25 | + { |
| 26 | + code: `"str".replace(/a(?<foo>b)c/, "_$1_")`, |
| 27 | + output: `"str".replace(/a(?<foo>b)c/, "_$<foo>_")`, |
| 28 | + errors: [ |
| 29 | + { |
| 30 | + message: |
| 31 | + "Unexpected indexed reference in replacement string.", |
| 32 | + line: 1, |
| 33 | + column: 32, |
| 34 | + }, |
| 35 | + ], |
| 36 | + }, |
| 37 | + { |
| 38 | + code: `"str".replaceAll(/a(?<foo>b)c/, "_$1_")`, |
| 39 | + output: `"str".replaceAll(/a(?<foo>b)c/, "_$<foo>_")`, |
| 40 | + errors: [ |
| 41 | + { |
| 42 | + message: |
| 43 | + "Unexpected indexed reference in replacement string.", |
| 44 | + line: 1, |
| 45 | + column: 35, |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + { |
| 50 | + code: `"str".replace(/(a)(?<foo>b)c/, "_$1$2_")`, |
| 51 | + output: `"str".replace(/(a)(?<foo>b)c/, "_$1$<foo>_")`, |
| 52 | + errors: [ |
| 53 | + { |
| 54 | + message: |
| 55 | + "Unexpected indexed reference in replacement string.", |
| 56 | + line: 1, |
| 57 | + column: 36, |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + { |
| 62 | + code: `unknown.replace(/a(?<foo>b)c/, "_$1_")`, |
| 63 | + output: `unknown.replace(/a(?<foo>b)c/, "_$<foo>_")`, |
| 64 | + options: [{ strictTypes: false }], |
| 65 | + errors: ["Unexpected indexed reference in replacement string."], |
| 66 | + }, |
| 67 | + { |
| 68 | + code: `unknown.replaceAll(/a(?<foo>b)c/, "_$1_")`, |
| 69 | + output: `unknown.replaceAll(/a(?<foo>b)c/, "_$<foo>_")`, |
| 70 | + options: [{ strictTypes: false }], |
| 71 | + errors: ["Unexpected indexed reference in replacement string."], |
| 72 | + }, |
| 73 | + ], |
| 74 | +}) |
0 commit comments