diff --git a/package.json b/package.json index 764a59044..f952f4e36 100644 --- a/package.json +++ b/package.json @@ -131,7 +131,7 @@ "eslint-plugin-sonarjs": "3.0.2", "eslint-plugin-unicorn": "59.0.1", "eslint-plugin-yml": "1.18.0", - "eslint-vitest-rule-tester": "1.1.0", + "eslint-vitest-rule-tester": "2.2.0", "fast-glob": "3.3.3", "husky": "9.1.7", "jsonc-eslint-parser": "2.4.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 66e623fbd..9a1b9f309 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -157,8 +157,8 @@ importers: specifier: 1.18.0 version: 1.18.0(eslint@9.25.1(jiti@2.4.2)) eslint-vitest-rule-tester: - specifier: 1.1.0 - version: 1.1.0(eslint@9.25.1(jiti@2.4.2))(typescript@5.8.3)(vitest@3.1.2(@types/debug@4.1.12)(@types/node@18.19.100)) + specifier: 2.2.0 + version: 2.2.0(eslint@9.25.1(jiti@2.4.2))(typescript@5.8.3)(vitest@3.1.2(@types/debug@4.1.12)(@types/node@18.19.100)) fast-glob: specifier: 3.3.3 version: 3.3.3 @@ -226,9 +226,6 @@ packages: '@antfu/install-pkg@1.0.0': resolution: {integrity: sha512-xvX6P/lo1B3ej0OsaErAjqgFYzYVcJpamjLAFLYh9vRJngBrMoUG7aVnrGTeqM7yxbyTD5p3F2+0/QUEh8Vzhw==} - '@antfu/utils@8.1.0': - resolution: {integrity: sha512-XPR7Jfwp0FFl/dFYPX8ZjpmU4/1mIXTjnZ1ba48BLMyKOV62/tiRjdsFcPs2hsYcSud4tzk7w3a3LjX8Fu3huA==} - '@babel/code-frame@7.27.1': resolution: {integrity: sha512-cjQ7ZlQ0Mv3b47hABuTevyTuYN4i+loJKGeV9flcCgIK37cCXRh+L1bd3iBHlynerhQ7BhCkn2BPbQUL+rGqFg==} engines: {node: '>=6.9.0'} @@ -2491,8 +2488,8 @@ packages: resolution: {integrity: sha512-UyLnSehNt62FFhSwjZlHmeokpRK59rcz29j+F1/aDgbkbRTk7wIc9XzdoasMUbRNKDM0qQt/+BJ4BrpFeABemw==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} - eslint-vitest-rule-tester@1.1.0: - resolution: {integrity: sha512-rilURDIBbHZKUDXdnlDnewJ3A5NdiK9HpYgdaeKY7rPpn+nI1KfvB/DyYMc0R8KgL9ziu5lbXVmWukgX0JPbAQ==} + eslint-vitest-rule-tester@2.2.0: + resolution: {integrity: sha512-4qnX3piKH1a41zBFHE2fQUKZI2/yhhpqJyEOTDGwP1jZ1tkcwvkXbtYNDcTY3YmirqqlNPAWw0UvIPW1rcEtLw==} peerDependencies: eslint: ^9.0.0 vitest: ^1.0.0 || ^2.0.0 || ^3.0.0 @@ -4756,8 +4753,6 @@ snapshots: package-manager-detector: 0.2.11 tinyexec: 0.3.2 - '@antfu/utils@8.1.0': {} - '@babel/code-frame@7.27.1': dependencies: '@babel/helper-validator-identifier': 7.27.1 @@ -7104,9 +7099,8 @@ snapshots: eslint-visitor-keys@4.2.0: {} - eslint-vitest-rule-tester@1.1.0(eslint@9.25.1(jiti@2.4.2))(typescript@5.8.3)(vitest@3.1.2(@types/debug@4.1.12)(@types/node@18.19.100)): + eslint-vitest-rule-tester@2.2.0(eslint@9.25.1(jiti@2.4.2))(typescript@5.8.3)(vitest@3.1.2(@types/debug@4.1.12)(@types/node@18.19.100)): dependencies: - '@antfu/utils': 8.1.0 '@types/eslint': 9.6.1 '@typescript-eslint/utils': 8.31.1(eslint@9.25.1(jiti@2.4.2))(typescript@5.8.3) eslint: 9.25.1(jiti@2.4.2) diff --git a/tests/rules/__snapshots__/functional-parameters.test.ts.snap b/tests/rules/__snapshots__/functional-parameters.test.ts.snap index 42dc916f9..58dc1b0eb 100644 --- a/tests/rules/__snapshots__/functional-parameters.test.ts.snap +++ b/tests/rules/__snapshots__/functional-parameters.test.ts.snap @@ -1,129 +1,341 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`functional-parameters > javascript - es latest > options > enforceParameterCount > atLeastOne 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Functions must have at least one parameter.", - "messageId": "paramCountAtLeastOne", - "nodeType": "FunctionDeclaration", - "ruleId": "functional-parameters", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Functions must have at least one parameter.", + "messageId": "paramCountAtLeastOne", + "nodeType": "FunctionDeclaration", + "ruleId": "functional-parameters", + "severity": 2, + }, + ], + "output": "function foo() { + console.log("hello world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Functions must have at least one parameter.", + "messageId": "paramCountAtLeastOne", + "nodeType": "FunctionDeclaration", + "ruleId": "rule-to-test/functional-parameters", + "severity": 2, + }, + ], + "output": "function foo() { + console.log("hello world"); +}", + }, + ], +} `; exports[`functional-parameters > javascript - es latest > options > enforceParameterCount > exactlyOne 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Functions must have exactly one parameter.", - "messageId": "paramCountExactlyOne", - "nodeType": "FunctionDeclaration", - "ruleId": "functional-parameters", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Functions must have exactly one parameter.", + "messageId": "paramCountExactlyOne", + "nodeType": "FunctionDeclaration", + "ruleId": "functional-parameters", + "severity": 2, + }, + ], + "output": "function foo(bar, baz) { + console.log(bar, baz); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Functions must have exactly one parameter.", + "messageId": "paramCountExactlyOne", + "nodeType": "FunctionDeclaration", + "ruleId": "rule-to-test/functional-parameters", + "severity": 2, + }, + ], + "output": "function foo(bar, baz) { + console.log(bar, baz); +}", + }, + ], +} `; exports[`functional-parameters > javascript - es latest > options > enforceParameterCount > exactlyOne 2`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Functions must have exactly one parameter.", - "messageId": "paramCountExactlyOne", - "nodeType": "FunctionDeclaration", - "ruleId": "functional-parameters", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Functions must have exactly one parameter.", + "messageId": "paramCountExactlyOne", + "nodeType": "FunctionDeclaration", + "ruleId": "functional-parameters", + "severity": 2, + }, + ], + "output": "function foo() { + console.log("hello world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Functions must have exactly one parameter.", + "messageId": "paramCountExactlyOne", + "nodeType": "FunctionDeclaration", + "ruleId": "rule-to-test/functional-parameters", + "severity": 2, + }, + ], + "output": "function foo() { + console.log("hello world"); +}", + }, + ], +} `; exports[`functional-parameters > javascript - es latest > options > enforceParameterCount > ignoreIIFE 1`] = ` -[ - { - "column": 2, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Functions must have at least one parameter.", - "messageId": "paramCountAtLeastOne", - "nodeType": "ArrowFunctionExpression", - "ruleId": "functional-parameters", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 2, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Functions must have at least one parameter.", + "messageId": "paramCountAtLeastOne", + "nodeType": "ArrowFunctionExpression", + "ruleId": "functional-parameters", + "severity": 2, + }, + ], + "output": "(() => { + console.log("hello world"); +})();", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 2, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Functions must have at least one parameter.", + "messageId": "paramCountAtLeastOne", + "nodeType": "ArrowFunctionExpression", + "ruleId": "rule-to-test/functional-parameters", + "severity": 2, + }, + ], + "output": "(() => { + console.log("hello world"); +})();", + }, + ], +} `; exports[`functional-parameters > javascript - es latest > options > enforceParameterCount > ignoreLambdaExpression 1`] = ` -[ - { - "column": 1, - "endColumn": 18, - "endLine": 1, - "line": 1, - "message": "Functions must have at least one parameter.", - "messageId": "paramCountAtLeastOne", - "nodeType": "FunctionDeclaration", - "ruleId": "functional-parameters", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 18, + "endLine": 1, + "line": 1, + "message": "Functions must have at least one parameter.", + "messageId": "paramCountAtLeastOne", + "nodeType": "FunctionDeclaration", + "ruleId": "functional-parameters", + "severity": 2, + }, + ], + "output": "function foo() {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 18, + "endLine": 1, + "line": 1, + "message": "Functions must have at least one parameter.", + "messageId": "paramCountAtLeastOne", + "nodeType": "FunctionDeclaration", + "ruleId": "rule-to-test/functional-parameters", + "severity": 2, + }, + ], + "output": "function foo() {}", + }, + ], +} `; exports[`functional-parameters > javascript - es latest > options > ignoreIdentifierPattern 1`] = ` -[ - { - "column": 14, - "endColumn": 20, - "endLine": 1, - "line": 1, - "message": "Unexpected rest parameter. Use a regular parameter of type array instead.", - "messageId": "restParam", - "nodeType": "RestElement", - "ruleId": "functional-parameters", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 20, + "endLine": 1, + "line": 1, + "message": "Unexpected rest parameter. Use a regular parameter of type array instead.", + "messageId": "restParam", + "nodeType": "RestElement", + "ruleId": "functional-parameters", + "severity": 2, + }, + ], + "output": "function foo(...bar) { + console.log(bar); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 20, + "endLine": 1, + "line": 1, + "message": "Unexpected rest parameter. Use a regular parameter of type array instead.", + "messageId": "restParam", + "nodeType": "RestElement", + "ruleId": "rule-to-test/functional-parameters", + "severity": 2, + }, + ], + "output": "function foo(...bar) { + console.log(bar); +}", + }, + ], +} `; exports[`functional-parameters > javascript - es latest > reports arguments keyword violations 1`] = ` -[ - { - "column": 15, - "endColumn": 24, - "endLine": 2, - "line": 2, - "message": "Unexpected use of \`arguments\`. Use regular function arguments instead.", - "messageId": "arguments", - "nodeType": "Identifier", - "ruleId": "functional-parameters", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 24, + "endLine": 2, + "line": 2, + "message": "Unexpected use of \`arguments\`. Use regular function arguments instead.", + "messageId": "arguments", + "nodeType": "Identifier", + "ruleId": "functional-parameters", + "severity": 2, + }, + ], + "output": "function foo(bar) { + console.log(arguments); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 24, + "endLine": 2, + "line": 2, + "message": "Unexpected use of \`arguments\`. Use regular function arguments instead.", + "messageId": "arguments", + "nodeType": "Identifier", + "ruleId": "rule-to-test/functional-parameters", + "severity": 2, + }, + ], + "output": "function foo(bar) { + console.log(arguments); +}", + }, + ], +} `; exports[`functional-parameters > javascript - es latest > reports rest parameter violations 1`] = ` -[ - { - "column": 14, - "endColumn": 20, - "endLine": 1, - "line": 1, - "message": "Unexpected rest parameter. Use a regular parameter of type array instead.", - "messageId": "restParam", - "nodeType": "RestElement", - "ruleId": "functional-parameters", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 20, + "endLine": 1, + "line": 1, + "message": "Unexpected rest parameter. Use a regular parameter of type array instead.", + "messageId": "restParam", + "nodeType": "RestElement", + "ruleId": "functional-parameters", + "severity": 2, + }, + ], + "output": "function foo(...bar) { + console.log(bar); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 20, + "endLine": 1, + "line": 1, + "message": "Unexpected rest parameter. Use a regular parameter of type array instead.", + "messageId": "restParam", + "nodeType": "RestElement", + "ruleId": "rule-to-test/functional-parameters", + "severity": 2, + }, + ], + "output": "function foo(...bar) { + console.log(bar); +}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-class-inheritance.test.ts.snap b/tests/rules/__snapshots__/no-class-inheritance.test.ts.snap index a11213fd1..33181f2e2 100644 --- a/tests/rules/__snapshots__/no-class-inheritance.test.ts.snap +++ b/tests/rules/__snapshots__/no-class-inheritance.test.ts.snap @@ -1,124 +1,296 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-class-inheritance > javascript - es latest > ignoreCodePattern > should report class inheritance with non-matching identifiers 1`] = ` -[ - { - "column": 11, - "endColumn": 22, - "endLine": 1, - "line": 1, - "message": "Unexpected inheritance, use composition instead.", - "messageId": "extends", - "nodeType": "ClassDeclaration", - "ruleId": "no-class-inheritance", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 11, + "endColumn": 22, + "endLine": 1, + "line": 1, + "message": "Unexpected inheritance, use composition instead.", + "messageId": "extends", + "nodeType": "ClassDeclaration", + "ruleId": "no-class-inheritance", + "severity": 2, + }, + ], + "output": "class Bar extends Foo {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 11, + "endColumn": 22, + "endLine": 1, + "line": 1, + "message": "Unexpected inheritance, use composition instead.", + "messageId": "extends", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-class-inheritance", + "severity": 2, + }, + ], + "output": "class Bar extends Foo {}", + }, + ], +} `; exports[`no-class-inheritance > javascript - es latest > options > ignoreIdentifierPattern > should report class inheritance with non-matching identifiers 1`] = ` -[ - { - "column": 11, - "endColumn": 22, - "endLine": 1, - "line": 1, - "message": "Unexpected inheritance, use composition instead.", - "messageId": "extends", - "nodeType": "ClassDeclaration", - "ruleId": "no-class-inheritance", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 11, + "endColumn": 22, + "endLine": 1, + "line": 1, + "message": "Unexpected inheritance, use composition instead.", + "messageId": "extends", + "nodeType": "ClassDeclaration", + "ruleId": "no-class-inheritance", + "severity": 2, + }, + ], + "output": "class Bar extends Foo {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 11, + "endColumn": 22, + "endLine": 1, + "line": 1, + "message": "Unexpected inheritance, use composition instead.", + "messageId": "extends", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-class-inheritance", + "severity": 2, + }, + ], + "output": "class Bar extends Foo {}", + }, + ], +} `; exports[`no-class-inheritance > javascript - es latest > reports class inheritance 1`] = ` -[ - { - "column": 11, - "endColumn": 22, - "endLine": 1, - "line": 1, - "message": "Unexpected inheritance, use composition instead.", - "messageId": "extends", - "nodeType": "ClassDeclaration", - "ruleId": "no-class-inheritance", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 11, + "endColumn": 22, + "endLine": 1, + "line": 1, + "message": "Unexpected inheritance, use composition instead.", + "messageId": "extends", + "nodeType": "ClassDeclaration", + "ruleId": "no-class-inheritance", + "severity": 2, + }, + ], + "output": "class Foo extends Bar {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 11, + "endColumn": 22, + "endLine": 1, + "line": 1, + "message": "Unexpected inheritance, use composition instead.", + "messageId": "extends", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-class-inheritance", + "severity": 2, + }, + ], + "output": "class Foo extends Bar {}", + }, + ], +} `; exports[`no-class-inheritance > typescript > ignoreCodePattern > should report class inheritance with non-matching identifiers 1`] = ` -[ - { - "column": 1, - "endColumn": 9, - "endLine": 1, - "line": 1, - "message": "Unexpected abstract class.", - "messageId": "abstract", - "nodeType": "ClassDeclaration", - "ruleId": "no-class-inheritance", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 1, + "line": 1, + "message": "Unexpected abstract class.", + "messageId": "abstract", + "nodeType": "ClassDeclaration", + "ruleId": "no-class-inheritance", + "severity": 2, + }, + ], + "output": "abstract class Bar {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 1, + "line": 1, + "message": "Unexpected abstract class.", + "messageId": "abstract", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-class-inheritance", + "severity": 2, + }, + ], + "output": "abstract class Bar {}", + }, + ], +} `; exports[`no-class-inheritance > typescript > options > ignoreIdentifierPattern > should report class inheritance with non-matching identifiers 1`] = ` -[ - { - "column": 1, - "endColumn": 9, - "endLine": 1, - "line": 1, - "message": "Unexpected abstract class.", - "messageId": "abstract", - "nodeType": "ClassDeclaration", - "ruleId": "no-class-inheritance", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 1, + "line": 1, + "message": "Unexpected abstract class.", + "messageId": "abstract", + "nodeType": "ClassDeclaration", + "ruleId": "no-class-inheritance", + "severity": 2, + }, + ], + "output": "abstract class Bar {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 1, + "line": 1, + "message": "Unexpected abstract class.", + "messageId": "abstract", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-class-inheritance", + "severity": 2, + }, + ], + "output": "abstract class Bar {}", + }, + ], +} `; exports[`no-class-inheritance > typescript > reports class inheritance 1`] = ` -[ - { - "column": 1, - "endColumn": 9, - "endLine": 1, - "line": 1, - "message": "Unexpected abstract class.", - "messageId": "abstract", - "nodeType": "ClassDeclaration", - "ruleId": "no-class-inheritance", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 1, + "line": 1, + "message": "Unexpected abstract class.", + "messageId": "abstract", + "nodeType": "ClassDeclaration", + "ruleId": "no-class-inheritance", + "severity": 2, + }, + ], + "output": "abstract class Foo {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 1, + "line": 1, + "message": "Unexpected abstract class.", + "messageId": "abstract", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-class-inheritance", + "severity": 2, + }, + ], + "output": "abstract class Foo {}", + }, + ], +} `; exports[`no-class-inheritance > typescript > reports class inheritance 2`] = ` -[ - { - "column": 1, - "endColumn": 9, - "endLine": 1, - "line": 1, - "message": "Unexpected abstract class.", - "messageId": "abstract", - "nodeType": "ClassDeclaration", - "ruleId": "no-class-inheritance", - "severity": 2, - }, - { - "column": 20, - "endColumn": 31, - "endLine": 1, - "line": 1, - "message": "Unexpected inheritance, use composition instead.", - "messageId": "extends", - "nodeType": "ClassDeclaration", - "ruleId": "no-class-inheritance", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 1, + "line": 1, + "message": "Unexpected abstract class.", + "messageId": "abstract", + "nodeType": "ClassDeclaration", + "ruleId": "no-class-inheritance", + "severity": 2, + }, + { + "column": 20, + "endColumn": 31, + "endLine": 1, + "line": 1, + "message": "Unexpected inheritance, use composition instead.", + "messageId": "extends", + "nodeType": "ClassDeclaration", + "ruleId": "no-class-inheritance", + "severity": 2, + }, + ], + "output": "abstract class Foo extends Bar {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 1, + "line": 1, + "message": "Unexpected abstract class.", + "messageId": "abstract", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-class-inheritance", + "severity": 2, + }, + { + "column": 20, + "endColumn": 31, + "endLine": 1, + "line": 1, + "message": "Unexpected inheritance, use composition instead.", + "messageId": "extends", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-class-inheritance", + "severity": 2, + }, + ], + "output": "abstract class Foo extends Bar {}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-classes.test.ts.snap b/tests/rules/__snapshots__/no-classes.test.ts.snap index cc30e10ef..32666eaa7 100644 --- a/tests/rules/__snapshots__/no-classes.test.ts.snap +++ b/tests/rules/__snapshots__/no-classes.test.ts.snap @@ -1,65 +1,157 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-classes > javascript - es latest > ignoreCodePattern > should report classes with non-matching identifiers 1`] = ` -[ - { - "column": 1, - "endColumn": 13, - "endLine": 1, - "line": 1, - "message": "Unexpected class, use functions not classes.", - "messageId": "generic", - "nodeType": "ClassDeclaration", - "ruleId": "no-classes", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 13, + "endLine": 1, + "line": 1, + "message": "Unexpected class, use functions not classes.", + "messageId": "generic", + "nodeType": "ClassDeclaration", + "ruleId": "no-classes", + "severity": 2, + }, + ], + "output": "class Bar {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 13, + "endLine": 1, + "line": 1, + "message": "Unexpected class, use functions not classes.", + "messageId": "generic", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-classes", + "severity": 2, + }, + ], + "output": "class Bar {}", + }, + ], +} `; exports[`no-classes > javascript - es latest > options > ignoreIdentifierPattern > should report classes with non-matching identifiers 1`] = ` -[ - { - "column": 1, - "endColumn": 13, - "endLine": 1, - "line": 1, - "message": "Unexpected class, use functions not classes.", - "messageId": "generic", - "nodeType": "ClassDeclaration", - "ruleId": "no-classes", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 13, + "endLine": 1, + "line": 1, + "message": "Unexpected class, use functions not classes.", + "messageId": "generic", + "nodeType": "ClassDeclaration", + "ruleId": "no-classes", + "severity": 2, + }, + ], + "output": "class Bar {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 13, + "endLine": 1, + "line": 1, + "message": "Unexpected class, use functions not classes.", + "messageId": "generic", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-classes", + "severity": 2, + }, + ], + "output": "class Bar {}", + }, + ], +} `; exports[`no-classes > javascript - es latest > reports class declarations 1`] = ` -[ - { - "column": 1, - "endColumn": 13, - "endLine": 1, - "line": 1, - "message": "Unexpected class, use functions not classes.", - "messageId": "generic", - "nodeType": "ClassDeclaration", - "ruleId": "no-classes", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 13, + "endLine": 1, + "line": 1, + "message": "Unexpected class, use functions not classes.", + "messageId": "generic", + "nodeType": "ClassDeclaration", + "ruleId": "no-classes", + "severity": 2, + }, + ], + "output": "class Foo {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 13, + "endLine": 1, + "line": 1, + "message": "Unexpected class, use functions not classes.", + "messageId": "generic", + "nodeType": "ClassDeclaration", + "ruleId": "rule-to-test/no-classes", + "severity": 2, + }, + ], + "output": "class Foo {}", + }, + ], +} `; exports[`no-classes > javascript - es latest > reports class declarations 2`] = ` -[ - { - "column": 15, - "endColumn": 23, - "endLine": 1, - "line": 1, - "message": "Unexpected class, use functions not classes.", - "messageId": "generic", - "nodeType": "ClassExpression", - "ruleId": "no-classes", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 23, + "endLine": 1, + "line": 1, + "message": "Unexpected class, use functions not classes.", + "messageId": "generic", + "nodeType": "ClassExpression", + "ruleId": "no-classes", + "severity": 2, + }, + ], + "output": "const klass = class {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 23, + "endLine": 1, + "line": 1, + "message": "Unexpected class, use functions not classes.", + "messageId": "generic", + "nodeType": "ClassExpression", + "ruleId": "rule-to-test/no-classes", + "severity": 2, + }, + ], + "output": "const klass = class {}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-conditional-statements.test.ts.snap b/tests/rules/__snapshots__/no-conditional-statements.test.ts.snap index e856609aa..4185fcd8a 100644 --- a/tests/rules/__snapshots__/no-conditional-statements.test.ts.snap +++ b/tests/rules/__snapshots__/no-conditional-statements.test.ts.snap @@ -1,65 +1,195 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-conditional-statements > typescript > if statements > options > allowReturningBranches > ifExhaustive > else required 1`] = ` -[ - { - "column": 3, - "endColumn": 4, - "endLine": 4, - "line": 2, - "message": "Incomplete if, it must have an else statement and every branch must contain a return statement.", - "messageId": "incompleteIf", - "nodeType": "IfStatement", - "ruleId": "no-conditional-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 4, + "endLine": 4, + "line": 2, + "message": "Incomplete if, it must have an else statement and every branch must contain a return statement.", + "messageId": "incompleteIf", + "nodeType": "IfStatement", + "ruleId": "no-conditional-statements", + "severity": 2, + }, + ], + "output": "function foo(i) { + if (i === 1) { + return 1; + } +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 4, + "endLine": 4, + "line": 2, + "message": "Incomplete if, it must have an else statement and every branch must contain a return statement.", + "messageId": "incompleteIf", + "nodeType": "IfStatement", + "ruleId": "rule-to-test/no-conditional-statements", + "severity": 2, + }, + ], + "output": "function foo(i) { + if (i === 1) { + return 1; + } +}", + }, + ], +} `; exports[`no-conditional-statements > typescript > if statements > reports if statements 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Unexpected if, use a conditional expression (ternary operator) instead.", - "messageId": "unexpectedIf", - "nodeType": "IfStatement", - "ruleId": "no-conditional-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected if, use a conditional expression (ternary operator) instead.", + "messageId": "unexpectedIf", + "nodeType": "IfStatement", + "ruleId": "no-conditional-statements", + "severity": 2, + }, + ], + "output": "if (true) { + console.log("hello world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected if, use a conditional expression (ternary operator) instead.", + "messageId": "unexpectedIf", + "nodeType": "IfStatement", + "ruleId": "rule-to-test/no-conditional-statements", + "severity": 2, + }, + ], + "output": "if (true) { + console.log("hello world"); +}", + }, + ], +} `; exports[`no-conditional-statements > typescript > switch statements > options > allowReturningBranches > ifExhaustive > requires default case 1`] = ` -[ - { - "column": 3, - "endColumn": 4, - "endLine": 7, - "line": 2, - "message": "Incomplete switch, it must be exhaustive or have an default case and every case must contain a return statement.", - "messageId": "incompleteSwitch", - "nodeType": "SwitchStatement", - "ruleId": "no-conditional-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 4, + "endLine": 7, + "line": 2, + "message": "Incomplete switch, it must be exhaustive or have an default case and every case must contain a return statement.", + "messageId": "incompleteSwitch", + "nodeType": "SwitchStatement", + "ruleId": "no-conditional-statements", + "severity": 2, + }, + ], + "output": "function foo(i) { + switch(i) { + case "a": + return 1; + case "b": + return 2; + } +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 4, + "endLine": 7, + "line": 2, + "message": "Incomplete switch, it must be exhaustive or have an default case and every case must contain a return statement.", + "messageId": "incompleteSwitch", + "nodeType": "SwitchStatement", + "ruleId": "rule-to-test/no-conditional-statements", + "severity": 2, + }, + ], + "output": "function foo(i) { + switch(i) { + case "a": + return 1; + case "b": + return 2; + } +}", + }, + ], +} `; exports[`no-conditional-statements > typescript > switch statements > reports switch statements 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 7, - "line": 1, - "message": "Unexpected switch, use a conditional expression (ternary operator) instead.", - "messageId": "unexpectedSwitch", - "nodeType": "SwitchStatement", - "ruleId": "no-conditional-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 7, + "line": 1, + "message": "Unexpected switch, use a conditional expression (ternary operator) instead.", + "messageId": "unexpectedSwitch", + "nodeType": "SwitchStatement", + "ruleId": "no-conditional-statements", + "severity": 2, + }, + ], + "output": "switch(i) { + case "a": + console.log("hello"); + case "b": + case "c": + console.log("world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 7, + "line": 1, + "message": "Unexpected switch, use a conditional expression (ternary operator) instead.", + "messageId": "unexpectedSwitch", + "nodeType": "SwitchStatement", + "ruleId": "rule-to-test/no-conditional-statements", + "severity": 2, + }, + ], + "output": "switch(i) { + case "a": + console.log("hello"); + case "b": + case "c": + console.log("world"); +}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-expression-statements.test.ts.snap b/tests/rules/__snapshots__/no-expression-statements.test.ts.snap index acb52a278..c1d3d211d 100644 --- a/tests/rules/__snapshots__/no-expression-statements.test.ts.snap +++ b/tests/rules/__snapshots__/no-expression-statements.test.ts.snap @@ -1,49 +1,122 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-expression-statements > javascript - es latest > options > ignoreCodePattern 1`] = ` -[ - { - "column": 1, - "endColumn": 17, - "endLine": 1, - "line": 1, - "message": "Using expressions to cause side-effects not allowed.", - "messageId": "generic", - "nodeType": "ExpressionStatement", - "ruleId": "no-expression-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 17, + "endLine": 1, + "line": 1, + "message": "Using expressions to cause side-effects not allowed.", + "messageId": "generic", + "nodeType": "ExpressionStatement", + "ruleId": "no-expression-statements", + "severity": 2, + }, + ], + "output": "console.trace();", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 17, + "endLine": 1, + "line": 1, + "message": "Using expressions to cause side-effects not allowed.", + "messageId": "generic", + "nodeType": "ExpressionStatement", + "ruleId": "rule-to-test/no-expression-statements", + "severity": 2, + }, + ], + "output": "console.trace();", + }, + ], +} `; exports[`no-expression-statements > javascript - es latest > reports expression statements 1`] = ` -[ - { - "column": 1, - "endColumn": 11, - "endLine": 2, - "line": 2, - "message": "Using expressions to cause side-effects not allowed.", - "messageId": "generic", - "nodeType": "ExpressionStatement", - "ruleId": "no-expression-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 11, + "endLine": 2, + "line": 2, + "message": "Using expressions to cause side-effects not allowed.", + "messageId": "generic", + "nodeType": "ExpressionStatement", + "ruleId": "no-expression-statements", + "severity": 2, + }, + ], + "output": "var x = []; +x.push(1);", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 11, + "endLine": 2, + "line": 2, + "message": "Using expressions to cause side-effects not allowed.", + "messageId": "generic", + "nodeType": "ExpressionStatement", + "ruleId": "rule-to-test/no-expression-statements", + "severity": 2, + }, + ], + "output": "var x = []; +x.push(1);", + }, + ], +} `; exports[`no-expression-statements > typescript > options > ignoreSelfReturning 1`] = ` -[ - { - "column": 1, - "endColumn": 7, - "endLine": 2, - "line": 2, - "message": "Using expressions to cause side-effects not allowed.", - "messageId": "generic", - "nodeType": "ExpressionStatement", - "ruleId": "no-expression-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 2, + "line": 2, + "message": "Using expressions to cause side-effects not allowed.", + "messageId": "generic", + "nodeType": "ExpressionStatement", + "ruleId": "no-expression-statements", + "severity": 2, + }, + ], + "output": "const foo = () => { return this; }; +foo();", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 2, + "line": 2, + "message": "Using expressions to cause side-effects not allowed.", + "messageId": "generic", + "nodeType": "ExpressionStatement", + "ruleId": "rule-to-test/no-expression-statements", + "severity": 2, + }, + ], + "output": "const foo = () => { return this; }; +foo();", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-let.test.ts.snap b/tests/rules/__snapshots__/no-let.test.ts.snap index deba1dc39..43b6690cf 100644 --- a/tests/rules/__snapshots__/no-let.test.ts.snap +++ b/tests/rules/__snapshots__/no-let.test.ts.snap @@ -1,147 +1,346 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-let > javascript - es latest > options > allowInForLoopInit > should not report let declarations in for loop init 1`] = ` -[ - { - "column": 1, - "endColumn": 7, - "endLine": 1, - "line": 1, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 2, - "line": 2, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 1, + "line": 1, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 2, + "line": 2, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + ], + "output": "let x; +let y = 0;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 1, + "line": 1, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 2, + "line": 2, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + ], + "output": "let x; +let y = 0;", + }, + ], +} `; exports[`no-let > javascript - es latest > options > allowInFunctions > should not report let declarations in arrow function declarations 1`] = ` -[ - { - "column": 1, - "endColumn": 7, - "endLine": 1, - "line": 1, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 2, - "line": 2, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 1, + "line": 1, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 2, + "line": 2, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + ], + "output": "let x; +let y = 0;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 1, + "line": 1, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 2, + "line": 2, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + ], + "output": "let x; +let y = 0;", + }, + ], +} `; exports[`no-let > javascript - es latest > options > allowInFunctions > should not report let declarations in function declarations 1`] = ` -[ - { - "column": 1, - "endColumn": 7, - "endLine": 1, - "line": 1, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 2, - "line": 2, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 1, + "line": 1, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 2, + "line": 2, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + ], + "output": "let x; +let y = 0;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 1, + "line": 1, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 2, + "line": 2, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + ], + "output": "let x; +let y = 0;", + }, + ], +} `; exports[`no-let > javascript - es latest > options > allowInFunctions > should not report let declarations in method declarations 1`] = ` -[ - { - "column": 1, - "endColumn": 7, - "endLine": 1, - "line": 1, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 2, - "line": 2, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 1, + "line": 1, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 2, + "line": 2, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + ], + "output": "let x; +let y = 0;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 1, + "line": 1, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 2, + "line": 2, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + ], + "output": "let x; +let y = 0;", + }, + ], +} `; exports[`no-let > javascript - es latest > should report let declarations 1`] = ` -[ - { - "column": 1, - "endColumn": 7, - "endLine": 1, - "line": 1, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, - { - "column": 3, - "endColumn": 9, - "endLine": 4, - "line": 4, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, - { - "column": 3, - "endColumn": 13, - "endLine": 5, - "line": 5, - "message": "Unexpected let, use const instead.", - "messageId": "generic", - "nodeType": "VariableDeclaration", - "ruleId": "no-let", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 1, + "line": 1, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + { + "column": 3, + "endColumn": 9, + "endLine": 4, + "line": 4, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + { + "column": 3, + "endColumn": 13, + "endLine": 5, + "line": 5, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "no-let", + "severity": 2, + }, + ], + "output": "let x; + +function foo() { + let y; + let z = 0; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 7, + "endLine": 1, + "line": 1, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + { + "column": 3, + "endColumn": 9, + "endLine": 4, + "line": 4, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + { + "column": 3, + "endColumn": 13, + "endLine": 5, + "line": 5, + "message": "Unexpected let, use const instead.", + "messageId": "generic", + "nodeType": "VariableDeclaration", + "ruleId": "rule-to-test/no-let", + "severity": 2, + }, + ], + "output": "let x; + +function foo() { + let y; + let z = 0; +}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-loop-statements.test.ts.snap b/tests/rules/__snapshots__/no-loop-statements.test.ts.snap index 4dfa61841..bc77cb250 100644 --- a/tests/rules/__snapshots__/no-loop-statements.test.ts.snap +++ b/tests/rules/__snapshots__/no-loop-statements.test.ts.snap @@ -1,97 +1,259 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-loop-statements > javascript - es latest > reports do while loop statements 1`] = ` -[ - { - "column": 1, - "endColumn": 16, - "endLine": 3, - "line": 1, - "message": "Unexpected loop, use map or reduce instead.", - "messageId": "generic", - "nodeType": "DoWhileStatement", - "ruleId": "no-loop-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 16, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "DoWhileStatement", + "ruleId": "no-loop-statements", + "severity": 2, + }, + ], + "output": "do { + console.log("hello world"); +} while (true);", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 16, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "DoWhileStatement", + "ruleId": "rule-to-test/no-loop-statements", + "severity": 2, + }, + ], + "output": "do { + console.log("hello world"); +} while (true);", + }, + ], +} `; exports[`no-loop-statements > javascript - es latest > reports for await loop statements 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Unexpected loop, use map or reduce instead.", - "messageId": "generic", - "nodeType": "ForOfStatement", - "ruleId": "no-loop-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "ForOfStatement", + "ruleId": "no-loop-statements", + "severity": 2, + }, + ], + "output": "for await (let i of []) { + console.log("hello world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "ForOfStatement", + "ruleId": "rule-to-test/no-loop-statements", + "severity": 2, + }, + ], + "output": "for await (let i of []) { + console.log("hello world"); +}", + }, + ], +} `; exports[`no-loop-statements > javascript - es latest > reports for in loop statements 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Unexpected loop, use map or reduce instead.", - "messageId": "generic", - "nodeType": "ForInStatement", - "ruleId": "no-loop-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "ForInStatement", + "ruleId": "no-loop-statements", + "severity": 2, + }, + ], + "output": "for (let i in []) { + console.log("hello world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "ForInStatement", + "ruleId": "rule-to-test/no-loop-statements", + "severity": 2, + }, + ], + "output": "for (let i in []) { + console.log("hello world"); +}", + }, + ], +} `; exports[`no-loop-statements > javascript - es latest > reports for loop statements 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Unexpected loop, use map or reduce instead.", - "messageId": "generic", - "nodeType": "ForStatement", - "ruleId": "no-loop-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "ForStatement", + "ruleId": "no-loop-statements", + "severity": 2, + }, + ], + "output": "for (let i = 0; i < 10; i++) { + console.log("hello world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "ForStatement", + "ruleId": "rule-to-test/no-loop-statements", + "severity": 2, + }, + ], + "output": "for (let i = 0; i < 10; i++) { + console.log("hello world"); +}", + }, + ], +} `; exports[`no-loop-statements > javascript - es latest > reports for of loop statements 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Unexpected loop, use map or reduce instead.", - "messageId": "generic", - "nodeType": "ForOfStatement", - "ruleId": "no-loop-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "ForOfStatement", + "ruleId": "no-loop-statements", + "severity": 2, + }, + ], + "output": "for (let i of []) { + console.log("hello world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "ForOfStatement", + "ruleId": "rule-to-test/no-loop-statements", + "severity": 2, + }, + ], + "output": "for (let i of []) { + console.log("hello world"); +}", + }, + ], +} `; exports[`no-loop-statements > javascript - es latest > reports while loop statements 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Unexpected loop, use map or reduce instead.", - "messageId": "generic", - "nodeType": "WhileStatement", - "ruleId": "no-loop-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "WhileStatement", + "ruleId": "no-loop-statements", + "severity": 2, + }, + ], + "output": "while (true) { + console.log("hello world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Unexpected loop, use map or reduce instead.", + "messageId": "generic", + "nodeType": "WhileStatement", + "ruleId": "rule-to-test/no-loop-statements", + "severity": 2, + }, + ], + "output": "while (true) { + console.log("hello world"); +}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-mixed-types.test.ts.snap b/tests/rules/__snapshots__/no-mixed-types.test.ts.snap index 4ece8e029..f894bb782 100644 --- a/tests/rules/__snapshots__/no-mixed-types.test.ts.snap +++ b/tests/rules/__snapshots__/no-mixed-types.test.ts.snap @@ -1,65 +1,175 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-mixed-types > typescript > options > checkInterfaces > should report mixed types in interfaces when enabled 1`] = ` -[ - { - "column": 1, - "endColumn": 46, - "endLine": 1, - "line": 1, - "message": "Only the same kind of members allowed in types.", - "messageId": "generic", - "nodeType": "TSInterfaceDeclaration", - "ruleId": "no-mixed-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 46, + "endLine": 1, + "line": 1, + "message": "Only the same kind of members allowed in types.", + "messageId": "generic", + "nodeType": "TSInterfaceDeclaration", + "ruleId": "no-mixed-types", + "severity": 2, + }, + ], + "output": "interface Foo { bar: string; baz(): number; }", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 46, + "endLine": 1, + "line": 1, + "message": "Only the same kind of members allowed in types.", + "messageId": "generic", + "nodeType": "TSInterfaceDeclaration", + "ruleId": "rule-to-test/no-mixed-types", + "severity": 2, + }, + ], + "output": "interface Foo { bar: string; baz(): number; }", + }, + ], +} `; exports[`no-mixed-types > typescript > options > checkTypeLiterals > should report mixed types in type literals when enabled 1`] = ` -[ - { - "column": 1, - "endColumn": 3, - "endLine": 4, - "line": 1, - "message": "Only the same kind of members allowed in types.", - "messageId": "generic", - "nodeType": "TSTypeAliasDeclaration", - "ruleId": "no-mixed-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 3, + "endLine": 4, + "line": 1, + "message": "Only the same kind of members allowed in types.", + "messageId": "generic", + "nodeType": "TSTypeAliasDeclaration", + "ruleId": "no-mixed-types", + "severity": 2, + }, + ], + "output": "type Foo = { + bar: string; + baz(): number; +};", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 3, + "endLine": 4, + "line": 1, + "message": "Only the same kind of members allowed in types.", + "messageId": "generic", + "nodeType": "TSTypeAliasDeclaration", + "ruleId": "rule-to-test/no-mixed-types", + "severity": 2, + }, + ], + "output": "type Foo = { + bar: string; + baz(): number; +};", + }, + ], +} `; exports[`no-mixed-types > typescript > reports mixed types in interfaces 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 4, - "line": 1, - "message": "Only the same kind of members allowed in types.", - "messageId": "generic", - "nodeType": "TSInterfaceDeclaration", - "ruleId": "no-mixed-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 4, + "line": 1, + "message": "Only the same kind of members allowed in types.", + "messageId": "generic", + "nodeType": "TSInterfaceDeclaration", + "ruleId": "no-mixed-types", + "severity": 2, + }, + ], + "output": "interface Foo { + bar: string; + baz(): number; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 4, + "line": 1, + "message": "Only the same kind of members allowed in types.", + "messageId": "generic", + "nodeType": "TSInterfaceDeclaration", + "ruleId": "rule-to-test/no-mixed-types", + "severity": 2, + }, + ], + "output": "interface Foo { + bar: string; + baz(): number; +}", + }, + ], +} `; exports[`no-mixed-types > typescript > reports mixed types in type literals 1`] = ` -[ - { - "column": 1, - "endColumn": 3, - "endLine": 4, - "line": 1, - "message": "Only the same kind of members allowed in types.", - "messageId": "generic", - "nodeType": "TSTypeAliasDeclaration", - "ruleId": "no-mixed-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 3, + "endLine": 4, + "line": 1, + "message": "Only the same kind of members allowed in types.", + "messageId": "generic", + "nodeType": "TSTypeAliasDeclaration", + "ruleId": "no-mixed-types", + "severity": 2, + }, + ], + "output": "type Foo = { + bar: string; + baz(): number; +};", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 3, + "endLine": 4, + "line": 1, + "message": "Only the same kind of members allowed in types.", + "messageId": "generic", + "nodeType": "TSTypeAliasDeclaration", + "ruleId": "rule-to-test/no-mixed-types", + "severity": 2, + }, + ], + "output": "type Foo = { + bar: string; + baz(): number; +};", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-promise-reject.test.ts.snap b/tests/rules/__snapshots__/no-promise-reject.test.ts.snap index 1dd6d8fc1..75a5c4efe 100644 --- a/tests/rules/__snapshots__/no-promise-reject.test.ts.snap +++ b/tests/rules/__snapshots__/no-promise-reject.test.ts.snap @@ -1,65 +1,185 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-promise-reject > javascript - es latest > reports Promise.reject 1`] = ` -[ - { - "column": 10, - "endColumn": 39, - "endLine": 2, - "line": 2, - "message": "Unexpected rejection, resolve an error instead.", - "messageId": "generic", - "nodeType": "CallExpression", - "ruleId": "no-promise-reject", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 10, + "endColumn": 39, + "endLine": 2, + "line": 2, + "message": "Unexpected rejection, resolve an error instead.", + "messageId": "generic", + "nodeType": "CallExpression", + "ruleId": "no-promise-reject", + "severity": 2, + }, + ], + "output": "function foo() { + return Promise.reject("hello world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 10, + "endColumn": 39, + "endLine": 2, + "line": 2, + "message": "Unexpected rejection, resolve an error instead.", + "messageId": "generic", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/no-promise-reject", + "severity": 2, + }, + ], + "output": "function foo() { + return Promise.reject("hello world"); +}", + }, + ], +} `; exports[`no-promise-reject > javascript - es latest > reports new Promise(reject) 1`] = ` -[ - { - "column": 32, - "endColumn": 38, - "endLine": 2, - "line": 2, - "message": "Unexpected rejection, resolve an error instead.", - "messageId": "generic", - "nodeType": "Identifier", - "ruleId": "no-promise-reject", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 32, + "endColumn": 38, + "endLine": 2, + "line": 2, + "message": "Unexpected rejection, resolve an error instead.", + "messageId": "generic", + "nodeType": "Identifier", + "ruleId": "no-promise-reject", + "severity": 2, + }, + ], + "output": "function foo() { + return new Promise((resolve, reject) => { + reject("hello world"); + }); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 32, + "endColumn": 38, + "endLine": 2, + "line": 2, + "message": "Unexpected rejection, resolve an error instead.", + "messageId": "generic", + "nodeType": "Identifier", + "ruleId": "rule-to-test/no-promise-reject", + "severity": 2, + }, + ], + "output": "function foo() { + return new Promise((resolve, reject) => { + reject("hello world"); + }); +}", + }, + ], +} `; exports[`no-promise-reject > javascript - es latest > reports throw in async functions 1`] = ` -[ - { - "column": 3, - "endColumn": 34, - "endLine": 2, - "line": 2, - "message": "Unexpected rejection, resolve an error instead.", - "messageId": "generic", - "nodeType": "ThrowStatement", - "ruleId": "no-promise-reject", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 34, + "endLine": 2, + "line": 2, + "message": "Unexpected rejection, resolve an error instead.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "no-promise-reject", + "severity": 2, + }, + ], + "output": "async function foo() { + throw new Error("hello world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 34, + "endLine": 2, + "line": 2, + "message": "Unexpected rejection, resolve an error instead.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "rule-to-test/no-promise-reject", + "severity": 2, + }, + ], + "output": "async function foo() { + throw new Error("hello world"); +}", + }, + ], +} `; exports[`no-promise-reject > javascript - es latest > reports throw in try without catch in async functions 1`] = ` -[ - { - "column": 5, - "endColumn": 30, - "endLine": 3, - "line": 3, - "message": "Unexpected rejection, resolve an error instead.", - "messageId": "generic", - "nodeType": "ThrowStatement", - "ruleId": "no-promise-reject", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 30, + "endLine": 3, + "line": 3, + "message": "Unexpected rejection, resolve an error instead.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "no-promise-reject", + "severity": 2, + }, + ], + "output": "async function foo() { + try { + throw new Error("hello"); + } finally { + console.log("world"); + } +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 30, + "endLine": 3, + "line": 3, + "message": "Unexpected rejection, resolve an error instead.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "rule-to-test/no-promise-reject", + "severity": 2, + }, + ], + "output": "async function foo() { + try { + throw new Error("hello"); + } finally { + console.log("world"); + } +}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-return-void.test.ts.snap b/tests/rules/__snapshots__/no-return-void.test.ts.snap index 062b8f3d1..e50686f30 100644 --- a/tests/rules/__snapshots__/no-return-void.test.ts.snap +++ b/tests/rules/__snapshots__/no-return-void.test.ts.snap @@ -1,97 +1,259 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-return-void > typescript > options > allowNull > reports null returning functions when disallowed 1`] = ` -[ - { - "column": 26, - "endColumn": 32, - "endLine": 1, - "line": 1, - "message": "Function must return a value.", - "messageId": "generic", - "nodeType": "TSTypeAnnotation", - "ruleId": "no-return-void", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 26, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "TSTypeAnnotation", + "ruleId": "no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number): null { + return null; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 26, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number): null { + return null; +}", + }, + ], +} `; exports[`no-return-void > typescript > options > allowNull > reports null returning functions with inferred return type when disallowed 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Function must return a value.", - "messageId": "generic", - "nodeType": "FunctionDeclaration", - "ruleId": "no-return-void", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "FunctionDeclaration", + "ruleId": "no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number) { + return null; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "FunctionDeclaration", + "ruleId": "rule-to-test/no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number) { + return null; +}", + }, + ], +} `; exports[`no-return-void > typescript > options > allowUndefined > reports undefined returning functions when disallowed 1`] = ` -[ - { - "column": 26, - "endColumn": 37, - "endLine": 1, - "line": 1, - "message": "Function must return a value.", - "messageId": "generic", - "nodeType": "TSTypeAnnotation", - "ruleId": "no-return-void", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 26, + "endColumn": 37, + "endLine": 1, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "TSTypeAnnotation", + "ruleId": "no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number): undefined { + return undefined; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 26, + "endColumn": 37, + "endLine": 1, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number): undefined { + return undefined; +}", + }, + ], +} `; exports[`no-return-void > typescript > options > allowUndefined > reports undefined returning functions with inferred return type when disallowed 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Function must return a value.", - "messageId": "generic", - "nodeType": "FunctionDeclaration", - "ruleId": "no-return-void", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "FunctionDeclaration", + "ruleId": "no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number) { + return undefined; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "FunctionDeclaration", + "ruleId": "rule-to-test/no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number) { + return undefined; +}", + }, + ], +} `; exports[`no-return-void > typescript > reports void returning functions 1`] = ` -[ - { - "column": 26, - "endColumn": 32, - "endLine": 1, - "line": 1, - "message": "Function must return a value.", - "messageId": "generic", - "nodeType": "TSTypeAnnotation", - "ruleId": "no-return-void", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 26, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "TSTypeAnnotation", + "ruleId": "no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number): void { + console.log(bar); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 26, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number): void { + console.log(bar); +}", + }, + ], +} `; exports[`no-return-void > typescript > reports void returning functions with inferred return type 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 3, - "line": 1, - "message": "Function must return a value.", - "messageId": "generic", - "nodeType": "FunctionDeclaration", - "ruleId": "no-return-void", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "FunctionDeclaration", + "ruleId": "no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number) { + console.log(bar); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 3, + "line": 1, + "message": "Function must return a value.", + "messageId": "generic", + "nodeType": "FunctionDeclaration", + "ruleId": "rule-to-test/no-return-void", + "severity": 2, + }, + ], + "output": "function foo(bar: number) { + console.log(bar); +}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-throw-statements.test.ts.snap b/tests/rules/__snapshots__/no-throw-statements.test.ts.snap index 9eba6a0b4..5b7ff63ee 100644 --- a/tests/rules/__snapshots__/no-throw-statements.test.ts.snap +++ b/tests/rules/__snapshots__/no-throw-statements.test.ts.snap @@ -1,81 +1,228 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-throw-statements > javascript - es latest > options > allowToRejectPromises > reports throw statements in functions nested in async functions 1`] = ` -[ - { - "column": 5, - "endColumn": 23, - "endLine": 3, - "line": 3, - "message": "Unexpected throw, throwing exceptions is not functional.", - "messageId": "generic", - "nodeType": "ThrowStatement", - "ruleId": "no-throw-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 23, + "endLine": 3, + "line": 3, + "message": "Unexpected throw, throwing exceptions is not functional.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "no-throw-statements", + "severity": 2, + }, + ], + "output": "async function foo() { + function bar() { + throw new Error(); + } +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 23, + "endLine": 3, + "line": 3, + "message": "Unexpected throw, throwing exceptions is not functional.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "rule-to-test/no-throw-statements", + "severity": 2, + }, + ], + "output": "async function foo() { + function bar() { + throw new Error(); + } +}", + }, + ], +} `; exports[`no-throw-statements > javascript - es latest > options > allowToRejectPromises > reports throw statements in try with catch in async functions 1`] = ` -[ - { - "column": 5, - "endColumn": 36, - "endLine": 3, - "line": 3, - "message": "Unexpected throw, throwing exceptions is not functional.", - "messageId": "generic", - "nodeType": "ThrowStatement", - "ruleId": "no-throw-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 36, + "endLine": 3, + "line": 3, + "message": "Unexpected throw, throwing exceptions is not functional.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "no-throw-statements", + "severity": 2, + }, + ], + "output": "async function foo() { + try { + throw new Error("hello world"); + } catch (e) { + console.log(e); + } +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 36, + "endLine": 3, + "line": 3, + "message": "Unexpected throw, throwing exceptions is not functional.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "rule-to-test/no-throw-statements", + "severity": 2, + }, + ], + "output": "async function foo() { + try { + throw new Error("hello world"); + } catch (e) { + console.log(e); + } +}", + }, + ], +} `; exports[`no-throw-statements > javascript - es latest > reports throw statements in async functions 1`] = ` -[ - { - "column": 3, - "endColumn": 21, - "endLine": 2, - "line": 2, - "message": "Unexpected throw, throwing exceptions is not functional.", - "messageId": "generic", - "nodeType": "ThrowStatement", - "ruleId": "no-throw-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 21, + "endLine": 2, + "line": 2, + "message": "Unexpected throw, throwing exceptions is not functional.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "no-throw-statements", + "severity": 2, + }, + ], + "output": "async function foo() { + throw new Error(); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 21, + "endLine": 2, + "line": 2, + "message": "Unexpected throw, throwing exceptions is not functional.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "rule-to-test/no-throw-statements", + "severity": 2, + }, + ], + "output": "async function foo() { + throw new Error(); +}", + }, + ], +} `; exports[`no-throw-statements > javascript - es latest > reports throw statements of Errors 1`] = ` -[ - { - "column": 3, - "endColumn": 21, - "endLine": 2, - "line": 2, - "message": "Unexpected throw, throwing exceptions is not functional.", - "messageId": "generic", - "nodeType": "ThrowStatement", - "ruleId": "no-throw-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 21, + "endLine": 2, + "line": 2, + "message": "Unexpected throw, throwing exceptions is not functional.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "no-throw-statements", + "severity": 2, + }, + ], + "output": "function foo() { + throw new Error(); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 21, + "endLine": 2, + "line": 2, + "message": "Unexpected throw, throwing exceptions is not functional.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "rule-to-test/no-throw-statements", + "severity": 2, + }, + ], + "output": "function foo() { + throw new Error(); +}", + }, + ], +} `; exports[`no-throw-statements > javascript - es latest > reports throw statements of strings 1`] = ` -[ - { - "column": 3, - "endColumn": 17, - "endLine": 2, - "line": 2, - "message": "Unexpected throw, throwing exceptions is not functional.", - "messageId": "generic", - "nodeType": "ThrowStatement", - "ruleId": "no-throw-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 17, + "endLine": 2, + "line": 2, + "message": "Unexpected throw, throwing exceptions is not functional.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "no-throw-statements", + "severity": 2, + }, + ], + "output": "function foo() { + throw 'error'; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 17, + "endLine": 2, + "line": 2, + "message": "Unexpected throw, throwing exceptions is not functional.", + "messageId": "generic", + "nodeType": "ThrowStatement", + "ruleId": "rule-to-test/no-throw-statements", + "severity": 2, + }, + ], + "output": "function foo() { + throw 'error'; +}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/no-try-statements.test.ts.snap b/tests/rules/__snapshots__/no-try-statements.test.ts.snap index 5c778fd71..2e4531993 100644 --- a/tests/rules/__snapshots__/no-try-statements.test.ts.snap +++ b/tests/rules/__snapshots__/no-try-statements.test.ts.snap @@ -1,49 +1,150 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`no-try-statements > javascript - es latest > options > allowCatch > reports try statements with catch and finally 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 7, - "line": 1, - "message": "Unexpected try-finally, this pattern is not functional.", - "messageId": "finally", - "nodeType": "TryStatement", - "ruleId": "no-try-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 7, + "line": 1, + "message": "Unexpected try-finally, this pattern is not functional.", + "messageId": "finally", + "nodeType": "TryStatement", + "ruleId": "no-try-statements", + "severity": 2, + }, + ], + "output": "try { + foo(); +} catch (e) { + console.log(e); +} finally { + console.log("world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 7, + "line": 1, + "message": "Unexpected try-finally, this pattern is not functional.", + "messageId": "finally", + "nodeType": "TryStatement", + "ruleId": "rule-to-test/no-try-statements", + "severity": 2, + }, + ], + "output": "try { + foo(); +} catch (e) { + console.log(e); +} finally { + console.log("world"); +}", + }, + ], +} `; exports[`no-try-statements > javascript - es latest > options > allowFinally > reports try statements with catch and finally 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 7, - "line": 1, - "message": "Unexpected try-catch, this pattern is not functional.", - "messageId": "catch", - "nodeType": "TryStatement", - "ruleId": "no-try-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 7, + "line": 1, + "message": "Unexpected try-catch, this pattern is not functional.", + "messageId": "catch", + "nodeType": "TryStatement", + "ruleId": "no-try-statements", + "severity": 2, + }, + ], + "output": "try { + foo(); +} catch (e) { + console.log(e); +} finally { + console.log("world"); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 7, + "line": 1, + "message": "Unexpected try-catch, this pattern is not functional.", + "messageId": "catch", + "nodeType": "TryStatement", + "ruleId": "rule-to-test/no-try-statements", + "severity": 2, + }, + ], + "output": "try { + foo(); +} catch (e) { + console.log(e); +} finally { + console.log("world"); +}", + }, + ], +} `; exports[`no-try-statements > javascript - es latest > reports try statements 1`] = ` -[ - { - "column": 1, - "endColumn": 2, - "endLine": 5, - "line": 1, - "message": "Unexpected try-catch, this pattern is not functional.", - "messageId": "catch", - "nodeType": "TryStatement", - "ruleId": "no-try-statements", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 5, + "line": 1, + "message": "Unexpected try-catch, this pattern is not functional.", + "messageId": "catch", + "nodeType": "TryStatement", + "ruleId": "no-try-statements", + "severity": 2, + }, + ], + "output": "try { + foo(); +} catch (e) { + console.log(e); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 2, + "endLine": 5, + "line": 1, + "message": "Unexpected try-catch, this pattern is not functional.", + "messageId": "catch", + "nodeType": "TryStatement", + "ruleId": "rule-to-test/no-try-statements", + "severity": 2, + }, + ], + "output": "try { + foo(); +} catch (e) { + console.log(e); +}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/prefer-property-signatures.test.ts.snap b/tests/rules/__snapshots__/prefer-property-signatures.test.ts.snap index 0a9771212..5c269e28a 100644 --- a/tests/rules/__snapshots__/prefer-property-signatures.test.ts.snap +++ b/tests/rules/__snapshots__/prefer-property-signatures.test.ts.snap @@ -1,33 +1,87 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`prefer-property-signatures > typescript > reports method signatures in interfaces 1`] = ` -[ - { - "column": 3, - "endColumn": 37, - "endLine": 2, - "line": 2, - "message": "Use a property signature instead of a method signature", - "messageId": "generic", - "nodeType": "TSMethodSignature", - "ruleId": "prefer-property-signatures", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 37, + "endLine": 2, + "line": 2, + "message": "Use a property signature instead of a method signature", + "messageId": "generic", + "nodeType": "TSMethodSignature", + "ruleId": "prefer-property-signatures", + "severity": 2, + }, + ], + "output": "interface Foo { + bar(a: number, b: string): number; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 37, + "endLine": 2, + "line": 2, + "message": "Use a property signature instead of a method signature", + "messageId": "generic", + "nodeType": "TSMethodSignature", + "ruleId": "rule-to-test/prefer-property-signatures", + "severity": 2, + }, + ], + "output": "interface Foo { + bar(a: number, b: string): number; +}", + }, + ], +} `; exports[`prefer-property-signatures > typescript > reports method signatures in type literals 1`] = ` -[ - { - "column": 3, - "endColumn": 37, - "endLine": 2, - "line": 2, - "message": "Use a property signature instead of a method signature", - "messageId": "generic", - "nodeType": "TSMethodSignature", - "ruleId": "prefer-property-signatures", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 37, + "endLine": 2, + "line": 2, + "message": "Use a property signature instead of a method signature", + "messageId": "generic", + "nodeType": "TSMethodSignature", + "ruleId": "prefer-property-signatures", + "severity": 2, + }, + ], + "output": "type Foo = { + bar(a: number, b: string): number; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 37, + "endLine": 2, + "line": 2, + "message": "Use a property signature instead of a method signature", + "messageId": "generic", + "nodeType": "TSMethodSignature", + "ruleId": "rule-to-test/prefer-property-signatures", + "severity": 2, + }, + ], + "output": "type Foo = { + bar(a: number, b: string): number; +}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/prefer-readonly-type.test.ts.snap b/tests/rules/__snapshots__/prefer-readonly-type.test.ts.snap index f39db760b..6c2accb5c 100644 --- a/tests/rules/__snapshots__/prefer-readonly-type.test.ts.snap +++ b/tests/rules/__snapshots__/prefer-readonly-type.test.ts.snap @@ -1,668 +1,1015 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`prefer-readonly-type > typescript > options > ignoreClass > reports non-field issues in classes - fieldsOnly 1`] = ` -[ - { - "column": 7, - "endColumn": 19, - "endLine": 9, - "fix": { - "range": [ - 136, - 136, - ], - "text": "readonly ", - }, - "line": 9, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSPropertySignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 7, + "endColumn": 19, + "endLine": 9, + "fix": { + "range": [ + 136, + 136, + ], + "text": "readonly ", + }, + "line": 9, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSPropertySignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "class Klass { + foo: number; + private bar: number; + static baz: number; + private static qux: number; + + foo() { + let bar: { + readonly foo: number; + }; + } +}", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "class Klass { + foo: number; + private bar: number; + static baz: number; + private static qux: number; + + foo() { + let bar: { + readonly foo: number; + }; + } +}", + }, + { + "fixed": false, + "messages": [], + "output": "class Klass { + foo: number; + private bar: number; + static baz: number; + private static qux: number; + + foo() { + let bar: { + readonly foo: number; + }; + } +}", + }, + ], +} `; exports[`prefer-readonly-type > typescript > reports issue inside interfaces 1`] = ` -[ - { - "column": 17, - "endColumn": 30, - "endLine": 2, - "fix": { - "range": [ - 32, - 37, - ], - "text": "ReadonlyArray", - }, - "line": 2, - "message": "Only readonly types allowed.", - "messageId": "type", - "nodeType": "TSTypeReference", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 25, - "endColumn": 38, - "endLine": 3, - "fix": { - "range": [ - 71, - 76, - ], - "text": "ReadonlyArray", - }, - "line": 3, - "message": "Only readonly types allowed.", - "messageId": "type", - "nodeType": "TSTypeReference", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 17, + "endColumn": 30, + "endLine": 2, + "fix": { + "range": [ + 32, + 37, + ], + "text": "ReadonlyArray", + }, + "line": 2, + "message": "Only readonly types allowed.", + "messageId": "type", + "nodeType": "TSTypeReference", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 25, + "endColumn": 38, + "endLine": 3, + "fix": { + "range": [ + 71, + 76, + ], + "text": "ReadonlyArray", + }, + "line": 3, + "message": "Only readonly types allowed.", + "messageId": "type", + "nodeType": "TSTypeReference", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "interface Foo { + readonly bar: ReadonlyArray; + readonly baz: Promise>; +}", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "interface Foo { + readonly bar: ReadonlyArray; + readonly baz: Promise>; +}", + }, + { + "fixed": false, + "messages": [], + "output": "interface Foo { + readonly bar: ReadonlyArray; + readonly baz: Promise>; +}", + }, + ], +} `; exports[`prefer-readonly-type > typescript > reports issues with index signatures 1`] = ` -[ - { - "column": 17, - "endColumn": 30, - "endLine": 3, - "fix": { - "range": [ - 60, - 65, - ], - "text": "ReadonlyArray", - }, - "line": 3, - "message": "Only readonly types allowed.", - "messageId": "type", - "nodeType": "TSTypeReference", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 25, - "endColumn": 38, - "endLine": 4, - "fix": { - "range": [ - 99, - 104, - ], - "text": "ReadonlyArray", - }, - "line": 4, - "message": "Only readonly types allowed.", - "messageId": "type", - "nodeType": "TSTypeReference", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 3, - "endColumn": 24, - "endLine": 8, - "fix": { - "range": [ - 140, - 140, - ], - "text": "readonly ", - }, - "line": 8, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSIndexSignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 3, - "endColumn": 34, - "endLine": 11, - "fix": { - "range": [ - 182, - 182, - ], - "text": "readonly ", - }, - "line": 11, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSIndexSignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 20, - "endColumn": 32, - "endLine": 11, - "fix": { - "range": [ - 199, - 199, - ], - "text": "readonly ", - }, - "line": 11, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSPropertySignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 17, + "endColumn": 30, + "endLine": 3, + "fix": { + "range": [ + 60, + 65, + ], + "text": "ReadonlyArray", + }, + "line": 3, + "message": "Only readonly types allowed.", + "messageId": "type", + "nodeType": "TSTypeReference", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 25, + "endColumn": 38, + "endLine": 4, + "fix": { + "range": [ + 99, + 104, + ], + "text": "ReadonlyArray", + }, + "line": 4, + "message": "Only readonly types allowed.", + "messageId": "type", + "nodeType": "TSTypeReference", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 3, + "endColumn": 24, + "endLine": 8, + "fix": { + "range": [ + 140, + 140, + ], + "text": "readonly ", + }, + "line": 8, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSIndexSignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 3, + "endColumn": 34, + "endLine": 11, + "fix": { + "range": [ + 182, + 182, + ], + "text": "readonly ", + }, + "line": 11, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSIndexSignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 20, + "endColumn": 32, + "endLine": 11, + "fix": { + "range": [ + 199, + 199, + ], + "text": "readonly ", + }, + "line": 11, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSPropertySignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "interface Foo { + readonly [key: string]: { + readonly a: ReadonlyArray; + readonly b: Promise>; + }; +} +interface Bar { + readonly [key: string]: string +} +interface Baz { + readonly [key: string]: { readonly prop: string } +}", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "interface Foo { + readonly [key: string]: { + readonly a: ReadonlyArray; + readonly b: Promise>; + }; +} +interface Bar { + readonly [key: string]: string +} +interface Baz { + readonly [key: string]: { readonly prop: string } +}", + }, + { + "fixed": false, + "messages": [], + "output": "interface Foo { + readonly [key: string]: { + readonly a: ReadonlyArray; + readonly b: Promise>; + }; +} +interface Bar { + readonly [key: string]: string +} +interface Baz { + readonly [key: string]: { readonly prop: string } +}", + }, + ], +} `; exports[`prefer-readonly-type > typescript > reports issues with mapped types 1`] = ` -[ - { - "column": 18, - "endColumn": 45, - "endLine": 1, - "fix": { - "range": [ - 18, - 18, - ], - "text": " readonly", - }, - "line": 1, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSMappedType", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 18, + "endColumn": 45, + "endLine": 1, + "fix": { + "range": [ + 18, + 18, + ], + "text": " readonly", + }, + "line": 1, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSMappedType", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "const func = (x: { readonly [key in string]: number }) => {}", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "const func = (x: { readonly [key in string]: number }) => {}", + }, + { + "fixed": false, + "messages": [], + "output": "const func = (x: { readonly [key in string]: number }) => {}", + }, + ], +} `; exports[`prefer-readonly-type > typescript > reports literals without readonly modifiers 1`] = ` -[ - { - "column": 3, - "endColumn": 13, - "endLine": 2, - "fix": { - "range": [ - 13, - 13, - ], - "text": "readonly ", - }, - "line": 2, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSPropertySignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 3, - "endColumn": 28, - "endLine": 3, - "fix": { - "range": [ - 26, - 26, - ], - "text": "readonly ", - }, - "line": 3, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSPropertySignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 3, - "endColumn": 19, - "endLine": 4, - "fix": { - "range": [ - 54, - 54, - ], - "text": "readonly ", - }, - "line": 4, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSPropertySignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 3, - "endColumn": 41, - "endLine": 5, - "fix": { - "range": [ - 73, - 73, - ], - "text": "readonly ", - }, - "line": 5, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSPropertySignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 3, - "endColumn": 25, - "endLine": 6, - "fix": { - "range": [ - 114, - 114, - ], - "text": "readonly ", - }, - "line": 6, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSIndexSignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 5, - "endColumn": 15, - "endLine": 8, - "fix": { - "range": [ - 157, - 157, - ], - "text": "readonly ", - }, - "line": 8, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSPropertySignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 5, - "endColumn": 30, - "endLine": 9, - "fix": { - "range": [ - 172, - 172, - ], - "text": "readonly ", - }, - "line": 9, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSPropertySignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 5, - "endColumn": 21, - "endLine": 10, - "fix": { - "range": [ - 202, - 202, - ], - "text": "readonly ", - }, - "line": 10, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSPropertySignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 5, - "endColumn": 43, - "endLine": 11, - "fix": { - "range": [ - 223, - 223, - ], - "text": "readonly ", - }, - "line": 11, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSPropertySignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 5, - "endColumn": 27, - "endLine": 12, - "fix": { - "range": [ - 266, - 266, - ], - "text": "readonly ", - }, - "line": 12, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSIndexSignature", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 3, + "endColumn": 13, + "endLine": 2, + "fix": { + "range": [ + 13, + 13, + ], + "text": "readonly ", + }, + "line": 2, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSPropertySignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 3, + "endColumn": 28, + "endLine": 3, + "fix": { + "range": [ + 26, + 26, + ], + "text": "readonly ", + }, + "line": 3, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSPropertySignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 3, + "endColumn": 19, + "endLine": 4, + "fix": { + "range": [ + 54, + 54, + ], + "text": "readonly ", + }, + "line": 4, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSPropertySignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 3, + "endColumn": 41, + "endLine": 5, + "fix": { + "range": [ + 73, + 73, + ], + "text": "readonly ", + }, + "line": 5, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSPropertySignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 3, + "endColumn": 25, + "endLine": 6, + "fix": { + "range": [ + 114, + 114, + ], + "text": "readonly ", + }, + "line": 6, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSIndexSignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 5, + "endColumn": 15, + "endLine": 8, + "fix": { + "range": [ + 157, + 157, + ], + "text": "readonly ", + }, + "line": 8, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSPropertySignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 5, + "endColumn": 30, + "endLine": 9, + "fix": { + "range": [ + 172, + 172, + ], + "text": "readonly ", + }, + "line": 9, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSPropertySignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 5, + "endColumn": 21, + "endLine": 10, + "fix": { + "range": [ + 202, + 202, + ], + "text": "readonly ", + }, + "line": 10, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSPropertySignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 5, + "endColumn": 43, + "endLine": 11, + "fix": { + "range": [ + 223, + 223, + ], + "text": "readonly ", + }, + "line": 11, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSPropertySignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 5, + "endColumn": 27, + "endLine": 12, + "fix": { + "range": [ + 266, + 266, + ], + "text": "readonly ", + }, + "line": 12, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSIndexSignature", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "let foo: { + readonly a: number, + readonly b: ReadonlyArray, + readonly c: () => string, + readonly d: { readonly [key: string]: string }, + readonly [key: string]: string, + readonly e: { + readonly a: number, + readonly b: ReadonlyArray, + readonly c: () => string, + readonly d: { readonly [key: string]: string }, + readonly [key: string]: string, + } +};", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "let foo: { + readonly a: number, + readonly b: ReadonlyArray, + readonly c: () => string, + readonly d: { readonly [key: string]: string }, + readonly [key: string]: string, + readonly e: { + readonly a: number, + readonly b: ReadonlyArray, + readonly c: () => string, + readonly d: { readonly [key: string]: string }, + readonly [key: string]: string, + } +};", + }, + { + "fixed": false, + "messages": [], + "output": "let foo: { + readonly a: number, + readonly b: ReadonlyArray, + readonly c: () => string, + readonly d: { readonly [key: string]: string }, + readonly [key: string]: string, + readonly e: { + readonly a: number, + readonly b: ReadonlyArray, + readonly c: () => string, + readonly d: { readonly [key: string]: string }, + readonly [key: string]: string, + } +};", + }, + ], +} `; exports[`prefer-readonly-type > typescript > reports mutable array (non-generic) 1`] = ` -[ - { - "column": 17, - "endColumn": 25, - "endLine": 1, - "fix": { - "range": [ - 16, - 16, - ], - "text": "readonly ", - }, - "line": 1, - "message": "Only readonly arrays allowed.", - "messageId": "array", - "nodeType": "TSArrayType", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 38, - "endColumn": 46, - "endLine": 1, - "fix": { - "range": [ - 37, - 37, - ], - "text": "readonly ", - }, - "line": 1, - "message": "Only readonly arrays allowed.", - "messageId": "array", - "nodeType": "TSArrayType", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 17, + "endColumn": 25, + "endLine": 1, + "fix": { + "range": [ + 16, + 16, + ], + "text": "readonly ", + }, + "line": 1, + "message": "Only readonly arrays allowed.", + "messageId": "array", + "nodeType": "TSArrayType", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 38, + "endColumn": 46, + "endLine": 1, + "fix": { + "range": [ + 37, + 37, + ], + "text": "readonly ", + }, + "line": 1, + "message": "Only readonly arrays allowed.", + "messageId": "array", + "nodeType": "TSArrayType", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "function foo(a: readonly number[], b: Promise) { + console.log(a, b); +}", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "function foo(a: readonly number[], b: Promise) { + console.log(a, b); +}", + }, + { + "fixed": false, + "messages": [], + "output": "function foo(a: readonly number[], b: Promise) { + console.log(a, b); +}", + }, + ], +} `; exports[`prefer-readonly-type > typescript > reports mutable arrays (generic) 1`] = ` -[ - { - "column": 17, - "endColumn": 30, - "endLine": 1, - "fix": { - "range": [ - 16, - 21, - ], - "text": "ReadonlyArray", - }, - "line": 1, - "message": "Only readonly types allowed.", - "messageId": "type", - "nodeType": "TSTypeReference", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 43, - "endColumn": 56, - "endLine": 1, - "fix": { - "range": [ - 42, - 47, - ], - "text": "ReadonlyArray", - }, - "line": 1, - "message": "Only readonly types allowed.", - "messageId": "type", - "nodeType": "TSTypeReference", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 17, + "endColumn": 30, + "endLine": 1, + "fix": { + "range": [ + 16, + 21, + ], + "text": "ReadonlyArray", + }, + "line": 1, + "message": "Only readonly types allowed.", + "messageId": "type", + "nodeType": "TSTypeReference", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 43, + "endColumn": 56, + "endLine": 1, + "fix": { + "range": [ + 42, + 47, + ], + "text": "ReadonlyArray", + }, + "line": 1, + "message": "Only readonly types allowed.", + "messageId": "type", + "nodeType": "TSTypeReference", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "function foo(a: ReadonlyArray, b: Promise>) { + console.log(a, b); +}", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "function foo(a: ReadonlyArray, b: Promise>) { + console.log(a, b); +}", + }, + { + "fixed": false, + "messages": [], + "output": "function foo(a: ReadonlyArray, b: Promise>) { + console.log(a, b); +}", + }, + ], +} `; exports[`prefer-readonly-type > typescript > reports mutable maps 1`] = ` -[ - { - "column": 17, - "endColumn": 28, - "endLine": 1, - "fix": { - "range": [ - 16, - 19, - ], - "text": "ReadonlyMap", - }, - "line": 1, - "message": "Only readonly types allowed.", - "messageId": "type", - "nodeType": "TSTypeReference", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 41, - "endColumn": 52, - "endLine": 1, - "fix": { - "range": [ - 40, - 43, - ], - "text": "ReadonlyMap", - }, - "line": 1, - "message": "Only readonly types allowed.", - "messageId": "type", - "nodeType": "TSTypeReference", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 17, + "endColumn": 28, + "endLine": 1, + "fix": { + "range": [ + 16, + 19, + ], + "text": "ReadonlyMap", + }, + "line": 1, + "message": "Only readonly types allowed.", + "messageId": "type", + "nodeType": "TSTypeReference", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 41, + "endColumn": 52, + "endLine": 1, + "fix": { + "range": [ + 40, + 43, + ], + "text": "ReadonlyMap", + }, + "line": 1, + "message": "Only readonly types allowed.", + "messageId": "type", + "nodeType": "TSTypeReference", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "function foo(a: ReadonlyMap, b: Promise>) { + console.log(a, b); +}", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "function foo(a: ReadonlyMap, b: Promise>) { + console.log(a, b); +}", + }, + { + "fixed": false, + "messages": [], + "output": "function foo(a: ReadonlyMap, b: Promise>) { + console.log(a, b); +}", + }, + ], +} `; exports[`prefer-readonly-type > typescript > reports mutable sets 1`] = ` -[ - { - "column": 17, - "endColumn": 28, - "endLine": 1, - "fix": { - "range": [ - 16, - 19, - ], - "text": "ReadonlySet", - }, - "line": 1, - "message": "Only readonly types allowed.", - "messageId": "type", - "nodeType": "TSTypeReference", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 41, - "endColumn": 52, - "endLine": 1, - "fix": { - "range": [ - 40, - 43, - ], - "text": "ReadonlySet", - }, - "line": 1, - "message": "Only readonly types allowed.", - "messageId": "type", - "nodeType": "TSTypeReference", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 17, + "endColumn": 28, + "endLine": 1, + "fix": { + "range": [ + 16, + 19, + ], + "text": "ReadonlySet", + }, + "line": 1, + "message": "Only readonly types allowed.", + "messageId": "type", + "nodeType": "TSTypeReference", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 41, + "endColumn": 52, + "endLine": 1, + "fix": { + "range": [ + 40, + 43, + ], + "text": "ReadonlySet", + }, + "line": 1, + "message": "Only readonly types allowed.", + "messageId": "type", + "nodeType": "TSTypeReference", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "function foo(a: ReadonlySet, b: Promise>) { + console.log(a, b); +}", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "function foo(a: ReadonlySet, b: Promise>) { + console.log(a, b); +}", + }, + { + "fixed": false, + "messages": [], + "output": "function foo(a: ReadonlySet, b: Promise>) { + console.log(a, b); +}", + }, + ], +} `; exports[`prefer-readonly-type > typescript > reports non-readonly class parameter properties 1`] = ` -[ - { - "column": 5, - "endColumn": 30, - "endLine": 3, - "fix": { - "range": [ - 41, - 41, - ], - "text": "readonly ", - }, - "line": 3, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSParameterProperty", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 5, - "endColumn": 36, - "endLine": 4, - "fix": { - "range": [ - 75, - 75, - ], - "text": "readonly ", - }, - "line": 4, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSParameterProperty", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 5, - "endColumn": 32, - "endLine": 5, - "fix": { - "range": [ - 110, - 110, - ], - "text": "readonly ", - }, - "line": 5, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "TSParameterProperty", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 5, + "endColumn": 30, + "endLine": 3, + "fix": { + "range": [ + 41, + 41, + ], + "text": "readonly ", + }, + "line": 3, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSParameterProperty", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 5, + "endColumn": 36, + "endLine": 4, + "fix": { + "range": [ + 75, + 75, + ], + "text": "readonly ", + }, + "line": 4, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSParameterProperty", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 5, + "endColumn": 32, + "endLine": 5, + "fix": { + "range": [ + 110, + 110, + ], + "text": "readonly ", + }, + "line": 5, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "TSParameterProperty", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "class Klass { + constructor ( + public readonly publicProp: string, + protected readonly protectedProp: string, + private readonly privateProp: string, + ) { } +}", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "class Klass { + constructor ( + public readonly publicProp: string, + protected readonly protectedProp: string, + private readonly privateProp: string, + ) { } +}", + }, + { + "fixed": false, + "messages": [], + "output": "class Klass { + constructor ( + public readonly publicProp: string, + protected readonly protectedProp: string, + private readonly privateProp: string, + ) { } +}", + }, + ], +} `; exports[`prefer-readonly-type > typescript > reports non-readonly class properties 1`] = ` -[ - { - "column": 3, - "endColumn": 15, - "endLine": 2, - "fix": { - "range": [ - 16, - 16, - ], - "text": "readonly ", - }, - "line": 2, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "PropertyDefinition", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 3, - "endColumn": 23, - "endLine": 3, - "fix": { - "range": [ - 39, - 39, - ], - "text": "readonly ", - }, - "line": 3, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "PropertyDefinition", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 3, - "endColumn": 22, - "endLine": 4, - "fix": { - "range": [ - 61, - 61, - ], - "text": "readonly ", - }, - "line": 4, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "PropertyDefinition", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, - { - "column": 3, - "endColumn": 30, - "endLine": 5, - "fix": { - "range": [ - 91, - 91, - ], - "text": "readonly ", - }, - "line": 5, - "message": "A readonly modifier is required.", - "messageId": "property", - "nodeType": "PropertyDefinition", - "ruleId": "prefer-readonly-type", - "severity": 2, - }, -] +{ + "fixed": true, + "messages": [ + { + "column": 3, + "endColumn": 15, + "endLine": 2, + "fix": { + "range": [ + 16, + 16, + ], + "text": "readonly ", + }, + "line": 2, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "PropertyDefinition", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 3, + "endColumn": 23, + "endLine": 3, + "fix": { + "range": [ + 39, + 39, + ], + "text": "readonly ", + }, + "line": 3, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "PropertyDefinition", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 3, + "endColumn": 22, + "endLine": 4, + "fix": { + "range": [ + 61, + 61, + ], + "text": "readonly ", + }, + "line": 4, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "PropertyDefinition", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + { + "column": 3, + "endColumn": 30, + "endLine": 5, + "fix": { + "range": [ + 91, + 91, + ], + "text": "readonly ", + }, + "line": 5, + "message": "A readonly modifier is required.", + "messageId": "property", + "nodeType": "PropertyDefinition", + "ruleId": "prefer-readonly-type", + "severity": 2, + }, + ], + "output": "class Klass { + readonly foo: number; + private readonly bar: number; + static readonly baz: number; + private static readonly qux: number; +}", + "steps": [ + { + "fixed": true, + "messages": [], + "output": "class Klass { + readonly foo: number; + private readonly bar: number; + static readonly baz: number; + private static readonly qux: number; +}", + }, + { + "fixed": false, + "messages": [], + "output": "class Klass { + readonly foo: number; + private readonly bar: number; + static readonly baz: number; + private static readonly qux: number; +}", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/prefer-tacit.test.ts.snap b/tests/rules/__snapshots__/prefer-tacit.test.ts.snap index 53e339684..618d9e21d 100644 --- a/tests/rules/__snapshots__/prefer-tacit.test.ts.snap +++ b/tests/rules/__snapshots__/prefer-tacit.test.ts.snap @@ -1,146 +1,332 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`prefer-tacit > typescript > options > checkMemberExpressions > report member expressions when enabled 1`] = ` -[ - { - "column": 1, - "endColumn": 35, - "endLine": 2, - "line": 2, - "message": "Potentially unnecessary function wrapper.", - "messageId": "generic", - "nodeType": "FunctionDeclaration", - "ruleId": "prefer-tacit", - "severity": 2, - "suggestions": [ - { - "desc": "Remove unnecessary function wrapper.", - "fix": { - "range": [ - 46, - 80, +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 35, + "endLine": 2, + "line": 2, + "message": "Potentially unnecessary function wrapper.", + "messageId": "generic", + "nodeType": "FunctionDeclaration", + "ruleId": "prefer-tacit", + "severity": 2, + "suggestions": [ + { + "desc": "Remove unnecessary function wrapper.", + "fix": { + "range": [ + 46, + 80, + ], + "text": "const foo = a.b.bind(a);", + }, + "messageId": "genericSuggestion", + }, + ], + }, + ], + "output": "declare const a: { b(arg: string): string; }; +function foo(x) { return a.b(x); }", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 35, + "endLine": 2, + "line": 2, + "message": "Potentially unnecessary function wrapper.", + "messageId": "generic", + "nodeType": "FunctionDeclaration", + "ruleId": "rule-to-test/prefer-tacit", + "severity": 2, + "suggestions": [ + { + "desc": "Remove unnecessary function wrapper.", + "fix": { + "range": [ + 46, + 80, + ], + "text": "const foo = a.b.bind(a);", + }, + "messageId": "genericSuggestion", + }, ], - "text": "const foo = a.b.bind(a);", }, - "messageId": "genericSuggestion", - }, - ], - }, -] + ], + "output": "declare const a: { b(arg: string): string; }; +function foo(x) { return a.b(x); }", + }, + ], +} `; exports[`prefer-tacit > typescript > options > checkMemberExpressions > report member expressions when enabled 2`] = ` -[ - { - "column": 13, - "endColumn": 33, - "endLine": 1, - "line": 1, - "message": "Potentially unnecessary function wrapper.", - "messageId": "generic", - "nodeType": "ArrowFunctionExpression", - "ruleId": "prefer-tacit", - "severity": 2, - "suggestions": [ - { - "desc": "Remove unnecessary function wrapper.", - "fix": { - "range": [ - 12, - 32, +{ + "fixed": false, + "messages": [ + { + "column": 13, + "endColumn": 33, + "endLine": 1, + "line": 1, + "message": "Potentially unnecessary function wrapper.", + "messageId": "generic", + "nodeType": "ArrowFunctionExpression", + "ruleId": "prefer-tacit", + "severity": 2, + "suggestions": [ + { + "desc": "Remove unnecessary function wrapper.", + "fix": { + "range": [ + 12, + 32, + ], + "text": "/a/.test.bind(/a/)", + }, + "messageId": "genericSuggestion", + }, + ], + }, + ], + "output": "[''].filter(str => /a/.test(str))", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 13, + "endColumn": 33, + "endLine": 1, + "line": 1, + "message": "Potentially unnecessary function wrapper.", + "messageId": "generic", + "nodeType": "ArrowFunctionExpression", + "ruleId": "rule-to-test/prefer-tacit", + "severity": 2, + "suggestions": [ + { + "desc": "Remove unnecessary function wrapper.", + "fix": { + "range": [ + 12, + 32, + ], + "text": "/a/.test.bind(/a/)", + }, + "messageId": "genericSuggestion", + }, ], - "text": "/a/.test.bind(/a/)", }, - "messageId": "genericSuggestion", - }, - ], - }, -] + ], + "output": "[''].filter(str => /a/.test(str))", + }, + ], +} `; exports[`prefer-tacit > typescript > reports functions that are just instantiations 1`] = ` -[ - { - "column": 1, - "endColumn": 41, - "endLine": 2, - "line": 2, - "message": "Potentially unnecessary function wrapper.", - "messageId": "generic", - "nodeType": "FunctionDeclaration", - "ruleId": "prefer-tacit", - "severity": 2, - "suggestions": [ - { - "desc": "Remove unnecessary function wrapper.", - "fix": { - "range": [ - 26, - 66, +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 41, + "endLine": 2, + "line": 2, + "message": "Potentially unnecessary function wrapper.", + "messageId": "generic", + "nodeType": "FunctionDeclaration", + "ruleId": "prefer-tacit", + "severity": 2, + "suggestions": [ + { + "desc": "Remove unnecessary function wrapper.", + "fix": { + "range": [ + 26, + 66, + ], + "text": "const foo = f;", + }, + "messageId": "genericSuggestion", + }, + ], + }, + ], + "output": "function f(x: T): T {} +function foo(x) { return f(x); }", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 41, + "endLine": 2, + "line": 2, + "message": "Potentially unnecessary function wrapper.", + "messageId": "generic", + "nodeType": "FunctionDeclaration", + "ruleId": "rule-to-test/prefer-tacit", + "severity": 2, + "suggestions": [ + { + "desc": "Remove unnecessary function wrapper.", + "fix": { + "range": [ + 26, + 66, + ], + "text": "const foo = f;", + }, + "messageId": "genericSuggestion", + }, ], - "text": "const foo = f;", }, - "messageId": "genericSuggestion", - }, - ], - }, -] + ], + "output": "function f(x: T): T {} +function foo(x) { return f(x); }", + }, + ], +} `; exports[`prefer-tacit > typescript > reports functions that can "safely" be changed 1`] = ` -[ - { - "column": 13, - "endColumn": 22, - "endLine": 2, - "line": 2, - "message": "Potentially unnecessary function wrapper.", - "messageId": "generic", - "nodeType": "ArrowFunctionExpression", - "ruleId": "prefer-tacit", - "severity": 2, - "suggestions": [ - { - "desc": "Remove unnecessary function wrapper.", - "fix": { - "range": [ - 29, - 38, +{ + "fixed": false, + "messages": [ + { + "column": 13, + "endColumn": 22, + "endLine": 2, + "line": 2, + "message": "Potentially unnecessary function wrapper.", + "messageId": "generic", + "nodeType": "ArrowFunctionExpression", + "ruleId": "prefer-tacit", + "severity": 2, + "suggestions": [ + { + "desc": "Remove unnecessary function wrapper.", + "fix": { + "range": [ + 29, + 38, + ], + "text": "f", + }, + "messageId": "genericSuggestion", + }, + ], + }, + ], + "output": "function f(x) {} +const foo = x => f(x);", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 13, + "endColumn": 22, + "endLine": 2, + "line": 2, + "message": "Potentially unnecessary function wrapper.", + "messageId": "generic", + "nodeType": "ArrowFunctionExpression", + "ruleId": "rule-to-test/prefer-tacit", + "severity": 2, + "suggestions": [ + { + "desc": "Remove unnecessary function wrapper.", + "fix": { + "range": [ + 29, + 38, + ], + "text": "f", + }, + "messageId": "genericSuggestion", + }, ], - "text": "f", }, - "messageId": "genericSuggestion", - }, - ], - }, -] + ], + "output": "function f(x) {} +const foo = x => f(x);", + }, + ], +} `; exports[`prefer-tacit > typescript > reports functions that can "safely" be changed 2`] = ` -[ - { - "column": 27, - "endColumn": 42, - "endLine": 1, - "line": 1, - "message": "Potentially unnecessary function wrapper.", - "messageId": "generic", - "nodeType": "ArrowFunctionExpression", - "ruleId": "prefer-tacit", - "severity": 2, - "suggestions": [ - { - "desc": "Remove unnecessary function wrapper.", - "fix": { - "range": [ - 26, - 41, +{ + "fixed": false, + "messages": [ + { + "column": 27, + "endColumn": 42, + "endLine": 1, + "line": 1, + "message": "Potentially unnecessary function wrapper.", + "messageId": "generic", + "nodeType": "ArrowFunctionExpression", + "ruleId": "prefer-tacit", + "severity": 2, + "suggestions": [ + { + "desc": "Remove unnecessary function wrapper.", + "fix": { + "range": [ + 26, + 41, + ], + "text": "Boolean", + }, + "messageId": "genericSuggestion", + }, + ], + }, + ], + "output": "const foo = [1, 2, 3].map(x => Boolean(x));", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 27, + "endColumn": 42, + "endLine": 1, + "line": 1, + "message": "Potentially unnecessary function wrapper.", + "messageId": "generic", + "nodeType": "ArrowFunctionExpression", + "ruleId": "rule-to-test/prefer-tacit", + "severity": 2, + "suggestions": [ + { + "desc": "Remove unnecessary function wrapper.", + "fix": { + "range": [ + 26, + 41, + ], + "text": "Boolean", + }, + "messageId": "genericSuggestion", + }, ], - "text": "Boolean", }, - "messageId": "genericSuggestion", - }, - ], - }, -] + ], + "output": "const foo = [1, 2, 3].map(x => Boolean(x));", + }, + ], +} `; diff --git a/tests/rules/__snapshots__/type-declaration-immutability.test.ts.snap b/tests/rules/__snapshots__/type-declaration-immutability.test.ts.snap index 71a2ee95e..8a8f9cd7e 100644 --- a/tests/rules/__snapshots__/type-declaration-immutability.test.ts.snap +++ b/tests/rules/__snapshots__/type-declaration-immutability.test.ts.snap @@ -1,273 +1,706 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`type-declaration-immutability > typescript > reports invalid deep arrays 1`] = ` -[ - { - "column": 6, - "endColumn": 25, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 25, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyDeepMyArray = ReadonlyArray<{ foo: string; }>;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 25, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyDeepMyArray = ReadonlyArray<{ foo: string; }>;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid deep maps 1`] = ` -[ - { - "column": 6, - "endColumn": 23, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 23, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyDeepMyMap = ReadonlyMap;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 23, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyDeepMyMap = ReadonlyMap;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid deep sets 1`] = ` -[ - { - "column": 6, - "endColumn": 23, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 23, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyDeepMySet = ReadonlySet<{ foo: string; }>;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 23, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyDeepMySet = ReadonlySet<{ foo: string; }>;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid immutable arrays 1`] = ` -[ - { - "column": 6, - "endColumn": 22, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 22, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ImmutableMyArray = ReadonlyArray<{ readonly foo: string; }>;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 22, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ImmutableMyArray = ReadonlyArray<{ readonly foo: string; }>;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid immutable maps 1`] = ` -[ - { - "column": 6, - "endColumn": 20, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 20, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ImmutableMyMap = ReadonlyMap;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 20, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ImmutableMyMap = ReadonlyMap;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid immutable sets 1`] = ` -[ - { - "column": 6, - "endColumn": 20, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 20, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ImmutableMySet = ReadonlySet<{ readonly foo: string; }>;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 20, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ImmutableMySet = ReadonlySet<{ readonly foo: string; }>;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid immutable/deep records 1`] = ` -[ - { - "column": 6, - "endColumn": 21, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 21, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyDeepFoo = { + readonly foo: number; + readonly bar: { + baz: string; + }; + };", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 21, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyDeep" (actual: "ReadonlyShallow").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyDeepFoo = { + readonly foo: number; + readonly bar: { + baz: string; + }; + };", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid immutable/deep records 2`] = ` -[ - { - "column": 6, - "endColumn": 18, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyShallow").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 18, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyShallow").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ImmutableFoo = { + readonly foo: number; + readonly bar: { + baz: string; + }; + };", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 18, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "Immutable" (actual: "ReadonlyShallow").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ImmutableFoo = { + readonly foo: number; + readonly bar: { + baz: string; + }; + };", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid shallow arrays 1`] = ` -[ - { - "column": 6, - "endColumn": 21, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 21, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyMyArray = Array;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 21, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyMyArray = Array;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid shallow maps 1`] = ` -[ - { - "column": 6, - "endColumn": 19, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 19, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyMyMap = Map;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 19, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyMyMap = Map;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid shallow records 1`] = ` -[ - { - "column": 6, - "endColumn": 17, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 17, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyFoo = { + foo: number; +};", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 17, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyFoo = { + foo: number; +};", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid shallow records 2`] = ` -[ - { - "column": 6, - "endColumn": 17, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 17, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyFoo = { + readonly foo: number; + bar: { + baz: string; + }; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 17, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyFoo = { + readonly foo: number; + bar: { + baz: string; + }; +}", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports invalid shallow sets 1`] = ` -[ - { - "column": 6, - "endColumn": 19, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "AtLeast", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 19, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyMySet = Set;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 19, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "AtLeast", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type ReadonlyMySet = Set;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports non-mutable primitives 1`] = ` -[ - { - "column": 6, - "endColumn": 19, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", - "messageId": "AtMost", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 19, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", + "messageId": "AtMost", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type MutableString = string;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 19, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", + "messageId": "AtMost", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type MutableString = string;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports non-mutable records 1`] = ` -[ - { - "column": 6, - "endColumn": 16, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", - "messageId": "AtMost", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 16, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", + "messageId": "AtMost", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type MutableFoo = { + readonly foo: number; +};", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 16, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", + "messageId": "AtMost", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type MutableFoo = { + readonly foo: number; +};", + }, + ], +} `; exports[`type-declaration-immutability > typescript > reports non-mutable records 2`] = ` -[ - { - "column": 6, - "endColumn": 16, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", - "messageId": "AtMost", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 16, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", + "messageId": "AtMost", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type MutableFoo = Readonly<{ + foo: number; +}>;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 16, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", + "messageId": "AtMost", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type MutableFoo = Readonly<{ + foo: number; +}>;", + }, + ], +} `; exports[`type-declaration-immutability > typescript > respects override settings 1`] = ` -[ - { - "column": 6, - "endColumn": 16, - "endLine": 1, - "line": 1, - "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", - "messageId": "AtMost", - "nodeType": "Identifier", - "ruleId": "type-declaration-immutability", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 16, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", + "messageId": "AtMost", + "nodeType": "Identifier", + "ruleId": "type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type MutableSet = Set;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 6, + "endColumn": 16, + "endLine": 1, + "line": 1, + "message": "This type is declare to have an immutability of at most "Mutable" (actual: "Immutable").", + "messageId": "AtMost", + "nodeType": "Identifier", + "ruleId": "rule-to-test/type-declaration-immutability", + "severity": 2, + }, + ], + "output": "type MutableSet = Set;", + }, + ], +} `; diff --git a/tests/rules/functional-parameters.test.ts b/tests/rules/functional-parameters.test.ts index 1f2af48a4..b783eae96 100644 --- a/tests/rules/functional-parameters.test.ts +++ b/tests/rules/functional-parameters.test.ts @@ -14,67 +14,67 @@ describe(name, () => { configs: esLatestConfig, }); - it("doesn't report non-issues", () => { - valid(dedent` + it("doesn't report non-issues", async () => { + await valid(dedent` var foo = { arguments: 2 }; foo.arguments = 3 `); - valid(dedent` + await valid(dedent` (function() { console.log("hello world"); })(); `); - valid(dedent` + await valid(dedent` (() => { console.log("hello world"); })(); `); - valid(dedent` + await valid(dedent` function foo([bar, ...baz]) { console.log(bar, baz); } `); }); - it("reports rest parameter violations", () => { + it("reports rest parameter violations", async () => { const code = dedent` function foo(...bar) { console.log(bar); } `; - const result = invalid({ + const result = await invalid({ code, errors: ["restParam"], }); - expect(result.messages).toMatchSnapshot(); + expect(result.result).toMatchSnapshot(); }); - it("reports arguments keyword violations", () => { + it("reports arguments keyword violations", async () => { const code = dedent` function foo(bar) { console.log(arguments); } `; - const result = invalid({ + const result = await invalid({ code, errors: ["arguments"], }); - expect(result.messages).toMatchSnapshot(); + expect(result.result).toMatchSnapshot(); }); describe("options", () => { describe("enforceParameterCount", () => { - it("atLeastOne", () => { - valid({ + it("atLeastOne", async () => { + await valid({ code: dedent` function foo(bar) { console.log(bar); @@ -83,7 +83,7 @@ describe(name, () => { options: [{ enforceParameterCount: "atLeastOne" }], }); - valid({ + await valid({ code: dedent` function foo(bar, baz) { console.log(bar, baz); @@ -92,7 +92,7 @@ describe(name, () => { options: [{ enforceParameterCount: "atLeastOne" }], }); - valid({ + await valid({ code: dedent` const foo = { get bar() { @@ -110,7 +110,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` const foo = { set bar(baz) { @@ -128,7 +128,7 @@ describe(name, () => { ], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` function foo() { console.log("hello world"); @@ -137,11 +137,11 @@ describe(name, () => { options: [{ enforceParameterCount: "atLeastOne" }], errors: ["paramCountAtLeastOne"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("exactlyOne", () => { - valid({ + it("exactlyOne", async () => { + await valid({ code: dedent` function foo(bar) { console.log(bar); @@ -150,7 +150,7 @@ describe(name, () => { options: [{ enforceParameterCount: "exactlyOne" }], }); - valid({ + await valid({ code: dedent` const foo = { get bar() { @@ -168,7 +168,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` const foo = { set bar(baz) { @@ -186,7 +186,7 @@ describe(name, () => { ], }); - const invalidResult1 = invalid({ + const invalidResult1 = await invalid({ code: dedent` function foo(bar, baz) { console.log(bar, baz); @@ -195,9 +195,9 @@ describe(name, () => { options: [{ enforceParameterCount: "exactlyOne" }], errors: ["paramCountExactlyOne"], }); - expect(invalidResult1.messages).toMatchSnapshot(); + expect(invalidResult1.result).toMatchSnapshot(); - const invalidResult2 = invalid({ + const invalidResult2 = await invalid({ code: dedent` function foo() { console.log("hello world"); @@ -206,11 +206,11 @@ describe(name, () => { options: [{ enforceParameterCount: "exactlyOne" }], errors: ["paramCountExactlyOne"], }); - expect(invalidResult2.messages).toMatchSnapshot(); + expect(invalidResult2.result).toMatchSnapshot(); }); - it("ignoreLambdaExpression", () => { - valid({ + it("ignoreLambdaExpression", async () => { + await valid({ code: dedent` function foo(param) {} foo(function () {}); @@ -218,7 +218,7 @@ describe(name, () => { options: [{ enforceParameterCount: { ignoreLambdaExpression: true } }], }); - valid({ + await valid({ code: dedent` function foo(param) {} foo(() => 1); @@ -226,7 +226,7 @@ describe(name, () => { options: [{ enforceParameterCount: { ignoreLambdaExpression: true } }], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` function foo() {} `, @@ -240,11 +240,11 @@ describe(name, () => { ], errors: ["paramCountAtLeastOne"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("ignoreIIFE", () => { - valid({ + it("ignoreIIFE", async () => { + await valid({ code: dedent` (() => { console.log("hello world"); @@ -260,7 +260,7 @@ describe(name, () => { ], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` (() => { console.log("hello world"); @@ -276,12 +276,12 @@ describe(name, () => { ], errors: ["paramCountAtLeastOne"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); - it("ignoreIdentifierPattern", () => { - valid({ + it("ignoreIdentifierPattern", async () => { + await valid({ code: dedent` function foo(...bar) { console.log(bar); @@ -290,7 +290,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^foo" }], }); - valid({ + await valid({ code: dedent` const baz = { foo(...bar) { @@ -301,7 +301,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^foo" }], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` function foo(...bar) { console.log(bar); @@ -310,11 +310,11 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^bar" }], errors: ["restParam"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("ignorePrefixSelector", () => { - valid({ + it("ignorePrefixSelector", async () => { + await valid({ code: dedent` [1, 2, 3].reduce( function(carry, current) { @@ -331,7 +331,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` [1, 2, 3].map( function(element, index) { @@ -348,7 +348,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` [1, 2, 3] .map( @@ -374,7 +374,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` [1, 2, 3].reduce( (carry, current) => carry + current, @@ -389,7 +389,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` [1, 2, 3].map( (element, index) => element + index, @@ -404,7 +404,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` [1, 2, 3] .map( @@ -436,14 +436,14 @@ describe(name, () => { }); describe("overrides", () => { - it('override value works - "allowRestParameter"', () => { + it('override value works - "allowRestParameter"', async () => { const code = dedent` function foo(...bar: string[]) { console.log(bar); } `; - valid({ + await valid({ code, options: { allowRestParameter: false, @@ -461,14 +461,14 @@ describe(name, () => { }); }); - it('override value works - "allowArgumentsKeyword"', () => { + it('override value works - "allowArgumentsKeyword"', async () => { const code = dedent` function foo(bar: string[]) { console.log(arguments); } `; - valid({ + await valid({ code, options: { allowArgumentsKeyword: false, @@ -486,14 +486,14 @@ describe(name, () => { }); }); - it('disbale override works - "allowRestParameter"', () => { + it('disbale override works - "allowRestParameter"', async () => { const code = dedent` function foo(...bar: string[]) { console.log(bar); } `; - valid({ + await valid({ code, options: { allowRestParameter: false, @@ -509,14 +509,14 @@ describe(name, () => { }); }); - it('disbale override works - "allowArgumentsKeyword"', () => { + it('disbale override works - "allowArgumentsKeyword"', async () => { const code = dedent` function foo(bar: string[]) { console.log(arguments); } `; - valid({ + await valid({ code, options: { allowArgumentsKeyword: false, diff --git a/tests/rules/immutable-data/__snapshots__/array.test.ts.snap b/tests/rules/immutable-data/__snapshots__/array.test.ts.snap index d085a4411..ca1bd56c0 100644 --- a/tests/rules/immutable-data/__snapshots__/array.test.ts.snap +++ b/tests/rules/immutable-data/__snapshots__/array.test.ts.snap @@ -1,549 +1,1223 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`immutable-data > typescript > options > ignoreImmediateMutation > reports immediately mutation when disabled 1`] = ` -[ - { - "column": 1, - "endColumn": 17, - "endLine": 1, - "line": 1, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 20, - "endLine": 2, - "line": 2, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 25, - "endLine": 3, - "line": 3, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 34, - "endLine": 4, - "line": 4, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 17, + "endLine": 1, + "line": 1, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 20, + "endLine": 2, + "line": 2, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 25, + "endLine": 3, + "line": 3, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 34, + "endLine": 4, + "line": 4, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "[0, 1, 2].sort(); +new Array(5).sort(); +Array.of(0, 1, 2).sort(); +Array.from({ length: 10 }).sort();", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 17, + "endLine": 1, + "line": 1, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 20, + "endLine": 2, + "line": 2, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 25, + "endLine": 3, + "line": 3, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 34, + "endLine": 4, + "line": 4, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "[0, 1, 2].sort(); +new Array(5).sort(); +Array.of(0, 1, 2).sort(); +Array.from({ length: 10 }).sort();", + }, + ], +} `; exports[`immutable-data > typescript > options > ignoreNonConstDeclarations > reports variables declared as const 1`] = ` -[ - { - "column": 1, - "endColumn": 12, - "endLine": 2, - "line": 2, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 3, - "line": 3, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 14, - "endLine": 4, - "line": 4, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 24, - "endLine": 5, - "line": 5, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 6, - "line": 6, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 7, - "line": 7, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 8, - "line": 8, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 14, - "endLine": 9, - "line": 9, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 10, - "line": 10, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 11, - "line": 11, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 20, - "endLine": 12, - "line": 12, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 15, - "endLine": 13, - "line": 13, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 12, + "endLine": 2, + "line": 2, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 3, + "line": 3, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 4, + "line": 4, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 24, + "endLine": 5, + "line": 5, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 6, + "line": 6, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 7, + "line": 7, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 8, + "line": 8, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 9, + "line": 9, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 10, + "line": 10, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 11, + "line": 11, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 20, + "endLine": 12, + "line": 12, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 15, + "endLine": 13, + "line": 13, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "const arr = [0, 1]; +arr[0] += 1; +arr[1]++; +delete arr[0]; +arr.copyWithin(0, 1, 2); +arr.fill(3); +arr.pop(); +arr.push(3); +arr.reverse(); +arr.shift(); +arr.sort(); +arr.splice(0, 1, 9); +arr.unshift(6);", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 12, + "endLine": 2, + "line": 2, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 3, + "line": 3, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 4, + "line": 4, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 24, + "endLine": 5, + "line": 5, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 6, + "line": 6, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 7, + "line": 7, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 8, + "line": 8, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 9, + "line": 9, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 10, + "line": 10, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 11, + "line": 11, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 20, + "endLine": 12, + "line": 12, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 15, + "endLine": 13, + "line": 13, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "const arr = [0, 1]; +arr[0] += 1; +arr[1]++; +delete arr[0]; +arr.copyWithin(0, 1, 2); +arr.fill(3); +arr.pop(); +arr.push(3); +arr.reverse(); +arr.shift(); +arr.sort(); +arr.splice(0, 1, 9); +arr.unshift(6);", + }, + ], +} `; exports[`immutable-data > typescript > report mutating array methods 1`] = ` -[ - { - "column": 1, - "endColumn": 22, - "endLine": 2, - "line": 2, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 3, - "line": 3, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 8, - "endLine": 4, - "line": 4, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 5, - "line": 5, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 6, - "line": 6, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 7, - "line": 7, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 8, - "line": 8, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 18, - "endLine": 9, - "line": 9, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 13, - "endLine": 10, - "line": 10, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 27, - "endLine": 12, - "line": 12, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 15, - "endLine": 13, - "line": 13, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 13, - "endLine": 14, - "line": 14, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 15, - "endLine": 15, - "line": 15, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 17, - "endLine": 16, - "line": 16, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 15, - "endLine": 17, - "line": 17, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 14, - "endLine": 18, - "line": 18, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 23, - "endLine": 19, - "line": 19, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 18, - "endLine": 20, - "line": 20, - "message": "Modifying an array is not allowed.", - "messageId": "array", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 22, + "endLine": 2, + "line": 2, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 3, + "line": 3, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 8, + "endLine": 4, + "line": 4, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 5, + "line": 5, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 6, + "line": 6, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 7, + "line": 7, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 8, + "line": 8, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 18, + "endLine": 9, + "line": 9, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 13, + "endLine": 10, + "line": 10, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 27, + "endLine": 12, + "line": 12, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 15, + "endLine": 13, + "line": 13, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 13, + "endLine": 14, + "line": 14, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 15, + "endLine": 15, + "line": 15, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 17, + "endLine": 16, + "line": 16, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 15, + "endLine": 17, + "line": 17, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 18, + "line": 18, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 23, + "endLine": 19, + "line": 19, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 18, + "endLine": 20, + "line": 20, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "var x = [5, 6]; +x.copyWithin(0, 1, 2); +x.fill(3); +x.pop(); +x.push(3); +x.reverse(); +x.shift(); +x.sort(); +x.splice(0, 1, 9); +x.unshift(6); +var y = [{ z: [3, 7] }]; +y[0].z.copyWithin(0, 1, 2); +y[0].z.fill(3); +y[0].z.pop(); +y[0].z.push(3); +y[0].z.reverse(); +y[0].z.shift(); +y[0].z.sort(); +y[0].z.splice(0, 1, 9); +y[0].z.unshift(6);", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 22, + "endLine": 2, + "line": 2, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 3, + "line": 3, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 8, + "endLine": 4, + "line": 4, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 5, + "line": 5, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 6, + "line": 6, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 7, + "line": 7, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 8, + "line": 8, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 18, + "endLine": 9, + "line": 9, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 13, + "endLine": 10, + "line": 10, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 27, + "endLine": 12, + "line": 12, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 15, + "endLine": 13, + "line": 13, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 13, + "endLine": 14, + "line": 14, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 15, + "endLine": 15, + "line": 15, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 17, + "endLine": 16, + "line": 16, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 15, + "endLine": 17, + "line": 17, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 18, + "line": 18, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 23, + "endLine": 19, + "line": 19, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 18, + "endLine": 20, + "line": 20, + "message": "Modifying an array is not allowed.", + "messageId": "array", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "var x = [5, 6]; +x.copyWithin(0, 1, 2); +x.fill(3); +x.pop(); +x.push(3); +x.reverse(); +x.shift(); +x.sort(); +x.splice(0, 1, 9); +x.unshift(6); +var y = [{ z: [3, 7] }]; +y[0].z.copyWithin(0, 1, 2); +y[0].z.fill(3); +y[0].z.pop(); +y[0].z.push(3); +y[0].z.reverse(); +y[0].z.shift(); +y[0].z.sort(); +y[0].z.splice(0, 1, 9); +y[0].z.unshift(6);", + }, + ], +} `; exports[`immutable-data > typescript > reports array mutations 1`] = ` -[ - { - "column": 1, - "endColumn": 9, - "endLine": 3, - "line": 3, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 14, - "endLine": 4, - "line": 4, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 5, - "line": 5, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 15, - "endLine": 6, - "line": 6, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 7, - "line": 7, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 17, - "endLine": 8, - "line": 8, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 7, - "endLine": 9, - "line": 9, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 10, - "line": 10, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 7, - "endLine": 11, - "line": 11, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 12, - "line": 12, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 5, - "endColumn": 13, - "endLine": 13, - "line": 13, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 5, - "endColumn": 18, - "endLine": 14, - "line": 14, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 13, - "endLine": 15, - "line": 15, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 18, - "endLine": 16, - "line": 16, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 3, + "line": 3, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 4, + "line": 4, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 5, + "line": 5, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 15, + "endLine": 6, + "line": 6, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 7, + "line": 7, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 17, + "endLine": 8, + "line": 8, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 7, + "endLine": 9, + "line": 9, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 10, + "line": 10, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 7, + "endLine": 11, + "line": 11, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 12, + "line": 12, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 13, + "endLine": 13, + "line": 13, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 18, + "endLine": 14, + "line": 14, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 13, + "endLine": 15, + "line": 15, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 18, + "endLine": 16, + "line": 16, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "var x = [5, 6]; +var y = [{ z: [3, 7] }]; +x[0] = 4; +y[0].z[0] = 4; +x[0] += 1; +y[0].z[0] += 1; +delete x[0]; +delete y[0].z[0]; +x[0]++; +y[0].z[0]++; +--x[0]; +--y[0].z[0]; +if (x[0] = 2) {} +if (y[0].z[0] = 2) {} +x.length = 5; +y[0].z.length = 1;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 3, + "line": 3, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 4, + "line": 4, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 5, + "line": 5, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 15, + "endLine": 6, + "line": 6, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 7, + "line": 7, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 17, + "endLine": 8, + "line": 8, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 7, + "endLine": 9, + "line": 9, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 10, + "line": 10, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 7, + "endLine": 11, + "line": 11, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 12, + "line": 12, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 13, + "endLine": 13, + "line": 13, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 18, + "endLine": 14, + "line": 14, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 13, + "endLine": 15, + "line": 15, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 18, + "endLine": 16, + "line": 16, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "var x = [5, 6]; +var y = [{ z: [3, 7] }]; +x[0] = 4; +y[0].z[0] = 4; +x[0] += 1; +y[0].z[0] += 1; +delete x[0]; +delete y[0].z[0]; +x[0]++; +y[0].z[0]++; +--x[0]; +--y[0].z[0]; +if (x[0] = 2) {} +if (y[0].z[0] = 2) {} +x.length = 5; +y[0].z.length = 1;", + }, + ], +} `; diff --git a/tests/rules/immutable-data/__snapshots__/map.test.ts.snap b/tests/rules/immutable-data/__snapshots__/map.test.ts.snap index 448ebc273..cb41f6f09 100644 --- a/tests/rules/immutable-data/__snapshots__/map.test.ts.snap +++ b/tests/rules/immutable-data/__snapshots__/map.test.ts.snap @@ -1,115 +1,266 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`immutable-data > typescript > options > ignoreImmediateMutation > reports immediately mutation when disabled 1`] = ` -[ - { - "column": 1, - "endColumn": 28, - "endLine": 1, - "line": 1, - "message": "Modifying a map is not allowed.", - "messageId": "map", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 28, - "endLine": 2, - "line": 2, - "message": "Modifying a map is not allowed.", - "messageId": "map", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 26, - "endLine": 3, - "line": 3, - "message": "Modifying a map is not allowed.", - "messageId": "map", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 28, + "endLine": 1, + "line": 1, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 28, + "endLine": 2, + "line": 2, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 26, + "endLine": 3, + "line": 3, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "new Map([[5, 6]]).set(4, 8); +new Map([[5, 6]]).delete(4); +new Map([[5, 6]]).clear();", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 28, + "endLine": 1, + "line": 1, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 28, + "endLine": 2, + "line": 2, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 26, + "endLine": 3, + "line": 3, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "new Map([[5, 6]]).set(4, 8); +new Map([[5, 6]]).delete(4); +new Map([[5, 6]]).clear();", + }, + ], +} `; exports[`immutable-data > typescript > options > ignoreNonConstDeclarations > reports variables declared as const 1`] = ` -[ - { - "column": 1, - "endColumn": 12, - "endLine": 2, - "line": 2, - "message": "Modifying a map is not allowed.", - "messageId": "map", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 3, - "line": 3, - "message": "Modifying a map is not allowed.", - "messageId": "map", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 4, - "line": 4, - "message": "Modifying a map is not allowed.", - "messageId": "map", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 12, + "endLine": 2, + "line": 2, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 3, + "line": 3, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 4, + "line": 4, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "const x = new Map([[5, 6]]); +x.set(4, 8); +x.delete(4); +x.clear();", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 12, + "endLine": 2, + "line": 2, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 3, + "line": 3, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 4, + "line": 4, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "const x = new Map([[5, 6]]); +x.set(4, 8); +x.delete(4); +x.clear();", + }, + ], +} `; exports[`immutable-data > typescript > report mutating map methods 1`] = ` -[ - { - "column": 1, - "endColumn": 12, - "endLine": 2, - "line": 2, - "message": "Modifying a map is not allowed.", - "messageId": "map", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 3, - "line": 3, - "message": "Modifying a map is not allowed.", - "messageId": "map", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 4, - "line": 4, - "message": "Modifying a map is not allowed.", - "messageId": "map", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 12, + "endLine": 2, + "line": 2, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 3, + "line": 3, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 4, + "line": 4, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "const x = new Map([[5, 6]]); +x.set(4, 8); +x.delete(4); +x.clear();", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 12, + "endLine": 2, + "line": 2, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 3, + "line": 3, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 4, + "line": 4, + "message": "Modifying a map is not allowed.", + "messageId": "map", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "const x = new Map([[5, 6]]); +x.set(4, 8); +x.delete(4); +x.clear();", + }, + ], +} `; diff --git a/tests/rules/immutable-data/__snapshots__/object.test.ts.snap b/tests/rules/immutable-data/__snapshots__/object.test.ts.snap index 7abd8b67c..6b9111595 100644 --- a/tests/rules/immutable-data/__snapshots__/object.test.ts.snap +++ b/tests/rules/immutable-data/__snapshots__/object.test.ts.snap @@ -1,1328 +1,1907 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`immutable-data > typescript > ignoreNonConstDeclarations > report parameters 1`] = ` -[ - { - "column": 3, - "endColumn": 16, - "endLine": 2, - "line": 2, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 19, - "endLine": 3, - "line": 3, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 4, - "line": 4, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 5, - "line": 5, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 6, - "line": 6, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 7, - "line": 7, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 8, - "line": 8, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 12, - "endLine": 9, - "line": 9, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 12, - "endLine": 10, - "line": 10, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 13, - "endLine": 11, - "line": 11, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 12, - "line": 12, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 13, - "line": 13, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 14, - "line": 14, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 12, - "endLine": 15, - "line": 15, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 13, - "endLine": 16, - "line": 16, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 16, - "endLine": 17, - "line": 17, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 8, - "endLine": 18, - "line": 18, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 8, - "endLine": 19, - "line": 19, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 8, - "endLine": 20, - "line": 20, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 8, - "endLine": 21, - "line": 21, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 7, - "endColumn": 14, - "endLine": 22, - "line": 22, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 7, - "endColumn": 12, - "endLine": 23, - "line": 23, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] -`; - -exports[`immutable-data > typescript > ignoreNonConstDeclarations > report variables declared as const 1`] = ` -[ - { - "column": 1, - "endColumn": 14, - "endLine": 2, - "line": 2, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 17, - "endLine": 3, - "line": 3, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 4, - "line": 4, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 5, - "line": 5, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 6, - "line": 6, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 7, - "line": 7, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 8, - "line": 8, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 9, - "line": 9, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 10, - "line": 10, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 11, - "line": 11, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 12, - "line": 12, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 13, - "line": 13, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 14, - "line": 14, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 15, - "line": 15, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 16, - "line": 16, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 14, - "endLine": 17, - "line": 17, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 18, - "line": 18, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 19, - "line": 19, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 20, - "line": 20, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 21, - "line": 21, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 5, - "endColumn": 12, - "endLine": 22, - "line": 22, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 5, - "endColumn": 10, - "endLine": 23, - "line": 23, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] -`; - exports[`immutable-data > typescript > options > ignoreNonConstDeclarations > report parameters 1`] = ` -[ - { - "column": 3, - "endColumn": 16, - "endLine": 2, - "line": 2, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 19, - "endLine": 3, - "line": 3, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 4, - "line": 4, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 5, - "line": 5, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 6, - "line": 6, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 7, - "line": 7, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 8, - "line": 8, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 12, - "endLine": 9, - "line": 9, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 12, - "endLine": 10, - "line": 10, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 13, - "endLine": 11, - "line": 11, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 12, - "line": 12, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 13, - "line": 13, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 11, - "endLine": 14, - "line": 14, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 12, - "endLine": 15, - "line": 15, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 13, - "endLine": 16, - "line": 16, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 16, - "endLine": 17, - "line": 17, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 8, - "endLine": 18, - "line": 18, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 8, - "endLine": 19, - "line": 19, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 8, - "endLine": 20, - "line": 20, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 3, - "endColumn": 8, - "endLine": 21, - "line": 21, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 7, - "endColumn": 14, - "endLine": 22, - "line": 22, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 7, - "endColumn": 12, - "endLine": 23, - "line": 23, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 16, + "endLine": 2, + "line": 2, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 19, + "endLine": 3, + "line": 3, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 4, + "line": 4, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 5, + "line": 5, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 6, + "line": 6, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 7, + "line": 7, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 8, + "line": 8, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 12, + "endLine": 9, + "line": 9, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 12, + "endLine": 10, + "line": 10, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 13, + "endLine": 11, + "line": 11, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 12, + "line": 12, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 13, + "line": 13, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 14, + "line": 14, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 12, + "endLine": 15, + "line": 15, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 13, + "endLine": 16, + "line": 16, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 16, + "endLine": 17, + "line": 17, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 8, + "endLine": 18, + "line": 18, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 8, + "endLine": 19, + "line": 19, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 8, + "endLine": 20, + "line": 20, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 8, + "endLine": 21, + "line": 21, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 7, + "endColumn": 14, + "endLine": 22, + "line": 22, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 7, + "endColumn": 12, + "endLine": 23, + "line": 23, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "function y(x: {a: 1, b: object}) { + x.foo = "bar"; + x["foo"] = "bar"; + x.a += 1; + x.a -= 1; + x.a *= 1; + x.a /= 1; + x.a %= 1; + x.a <<= 1; + x.a >>= 1; + x.a >>>= 1; + x.a &= 1; + x.a |= 1; + x.a ^= 1; + x.a **= 1; + delete x.a; + delete x["a"]; + x.a++; + x.a--; + ++x.a; + --x.a; + if (x.a = 2) {} + if (x.a++) {} + Object.assign(x, { c: "world" }); + Object.assign(x.b, { c: "world" }); + Object.defineProperties(x, { d: { value: 2, writable: false }}); + Object.defineProperty(x, "e", { value: 3, writable: false }); + Object.setPrototypeOf(x, null); +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 16, + "endLine": 2, + "line": 2, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 19, + "endLine": 3, + "line": 3, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 4, + "line": 4, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 5, + "line": 5, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 6, + "line": 6, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 7, + "line": 7, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 8, + "line": 8, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 12, + "endLine": 9, + "line": 9, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 12, + "endLine": 10, + "line": 10, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 13, + "endLine": 11, + "line": 11, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 12, + "line": 12, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 13, + "line": 13, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 11, + "endLine": 14, + "line": 14, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 12, + "endLine": 15, + "line": 15, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 13, + "endLine": 16, + "line": 16, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 16, + "endLine": 17, + "line": 17, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 8, + "endLine": 18, + "line": 18, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 8, + "endLine": 19, + "line": 19, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 8, + "endLine": 20, + "line": 20, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 3, + "endColumn": 8, + "endLine": 21, + "line": 21, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 7, + "endColumn": 14, + "endLine": 22, + "line": 22, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 7, + "endColumn": 12, + "endLine": 23, + "line": 23, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "function y(x: {a: 1, b: object}) { + x.foo = "bar"; + x["foo"] = "bar"; + x.a += 1; + x.a -= 1; + x.a *= 1; + x.a /= 1; + x.a %= 1; + x.a <<= 1; + x.a >>= 1; + x.a >>>= 1; + x.a &= 1; + x.a |= 1; + x.a ^= 1; + x.a **= 1; + delete x.a; + delete x["a"]; + x.a++; + x.a--; + ++x.a; + --x.a; + if (x.a = 2) {} + if (x.a++) {} + Object.assign(x, { c: "world" }); + Object.assign(x.b, { c: "world" }); + Object.defineProperties(x, { d: { value: 2, writable: false }}); + Object.defineProperty(x, "e", { value: 3, writable: false }); + Object.setPrototypeOf(x, null); +}", + }, + ], +} `; exports[`immutable-data > typescript > options > ignoreNonConstDeclarations > report variables declared as const 1`] = ` -[ - { - "column": 1, - "endColumn": 14, - "endLine": 2, - "line": 2, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 17, - "endLine": 3, - "line": 3, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 4, - "line": 4, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 5, - "line": 5, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 6, - "line": 6, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 7, - "line": 7, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 8, - "line": 8, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 9, - "line": 9, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 10, - "line": 10, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 11, - "line": 11, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 12, - "line": 12, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 13, - "line": 13, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 14, - "line": 14, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 15, - "line": 15, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 16, - "line": 16, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 14, - "endLine": 17, - "line": 17, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 18, - "line": 18, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 19, - "line": 19, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 20, - "line": 20, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 21, - "line": 21, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 5, - "endColumn": 12, - "endLine": 22, - "line": 22, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 5, - "endColumn": 10, - "endLine": 23, - "line": 23, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 14, + "endLine": 2, + "line": 2, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 17, + "endLine": 3, + "line": 3, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 4, + "line": 4, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 5, + "line": 5, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 6, + "line": 6, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 7, + "line": 7, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 8, + "line": 8, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 9, + "line": 9, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 10, + "line": 10, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 11, + "line": 11, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 12, + "line": 12, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 13, + "line": 13, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 14, + "line": 14, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 15, + "line": 15, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 16, + "line": 16, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 17, + "line": 17, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 18, + "line": 18, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 19, + "line": 19, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 20, + "line": 20, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 21, + "line": 21, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 12, + "endLine": 22, + "line": 22, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 10, + "endLine": 23, + "line": 23, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "const x = {a: 1, b:{}}; +x.foo = "bar"; +x["foo"] = "bar"; +x.a += 1; +x.a -= 1; +x.a *= 1; +x.a /= 1; +x.a %= 1; +x.a <<= 1; +x.a >>= 1; +x.a >>>= 1; +x.a &= 1; +x.a |= 1; +x.a ^= 1; +x.a **= 1; +delete x.a; +delete x["a"]; +x.a++; +x.a--; +++x.a; +--x.a; +if (x.a = 2) {} +if (x.a++) {} +Object.assign(x, { c: "world" }); +Object.assign(x.b, { c: "world" }); +Object.defineProperties(x, { d: { value: 2, writable: false }}); +Object.defineProperty(x, "e", { value: 3, writable: false }); +Object.setPrototypeOf(x, null);", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 14, + "endLine": 2, + "line": 2, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 17, + "endLine": 3, + "line": 3, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 4, + "line": 4, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 5, + "line": 5, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 6, + "line": 6, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 7, + "line": 7, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 8, + "line": 8, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 9, + "line": 9, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 10, + "line": 10, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 11, + "line": 11, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 12, + "line": 12, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 13, + "line": 13, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 14, + "line": 14, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 15, + "line": 15, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 16, + "line": 16, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 17, + "line": 17, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 18, + "line": 18, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 19, + "line": 19, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 20, + "line": 20, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 21, + "line": 21, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 12, + "endLine": 22, + "line": 22, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 10, + "endLine": 23, + "line": 23, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "const x = {a: 1, b:{}}; +x.foo = "bar"; +x["foo"] = "bar"; +x.a += 1; +x.a -= 1; +x.a *= 1; +x.a /= 1; +x.a %= 1; +x.a <<= 1; +x.a >>= 1; +x.a >>>= 1; +x.a &= 1; +x.a |= 1; +x.a ^= 1; +x.a **= 1; +delete x.a; +delete x["a"]; +x.a++; +x.a--; +++x.a; +--x.a; +if (x.a = 2) {} +if (x.a++) {} +Object.assign(x, { c: "world" }); +Object.assign(x.b, { c: "world" }); +Object.defineProperties(x, { d: { value: 2, writable: false }}); +Object.defineProperty(x, "e", { value: 3, writable: false }); +Object.setPrototypeOf(x, null);", + }, + ], +} `; exports[`immutable-data > typescript > report object mutating patterns 1`] = ` -[ - { - "column": 1, - "endColumn": 14, - "endLine": 2, - "line": 2, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 17, - "endLine": 3, - "line": 3, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 4, - "line": 4, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 5, - "line": 5, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 6, - "line": 6, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 7, - "line": 7, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 8, - "line": 8, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 9, - "line": 9, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 10, - "line": 10, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 11, - "line": 11, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 12, - "line": 12, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 13, - "line": 13, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 9, - "endLine": 14, - "line": 14, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 15, - "line": 15, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 11, - "endLine": 16, - "line": 16, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 14, - "endLine": 17, - "line": 17, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UnaryExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 18, - "line": 18, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 19, - "line": 19, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 20, - "line": 20, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 6, - "endLine": 21, - "line": 21, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 5, - "endColumn": 12, - "endLine": 22, - "line": 22, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 5, - "endColumn": 10, - "endLine": 23, - "line": 23, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "UpdateExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 14, + "endLine": 2, + "line": 2, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 17, + "endLine": 3, + "line": 3, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 4, + "line": 4, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 5, + "line": 5, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 6, + "line": 6, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 7, + "line": 7, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 8, + "line": 8, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 9, + "line": 9, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 10, + "line": 10, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 11, + "line": 11, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 12, + "line": 12, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 13, + "line": 13, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 14, + "line": 14, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 15, + "line": 15, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 16, + "line": 16, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 17, + "line": 17, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 18, + "line": 18, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 19, + "line": 19, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 20, + "line": 20, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 21, + "line": 21, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 12, + "endLine": 22, + "line": 22, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 10, + "endLine": 23, + "line": 23, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "var x = {a: 1}; +x.foo = "bar"; +x["foo"] = "bar"; +x.a += 1; +x.a -= 1; +x.a *= 1; +x.a /= 1; +x.a %= 1; +x.a <<= 1; +x.a >>= 1; +x.a >>>= 1; +x.a &= 1; +x.a |= 1; +x.a ^= 1; +x.a **= 1; +delete x.a; +delete x["a"]; +x.a++; +x.a--; +++x.a; +--x.a; +if (x.a = 2) {} +if (x.a++) {} +var y = x.a; +var z = x["a"]; +if (x.a && y.a) {} +var w = ~x.a; +if (!x.a) {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 14, + "endLine": 2, + "line": 2, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 17, + "endLine": 3, + "line": 3, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 4, + "line": 4, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 5, + "line": 5, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 6, + "line": 6, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 7, + "line": 7, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 8, + "line": 8, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 9, + "line": 9, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 10, + "line": 10, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 11, + "line": 11, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 12, + "line": 12, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 13, + "line": 13, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 9, + "endLine": 14, + "line": 14, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 15, + "line": 15, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 11, + "endLine": 16, + "line": 16, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 14, + "endLine": 17, + "line": 17, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UnaryExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 18, + "line": 18, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 19, + "line": 19, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 20, + "line": 20, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 6, + "endLine": 21, + "line": 21, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 12, + "endLine": 22, + "line": 22, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 10, + "endLine": 23, + "line": 23, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "UpdateExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "var x = {a: 1}; +x.foo = "bar"; +x["foo"] = "bar"; +x.a += 1; +x.a -= 1; +x.a *= 1; +x.a /= 1; +x.a %= 1; +x.a <<= 1; +x.a >>= 1; +x.a >>>= 1; +x.a &= 1; +x.a |= 1; +x.a ^= 1; +x.a **= 1; +delete x.a; +delete x["a"]; +x.a++; +x.a--; +++x.a; +--x.a; +if (x.a = 2) {} +if (x.a++) {} +var y = x.a; +var z = x["a"]; +if (x.a && y.a) {} +var w = ~x.a; +if (!x.a) {}", + }, + ], +} `; exports[`immutable-data > typescript > reports Object.assign() on identifiers 1`] = ` -[ - { - "column": 9, - "endColumn": 44, - "endLine": 3, - "line": 3, - "message": "Modifying properties of existing object not allowed.", - "messageId": "object", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 9, - "endColumn": 48, - "endLine": 4, - "line": 4, - "message": "Modifying properties of existing object not allowed.", - "messageId": "object", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 9, + "endColumn": 44, + "endLine": 3, + "line": 3, + "message": "Modifying properties of existing object not allowed.", + "messageId": "object", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 9, + "endColumn": 48, + "endLine": 4, + "line": 4, + "message": "Modifying properties of existing object not allowed.", + "messageId": "object", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "var x = { msg1: "hello", obj: { a: 1, b: 2} }; + +var a = Object.assign(x, { msg2: "world" }); +var b = Object.assign(x.obj, { msg2: "world" });", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 9, + "endColumn": 44, + "endLine": 3, + "line": 3, + "message": "Modifying properties of existing object not allowed.", + "messageId": "object", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 9, + "endColumn": 48, + "endLine": 4, + "line": 4, + "message": "Modifying properties of existing object not allowed.", + "messageId": "object", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "var x = { msg1: "hello", obj: { a: 1, b: 2} }; + +var a = Object.assign(x, { msg2: "world" }); +var b = Object.assign(x.obj, { msg2: "world" });", + }, + ], +} `; exports[`immutable-data > typescript > reports field mutation in class methods 1`] = ` -[ - { - "column": 5, - "endColumn": 17, - "endLine": 9, - "line": 9, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 5, - "endColumn": 17, - "endLine": 10, - "line": 10, - "message": "Modifying an existing object/array is not allowed.", - "messageId": "generic", - "nodeType": "AssignmentExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 17, + "endLine": 9, + "line": 9, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 17, + "endLine": 10, + "line": 10, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "class Klass { + bar = 1; + + constructor() { + this.baz = "hello"; + } + + zoo() { + this.bar = 2; + this.baz = 3; + } +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 17, + "endLine": 9, + "line": 9, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 5, + "endColumn": 17, + "endLine": 10, + "line": 10, + "message": "Modifying an existing object/array is not allowed.", + "messageId": "generic", + "nodeType": "AssignmentExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "class Klass { + bar = 1; + + constructor() { + this.baz = "hello"; + } + + zoo() { + this.bar = 2; + this.baz = 3; + } +}", + }, + ], +} `; exports[`immutable-data > typescript > reports object mutating methods 1`] = ` -[ - { - "column": 1, - "endColumn": 66, - "endLine": 2, - "line": 2, - "message": "Modifying properties of existing object not allowed.", - "messageId": "object", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 63, - "endLine": 3, - "line": 3, - "message": "Modifying properties of existing object not allowed.", - "messageId": "object", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 33, - "endLine": 4, - "line": 4, - "message": "Modifying properties of existing object not allowed.", - "messageId": "object", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 66, + "endLine": 2, + "line": 2, + "message": "Modifying properties of existing object not allowed.", + "messageId": "object", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 63, + "endLine": 3, + "line": 3, + "message": "Modifying properties of existing object not allowed.", + "messageId": "object", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 33, + "endLine": 4, + "line": 4, + "message": "Modifying properties of existing object not allowed.", + "messageId": "object", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "var foo = { a: 1 }; +Object.defineProperties(foo, { b: { value: 2, writable: false }}); +Object.defineProperty(foo, "c", { value: 3, writable: false }); +Object.setPrototypeOf(foo, null);", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 66, + "endLine": 2, + "line": 2, + "message": "Modifying properties of existing object not allowed.", + "messageId": "object", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 63, + "endLine": 3, + "line": 3, + "message": "Modifying properties of existing object not allowed.", + "messageId": "object", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 33, + "endLine": 4, + "line": 4, + "message": "Modifying properties of existing object not allowed.", + "messageId": "object", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "var foo = { a: 1 }; +Object.defineProperties(foo, { b: { value: 2, writable: false }}); +Object.defineProperty(foo, "c", { value: 3, writable: false }); +Object.setPrototypeOf(foo, null);", + }, + ], +} `; diff --git a/tests/rules/immutable-data/__snapshots__/set.test.ts.snap b/tests/rules/immutable-data/__snapshots__/set.test.ts.snap index 9767b2d7d..a73399cac 100644 --- a/tests/rules/immutable-data/__snapshots__/set.test.ts.snap +++ b/tests/rules/immutable-data/__snapshots__/set.test.ts.snap @@ -1,115 +1,266 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`immutable-data > typescript > options > ignoreImmediateMutation > reports immediately mutation when disabled 1`] = ` -[ - { - "column": 1, - "endColumn": 23, - "endLine": 1, - "line": 1, - "message": "Modifying a set is not allowed.", - "messageId": "set", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 26, - "endLine": 2, - "line": 2, - "message": "Modifying a set is not allowed.", - "messageId": "set", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 24, - "endLine": 3, - "line": 3, - "message": "Modifying a set is not allowed.", - "messageId": "set", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 23, + "endLine": 1, + "line": 1, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 26, + "endLine": 2, + "line": 2, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 24, + "endLine": 3, + "line": 3, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "new Set([5, 6]).add(4); +new Set([5, 6]).delete(4); +new Set([5, 6]).clear();", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 23, + "endLine": 1, + "line": 1, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 26, + "endLine": 2, + "line": 2, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 24, + "endLine": 3, + "line": 3, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "new Set([5, 6]).add(4); +new Set([5, 6]).delete(4); +new Set([5, 6]).clear();", + }, + ], +} `; exports[`immutable-data > typescript > options > ignoreNonConstDeclarations > reports variables declared as const 1`] = ` -[ - { - "column": 1, - "endColumn": 9, - "endLine": 2, - "line": 2, - "message": "Modifying a set is not allowed.", - "messageId": "set", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 3, - "line": 3, - "message": "Modifying a set is not allowed.", - "messageId": "set", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 4, - "line": 4, - "message": "Modifying a set is not allowed.", - "messageId": "set", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 2, + "line": 2, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 3, + "line": 3, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 4, + "line": 4, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "const x = new Set([5, 6]); +x.add(4); +x.delete(4); +x.clear();", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 2, + "line": 2, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 3, + "line": 3, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 4, + "line": 4, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "const x = new Set([5, 6]); +x.add(4); +x.delete(4); +x.clear();", + }, + ], +} `; exports[`immutable-data > typescript > report mutating set methods 1`] = ` -[ - { - "column": 1, - "endColumn": 9, - "endLine": 2, - "line": 2, - "message": "Modifying a set is not allowed.", - "messageId": "set", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 12, - "endLine": 3, - "line": 3, - "message": "Modifying a set is not allowed.", - "messageId": "set", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, - { - "column": 1, - "endColumn": 10, - "endLine": 4, - "line": 4, - "message": "Modifying a set is not allowed.", - "messageId": "set", - "nodeType": "CallExpression", - "ruleId": "immutable-data", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 2, + "line": 2, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 3, + "line": 3, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 4, + "line": 4, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "immutable-data", + "severity": 2, + }, + ], + "output": "const x = new Set([5, 6]); +x.add(4); +x.delete(4); +x.clear();", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 1, + "endColumn": 9, + "endLine": 2, + "line": 2, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 12, + "endLine": 3, + "line": 3, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + { + "column": 1, + "endColumn": 10, + "endLine": 4, + "line": 4, + "message": "Modifying a set is not allowed.", + "messageId": "set", + "nodeType": "CallExpression", + "ruleId": "rule-to-test/immutable-data", + "severity": 2, + }, + ], + "output": "const x = new Set([5, 6]); +x.add(4); +x.delete(4); +x.clear();", + }, + ], +} `; diff --git a/tests/rules/immutable-data/array.test.ts b/tests/rules/immutable-data/array.test.ts index 7da695984..06163d32c 100644 --- a/tests/rules/immutable-data/array.test.ts +++ b/tests/rules/immutable-data/array.test.ts @@ -14,8 +14,8 @@ describe(name, () => { configs: typescriptConfig, }); - it("reports array mutations", () => { - const invalidResult = invalid({ + it("reports array mutations", async () => { + const invalidResult = await invalid({ code: dedent` var x = [5, 6]; var y = [{ z: [3, 7] }]; @@ -52,11 +52,11 @@ describe(name, () => { "generic", ], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("report mutating array methods", () => { - const invalidResult = invalid({ + it("report mutating array methods", async () => { + const invalidResult = await invalid({ code: dedent` var x = [5, 6]; x.copyWithin(0, 1, 2); @@ -100,11 +100,11 @@ describe(name, () => { "array", ], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report non-mutating array methods", () => { - valid(dedent` + it("doesn't report non-mutating array methods", async () => { + await valid(dedent` var x = [5, 6]; var y = [{ z: [3, 7] }]; @@ -128,8 +128,8 @@ describe(name, () => { `); }); - it("allows mutating array methods to be chained to array accessor/iteration methods", () => { - valid(dedent` + it("allows mutating array methods to be chained to array accessor/iteration methods", async () => { + await valid(dedent` x.slice().copyWithin(0, 1, 2); x.slice().fill(3); x.slice().pop(); @@ -192,8 +192,8 @@ describe(name, () => { `); }); - it("doesn't report mutating array methods on non-array objects", () => { - valid(dedent` + it("doesn't report mutating array methods on non-array objects", async () => { + await valid(dedent` var z = { copyWithin: function () {}, fill: function () {}, @@ -220,8 +220,8 @@ describe(name, () => { describe("options", () => { describe("ignoreNonConstDeclarations", () => { - it("reports variables declared as const", () => { - const invalidResult = invalid({ + it("reports variables declared as const", async () => { + const invalidResult = await invalid({ code: dedent` const arr = [0, 1]; arr[0] += 1; @@ -257,11 +257,11 @@ describe(name, () => { "array", ], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report variables not declared as const", () => { - valid({ + it("doesn't report variables not declared as const", async () => { + await valid({ code: dedent` let arr = [0, 1]; arr[0] += 1; @@ -284,7 +284,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` let arr = [0]; for (const x of [1, 2]) { @@ -311,7 +311,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` const constArr = [[0, 1], [2, 3]]; let arr = constArr[0]; @@ -338,8 +338,8 @@ describe(name, () => { }); describe("ignoreImmediateMutation", () => { - it("doesn't report immediately mutation when enabled", () => { - valid({ + it("doesn't report immediately mutation when enabled", async () => { + await valid({ code: dedent` [0, 1, 2].copyWithin(0, 1, 2); [0, 1, 2].fill(3); @@ -388,7 +388,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` Object.entries({}).sort(); Object.keys({}).sort(); @@ -401,7 +401,7 @@ describe(name, () => { ], }); - valid({ + await valid({ code: dedent` "foo".split("").sort(); const bar = "bar"; @@ -416,8 +416,8 @@ describe(name, () => { }); }); - it("reports immediately mutation when disabled", () => { - const invalidResult = invalid({ + it("reports immediately mutation when disabled", async () => { + const invalidResult = await invalid({ code: dedent` [0, 1, 2].sort(); new Array(5).sort(); @@ -431,7 +431,7 @@ describe(name, () => { ], errors: ["array", "array", "array", "array"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); diff --git a/tests/rules/immutable-data/map.test.ts b/tests/rules/immutable-data/map.test.ts index 926ee0c83..7c1354e25 100644 --- a/tests/rules/immutable-data/map.test.ts +++ b/tests/rules/immutable-data/map.test.ts @@ -14,8 +14,8 @@ describe(name, () => { configs: typescriptConfig, }); - it("report mutating map methods", () => { - const invalidResult = invalid({ + it("report mutating map methods", async () => { + const invalidResult = await invalid({ code: dedent` const x = new Map([[5, 6]]); x.set(4, 8); @@ -25,11 +25,11 @@ describe(name, () => { options: [], errors: ["map", "map", "map"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report mutating map methods when ignoring maps and sets", () => { - valid({ + it("doesn't report mutating map methods when ignoring maps and sets", async () => { + await valid({ code: dedent` const x = new Map([[5, 6]]); x.set(4, 8); @@ -44,8 +44,8 @@ describe(name, () => { }); }); - it("doesn't report non-mutating map methods", () => { - valid(dedent` + it("doesn't report non-mutating map methods", async () => { + await valid(dedent` const x = new Map([[5, 6]]); x.size; x.has(4); @@ -55,8 +55,8 @@ describe(name, () => { `); }); - it("doesn't report mutating map methods on non-map objects", () => { - valid(dedent` + it("doesn't report mutating map methods on non-map objects", async () => { + await valid(dedent` const z = { set: function () {}, delete: function () {}, @@ -71,8 +71,8 @@ describe(name, () => { describe("options", () => { describe("ignoreNonConstDeclarations", () => { - it("reports variables declared as const", () => { - const invalidResult = invalid({ + it("reports variables declared as const", async () => { + const invalidResult = await invalid({ code: dedent` const x = new Map([[5, 6]]); x.set(4, 8); @@ -86,11 +86,11 @@ describe(name, () => { ], errors: ["map", "map", "map"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report variables not declared as const", () => { - valid({ + it("doesn't report variables not declared as const", async () => { + await valid({ code: dedent` let x = new Map([[5, 6]]); x.set(4, 8); @@ -107,8 +107,8 @@ describe(name, () => { }); describe("ignoreImmediateMutation", () => { - it("doesn't report immediately mutation when enabled", () => { - valid({ + it("doesn't report immediately mutation when enabled", async () => { + await valid({ code: dedent` new Map([[5, 6]]).set(4, 8); new Map([[5, 6]]).delete(4); @@ -122,8 +122,8 @@ describe(name, () => { }); }); - it("reports immediately mutation when disabled", () => { - const invalidResult = invalid({ + it("reports immediately mutation when disabled", async () => { + const invalidResult = await invalid({ code: dedent` new Map([[5, 6]]).set(4, 8); new Map([[5, 6]]).delete(4); @@ -136,7 +136,7 @@ describe(name, () => { ], errors: ["map", "map", "map"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); diff --git a/tests/rules/immutable-data/object.test.ts b/tests/rules/immutable-data/object.test.ts index 226824bb7..4ce2fd246 100644 --- a/tests/rules/immutable-data/object.test.ts +++ b/tests/rules/immutable-data/object.test.ts @@ -14,8 +14,8 @@ describe(name, () => { configs: typescriptConfig, }); - it("report object mutating patterns", () => { - const invalidResult = invalid({ + it("report object mutating patterns", async () => { + const invalidResult = await invalid({ code: dedent` var x = {a: 1}; x.foo = "bar"; @@ -71,11 +71,11 @@ describe(name, () => { "generic", ], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports Object.assign() on identifiers", () => { - const invalidResult = invalid({ + it("reports Object.assign() on identifiers", async () => { + const invalidResult = await invalid({ code: dedent` var x = { msg1: "hello", obj: { a: 1, b: 2} }; @@ -84,11 +84,11 @@ describe(name, () => { `, errors: ["object", "object"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports object mutating methods", () => { - const invalidResult = invalid({ + it("reports object mutating methods", async () => { + const invalidResult = await invalid({ code: dedent` var foo = { a: 1 }; Object.defineProperties(foo, { b: { value: 2, writable: false }}); @@ -97,11 +97,11 @@ describe(name, () => { `, errors: ["object", "object", "object"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports field mutation in class methods", () => { - const invalidResult = invalid({ + it("reports field mutation in class methods", async () => { + const invalidResult = await invalid({ code: dedent` class Klass { bar = 1; @@ -118,11 +118,11 @@ describe(name, () => { `, errors: ["generic", "generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report non-object mutating patterns", () => { - valid(dedent` + it("doesn't report non-object mutating patterns", async () => { + await valid(dedent` var foo = function () {}; var bar = { x: 1, @@ -146,8 +146,8 @@ describe(name, () => { `); }); - it("doesn't report Object.assign() on non-identifiers", () => { - valid(dedent` + it("doesn't report Object.assign() on non-identifiers", async () => { + await valid(dedent` var x = { msg1: "hello", obj: { a: 1, b: 2}, func: function() {} }; var bar = function(a, b, c) { return { a: a, b: b, c: c }; }; @@ -157,8 +157,8 @@ describe(name, () => { `); }); - it("doesn't report initialization of class members in constructor", () => { - valid(dedent` + it("doesn't report initialization of class members in constructor", async () => { + await valid(dedent` class Klass { bar = 1; constructor() { @@ -170,8 +170,8 @@ describe(name, () => { describe("options", () => { describe("ignoreNonConstDeclarations", () => { - it("report variables declared as const", () => { - const invalidResult = invalid({ + it("report variables declared as const", async () => { + const invalidResult = await invalid({ code: dedent` const x = {a: 1, b:{}}; x.foo = "bar"; @@ -228,11 +228,11 @@ describe(name, () => { "generic", ], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("report parameters", () => { - const invalidResult = invalid({ + it("report parameters", async () => { + const invalidResult = await invalid({ code: dedent` function y(x: {a: 1, b: object}) { x.foo = "bar"; @@ -296,11 +296,11 @@ describe(name, () => { "generic", ], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report variables not declared as const", () => { - valid({ + it("doesn't report variables not declared as const", async () => { + await valid({ code: dedent` let x = {a: 1, b:{}}; x.foo = "bar"; @@ -335,8 +335,8 @@ describe(name, () => { }); }); - it("doesn't report parameters", () => { - valid({ + it("doesn't report parameters", async () => { + await valid({ code: dedent` function y(x: {a: 1, b: object}) { x.foo = "bar"; @@ -380,8 +380,8 @@ describe(name, () => { }); describe("ignoreIdentifierPattern", () => { - it("ignores variables declared as mutable", () => { - valid({ + it("ignores variables declared as mutable", async () => { + await valid({ code: dedent` var mutableVar = { a: 1 }; delete mutableVar.a; @@ -393,8 +393,8 @@ describe(name, () => { }); describe("ignoreAccessorPattern", () => { - it("ignores variables declared as mutable", () => { - valid({ + it("ignores variables declared as mutable", async () => { + await valid({ code: dedent` function y(mutable_x: {a: 1, b: object}) { mutable_x.foo = "bar"; @@ -437,8 +437,8 @@ describe(name, () => { }); }); - it("ignores class fields", () => { - valid({ + it("ignores class fields", async () => { + await valid({ code: dedent` class Klass { mutate() { diff --git a/tests/rules/immutable-data/set.test.ts b/tests/rules/immutable-data/set.test.ts index 55c18d41f..ff02ca397 100644 --- a/tests/rules/immutable-data/set.test.ts +++ b/tests/rules/immutable-data/set.test.ts @@ -14,8 +14,8 @@ describe(name, () => { configs: typescriptConfig, }); - it("report mutating set methods", () => { - const invalidResult = invalid({ + it("report mutating set methods", async () => { + const invalidResult = await invalid({ code: dedent` const x = new Set([5, 6]); x.add(4); @@ -25,11 +25,11 @@ describe(name, () => { options: [], errors: ["set", "set", "set"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report mutating set methods when ignoring maps and sets", () => { - valid({ + it("doesn't report mutating set methods when ignoring maps and sets", async () => { + await valid({ code: dedent` const x = new Set([5, 6]); x.add(4); @@ -44,8 +44,8 @@ describe(name, () => { }); }); - it("doesn't report non-mutating set methods", () => { - valid(dedent` + it("doesn't report non-mutating set methods", async () => { + await valid(dedent` const x = new Set([5, 6]); x.size; x.has(4); @@ -59,8 +59,8 @@ describe(name, () => { `); }); - it("allows mutating set methods to be chained to new set methods", () => { - valid(dedent` + it("allows mutating set methods to be chained to new set methods", async () => { + await valid(dedent` const x = new Set([5, 6]); x.difference(new Set([1, 2, 3])).add(4); @@ -105,8 +105,8 @@ describe(name, () => { `); }); - it("doesn't report mutating set methods on non-set objects", () => { - valid(dedent` + it("doesn't report mutating set methods on non-set objects", async () => { + await valid(dedent` const z = { add: function () {}, delete: function () {}, @@ -121,8 +121,8 @@ describe(name, () => { describe("options", () => { describe("ignoreNonConstDeclarations", () => { - it("reports variables declared as const", () => { - const invalidResult = invalid({ + it("reports variables declared as const", async () => { + const invalidResult = await invalid({ code: dedent` const x = new Set([5, 6]); x.add(4); @@ -136,11 +136,11 @@ describe(name, () => { ], errors: ["set", "set", "set"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report variables not declared as const", () => { - valid({ + it("doesn't report variables not declared as const", async () => { + await valid({ code: dedent` let x = new Set([5, 6]); x.add(4); @@ -157,8 +157,8 @@ describe(name, () => { }); describe("ignoreImmediateMutation", () => { - it("doesn't report immediately mutation when enabled", () => { - valid({ + it("doesn't report immediately mutation when enabled", async () => { + await valid({ code: dedent` new Set([5, 6]).add(4); new Set([5, 6]).delete(4); @@ -172,8 +172,8 @@ describe(name, () => { }); }); - it("reports immediately mutation when disabled", () => { - const invalidResult = invalid({ + it("reports immediately mutation when disabled", async () => { + const invalidResult = await invalid({ code: dedent` new Set([5, 6]).add(4); new Set([5, 6]).delete(4); @@ -186,7 +186,7 @@ describe(name, () => { ], errors: ["set", "set", "set"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); diff --git a/tests/rules/no-class-inheritance.test.ts b/tests/rules/no-class-inheritance.test.ts index 5bf242a19..92835840a 100644 --- a/tests/rules/no-class-inheritance.test.ts +++ b/tests/rules/no-class-inheritance.test.ts @@ -14,22 +14,22 @@ describe(name, () => { configs: esLatestConfig, }); - it("doesn't report non-issues", () => { - valid("class Foo {}"); + it("doesn't report non-issues", async () => { + await valid("class Foo {}"); }); - it("reports class inheritance", () => { - const invalidResult1 = invalid({ + it("reports class inheritance", async () => { + const invalidResult1 = await invalid({ code: "class Foo extends Bar {}", errors: ["extends"], }); - expect(invalidResult1.messages).toMatchSnapshot(); + expect(invalidResult1.result).toMatchSnapshot(); }); describe("options", () => { describe("ignoreIdentifierPattern", () => { - it("should not report class inheritance with matching identifiers", () => { - valid({ + it("should not report class inheritance with matching identifiers", async () => { + await valid({ code: dedent` class Foo extends Bar {} `, @@ -37,22 +37,22 @@ describe(name, () => { }); }); - it("should report class inheritance with non-matching identifiers", () => { - const invalidResult = invalid({ + it("should report class inheritance with non-matching identifiers", async () => { + const invalidResult = await invalid({ code: dedent` class Bar extends Foo {} `, options: [{ ignoreIdentifierPattern: "^Foo$" }], errors: ["extends"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); describe("ignoreCodePattern", () => { - it("should not report class inheritance with matching identifiers", () => { - valid({ + it("should not report class inheritance with matching identifiers", async () => { + await valid({ code: dedent` class Foo extends Bar {} `, @@ -60,15 +60,15 @@ describe(name, () => { }); }); - it("should report class inheritance with non-matching identifiers", () => { - const invalidResult = invalid({ + it("should report class inheritance with non-matching identifiers", async () => { + const invalidResult = await invalid({ code: dedent` class Bar extends Foo {} `, options: [{ ignoreCodePattern: "class Foo" }], errors: ["extends"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); @@ -80,30 +80,30 @@ describe(name, () => { configs: typescriptConfig, }); - it("doesn't report non-issues", () => { - valid("class Foo {}"); - valid("class Foo implements Bar {}"); - valid("interface Foo extends Bar {}"); + it("doesn't report non-issues", async () => { + await valid("class Foo {}"); + await valid("class Foo implements Bar {}"); + await valid("interface Foo extends Bar {}"); }); - it("reports class inheritance", () => { - const invalidResult1 = invalid({ + it("reports class inheritance", async () => { + const invalidResult1 = await invalid({ code: "abstract class Foo {}", errors: ["abstract"], }); - expect(invalidResult1.messages).toMatchSnapshot(); + expect(invalidResult1.result).toMatchSnapshot(); - const invalidResult2 = invalid({ + const invalidResult2 = await invalid({ code: "abstract class Foo extends Bar {}", errors: ["abstract", "extends"], }); - expect(invalidResult2.messages).toMatchSnapshot(); + expect(invalidResult2.result).toMatchSnapshot(); }); describe("options", () => { describe("ignoreIdentifierPattern", () => { - it("should not report class inheritance with matching identifiers", () => { - valid({ + it("should not report class inheritance with matching identifiers", async () => { + await valid({ code: dedent` abstract class Foo {} `, @@ -111,22 +111,22 @@ describe(name, () => { }); }); - it("should report class inheritance with non-matching identifiers", () => { - const invalidResult = invalid({ + it("should report class inheritance with non-matching identifiers", async () => { + const invalidResult = await invalid({ code: dedent` abstract class Bar {} `, options: [{ ignoreIdentifierPattern: "^Foo$" }], errors: ["abstract"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); describe("ignoreCodePattern", () => { - it("should not report class inheritance with matching identifiers", () => { - valid({ + it("should not report class inheritance with matching identifiers", async () => { + await valid({ code: dedent` abstract class Foo {} `, @@ -134,15 +134,15 @@ describe(name, () => { }); }); - it("should report class inheritance with non-matching identifiers", () => { - const invalidResult = invalid({ + it("should report class inheritance with non-matching identifiers", async () => { + const invalidResult = await invalid({ code: dedent` abstract class Bar {} `, options: [{ ignoreCodePattern: "class Foo" }], errors: ["abstract"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); diff --git a/tests/rules/no-classes.test.ts b/tests/rules/no-classes.test.ts index b78e8951f..fb5cd3ac7 100644 --- a/tests/rules/no-classes.test.ts +++ b/tests/rules/no-classes.test.ts @@ -14,28 +14,28 @@ describe(name, () => { configs: esLatestConfig, }); - it("doesn't report non-issues", () => { - valid("function Foo() {}"); + it("doesn't report non-issues", async () => { + await valid("function Foo() {}"); }); - it("reports class declarations", () => { - const invalidResult1 = invalid({ + it("reports class declarations", async () => { + const invalidResult1 = await invalid({ code: "class Foo {}", errors: ["generic"], }); - expect(invalidResult1.messages).toMatchSnapshot(); + expect(invalidResult1.result).toMatchSnapshot(); - const invalidResult2 = invalid({ + const invalidResult2 = await invalid({ code: "const klass = class {}", errors: ["generic"], }); - expect(invalidResult2.messages).toMatchSnapshot(); + expect(invalidResult2.result).toMatchSnapshot(); }); describe("options", () => { describe("ignoreIdentifierPattern", () => { - it("should not report classes with matching identifiers", () => { - valid({ + it("should not report classes with matching identifiers", async () => { + await valid({ code: dedent` class Foo {} `, @@ -43,22 +43,22 @@ describe(name, () => { }); }); - it("should report classes with non-matching identifiers", () => { - const invalidResult = invalid({ + it("should report classes with non-matching identifiers", async () => { + const invalidResult = await invalid({ code: dedent` class Bar {} `, options: [{ ignoreIdentifierPattern: "^Foo$" }], errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); describe("ignoreCodePattern", () => { - it("should not report classes with matching identifiers", () => { - valid({ + it("should not report classes with matching identifiers", async () => { + await valid({ code: dedent` class Foo {} `, @@ -66,15 +66,15 @@ describe(name, () => { }); }); - it("should report classes with non-matching identifiers", () => { - const invalidResult = invalid({ + it("should report classes with non-matching identifiers", async () => { + const invalidResult = await invalid({ code: dedent` class Bar {} `, options: [{ ignoreCodePattern: "class Foo" }], errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); diff --git a/tests/rules/no-conditional-statements.test.ts b/tests/rules/no-conditional-statements.test.ts index 04a2d24ef..a0cbbf623 100644 --- a/tests/rules/no-conditional-statements.test.ts +++ b/tests/rules/no-conditional-statements.test.ts @@ -15,8 +15,8 @@ describe(name, () => { }); describe("if statements", () => { - it("reports if statements", () => { - const invalidResult = invalid({ + it("reports if statements", async () => { + const invalidResult = await invalid({ code: dedent` if (true) { console.log("hello world"); @@ -24,14 +24,14 @@ describe(name, () => { `, errors: ["unexpectedIf"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); describe("options", () => { describe("allowReturningBranches", () => { describe("true", () => { - it("allows return", () => { - valid({ + it("allows return", async () => { + await valid({ code: dedent` function foo(i) { if (i === 1) { @@ -43,8 +43,8 @@ describe(name, () => { }); }); - it("supports break and continue as returning", () => { - valid({ + it("supports break and continue as returning", async () => { + await valid({ code: dedent` for(var i = 0; i < j; i++) { if (e === 1) { @@ -59,8 +59,8 @@ describe(name, () => { }); }); - it("supports throw as returning", () => { - valid({ + it("supports throw as returning", async () => { + await valid({ code: dedent` function foo(i) { if (i === 1) { @@ -72,8 +72,8 @@ describe(name, () => { }); }); - it("supports never as returning", () => { - valid({ + it("supports never as returning", async () => { + await valid({ code: dedent` declare function neverReturn(): never; function foo(i) { @@ -88,8 +88,8 @@ describe(name, () => { }); describe("ifExhaustive", () => { - it("else required", () => { - valid({ + it("else required", async () => { + await valid({ code: dedent` function foo(i) { if (i === 1) { @@ -102,7 +102,7 @@ describe(name, () => { options: [{ allowReturningBranches: "ifExhaustive" }], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` function foo(i) { if (i === 1) { @@ -113,11 +113,11 @@ describe(name, () => { options: [{ allowReturningBranches: "ifExhaustive" }], errors: ["incompleteIf"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("supports break and continue as returning", () => { - valid({ + it("supports break and continue as returning", async () => { + await valid({ code: dedent` for(var i = 0; i < j; i++) { if (e === 1) { @@ -134,8 +134,8 @@ describe(name, () => { }); describe("ignoreCodePattern", () => { - it("ignores matching conditionals", () => { - valid({ + it("ignores matching conditionals", async () => { + await valid({ code: dedent` if (import.meta.vitest) { const { it, expect } = import.meta.vitest; @@ -149,8 +149,8 @@ describe(name, () => { }); describe("switch statements", () => { - it("reports switch statements", () => { - const invalidResult = invalid({ + it("reports switch statements", async () => { + const invalidResult = await invalid({ code: dedent` switch(i) { case "a": @@ -162,14 +162,14 @@ describe(name, () => { `, errors: ["unexpectedSwitch"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); describe("options", () => { describe("allowReturningBranches", () => { describe("true", () => { - it("allows return", () => { - valid({ + it("allows return", async () => { + await valid({ code: dedent` function foo(i) { switch(i) { @@ -186,8 +186,8 @@ describe(name, () => { }); }); - it("supports break and continue as returning", () => { - valid({ + it("supports break and continue as returning", async () => { + await valid({ code: dedent` label: for(var i = 0; i < j; i++) { switch(i) { @@ -202,8 +202,8 @@ describe(name, () => { }); }); - it("supports throw as returning", () => { - valid({ + it("supports throw as returning", async () => { + await valid({ code: dedent` function foo(i) { switch(i) { @@ -218,8 +218,8 @@ describe(name, () => { }); }); - it("supports never as returning", () => { - valid({ + it("supports never as returning", async () => { + await valid({ code: dedent` declare function neverReturn(): never; function foo(i) { @@ -237,8 +237,8 @@ describe(name, () => { }); describe("ifExhaustive", () => { - it("requires default case", () => { - valid({ + it("requires default case", async () => { + await valid({ code: dedent` function foo(i) { switch(i) { @@ -254,7 +254,7 @@ describe(name, () => { options: [{ allowReturningBranches: "ifExhaustive" }], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` function foo(i) { switch(i) { @@ -268,11 +268,11 @@ describe(name, () => { options: [{ allowReturningBranches: "ifExhaustive" }], errors: ["incompleteSwitch"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("supports exhaustive type testing", () => { - valid({ + it("supports exhaustive type testing", async () => { + await valid({ code: dedent` type T = "a" | "b"; function foo(i: T) { diff --git a/tests/rules/no-expression-statements.test.ts b/tests/rules/no-expression-statements.test.ts index 66677e471..f2e6c08e7 100644 --- a/tests/rules/no-expression-statements.test.ts +++ b/tests/rules/no-expression-statements.test.ts @@ -14,35 +14,35 @@ describe(name, () => { configs: esLatestConfig, }); - it("reports expression statements", () => { - const invalidResult = invalid({ + it("reports expression statements", async () => { + const invalidResult = await invalid({ code: dedent` var x = []; x.push(1); `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report variable declarations", () => { - valid(dedent` + it("doesn't report variable declarations", async () => { + await valid(dedent` var x = []; let y = []; const z = []; `); }); - it("doesn't report directive prologues", () => { - valid(dedent` + it("doesn't report directive prologues", async () => { + await valid(dedent` "use strict"; "use server"; "use client"; `); }); - it("doesn't report yield", () => { - valid(dedent` + it("doesn't report yield", async () => { + await valid(dedent` export function* foo() { yield "hello"; return "world"; @@ -51,8 +51,8 @@ describe(name, () => { }); describe("options", () => { - it("ignoreCodePattern", () => { - valid({ + it("ignoreCodePattern", async () => { + await valid({ code: dedent` console.log("yo"); console.error("yo"); @@ -60,19 +60,19 @@ describe(name, () => { options: [{ ignoreCodePattern: "^console\\." }], }); - valid({ + await valid({ code: dedent` assert(1 !== 2); `, options: [{ ignoreCodePattern: "^assert" }], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: `console.trace();`, options: [{ ignoreCodePattern: "^console\\.log" }], errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); @@ -85,8 +85,8 @@ describe(name, () => { }); describe("options", () => { - it("ignoreVoid", () => { - valid({ + it("ignoreVoid", async () => { + await valid({ code: dedent` console.log("yo"); console.error("yo"); @@ -94,7 +94,7 @@ describe(name, () => { options: [{ ignoreVoid: true }], }); - valid({ + await valid({ code: dedent` function foo() { return Promise.resolve(); } foo(); @@ -103,8 +103,8 @@ describe(name, () => { }); }); - it("ignoreSelfReturning", () => { - valid({ + it("ignoreSelfReturning", async () => { + await valid({ code: dedent` function foo() { return this; } foo(); @@ -112,7 +112,7 @@ describe(name, () => { options: [{ ignoreSelfReturning: true }], }); - valid({ + await valid({ code: dedent` const foo = { bar() { return this; }}; foo.bar(); @@ -120,7 +120,7 @@ describe(name, () => { options: [{ ignoreSelfReturning: true }], }); - valid({ + await valid({ code: dedent` class Foo { bar() { return this; }}; const foo = new Foo(); @@ -129,7 +129,7 @@ describe(name, () => { options: [{ ignoreSelfReturning: true }], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` const foo = () => { return this; }; foo(); @@ -137,7 +137,7 @@ describe(name, () => { options: [{ ignoreSelfReturning: true }], errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); diff --git a/tests/rules/no-let.test.ts b/tests/rules/no-let.test.ts index 067476160..4956c3a49 100644 --- a/tests/rules/no-let.test.ts +++ b/tests/rules/no-let.test.ts @@ -14,8 +14,8 @@ describe(name, () => { configs: esLatestConfig, }); - it("should report let declarations", () => { - const invalidResult = invalid({ + it("should report let declarations", async () => { + const invalidResult = await invalid({ code: dedent` let x; @@ -26,13 +26,13 @@ describe(name, () => { `, errors: ["generic", "generic", "generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); describe("options", () => { describe("allowInFunctions", () => { - it("should not report let declarations in function declarations", () => { - valid({ + it("should not report let declarations in function declarations", async () => { + await valid({ code: dedent` function foo() { let x; @@ -42,7 +42,7 @@ describe(name, () => { options: [{ allowInFunctions: true }], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` let x; let y = 0; @@ -50,11 +50,11 @@ describe(name, () => { options: [{ allowInFunctions: true }], errors: ["generic", "generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("should not report let declarations in arrow function declarations", () => { - valid({ + it("should not report let declarations in arrow function declarations", async () => { + await valid({ code: dedent` const foo = () => { let x; @@ -64,7 +64,7 @@ describe(name, () => { options: [{ allowInFunctions: true }], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` let x; let y = 0; @@ -72,11 +72,11 @@ describe(name, () => { options: [{ allowInFunctions: true }], errors: ["generic", "generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("should not report let declarations in method declarations", () => { - valid({ + it("should not report let declarations in method declarations", async () => { + await valid({ code: dedent` class Foo { foo() { @@ -88,7 +88,7 @@ describe(name, () => { options: [{ allowInFunctions: true }], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` let x; let y = 0; @@ -96,18 +96,18 @@ describe(name, () => { options: [{ allowInFunctions: true }], errors: ["generic", "generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); describe("allowInForLoopInit", () => { - it("should not report let declarations in for loop init", () => { - valid({ + it("should not report let declarations in for loop init", async () => { + await valid({ code: `for (let x = 0; x < 1; x++);`, options: [{ allowInForLoopInit: true }], }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: dedent` let x; let y = 0; @@ -115,13 +115,13 @@ describe(name, () => { options: [{ allowInFunctions: true }], errors: ["generic", "generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); describe("ignoreIdentifierPattern", () => { - it("should not report let declarations with matching identifiers", () => { - valid({ + it("should not report let declarations with matching identifiers", async () => { + await valid({ code: dedent` let mutable; let mutableX @@ -129,7 +129,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^mutable" }], }); - valid({ + await valid({ code: dedent` let mutable = 0; let mutableX = 0 @@ -137,22 +137,22 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^mutable" }], }); - valid({ + await valid({ code: `for (let mutableX = 0; x < 1; x++);`, options: [{ ignoreIdentifierPattern: "^mutable" }], }); - valid({ + await valid({ code: `for (let mutableX in {});`, options: [{ ignoreIdentifierPattern: "^mutable" }], }); - valid({ + await valid({ code: `for (let mutableX of []);`, options: [{ ignoreIdentifierPattern: "^mutable" }], }); - valid({ + await valid({ code: dedent` function foo() { let mutableX; @@ -162,7 +162,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^mutable" }], }); - valid({ + await valid({ code: dedent` const foo = () => { let mutableX; @@ -172,7 +172,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^mutable" }], }); - valid({ + await valid({ code: dedent` class Foo { foo() { @@ -184,7 +184,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^mutable" }], }); - valid({ + await valid({ code: dedent` let Mutable; let xMutable; @@ -192,7 +192,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - valid({ + await valid({ code: dedent` let Mutable = 0; let xMutable = 0; @@ -200,22 +200,22 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - valid({ + await valid({ code: `for (let xMutable = 0; xMutable < 1; xMutable++);`, options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - valid({ + await valid({ code: `for (let xMutable in {});`, options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - valid({ + await valid({ code: `for (let xMutable of []);`, options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - valid({ + await valid({ code: dedent` function foo() { let xMutable; @@ -225,7 +225,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - valid({ + await valid({ code: dedent` const foo = () => { let xMutable; @@ -235,7 +235,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - valid({ + await valid({ code: dedent` class Foo { foo() { @@ -248,8 +248,8 @@ describe(name, () => { }); }); - it("should report non ignored let declarations", () => { - invalid({ + it("should report non ignored let declarations", async () => { + await invalid({ code: dedent` let immutable; let immutableX; @@ -257,7 +257,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^mutable" }], }); - invalid({ + await invalid({ code: dedent` let immutable = 0; let immutableX = 0; @@ -265,22 +265,22 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^mutable" }], }); - invalid({ + await invalid({ code: `for (let immutableX = 0; immutableX < 1; immutableX++);`, options: [{ ignoreIdentifierPattern: "^mutable" }], }); - invalid({ + await invalid({ code: `for (let immutableX in {});`, options: [{ ignoreIdentifierPattern: "^mutable" }], }); - invalid({ + await invalid({ code: `for (let immutableX of []);`, options: [{ ignoreIdentifierPattern: "^mutable" }], }); - invalid({ + await invalid({ code: dedent` function foo() { let immutableX; @@ -290,7 +290,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^mutable" }], }); - invalid({ + await invalid({ code: dedent` const foo = () => { let immutableX; @@ -300,7 +300,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^mutable" }], }); - invalid({ + await invalid({ code: dedent` class Foo { foo() { @@ -312,7 +312,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "^mutable" }], }); - invalid({ + await invalid({ code: dedent` let Immutable; let xImmutable; @@ -320,7 +320,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - invalid({ + await invalid({ code: dedent` let Immutable = 0; let xImmutable = 0; @@ -328,22 +328,22 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - invalid({ + await invalid({ code: `for (let xImmutable = 0; x < 1; x++);`, options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - invalid({ + await invalid({ code: `for (let xImmutable in {});`, options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - invalid({ + await invalid({ code: `for (let xImmutable of []);`, options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - invalid({ + await invalid({ code: dedent` function foo() { let xImmutable; @@ -353,7 +353,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - invalid({ + await invalid({ code: dedent` const foo = () => { let xImmutable; @@ -363,7 +363,7 @@ describe(name, () => { options: [{ ignoreIdentifierPattern: "Mutable$" }], }); - invalid({ + await invalid({ code: dedent` class Foo { foo() { diff --git a/tests/rules/no-loop-statements.test.ts b/tests/rules/no-loop-statements.test.ts index 0de32769a..7ba04b021 100644 --- a/tests/rules/no-loop-statements.test.ts +++ b/tests/rules/no-loop-statements.test.ts @@ -14,8 +14,8 @@ describe(name, () => { configs: esLatestConfig, }); - it("doesn't report non-issues", () => { - valid({ + it("doesn't report non-issues", async () => { + await valid({ code: dedent` if (true) { console.log("hello world"); @@ -24,8 +24,8 @@ describe(name, () => { }); }); - it("reports while loop statements", () => { - const invalidResult = invalid({ + it("reports while loop statements", async () => { + const invalidResult = await invalid({ code: dedent` while (true) { console.log("hello world"); @@ -33,11 +33,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports do while loop statements", () => { - const invalidResult = invalid({ + it("reports do while loop statements", async () => { + const invalidResult = await invalid({ code: dedent` do { console.log("hello world"); @@ -45,11 +45,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports for loop statements", () => { - const invalidResult = invalid({ + it("reports for loop statements", async () => { + const invalidResult = await invalid({ code: dedent` for (let i = 0; i < 10; i++) { console.log("hello world"); @@ -57,11 +57,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports for in loop statements", () => { - const invalidResult = invalid({ + it("reports for in loop statements", async () => { + const invalidResult = await invalid({ code: dedent` for (let i in []) { console.log("hello world"); @@ -69,11 +69,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports for of loop statements", () => { - const invalidResult = invalid({ + it("reports for of loop statements", async () => { + const invalidResult = await invalid({ code: dedent` for (let i of []) { console.log("hello world"); @@ -81,11 +81,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports for await loop statements", () => { - const invalidResult = invalid({ + it("reports for await loop statements", async () => { + const invalidResult = await invalid({ code: dedent` for await (let i of []) { console.log("hello world"); @@ -93,7 +93,7 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); diff --git a/tests/rules/no-mixed-types.test.ts b/tests/rules/no-mixed-types.test.ts index da2a46bc4..5d9622cd6 100644 --- a/tests/rules/no-mixed-types.test.ts +++ b/tests/rules/no-mixed-types.test.ts @@ -14,8 +14,8 @@ describe(name, () => { configs: typescriptConfig, }); - it("doesn't report non-issues", () => { - valid({ + it("doesn't report non-issues", async () => { + await valid({ code: dedent` type Foo = { bar: string; @@ -24,7 +24,7 @@ describe(name, () => { `, }); - valid({ + await valid({ code: dedent` interface Foo { bar: string; @@ -33,7 +33,7 @@ describe(name, () => { `, }); - valid({ + await valid({ code: dedent` type Foo = { bar: () =>string; @@ -42,7 +42,7 @@ describe(name, () => { `, }); - valid({ + await valid({ code: dedent` interface Foo { bar: () => string; @@ -52,8 +52,8 @@ describe(name, () => { }); }); - it("reports mixed types in interfaces", () => { - const invalidResult = invalid({ + it("reports mixed types in interfaces", async () => { + const invalidResult = await invalid({ code: dedent` interface Foo { bar: string; @@ -62,11 +62,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mixed types in type literals", () => { - const invalidResult = invalid({ + it("reports mixed types in type literals", async () => { + const invalidResult = await invalid({ code: dedent` type Foo = { bar: string; @@ -75,13 +75,13 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); describe("options", () => { describe("checkTypeLiterals", () => { - it("should report mixed types in type literals when enabled", () => { - const invalidResult = invalid({ + it("should report mixed types in type literals when enabled", async () => { + const invalidResult = await invalid({ code: dedent` type Foo = { bar: string; @@ -91,11 +91,11 @@ describe(name, () => { options: [{ checkTypeLiterals: true }], errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("should not report mixed types in type literals when disabled", () => { - valid({ + it("should not report mixed types in type literals when disabled", async () => { + await valid({ code: dedent` type Foo = { bar: string; @@ -108,19 +108,19 @@ describe(name, () => { }); describe("checkInterfaces", () => { - it("should report mixed types in interfaces when enabled", () => { - const invalidResult = invalid({ + it("should report mixed types in interfaces when enabled", async () => { + const invalidResult = await invalid({ code: dedent` interface Foo { bar: string; baz(): number; } `, options: [{ checkInterfaces: true }], errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("should not report mixed types in interfaces when disabled", () => { - valid({ + it("should not report mixed types in interfaces when disabled", async () => { + await valid({ code: dedent` interface Foo { bar: string; diff --git a/tests/rules/no-promise-reject.test.ts b/tests/rules/no-promise-reject.test.ts index b0f1da825..d0e4ed827 100644 --- a/tests/rules/no-promise-reject.test.ts +++ b/tests/rules/no-promise-reject.test.ts @@ -14,8 +14,8 @@ describe(name, () => { configs: esLatestConfig, }); - it("reports Promise.reject", () => { - const invalidResult = invalid({ + it("reports Promise.reject", async () => { + const invalidResult = await invalid({ code: dedent` function foo() { return Promise.reject("hello world"); @@ -23,11 +23,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports new Promise(reject)", () => { - const invalidResult = invalid({ + it("reports new Promise(reject)", async () => { + const invalidResult = await invalid({ code: dedent` function foo() { return new Promise((resolve, reject) => { @@ -37,11 +37,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports throw in async functions", () => { - const invalidResult = invalid({ + it("reports throw in async functions", async () => { + const invalidResult = await invalid({ code: dedent` async function foo() { throw new Error("hello world"); @@ -49,11 +49,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports throw in try without catch in async functions", () => { - const invalidResult = invalid({ + it("reports throw in try without catch in async functions", async () => { + const invalidResult = await invalid({ code: dedent` async function foo() { try { @@ -65,11 +65,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report Promise.resolve", () => { - valid({ + it("doesn't report Promise.resolve", async () => { + await valid({ code: dedent` function foo() { return Promise.resolve("hello world"); @@ -78,8 +78,8 @@ describe(name, () => { }); }); - it("doesn't report new Promise(resolve)", () => { - valid({ + it("doesn't report new Promise(resolve)", async () => { + await valid({ code: dedent` function foo() { return new Promise((resolve) => { @@ -90,8 +90,8 @@ describe(name, () => { }); }); - it("doesn't report throw in try in async functions", () => { - valid({ + it("doesn't report throw in try in async functions", async () => { + await valid({ code: dedent` async function foo() { try { diff --git a/tests/rules/no-return-void.test.ts b/tests/rules/no-return-void.test.ts index 7434b16f6..bda5e20d2 100644 --- a/tests/rules/no-return-void.test.ts +++ b/tests/rules/no-return-void.test.ts @@ -14,8 +14,8 @@ describe(name, () => { configs: typescriptConfig, }); - it("doesn't report non-void returning functions", () => { - valid({ + it("doesn't report non-void returning functions", async () => { + await valid({ code: dedent` function foo(bar: number): number { return bar + 1; @@ -24,8 +24,8 @@ describe(name, () => { }); }); - it("doesn't report non-void returning functions with inferred return type", () => { - valid({ + it("doesn't report non-void returning functions with inferred return type", async () => { + await valid({ code: dedent` function foo(bar: number) { return bar + 1; @@ -34,8 +34,8 @@ describe(name, () => { }); }); - it("reports void returning functions", () => { - const invalidResult = invalid({ + it("reports void returning functions", async () => { + const invalidResult = await invalid({ code: dedent` function foo(bar: number): void { console.log(bar); @@ -43,11 +43,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports void returning functions with inferred return type", () => { - const invalidResult = invalid({ + it("reports void returning functions with inferred return type", async () => { + const invalidResult = await invalid({ code: dedent` function foo(bar: number) { console.log(bar); @@ -55,13 +55,13 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); describe("options", () => { describe("allowNull", () => { - it("doesn't report null returning functions when allowed", () => { - valid({ + it("doesn't report null returning functions when allowed", async () => { + await valid({ code: dedent` function foo(bar: number): null { return null; @@ -71,8 +71,8 @@ describe(name, () => { }); }); - it("doesn't report null returning functions with inferred return type when allowed", () => { - valid({ + it("doesn't report null returning functions with inferred return type when allowed", async () => { + await valid({ code: dedent` function foo(bar: number) { return null; @@ -82,8 +82,8 @@ describe(name, () => { }); }); - it("reports null returning functions when disallowed", () => { - const invalidResult = invalid({ + it("reports null returning functions when disallowed", async () => { + const invalidResult = await invalid({ code: dedent` function foo(bar: number): null { return null; @@ -92,11 +92,11 @@ describe(name, () => { options: [{ allowNull: false }], errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports null returning functions with inferred return type when disallowed", () => { - const invalidResult = invalid({ + it("reports null returning functions with inferred return type when disallowed", async () => { + const invalidResult = await invalid({ code: dedent` function foo(bar: number) { return null; @@ -105,13 +105,13 @@ describe(name, () => { options: [{ allowNull: false }], errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); describe("allowUndefined", () => { - it("doesn't report undefined returning functions when allowed", () => { - valid({ + it("doesn't report undefined returning functions when allowed", async () => { + await valid({ code: dedent` function foo(bar: number): undefined { return undefined; @@ -121,8 +121,8 @@ describe(name, () => { }); }); - it("doesn't report undefined returning functions with inferred return type when allowed", () => { - valid({ + it("doesn't report undefined returning functions with inferred return type when allowed", async () => { + await valid({ code: dedent` function foo(bar: number) { return undefined; @@ -132,8 +132,8 @@ describe(name, () => { }); }); - it("reports undefined returning functions when disallowed", () => { - const invalidResult = invalid({ + it("reports undefined returning functions when disallowed", async () => { + const invalidResult = await invalid({ code: dedent` function foo(bar: number): undefined { return undefined; @@ -142,11 +142,11 @@ describe(name, () => { options: [{ allowUndefined: false }], errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports undefined returning functions with inferred return type when disallowed", () => { - const invalidResult = invalid({ + it("reports undefined returning functions with inferred return type when disallowed", async () => { + const invalidResult = await invalid({ code: dedent` function foo(bar: number) { return undefined; @@ -155,13 +155,13 @@ describe(name, () => { options: [{ allowUndefined: false }], errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); describe("ignoreInferredTypes", () => { - it("doesn't report inferred void return type", () => { - valid({ + it("doesn't report inferred void return type", async () => { + await valid({ code: dedent` function foo(bar: number) { console.log(bar); @@ -171,8 +171,8 @@ describe(name, () => { }); }); - it("doesn't report inferred null return type", () => { - valid({ + it("doesn't report inferred null return type", async () => { + await valid({ code: dedent` function foo(bar: number) { return null; @@ -182,8 +182,8 @@ describe(name, () => { }); }); - it("doesn't report inferred undefined return type", () => { - valid({ + it("doesn't report inferred undefined return type", async () => { + await valid({ code: dedent` function foo(bar: number) { return undefined; diff --git a/tests/rules/no-this-expressions.test.ts b/tests/rules/no-this-expressions.test.ts index 3d5ab9de8..4d6720f8f 100644 --- a/tests/rules/no-this-expressions.test.ts +++ b/tests/rules/no-this-expressions.test.ts @@ -14,18 +14,20 @@ describe(name, () => { configs: esLatestConfig, }); - it("reports this expressions", () => { + it("reports this expressions", async () => { const code = dedent` function foo() { this.bar(); } `; - expect(invalid(code)).toMatchSnapshot(); + const { result } = await invalid(code); + + expect(result).toMatchSnapshot(); }); - it("doesn't report non-issues", () => { - valid(dedent` + it("doesn't report non-issues", async () => { + await valid(dedent` function foo() { bar(); } diff --git a/tests/rules/no-throw-statements.test.ts b/tests/rules/no-throw-statements.test.ts index ff8e9dbe1..a44189c45 100644 --- a/tests/rules/no-throw-statements.test.ts +++ b/tests/rules/no-throw-statements.test.ts @@ -14,16 +14,16 @@ describe(name, () => { configs: esLatestConfig, }); - it("doesn't report non-issues", () => { - valid(dedent` + it("doesn't report non-issues", async () => { + await valid(dedent` function foo() { bar(); } `); }); - it("reports throw statements of strings", () => { - const invalidResult = invalid({ + it("reports throw statements of strings", async () => { + const invalidResult = await invalid({ code: dedent` function foo() { throw 'error'; @@ -31,11 +31,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports throw statements of Errors", () => { - const invalidResult = invalid({ + it("reports throw statements of Errors", async () => { + const invalidResult = await invalid({ code: dedent` function foo() { throw new Error(); @@ -43,11 +43,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports throw statements in async functions", () => { - const invalidResult = invalid({ + it("reports throw statements in async functions", async () => { + const invalidResult = await invalid({ code: dedent` async function foo() { throw new Error(); @@ -55,13 +55,13 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); describe("options", () => { describe("allowToRejectPromises", () => { - it("doesn't report throw statements in async functions", () => { - valid({ + it("doesn't report throw statements in async functions", async () => { + await valid({ code: dedent` async function foo() { throw new Error(); @@ -71,8 +71,8 @@ describe(name, () => { }); }); - it("doesn't report throw statements in try without catch in async functions", () => { - valid({ + it("doesn't report throw statements in try without catch in async functions", async () => { + await valid({ code: dedent` async function foo() { try { @@ -86,8 +86,8 @@ describe(name, () => { }); }); - it("reports throw statements in try with catch in async functions", () => { - const invalidResult = invalid({ + it("reports throw statements in try with catch in async functions", async () => { + const invalidResult = await invalid({ code: dedent` async function foo() { try { @@ -100,11 +100,11 @@ describe(name, () => { errors: ["generic"], options: [{ allowToRejectPromises: true }], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports throw statements in functions nested in async functions", () => { - const invalidResult = invalid({ + it("reports throw statements in functions nested in async functions", async () => { + const invalidResult = await invalid({ code: dedent` async function foo() { function bar() { @@ -115,7 +115,7 @@ describe(name, () => { errors: ["generic"], options: [{ allowToRejectPromises: true }], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); }); @@ -130,8 +130,8 @@ describe(name, () => { describe("options", () => { describe("allowToRejectPromises", () => { - it("doesn't report throw statements in promise then handlers", () => { - valid({ + it("doesn't report throw statements in promise then handlers", async () => { + await valid({ code: dedent` function foo() { Promise.resolve().then(() => { @@ -143,8 +143,8 @@ describe(name, () => { }); }); - it("doesn't report throw statements in promise catch handlers", () => { - valid({ + it("doesn't report throw statements in promise catch handlers", async () => { + await valid({ code: dedent` function foo() { Promise.resolve().catch(() => { @@ -156,8 +156,8 @@ describe(name, () => { }); }); - it("doesn't report throw statements in promise handlers", () => { - valid({ + it("doesn't report throw statements in promise handlers", async () => { + await valid({ code: dedent` function foo() { Promise.resolve().then(() => { diff --git a/tests/rules/no-try-statements.test.ts b/tests/rules/no-try-statements.test.ts index 0178f94f4..f41ace235 100644 --- a/tests/rules/no-try-statements.test.ts +++ b/tests/rules/no-try-statements.test.ts @@ -14,14 +14,14 @@ describe(name, () => { configs: esLatestConfig, }); - it("doesn't report non-issues", () => { - valid(dedent` + it("doesn't report non-issues", async () => { + await valid(dedent` foo(); `); }); - it("reports try statements", () => { - const invalidResult = invalid({ + it("reports try statements", async () => { + const invalidResult = await invalid({ code: dedent` try { foo(); @@ -31,13 +31,13 @@ describe(name, () => { `, errors: ["catch"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); describe("options", () => { describe("allowCatch", () => { - it("doesn't report try statements with catch", () => { - valid({ + it("doesn't report try statements with catch", async () => { + await valid({ code: dedent` try { foo(); @@ -49,8 +49,8 @@ describe(name, () => { }); }); - it("reports try statements with catch and finally", () => { - const invalidResult = invalid({ + it("reports try statements with catch and finally", async () => { + const invalidResult = await invalid({ code: dedent` try { foo(); @@ -63,13 +63,13 @@ describe(name, () => { errors: ["finally"], options: [{ allowCatch: true, allowFinally: false }], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); describe("allowFinally", () => { - it("doesn't report try statements with finally", () => { - valid({ + it("doesn't report try statements with finally", async () => { + await valid({ code: dedent` try { foo(); @@ -81,8 +81,8 @@ describe(name, () => { }); }); - it("reports try statements with catch and finally", () => { - const invalidResult = invalid({ + it("reports try statements with catch and finally", async () => { + const invalidResult = await invalid({ code: dedent` try { foo(); @@ -95,12 +95,12 @@ describe(name, () => { errors: ["catch"], options: [{ allowCatch: false, allowFinally: true }], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); - it("doesn't report try statements with catch and finally if both are allowed", () => { - valid({ + it("doesn't report try statements with catch and finally if both are allowed", async () => { + await valid({ code: dedent` try { foo(); diff --git a/tests/rules/prefer-immutable-types/__snapshots__/parameters.test.ts.snap b/tests/rules/prefer-immutable-types/__snapshots__/parameters.test.ts.snap index d7d03b8ad..86666f606 100644 --- a/tests/rules/prefer-immutable-types/__snapshots__/parameters.test.ts.snap +++ b/tests/rules/prefer-immutable-types/__snapshots__/parameters.test.ts.snap @@ -1,627 +1,1360 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`prefer-immutable-types > typescript > allows for user fixes 1`] = ` -[ - { - "column": 14, - "endColumn": 32, - "endLine": 1, - "line": 1, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 14, - "endColumn": 27, - "endLine": 2, - "line": 2, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 14, - "endColumn": 30, - "endLine": 3, - "fix": { - "range": [ - 85, - 96, - ], - "text": "Readonly>", - }, - "line": 3, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 14, - "endColumn": 38, - "endLine": 4, - "fix": { - "range": [ - 119, - 138, - ], - "text": "Readonly>", - }, - "line": 4, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 14, - "endColumn": 40, - "endLine": 5, - "line": 5, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 14, - "endColumn": 36, - "endLine": 6, - "line": 6, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 14, - "endColumn": 38, - "endLine": 7, - "fix": { - "range": [ - 245, - 264, +{ + "fixed": true, + "messages": [ + { + "column": 14, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 27, + "endLine": 2, + "line": 2, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 30, + "endLine": 3, + "fix": { + "range": [ + 85, + 96, + ], + "text": "Readonly>", + }, + "line": 3, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 38, + "endLine": 4, + "fix": { + "range": [ + 119, + 138, + ], + "text": "Readonly>", + }, + "line": 4, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 40, + "endLine": 5, + "line": 5, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 36, + "endLine": 6, + "line": 6, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 38, + "endLine": 7, + "fix": { + "range": [ + 245, + 264, + ], + "text": "Readonly>", + }, + "line": 7, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 46, + "endLine": 8, + "fix": { + "range": [ + 287, + 314, + ], + "text": "Readonly>", + }, + "line": 8, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(arg: Array) {} +function foo(arg: string[]) {} +function foo(arg: Readonly>) {} +function foo(arg: Readonly>) {} +function foo(arg: ReadonlyArray) {} +function foo(arg: readonly string[]) {} +function foo(arg: Readonly>) {} +function foo(arg: Readonly>) {}", + "steps": [ + { + "fixed": true, + "messages": [ + { + "column": 14, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 27, + "endLine": 2, + "line": 2, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 40, + "endLine": 5, + "line": 5, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 36, + "endLine": 6, + "line": 6, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, ], - "text": "Readonly>", - }, - "line": 7, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 14, - "endColumn": 46, - "endLine": 8, - "fix": { - "range": [ - 287, - 314, + "output": "function foo(arg: Array) {} +function foo(arg: string[]) {} +function foo(arg: Readonly>) {} +function foo(arg: Readonly>) {} +function foo(arg: ReadonlyArray) {} +function foo(arg: readonly string[]) {} +function foo(arg: Readonly>) {} +function foo(arg: Readonly>) {}", + }, + { + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 27, + "endLine": 2, + "line": 2, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 40, + "endLine": 5, + "line": 5, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 14, + "endColumn": 36, + "endLine": 6, + "line": 6, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, ], - "text": "Readonly>", - }, - "line": 8, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] + "output": "function foo(arg: Array) {} +function foo(arg: string[]) {} +function foo(arg: Readonly>) {} +function foo(arg: Readonly>) {} +function foo(arg: ReadonlyArray) {} +function foo(arg: readonly string[]) {} +function foo(arg: Readonly>) {} +function foo(arg: Readonly>) {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > allows for user suggestions 1`] = ` -[ - { - "column": 14, - "endColumn": 35, - "endLine": 1, - "line": 1, - "message": "Parameter should have an immutability of at least "ReadonlyDeep" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Replace with: ReadonlyDeep<{ foo: string }>", - }, - "desc": "Replace with: ReadonlyDeep<{ foo: string }>", - "fix": { - "range": [ - 19, - 34, +{ + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 35, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "ReadonlyDeep" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Replace with: ReadonlyDeep<{ foo: string }>", + }, + "desc": "Replace with: ReadonlyDeep<{ foo: string }>", + "fix": { + "range": [ + 19, + 34, + ], + "text": "ReadonlyDeep<{ foo: string }>", + }, + "messageId": "userDefined", + }, + ], + }, + ], + "output": "function foo(arg1: { foo: string }) {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 35, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "ReadonlyDeep" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Replace with: ReadonlyDeep<{ foo: string }>", + }, + "desc": "Replace with: ReadonlyDeep<{ foo: string }>", + "fix": { + "range": [ + 19, + 34, + ], + "text": "ReadonlyDeep<{ foo: string }>", + }, + "messageId": "userDefined", + }, ], - "text": "ReadonlyDeep<{ foo: string }>", }, - "messageId": "userDefined", - }, - ], - }, -] + ], + "output": "function foo(arg1: { foo: string }) {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > doesn't report type assertion parameters 1`] = ` -[ - { - "column": 46, - "endColumn": 76, - "endLine": 1, - "line": 1, - "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 51, - 75, +{ + "fixed": false, + "messages": [ + { + "column": 46, + "endColumn": 76, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 51, + 75, + ], + "text": "Readonly<{ foo: string | number }>", + }, + "messageId": "userDefined", + }, + ], + }, + ], + "output": "function foo(arg0: { foo: string | number }, arg1: { foo: string | number }): arg0 is { foo: number } {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 46, + "endColumn": 76, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 51, + 75, + ], + "text": "Readonly<{ foo: string | number }>", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly<{ foo: string | number }>", }, - "messageId": "userDefined", - }, - ], - }, -] + ], + "output": "function foo(arg0: { foo: string | number }, arg1: { foo: string | number }): arg0 is { foo: number } {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports mutable class parameter properties 1`] = ` -[ - { - "column": 5, - "endColumn": 30, - "endLine": 3, - "line": 3, - "message": "Property should have a readonly modifier.", - "messageId": "propertyModifier", - "nodeType": "TSParameterProperty", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "desc": "Add readonly modifier.", - "fix": { - "range": [ - 41, - 41, +{ + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 30, + "endLine": 3, + "line": 3, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 41, + 41, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, + ], + }, + { + "column": 5, + "endColumn": 36, + "endLine": 4, + "line": 4, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 75, + 75, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, + ], + }, + { + "column": 5, + "endColumn": 32, + "endLine": 5, + "line": 5, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 110, + 110, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, + ], + }, + ], + "output": "class Klass { + constructor ( + public publicProp: string, + protected protectedProp: string, + private privateProp: string, +) { } +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 30, + "endLine": 3, + "line": 3, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 41, + 41, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, ], - "text": "readonly ", }, - "messageId": "propertyModifierSuggestion", - }, - ], - }, - { - "column": 5, - "endColumn": 36, - "endLine": 4, - "line": 4, - "message": "Property should have a readonly modifier.", - "messageId": "propertyModifier", - "nodeType": "TSParameterProperty", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "desc": "Add readonly modifier.", - "fix": { - "range": [ - 75, - 75, + { + "column": 5, + "endColumn": 36, + "endLine": 4, + "line": 4, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 75, + 75, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, ], - "text": "readonly ", }, - "messageId": "propertyModifierSuggestion", - }, - ], - }, - { - "column": 5, - "endColumn": 32, - "endLine": 5, - "line": 5, - "message": "Property should have a readonly modifier.", - "messageId": "propertyModifier", - "nodeType": "TSParameterProperty", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "desc": "Add readonly modifier.", - "fix": { - "range": [ - 110, - 110, + { + "column": 5, + "endColumn": 32, + "endLine": 5, + "line": 5, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 110, + 110, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, ], - "text": "readonly ", }, - "messageId": "propertyModifierSuggestion", - }, - ], - }, -] + ], + "output": "class Klass { + constructor ( + public publicProp: string, + protected protectedProp: string, + private privateProp: string, +) { } +}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports mutable class parameter properties and suggests a fix for ReadonlyShallow 1`] = ` -[ - { - "column": 5, - "endColumn": 30, - "endLine": 3, - "line": 3, - "message": "Property should have a readonly modifier.", - "messageId": "propertyModifier", - "nodeType": "TSParameterProperty", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "desc": "Add readonly modifier.", - "fix": { - "range": [ - 41, - 41, +{ + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 30, + "endLine": 3, + "line": 3, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 41, + 41, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, + ], + }, + { + "column": 5, + "endColumn": 36, + "endLine": 4, + "line": 4, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 75, + 75, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, + ], + }, + { + "column": 5, + "endColumn": 32, + "endLine": 5, + "line": 5, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 110, + 110, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, + ], + }, + ], + "output": "class Klass { + constructor ( + public publicProp: string, + protected protectedProp: string, + private privateProp: string, +) { } +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 5, + "endColumn": 30, + "endLine": 3, + "line": 3, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 41, + 41, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, ], - "text": "readonly ", }, - "messageId": "propertyModifierSuggestion", - }, - ], - }, - { - "column": 5, - "endColumn": 36, - "endLine": 4, - "line": 4, - "message": "Property should have a readonly modifier.", - "messageId": "propertyModifier", - "nodeType": "TSParameterProperty", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "desc": "Add readonly modifier.", - "fix": { - "range": [ - 75, - 75, + { + "column": 5, + "endColumn": 36, + "endLine": 4, + "line": 4, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 75, + 75, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, ], - "text": "readonly ", }, - "messageId": "propertyModifierSuggestion", - }, - ], - }, - { - "column": 5, - "endColumn": 32, - "endLine": 5, - "line": 5, - "message": "Property should have a readonly modifier.", - "messageId": "propertyModifier", - "nodeType": "TSParameterProperty", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "desc": "Add readonly modifier.", - "fix": { - "range": [ - 110, - 110, + { + "column": 5, + "endColumn": 32, + "endLine": 5, + "line": 5, + "message": "Property should have a readonly modifier.", + "messageId": "propertyModifier", + "nodeType": "TSParameterProperty", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "desc": "Add readonly modifier.", + "fix": { + "range": [ + 110, + 110, + ], + "text": "readonly ", + }, + "messageId": "propertyModifierSuggestion", + }, ], - "text": "readonly ", }, - "messageId": "propertyModifierSuggestion", - }, - ], - }, -] + ], + "output": "class Klass { + constructor ( + public publicProp: string, + protected protectedProp: string, + private privateProp: string, +) { } +}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports mutable records parameters 1`] = ` -[ - { - "column": 14, - "endColumn": 35, - "endLine": 1, - "line": 1, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 37, - "endColumn": 58, - "endLine": 1, - "line": 1, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 35, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 37, + "endColumn": 58, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(arg1: { foo: string }, arg2: { foo: number }) {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 35, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 37, + "endColumn": 58, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(arg1: { foo: string }, arg2: { foo: number }) {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports mutable records parameters and suggests a fix for ReadonlyShallow 1`] = ` -[ - { - "column": 14, - "endColumn": 35, - "endLine": 1, - "line": 1, - "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 19, - 34, +{ + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 35, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 19, + 34, + ], + "text": "Readonly<{ foo: string }>", + }, + "messageId": "userDefined", + }, + ], + }, + { + "column": 37, + "endColumn": 58, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 42, + 57, + ], + "text": "Readonly<{ foo: number }>", + }, + "messageId": "userDefined", + }, + ], + }, + ], + "output": "function foo(arg1: { foo: string }, arg2: { foo: number }) {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 35, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 19, + 34, + ], + "text": "Readonly<{ foo: string }>", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly<{ foo: string }>", }, - "messageId": "userDefined", - }, - ], - }, - { - "column": 37, - "endColumn": 58, - "endLine": 1, - "line": 1, - "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 42, - 57, + { + "column": 37, + "endColumn": 58, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 42, + 57, + ], + "text": "Readonly<{ foo: number }>", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly<{ foo: number }>", }, - "messageId": "userDefined", - }, - ], - }, -] + ], + "output": "function foo(arg1: { foo: string }, arg2: { foo: number }) {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports non-immutable map parameters 1`] = ` -[ - { - "column": 14, - "endColumn": 46, - "endLine": 1, - "line": 1, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 46, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(arg: ReadonlyMap) {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 46, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(arg: ReadonlyMap) {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports non-immutable set parameters 1`] = ` -[ - { - "column": 14, - "endColumn": 38, - "endLine": 1, - "line": 1, - "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 38, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(arg: ReadonlySet) {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 38, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(arg: ReadonlySet) {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > suggest multiple fixes for collections for ReadonlyShallow 1`] = ` -[ - { - "column": 14, - "endColumn": 32, - "endLine": 1, - "line": 1, - "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Use ReadonlyArray instead of Array.", - }, - "desc": "Use ReadonlyArray instead of Array.", - "fix": { - "range": [ - 18, - 31, - ], - "text": "ReadonlyArray", +{ + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyArray instead of Array.", + }, + "desc": "Use ReadonlyArray instead of Array.", + "fix": { + "range": [ + 18, + 31, + ], + "text": "ReadonlyArray", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 18, - 31, - ], - "text": "Readonly>", + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 18, + 31, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - ], - }, - { - "column": 14, - "endColumn": 27, - "endLine": 2, - "line": 2, - "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Prepend with readonly.", - }, - "desc": "Prepend with readonly.", - "fix": { - "range": [ - 54, - 62, - ], - "text": "readonly string[]", + ], + }, + { + "column": 14, + "endColumn": 27, + "endLine": 2, + "line": 2, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Prepend with readonly.", + }, + "desc": "Prepend with readonly.", + "fix": { + "range": [ + 54, + 62, + ], + "text": "readonly string[]", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 54, - 62, - ], - "text": "Readonly", + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 54, + 62, + ], + "text": "Readonly", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - ], - }, - { - "column": 14, - "endColumn": 30, - "endLine": 3, - "line": 3, - "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Use ReadonlySet instead of Set.", - }, - "desc": "Use ReadonlySet instead of Set.", - "fix": { - "range": [ - 85, - 96, + ], + }, + { + "column": 14, + "endColumn": 30, + "endLine": 3, + "line": 3, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlySet instead of Set.", + }, + "desc": "Use ReadonlySet instead of Set.", + "fix": { + "range": [ + 85, + 96, + ], + "text": "ReadonlySet", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 85, + 96, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, + ], + }, + { + "column": 14, + "endColumn": 38, + "endLine": 4, + "line": 4, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyMap instead of Map.", + }, + "desc": "Use ReadonlyMap instead of Map.", + "fix": { + "range": [ + 119, + 138, + ], + "text": "ReadonlyMap", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 119, + 138, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, + ], + }, + ], + "output": "function foo(arg: Array) {} +function foo(arg: string[]) {} +function foo(arg: Set) {} +function foo(arg: Map) {} +function foo(arg: ReadonlyArray) {} +function foo(arg: readonly string[]) {} +function foo(arg: ReadonlySet) {} +function foo(arg: ReadonlyMap) {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 14, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyArray instead of Array.", + }, + "desc": "Use ReadonlyArray instead of Array.", + "fix": { + "range": [ + 18, + 31, + ], + "text": "ReadonlyArray", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 18, + 31, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, ], - "text": "ReadonlySet", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 85, - 96, + { + "column": 14, + "endColumn": 27, + "endLine": 2, + "line": 2, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Prepend with readonly.", + }, + "desc": "Prepend with readonly.", + "fix": { + "range": [ + 54, + 62, + ], + "text": "readonly string[]", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 54, + 62, + ], + "text": "Readonly", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly>", }, - "messageId": "userDefined", - }, - ], - }, - { - "column": 14, - "endColumn": 38, - "endLine": 4, - "line": 4, - "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "parameter", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Use ReadonlyMap instead of Map.", - }, - "desc": "Use ReadonlyMap instead of Map.", - "fix": { - "range": [ - 119, - 138, + { + "column": 14, + "endColumn": 30, + "endLine": 3, + "line": 3, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlySet instead of Set.", + }, + "desc": "Use ReadonlySet instead of Set.", + "fix": { + "range": [ + 85, + 96, + ], + "text": "ReadonlySet", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 85, + 96, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, ], - "text": "ReadonlyMap", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 119, - 138, + { + "column": 14, + "endColumn": 38, + "endLine": 4, + "line": 4, + "message": "Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "parameter", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyMap instead of Map.", + }, + "desc": "Use ReadonlyMap instead of Map.", + "fix": { + "range": [ + 119, + 138, + ], + "text": "ReadonlyMap", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 119, + 138, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly>", }, - "messageId": "userDefined", - }, - ], - }, -] + ], + "output": "function foo(arg: Array) {} +function foo(arg: string[]) {} +function foo(arg: Set) {} +function foo(arg: Map) {} +function foo(arg: ReadonlyArray) {} +function foo(arg: readonly string[]) {} +function foo(arg: ReadonlySet) {} +function foo(arg: ReadonlyMap) {}", + }, + ], +} `; diff --git a/tests/rules/prefer-immutable-types/__snapshots__/return-types.test.ts.snap b/tests/rules/prefer-immutable-types/__snapshots__/return-types.test.ts.snap index 3bbf5e213..7fc39f9d9 100644 --- a/tests/rules/prefer-immutable-types/__snapshots__/return-types.test.ts.snap +++ b/tests/rules/prefer-immutable-types/__snapshots__/return-types.test.ts.snap @@ -1,403 +1,867 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`prefer-immutable-types > typescript > allows for user fixes 1`] = ` -[ - { - "column": 15, - "endColumn": 30, - "endLine": 1, - "line": 1, - "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 15, - "endColumn": 25, - "endLine": 2, - "line": 2, - "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 15, - "endColumn": 28, - "endLine": 3, - "fix": { - "range": [ - 77, - 88, - ], - "text": "Readonly>", +{ + "fixed": true, + "messages": [ + { + "column": 15, + "endColumn": 30, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, }, - "line": 3, - "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 15, - "endColumn": 36, - "endLine": 4, - "fix": { - "range": [ - 108, - 127, - ], - "text": "Readonly>", + { + "column": 15, + "endColumn": 25, + "endLine": 2, + "line": 2, + "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, }, - "line": 4, - "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 15, - "endColumn": 38, - "endLine": 5, - "line": 5, - "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 15, - "endColumn": 34, - "endLine": 6, - "line": 6, - "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 15, - "endColumn": 36, - "endLine": 7, - "fix": { - "range": [ - 225, - 244, + { + "column": 15, + "endColumn": 28, + "endLine": 3, + "fix": { + "range": [ + 77, + 88, + ], + "text": "Readonly>", + }, + "line": 3, + "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 36, + "endLine": 4, + "fix": { + "range": [ + 108, + 127, + ], + "text": "Readonly>", + }, + "line": 4, + "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 38, + "endLine": 5, + "line": 5, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 34, + "endLine": 6, + "line": 6, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 36, + "endLine": 7, + "fix": { + "range": [ + 225, + 244, + ], + "text": "Readonly>", + }, + "line": 7, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 44, + "endLine": 8, + "fix": { + "range": [ + 264, + 291, + ], + "text": "Readonly>", + }, + "line": 8, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(): Array {} +function foo(): string[] {} +function foo(): Readonly> {} +function foo(): Readonly> {} +function foo(): ReadonlyArray {} +function foo(): readonly string[] {} +function foo(): Readonly> {} +function foo(): Readonly> {}", + "steps": [ + { + "fixed": true, + "messages": [ + { + "column": 15, + "endColumn": 30, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 25, + "endLine": 2, + "line": 2, + "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 38, + "endLine": 5, + "line": 5, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 34, + "endLine": 6, + "line": 6, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, ], - "text": "Readonly>", + "output": "function foo(): Array {} +function foo(): string[] {} +function foo(): Readonly> {} +function foo(): Readonly> {} +function foo(): ReadonlyArray {} +function foo(): readonly string[] {} +function foo(): Readonly> {} +function foo(): Readonly> {}", }, - "line": 7, - "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 15, - "endColumn": 44, - "endLine": 8, - "fix": { - "range": [ - 264, - 291, + { + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 30, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 25, + "endLine": 2, + "line": 2, + "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 38, + "endLine": 5, + "line": 5, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 15, + "endColumn": 34, + "endLine": 6, + "line": 6, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, ], - "text": "Readonly>", + "output": "function foo(): Array {} +function foo(): string[] {} +function foo(): Readonly> {} +function foo(): Readonly> {} +function foo(): ReadonlyArray {} +function foo(): readonly string[] {} +function foo(): Readonly> {} +function foo(): Readonly> {}", }, - "line": 8, - "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] + ], +} `; exports[`prefer-immutable-types > typescript > allows for user suggestions 1`] = ` -[ - { - "column": 15, - "endColumn": 32, - "endLine": 1, - "line": 1, - "message": "Return type should have an immutability of at least "ReadonlyDeep" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Replace with: ReadonlyDeep<{ foo: string }>", +{ + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "ReadonlyDeep" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Replace with: ReadonlyDeep<{ foo: string }>", + }, + "desc": "Replace with: ReadonlyDeep<{ foo: string }>", + "fix": { + "range": [ + 16, + 31, + ], + "text": "ReadonlyDeep<{ foo: string }>", + }, + "messageId": "userDefined", }, - "desc": "Replace with: ReadonlyDeep<{ foo: string }>", - "fix": { - "range": [ - 16, - 31, + ], + }, + ], + "output": "function foo(): { foo: string } {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "ReadonlyDeep" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Replace with: ReadonlyDeep<{ foo: string }>", + }, + "desc": "Replace with: ReadonlyDeep<{ foo: string }>", + "fix": { + "range": [ + 16, + 31, + ], + "text": "ReadonlyDeep<{ foo: string }>", + }, + "messageId": "userDefined", + }, ], - "text": "ReadonlyDeep<{ foo: string }>", }, - "messageId": "userDefined", - }, - ], - }, -] + ], + "output": "function foo(): { foo: string } {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports mutable records return types 1`] = ` -[ - { - "column": 15, - "endColumn": 32, - "endLine": 1, - "line": 1, - "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(): { foo: string } {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(): { foo: string } {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports mutable records return types and suggests a fix for ReadonlyShallow 1`] = ` -[ - { - "column": 15, - "endColumn": 32, - "endLine": 1, - "line": 1, - "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Surround with Readonly.", +{ + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 16, + 31, + ], + "text": "Readonly<{ foo: string }>", + }, + "messageId": "userDefined", }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 16, - 31, + ], + }, + ], + "output": "function foo(): { foo: string } {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 32, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 16, + 31, + ], + "text": "Readonly<{ foo: string }>", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly<{ foo: string }>", }, - "messageId": "userDefined", - }, - ], - }, -] + ], + "output": "function foo(): { foo: string } {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports non-immutable map return types 1`] = ` -[ - { - "column": 15, - "endColumn": 44, - "endLine": 1, - "line": 1, - "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 44, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(): ReadonlyMap {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 44, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(): ReadonlyMap {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports non-immutable set return types 1`] = ` -[ - { - "column": 15, - "endColumn": 36, - "endLine": 1, - "line": 1, - "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 36, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(): ReadonlySet {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 36, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + ], + "output": "function foo(): ReadonlySet {}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > suggest multiple fixes for collections for ReadonlyShallow 1`] = ` -[ - { - "column": 15, - "endColumn": 30, - "endLine": 1, - "line": 1, - "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Use ReadonlyArray instead of Array.", +{ + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 30, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyArray instead of Array.", + }, + "desc": "Use ReadonlyArray instead of Array.", + "fix": { + "range": [ + 16, + 29, + ], + "text": "ReadonlyArray", + }, + "messageId": "userDefined", }, - "desc": "Use ReadonlyArray instead of Array.", - "fix": { - "range": [ - 16, - 29, - ], - "text": "ReadonlyArray", + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 16, + 29, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 16, - 29, - ], - "text": "Readonly>", + ], + }, + { + "column": 15, + "endColumn": 25, + "endLine": 2, + "line": 2, + "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Prepend with readonly.", + }, + "desc": "Prepend with readonly.", + "fix": { + "range": [ + 49, + 57, + ], + "text": "readonly string[]", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - ], - }, - { - "column": 15, - "endColumn": 25, - "endLine": 2, - "line": 2, - "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Prepend with readonly.", + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 49, + 57, + ], + "text": "Readonly", + }, + "messageId": "userDefined", }, - "desc": "Prepend with readonly.", - "fix": { - "range": [ - 49, - 57, - ], - "text": "readonly string[]", + ], + }, + { + "column": 15, + "endColumn": 28, + "endLine": 3, + "line": 3, + "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlySet instead of Set.", + }, + "desc": "Use ReadonlySet instead of Set.", + "fix": { + "range": [ + 77, + 88, + ], + "text": "ReadonlySet", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 77, + 88, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 49, - 57, - ], - "text": "Readonly", + ], + }, + { + "column": 15, + "endColumn": 36, + "endLine": 4, + "line": 4, + "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyMap instead of Map.", + }, + "desc": "Use ReadonlyMap instead of Map.", + "fix": { + "range": [ + 108, + 127, + ], + "text": "ReadonlyMap", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - ], - }, - { - "column": 15, - "endColumn": 28, - "endLine": 3, - "line": 3, - "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Use ReadonlySet instead of Set.", + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 108, + 127, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", }, - "desc": "Use ReadonlySet instead of Set.", - "fix": { - "range": [ - 77, - 88, + ], + }, + ], + "output": "function foo(): Array {} +function foo(): string[] {} +function foo(): Set {} +function foo(): Map {} +function foo(): ReadonlyArray {} +function foo(): readonly string[] {} +function foo(): ReadonlySet {} +function foo(): ReadonlyMap {}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 15, + "endColumn": 30, + "endLine": 1, + "line": 1, + "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyArray instead of Array.", + }, + "desc": "Use ReadonlyArray instead of Array.", + "fix": { + "range": [ + 16, + 29, + ], + "text": "ReadonlyArray", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 16, + 29, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, ], - "text": "ReadonlySet", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 77, - 88, + { + "column": 15, + "endColumn": 25, + "endLine": 2, + "line": 2, + "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Prepend with readonly.", + }, + "desc": "Prepend with readonly.", + "fix": { + "range": [ + 49, + 57, + ], + "text": "readonly string[]", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 49, + 57, + ], + "text": "Readonly", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly>", - }, - "messageId": "userDefined", - }, - ], - }, - { - "column": 15, - "endColumn": 36, - "endLine": 4, - "line": 4, - "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "returnType", - "nodeType": "TSTypeAnnotation", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Use ReadonlyMap instead of Map.", }, - "desc": "Use ReadonlyMap instead of Map.", - "fix": { - "range": [ - 108, - 127, + { + "column": 15, + "endColumn": 28, + "endLine": 3, + "line": 3, + "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlySet instead of Set.", + }, + "desc": "Use ReadonlySet instead of Set.", + "fix": { + "range": [ + 77, + 88, + ], + "text": "ReadonlySet", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 77, + 88, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, ], - "text": "ReadonlyMap", - }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 108, - 127, + { + "column": 15, + "endColumn": 36, + "endLine": 4, + "line": 4, + "message": "Return type should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "returnType", + "nodeType": "TSTypeAnnotation", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyMap instead of Map.", + }, + "desc": "Use ReadonlyMap instead of Map.", + "fix": { + "range": [ + 108, + 127, + ], + "text": "ReadonlyMap", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 108, + 127, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly>", }, - "messageId": "userDefined", - }, - ], - }, -] + ], + "output": "function foo(): Array {} +function foo(): string[] {} +function foo(): Set {} +function foo(): Map {} +function foo(): ReadonlyArray {} +function foo(): readonly string[] {} +function foo(): ReadonlySet {} +function foo(): ReadonlyMap {}", + }, + ], +} `; diff --git a/tests/rules/prefer-immutable-types/__snapshots__/variables.test.ts.snap b/tests/rules/prefer-immutable-types/__snapshots__/variables.test.ts.snap index 7a5f65e87..32f067427 100644 --- a/tests/rules/prefer-immutable-types/__snapshots__/variables.test.ts.snap +++ b/tests/rules/prefer-immutable-types/__snapshots__/variables.test.ts.snap @@ -1,452 +1,982 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`prefer-immutable-types > typescript > allows for user fixes 1`] = ` -[ - { - "column": 7, - "endColumn": 25, - "endLine": 1, - "line": 1, - "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 7, - "endColumn": 20, - "endLine": 2, - "line": 2, - "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 7, - "endColumn": 23, - "endLine": 3, - "fix": { - "range": [ - 82, - 93, - ], - "text": "Readonly>", +{ + "fixed": true, + "messages": [ + { + "column": 7, + "endColumn": 25, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, }, - "line": 3, - "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 7, - "endColumn": 31, - "endLine": 4, - "fix": { - "range": [ - 118, - 137, - ], - "text": "Readonly>", + { + "column": 7, + "endColumn": 20, + "endLine": 2, + "line": 2, + "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 23, + "endLine": 3, + "fix": { + "range": [ + 82, + 93, + ], + "text": "Readonly>", + }, + "line": 3, + "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 31, + "endLine": 4, + "fix": { + "range": [ + 118, + 137, + ], + "text": "Readonly>", + }, + "line": 4, + "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 33, + "endLine": 5, + "line": 5, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 29, + "endLine": 6, + "line": 6, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 31, + "endLine": 7, + "fix": { + "range": [ + 250, + 269, + ], + "text": "Readonly>", + }, + "line": 7, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, }, - "line": 4, - "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 7, - "endColumn": 33, - "endLine": 5, - "line": 5, - "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 7, - "endColumn": 29, - "endLine": 6, - "line": 6, - "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 7, - "endColumn": 31, - "endLine": 7, - "fix": { - "range": [ - 250, - 269, + { + "column": 7, + "endColumn": 39, + "endLine": 8, + "fix": { + "range": [ + 294, + 321, + ], + "text": "Readonly>", + }, + "line": 8, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "const foo: Array = {} as any; +const foo: string[] = {} as any; +const foo: Readonly> = {} as any; +const foo: Readonly> = {} as any; +const foo: ReadonlyArray = {} as any; +const foo: readonly string[] = {} as any; +const foo: Readonly> = {} as any; +const foo: Readonly> = {} as any;", + "steps": [ + { + "fixed": true, + "messages": [ + { + "column": 7, + "endColumn": 25, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 20, + "endLine": 2, + "line": 2, + "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 33, + "endLine": 5, + "line": 5, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 29, + "endLine": 6, + "line": 6, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, ], - "text": "Readonly>", + "output": "const foo: Array = {} as any; +const foo: string[] = {} as any; +const foo: Readonly> = {} as any; +const foo: Readonly> = {} as any; +const foo: ReadonlyArray = {} as any; +const foo: readonly string[] = {} as any; +const foo: Readonly> = {} as any; +const foo: Readonly> = {} as any;", }, - "line": 7, - "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 7, - "endColumn": 39, - "endLine": 8, - "fix": { - "range": [ - 294, - 321, + { + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 25, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 20, + "endLine": 2, + "line": 2, + "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 33, + "endLine": 5, + "line": 5, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 7, + "endColumn": 29, + "endLine": 6, + "line": 6, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, ], - "text": "Readonly>", + "output": "const foo: Array = {} as any; +const foo: string[] = {} as any; +const foo: Readonly> = {} as any; +const foo: Readonly> = {} as any; +const foo: ReadonlyArray = {} as any; +const foo: readonly string[] = {} as any; +const foo: Readonly> = {} as any; +const foo: Readonly> = {} as any;", }, - "line": 8, - "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] + ], +} `; exports[`prefer-immutable-types > typescript > allows for user suggestions 1`] = ` -[ - { - "column": 7, - "endColumn": 27, - "endLine": 1, - "line": 1, - "message": "Variable should have an immutability of at least "ReadonlyDeep" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Replace with: ReadonlyDeep<{ foo: string }>", +{ + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 27, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "ReadonlyDeep" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Replace with: ReadonlyDeep<{ foo: string }>", + }, + "desc": "Replace with: ReadonlyDeep<{ foo: string }>", + "fix": { + "range": [ + 11, + 26, + ], + "text": "ReadonlyDeep<{ foo: string }>", + }, + "messageId": "userDefined", }, - "desc": "Replace with: ReadonlyDeep<{ foo: string }>", - "fix": { - "range": [ - 11, - 26, + ], + }, + ], + "output": "const foo: { foo: string } = {} as any;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 27, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "ReadonlyDeep" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Replace with: ReadonlyDeep<{ foo: string }>", + }, + "desc": "Replace with: ReadonlyDeep<{ foo: string }>", + "fix": { + "range": [ + 11, + 26, + ], + "text": "ReadonlyDeep<{ foo: string }>", + }, + "messageId": "userDefined", + }, ], - "text": "ReadonlyDeep<{ foo: string }>", }, - "messageId": "userDefined", - }, - ], - }, -] + ], + "output": "const foo: { foo: string } = {} as any;", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports mutable class parameter properties 1`] = ` -[ - { - "column": 3, - "endColumn": 33, - "endLine": 2, - "line": 2, - "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "propertyImmutability", - "nodeType": "PropertyDefinition", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 3, - "endColumn": 41, - "endLine": 3, - "line": 3, - "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "propertyImmutability", - "nodeType": "PropertyDefinition", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 3, - "endColumn": 40, - "endLine": 4, - "line": 4, - "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "propertyImmutability", - "nodeType": "PropertyDefinition", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, - { - "column": 3, - "endColumn": 48, - "endLine": 5, - "line": 5, - "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "propertyImmutability", - "nodeType": "PropertyDefinition", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 33, + "endLine": 2, + "line": 2, + "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "propertyImmutability", + "nodeType": "PropertyDefinition", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 3, + "endColumn": 41, + "endLine": 3, + "line": 3, + "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "propertyImmutability", + "nodeType": "PropertyDefinition", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 3, + "endColumn": 40, + "endLine": 4, + "line": 4, + "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "propertyImmutability", + "nodeType": "PropertyDefinition", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + { + "column": 3, + "endColumn": 48, + "endLine": 5, + "line": 5, + "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "propertyImmutability", + "nodeType": "PropertyDefinition", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "class Klass { + readonly foo: { foo: number }; + private readonly bar: { foo: number }; + static readonly baz: { foo: number }; + private static readonly qux: { foo: number }; +}", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 3, + "endColumn": 33, + "endLine": 2, + "line": 2, + "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "propertyImmutability", + "nodeType": "PropertyDefinition", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 3, + "endColumn": 41, + "endLine": 3, + "line": 3, + "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "propertyImmutability", + "nodeType": "PropertyDefinition", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 3, + "endColumn": 40, + "endLine": 4, + "line": 4, + "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "propertyImmutability", + "nodeType": "PropertyDefinition", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + { + "column": 3, + "endColumn": 48, + "endLine": 5, + "line": 5, + "message": "Property should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "propertyImmutability", + "nodeType": "PropertyDefinition", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + ], + "output": "class Klass { + readonly foo: { foo: number }; + private readonly bar: { foo: number }; + static readonly baz: { foo: number }; + private static readonly qux: { foo: number }; +}", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports mutable records variables 1`] = ` -[ - { - "column": 7, - "endColumn": 27, - "endLine": 1, - "line": 1, - "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 27, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "const foo: { foo: string } = {} as any;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 27, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "Immutable" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + ], + "output": "const foo: { foo: string } = {} as any;", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports mutable records variables and suggests a fix for ReadonlyShallow 1`] = ` -[ - { - "column": 7, - "endColumn": 27, - "endLine": 1, - "line": 1, - "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Surround with Readonly.", +{ + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 27, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 11, + 26, + ], + "text": "Readonly<{ foo: string }>", + }, + "messageId": "userDefined", }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 11, - 26, + ], + }, + ], + "output": "const foo: { foo: string } = {} as any;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 27, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 11, + 26, + ], + "text": "Readonly<{ foo: string }>", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly<{ foo: string }>", }, - "messageId": "userDefined", - }, - ], - }, -] + ], + "output": "const foo: { foo: string } = {} as any;", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports non-immutable map variables 1`] = ` -[ - { - "column": 7, - "endColumn": 39, - "endLine": 1, - "line": 1, - "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 39, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "const foo: ReadonlyMap = {} as any;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 39, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + ], + "output": "const foo: ReadonlyMap = {} as any;", + }, + ], +} `; exports[`prefer-immutable-types > typescript > reports non-immutable set variables 1`] = ` -[ - { - "column": 7, - "endColumn": 31, - "endLine": 1, - "line": 1, - "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - }, -] +{ + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 31, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + }, + ], + "output": "const foo: ReadonlySet = {} as any;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 31, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "Immutable" (actual: "ReadonlyDeep").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + }, + ], + "output": "const foo: ReadonlySet = {} as any;", + }, + ], +} `; exports[`prefer-immutable-types > typescript > suggest multiple fixes for collections for ReadonlyShallow 1`] = ` -[ - { - "column": 7, - "endColumn": 25, - "endLine": 1, - "line": 1, - "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Use ReadonlyArray instead of Array.", - }, - "desc": "Use ReadonlyArray instead of Array.", - "fix": { - "range": [ - 11, - 24, - ], - "text": "ReadonlyArray", +{ + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 25, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyArray instead of Array.", + }, + "desc": "Use ReadonlyArray instead of Array.", + "fix": { + "range": [ + 11, + 24, + ], + "text": "ReadonlyArray", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 11, + 24, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 11, - 24, - ], - "text": "Readonly>", + ], + }, + { + "column": 7, + "endColumn": 20, + "endLine": 2, + "line": 2, + "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Prepend with readonly.", + }, + "desc": "Prepend with readonly.", + "fix": { + "range": [ + 49, + 57, + ], + "text": "readonly string[]", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - ], - }, - { - "column": 7, - "endColumn": 20, - "endLine": 2, - "line": 2, - "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Prepend with readonly.", + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 49, + 57, + ], + "text": "Readonly", + }, + "messageId": "userDefined", }, - "desc": "Prepend with readonly.", - "fix": { - "range": [ - 49, - 57, - ], - "text": "readonly string[]", + ], + }, + { + "column": 7, + "endColumn": 23, + "endLine": 3, + "line": 3, + "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlySet instead of Set.", + }, + "desc": "Use ReadonlySet instead of Set.", + "fix": { + "range": [ + 82, + 93, + ], + "text": "ReadonlySet", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 82, + 93, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 49, - 57, - ], - "text": "Readonly", + ], + }, + { + "column": 7, + "endColumn": 31, + "endLine": 4, + "line": 4, + "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyMap instead of Map.", + }, + "desc": "Use ReadonlyMap instead of Map.", + "fix": { + "range": [ + 118, + 137, + ], + "text": "ReadonlyMap", + }, + "messageId": "userDefined", }, - "messageId": "userDefined", - }, - ], - }, - { - "column": 7, - "endColumn": 23, - "endLine": 3, - "line": 3, - "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Use ReadonlySet instead of Set.", + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 118, + 137, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", }, - "desc": "Use ReadonlySet instead of Set.", - "fix": { - "range": [ - 82, - 93, + ], + }, + ], + "output": "const foo: Array = {} as any; +const foo: string[] = {} as any; +const foo: Set = {} as any; +const foo: Map = {} as any; +const foo: ReadonlyArray = {} as any; +const foo: readonly string[] = {} as any; +const foo: ReadonlySet = {} as any; +const foo: ReadonlyMap = {} as any;", + "steps": [ + { + "fixed": false, + "messages": [ + { + "column": 7, + "endColumn": 25, + "endLine": 1, + "line": 1, + "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyArray instead of Array.", + }, + "desc": "Use ReadonlyArray instead of Array.", + "fix": { + "range": [ + 11, + 24, + ], + "text": "ReadonlyArray", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 11, + 24, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, ], - "text": "ReadonlySet", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 82, - 93, + { + "column": 7, + "endColumn": 20, + "endLine": 2, + "line": 2, + "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Prepend with readonly.", + }, + "desc": "Prepend with readonly.", + "fix": { + "range": [ + 49, + 57, + ], + "text": "readonly string[]", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 49, + 57, + ], + "text": "Readonly", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly>", }, - "messageId": "userDefined", - }, - ], - }, - { - "column": 7, - "endColumn": 31, - "endLine": 4, - "line": 4, - "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", - "messageId": "variable", - "nodeType": "Identifier", - "ruleId": "prefer-immutable-types", - "severity": 2, - "suggestions": [ - { - "data": { - "message": "Use ReadonlyMap instead of Map.", - }, - "desc": "Use ReadonlyMap instead of Map.", - "fix": { - "range": [ - 118, - 137, + { + "column": 7, + "endColumn": 23, + "endLine": 3, + "line": 3, + "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlySet instead of Set.", + }, + "desc": "Use ReadonlySet instead of Set.", + "fix": { + "range": [ + 82, + 93, + ], + "text": "ReadonlySet", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 82, + 93, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, ], - "text": "ReadonlyMap", }, - "messageId": "userDefined", - }, - { - "data": { - "message": "Surround with Readonly.", - }, - "desc": "Surround with Readonly.", - "fix": { - "range": [ - 118, - 137, + { + "column": 7, + "endColumn": 31, + "endLine": 4, + "line": 4, + "message": "Variable should have an immutability of at least "ReadonlyShallow" (actual: "Mutable").", + "messageId": "variable", + "nodeType": "Identifier", + "ruleId": "rule-to-test/prefer-immutable-types", + "severity": 2, + "suggestions": [ + { + "data": { + "message": "Use ReadonlyMap instead of Map.", + }, + "desc": "Use ReadonlyMap instead of Map.", + "fix": { + "range": [ + 118, + 137, + ], + "text": "ReadonlyMap", + }, + "messageId": "userDefined", + }, + { + "data": { + "message": "Surround with Readonly.", + }, + "desc": "Surround with Readonly.", + "fix": { + "range": [ + 118, + 137, + ], + "text": "Readonly>", + }, + "messageId": "userDefined", + }, ], - "text": "Readonly>", }, - "messageId": "userDefined", - }, - ], - }, -] + ], + "output": "const foo: Array = {} as any; +const foo: string[] = {} as any; +const foo: Set = {} as any; +const foo: Map = {} as any; +const foo: ReadonlyArray = {} as any; +const foo: readonly string[] = {} as any; +const foo: ReadonlySet = {} as any; +const foo: ReadonlyMap = {} as any;", + }, + ], +} `; diff --git a/tests/rules/prefer-immutable-types/parameters.test.ts b/tests/rules/prefer-immutable-types/parameters.test.ts index 39a5b046c..57b08a4dd 100644 --- a/tests/rules/prefer-immutable-types/parameters.test.ts +++ b/tests/rules/prefer-immutable-types/parameters.test.ts @@ -14,44 +14,44 @@ describe(name, () => { configs: typescriptConfig, }); - it("reports non-immutable set parameters", () => { - const invalidResult = invalid({ + it("reports non-immutable set parameters", async () => { + const invalidResult = await invalid({ code: "function foo(arg: ReadonlySet) {}", errors: ["parameter"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports non-immutable map parameters", () => { - const invalidResult = invalid({ + it("reports non-immutable map parameters", async () => { + const invalidResult = await invalid({ code: "function foo(arg: ReadonlyMap) {}", errors: ["parameter"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mutable records parameters", () => { - const invalidResult = invalid({ + it("reports mutable records parameters", async () => { + const invalidResult = await invalid({ code: "function foo(arg1: { foo: string }, arg2: { foo: number }) {}", errors: ["parameter", "parameter"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mutable records parameters and suggests a fix for ReadonlyShallow", () => { - const invalidResult = invalid({ + it("reports mutable records parameters and suggests a fix for ReadonlyShallow", async () => { + const invalidResult = await invalid({ code: "function foo(arg1: { foo: string }, arg2: { foo: number }) {}", options: [{ parameters: "ReadonlyShallow" }], errors: ["parameter", "parameter"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(1); } }); - it("reports mutable class parameter properties", () => { - const invalidResult = invalid({ + it("reports mutable class parameter properties", async () => { + const invalidResult = await invalid({ code: dedent` class Klass { constructor ( @@ -63,11 +63,11 @@ describe(name, () => { `, errors: ["propertyModifier", "propertyModifier", "propertyModifier"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mutable class parameter properties and suggests a fix for ReadonlyShallow", () => { - const invalidResult = invalid({ + it("reports mutable class parameter properties and suggests a fix for ReadonlyShallow", async () => { + const invalidResult = await invalid({ code: dedent` class Klass { constructor ( @@ -80,26 +80,26 @@ describe(name, () => { options: [{ parameters: "ReadonlyShallow" }], errors: ["propertyModifier", "propertyModifier", "propertyModifier"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(1); } }); - it("doesn't report type assertion parameters", () => { - const invalidResult = invalid({ + it("doesn't report type assertion parameters", async () => { + const invalidResult = await invalid({ code: "function foo(arg0: { foo: string | number }, arg1: { foo: string | number }): arg0 is { foo: number } {}", options: [{ parameters: "ReadonlyShallow" }], errors: ["parameter"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(1); } }); - it("suggest multiple fixes for collections for ReadonlyShallow", () => { - const invalidResult = invalid({ + it("suggest multiple fixes for collections for ReadonlyShallow", async () => { + const invalidResult = await invalid({ code: dedent` function foo(arg: Array) {} function foo(arg: string[]) {} @@ -113,14 +113,14 @@ describe(name, () => { options: [{ parameters: "ReadonlyShallow" }], errors: ["parameter", "parameter", "parameter", "parameter"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(2); } }); - it("allows for user suggestions", () => { - const invalidResult = invalid({ + it("allows for user suggestions", async () => { + const invalidResult = await invalid({ code: "function foo(arg1: { foo: string }) {}", options: [ { @@ -139,14 +139,14 @@ describe(name, () => { ], errors: ["parameter"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(1); } }); - it("allows for user fixes", () => { - const invalidResult = invalid({ + it("allows for user fixes", async () => { + const invalidResult = await invalid({ code: dedent` function foo(arg: Array) {} function foo(arg: string[]) {} @@ -182,35 +182,35 @@ describe(name, () => { ], verifyAfterFix: false, // "fix" doesn't fix arrays so they will still report. }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); it.each([ [[{ parameters: "ReadonlyShallow" }]], [[{ parameters: "ReadonlyDeep" }]], [[{ parameters: "Immutable" }]], - ])("doesn't reports valid parameters", (options) => { - valid({ + ])("doesn't reports valid parameters", async (options) => { + await valid({ code: "function foo(arg: boolean) {}", options, }); - valid({ + await valid({ code: "function foo(arg: true) {}", options, }); - valid({ + await valid({ code: "function foo(arg: string) {}", options, }); - valid({ + await valid({ code: "function foo(arg: 'bar') {}", options, }); - valid({ + await valid({ code: "function foo(arg: undefined) {}", options, }); - valid({ + await valid({ code: "function foo(arg: readonly string[]) {}", options, settings: { @@ -224,57 +224,57 @@ describe(name, () => { }, }, }); - valid({ + await valid({ code: "function foo(arg: { readonly foo: string }) {}", options, }); - valid({ + await valid({ code: "function foo(arg: { readonly foo: { readonly bar: number } }) {}", options, }); - valid({ + await valid({ code: "function foo(arg: Readonly>) {}", options, }); - valid({ + await valid({ code: "function foo(arg: Readonly>) {}", options, }); - valid({ + await valid({ code: "function foo(arg: { foo: string | number }): arg is { foo: number } {}", options, }); if (options[0]!.parameters !== "Immutable") { - valid({ + await valid({ code: "function foo(arg: { foo(): void }) {}", options, }); - valid({ + await valid({ code: "function foo(arg: ReadonlyArray) {}", options, }); - valid({ + await valid({ code: "function foo(arg: readonly [string, number]) {}", options, }); - valid({ + await valid({ code: "function foo(arg: Readonly<[string, number]>) {}", options, }); - valid({ + await valid({ code: "function foo(arg: { foo: () => void }) {}", options, }); - valid({ + await valid({ code: "function foo(arg: ReadonlySet) {}", options, }); - valid({ + await valid({ code: "function foo(arg: ReadonlyMap) {}", options, }); - valid({ + await valid({ code: dedent` class Foo { constructor( @@ -287,7 +287,7 @@ describe(name, () => { `, options, }); - valid({ + await valid({ code: dedent` interface Foo { (arg: readonly string[]): void; @@ -295,7 +295,7 @@ describe(name, () => { `, options, }); - valid({ + await valid({ code: dedent` interface Foo { new (arg: readonly string[]): void; diff --git a/tests/rules/prefer-immutable-types/return-types.test.ts b/tests/rules/prefer-immutable-types/return-types.test.ts index 59359d196..fddceee9a 100644 --- a/tests/rules/prefer-immutable-types/return-types.test.ts +++ b/tests/rules/prefer-immutable-types/return-types.test.ts @@ -14,44 +14,44 @@ describe(name, () => { configs: typescriptConfig, }); - it("reports non-immutable set return types", () => { - const invalidResult = invalid({ + it("reports non-immutable set return types", async () => { + const invalidResult = await invalid({ code: "function foo(): ReadonlySet {}", errors: ["returnType"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports non-immutable map return types", () => { - const invalidResult = invalid({ + it("reports non-immutable map return types", async () => { + const invalidResult = await invalid({ code: "function foo(): ReadonlyMap {}", errors: ["returnType"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mutable records return types", () => { - const invalidResult = invalid({ + it("reports mutable records return types", async () => { + const invalidResult = await invalid({ code: "function foo(): { foo: string } {}", errors: ["returnType"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mutable records return types and suggests a fix for ReadonlyShallow", () => { - const invalidResult = invalid({ + it("reports mutable records return types and suggests a fix for ReadonlyShallow", async () => { + const invalidResult = await invalid({ code: "function foo(): { foo: string } {}", options: [{ returnTypes: "ReadonlyShallow" }], errors: ["returnType"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(1); } }); - it("doesn't report type assertion return types", () => { - valid({ + it("doesn't report type assertion return types", async () => { + await valid({ code: "function foo(arg0: { foo: string | number }, arg1: { foo: string | number }): arg0 is { foo: number } {}", options: [ { @@ -62,8 +62,8 @@ describe(name, () => { }); }); - it("suggest multiple fixes for collections for ReadonlyShallow", () => { - const invalidResult = invalid({ + it("suggest multiple fixes for collections for ReadonlyShallow", async () => { + const invalidResult = await invalid({ code: dedent` function foo(): Array {} function foo(): string[] {} @@ -77,14 +77,14 @@ describe(name, () => { options: [{ returnTypes: "ReadonlyShallow" }], errors: ["returnType", "returnType", "returnType", "returnType"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(2); } }); - it("allows for user suggestions", () => { - const invalidResult = invalid({ + it("allows for user suggestions", async () => { + const invalidResult = await invalid({ code: "function foo(): { foo: string } {}", options: [ { @@ -103,14 +103,14 @@ describe(name, () => { ], errors: ["returnType"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(1); } }); - it("allows for user fixes", () => { - const invalidResult = invalid({ + it("allows for user fixes", async () => { + const invalidResult = await invalid({ code: dedent` function foo(): Array {} function foo(): string[] {} @@ -146,35 +146,35 @@ describe(name, () => { ], verifyAfterFix: false, // "fix" doesn't fix arrays so they will still report. }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); it.each([ [[{ returnTypes: "ReadonlyShallow" }]], [[{ returnTypes: "ReadonlyDeep" }]], [[{ returnTypes: "Immutable" }]], - ])("doesn't reports valid return types", (options) => { - valid({ + ])("doesn't reports valid return types", async (options) => { + await valid({ code: "function foo(): boolean {}", options, }); - valid({ + await valid({ code: "function foo(): true {}", options, }); - valid({ + await valid({ code: "function foo(): string {}", options, }); - valid({ + await valid({ code: "function foo(): 'bar' {}", options, }); - valid({ + await valid({ code: "function foo(): undefined {}", options, }); - valid({ + await valid({ code: "function foo(): readonly string[] {}", options, settings: { @@ -188,57 +188,57 @@ describe(name, () => { }, }, }); - valid({ + await valid({ code: "function foo(): { readonly foo: string } {}", options, }); - valid({ + await valid({ code: "function foo(): { readonly foo: { readonly bar: number } } {}", options, }); - valid({ + await valid({ code: "function foo(): Readonly> {}", options, }); - valid({ + await valid({ code: "function foo(): Readonly> {}", options, }); - valid({ + await valid({ code: "function foo(arg: { foo: string | number }): arg is { foo: number } {}", options, }); if (options[0]!.returnTypes !== "Immutable") { - valid({ + await valid({ code: "function foo(): { foo(): void } {}", options, }); - valid({ + await valid({ code: "function foo(): ReadonlyArray {}", options, }); - valid({ + await valid({ code: "function foo(): readonly [string, number] {}", options, }); - valid({ + await valid({ code: "function foo(): Readonly<[string, number]> {}", options, }); - valid({ + await valid({ code: "function foo(): { foo: () => void } {}", options, }); - valid({ + await valid({ code: "function foo(): ReadonlySet {}", options, }); - valid({ + await valid({ code: "function foo(): ReadonlyMap {}", options, }); - valid({ + await valid({ code: dedent` interface Foo { (): readonly string[]; @@ -246,7 +246,7 @@ describe(name, () => { `, options, }); - valid({ + await valid({ code: dedent` interface Foo { new (): readonly string[]; diff --git a/tests/rules/prefer-immutable-types/variables.test.ts b/tests/rules/prefer-immutable-types/variables.test.ts index 1b3a7c74e..4884cc125 100644 --- a/tests/rules/prefer-immutable-types/variables.test.ts +++ b/tests/rules/prefer-immutable-types/variables.test.ts @@ -14,44 +14,44 @@ describe(name, () => { configs: typescriptConfig, }); - it("reports non-immutable set variables", () => { - const invalidResult = invalid({ + it("reports non-immutable set variables", async () => { + const invalidResult = await invalid({ code: "const foo: ReadonlySet = {} as any;", errors: ["variable"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports non-immutable map variables", () => { - const invalidResult = invalid({ + it("reports non-immutable map variables", async () => { + const invalidResult = await invalid({ code: "const foo: ReadonlyMap = {} as any;", errors: ["variable"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mutable records variables", () => { - const invalidResult = invalid({ + it("reports mutable records variables", async () => { + const invalidResult = await invalid({ code: "const foo: { foo: string } = {} as any;", errors: ["variable"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mutable records variables and suggests a fix for ReadonlyShallow", () => { - const invalidResult = invalid({ + it("reports mutable records variables and suggests a fix for ReadonlyShallow", async () => { + const invalidResult = await invalid({ code: "const foo: { foo: string } = {} as any;", options: [{ variables: "ReadonlyShallow" }], errors: ["variable"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(1); } }); - it("doesn't report type assertion variables", () => { - valid({ + it("doesn't report type assertion variables", async () => { + await valid({ code: "function foo(arg0: { foo: string | number }, arg1: { foo: string | number }): arg0 is { foo: number } {}", options: [ { @@ -62,8 +62,8 @@ describe(name, () => { }); }); - it("suggest multiple fixes for collections for ReadonlyShallow", () => { - const invalidResult = invalid({ + it("suggest multiple fixes for collections for ReadonlyShallow", async () => { + const invalidResult = await invalid({ code: dedent` const foo: Array = {} as any; const foo: string[] = {} as any; @@ -77,14 +77,14 @@ describe(name, () => { options: [{ variables: "ReadonlyShallow" }], errors: ["variable", "variable", "variable", "variable"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(2); } }); - it("reports mutable class parameter properties", () => { - const invalidResult = invalid({ + it("reports mutable class parameter properties", async () => { + const invalidResult = await invalid({ code: dedent` class Klass { readonly foo: { foo: number }; @@ -95,11 +95,11 @@ describe(name, () => { `, errors: ["propertyImmutability", "propertyImmutability", "propertyImmutability", "propertyImmutability"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("allows for user suggestions", () => { - const invalidResult = invalid({ + it("allows for user suggestions", async () => { + const invalidResult = await invalid({ code: "const foo: { foo: string } = {} as any;", options: [ { @@ -118,14 +118,14 @@ describe(name, () => { ], errors: ["variable"], }); - expect(invalidResult.messages).toMatchSnapshot(); - for (const message of invalidResult.messages) { + expect(invalidResult.result).toMatchSnapshot(); + for (const message of invalidResult.result.messages) { expect(message.suggestions).toHaveLength(1); } }); - it("allows for user fixes", () => { - const invalidResult = invalid({ + it("allows for user fixes", async () => { + const invalidResult = await invalid({ code: dedent` const foo: Array = {} as any; const foo: string[] = {} as any; @@ -152,33 +152,33 @@ describe(name, () => { errors: ["variable", "variable", "variable", "variable", "variable", "variable", "variable", "variable"], verifyAfterFix: false, // "fix" doesn't fix arrays so they will still report. }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); it.each([[[{ variables: "ReadonlyShallow" }]], [[{ variables: "ReadonlyDeep" }]], [[{ variables: "Immutable" }]]])( "doesn't reports valid variables", - (options) => { - valid({ + async (options) => { + await valid({ code: "const foo: boolean = {} as any;", options, }); - valid({ + await valid({ code: "const foo: true = {} as any;", options, }); - valid({ + await valid({ code: "const foo: string = {} as any;", options, }); - valid({ + await valid({ code: "const foo: 'bar' = {} as any;", options, }); - valid({ + await valid({ code: "const foo: undefined = {} as any;", options, }); - valid({ + await valid({ code: "const foo: readonly string[] = {} as any;", options, settings: { @@ -192,61 +192,61 @@ describe(name, () => { }, }, }); - valid({ + await valid({ code: "const foo: { readonly foo: string } = {} as any;", options, }); - valid({ + await valid({ code: "const foo: { readonly foo: { readonly bar: number } } = {} as any;", options, }); - valid({ + await valid({ code: "const foo: Readonly> = {} as any;", options, }); - valid({ + await valid({ code: "const foo: Readonly> = {} as any;", options, }); - valid({ + await valid({ code: "function foo(arg: { foo: string | number }): arg is { foo: number } {}", options, }); - valid({ + await valid({ code: "const [a, b] = [1, 2];", options, }); - valid({ + await valid({ code: "const { a, b } = { a: 1, b: 2 };", options, }); if (options[0]!.variables !== "Immutable") { - valid({ + await valid({ code: "const foo: { foo(): void } = {} as any;", options, }); - valid({ + await valid({ code: "const foo: ReadonlyArray = {} as any;", options, }); - valid({ + await valid({ code: "const foo: readonly [string, number] = {} as any;", options, }); - valid({ + await valid({ code: "const foo: Readonly<[string, number]> = {} as any;", options, }); - valid({ + await valid({ code: "const foo: { foo: () => void } = {} as any;", options, }); - valid({ + await valid({ code: "const foo: ReadonlySet = {} as any;", options, }); - valid({ + await valid({ code: "const foo: ReadonlyMap = {} as any;", options, }); @@ -256,8 +256,8 @@ describe(name, () => { describe("options", () => { describe("ignoreClasses", () => { - it("doesn't report class fields", () => { - valid({ + it("doesn't report class fields", async () => { + await valid({ code: dedent` class Klass { foo: number; @@ -272,8 +272,8 @@ describe(name, () => { }); describe("ignoreInFunctions", () => { - it("doesn't report variables in functions", () => { - valid({ + it("doesn't report variables in functions", async () => { + await valid({ code: dedent` function foo() { let foo: { @@ -290,8 +290,8 @@ describe(name, () => { }); describe("ignoreNamePattern", () => { - it("doesn't report ignored names", () => { - valid({ + it("doesn't report ignored names", async () => { + await valid({ code: "let mutableFoo: string[] = [];", options: [{ ignoreNamePattern: "^mutable" }], }); @@ -299,8 +299,8 @@ describe(name, () => { }); describe("ignoreTypePattern", () => { - it("doesn't report ignored types", () => { - valid({ + it("doesn't report ignored types", async () => { + await valid({ code: dedent` let fooMutable: Readonly< string[] diff --git a/tests/rules/prefer-property-signatures.test.ts b/tests/rules/prefer-property-signatures.test.ts index 129bc52ed..e6a4fbc5f 100644 --- a/tests/rules/prefer-property-signatures.test.ts +++ b/tests/rules/prefer-property-signatures.test.ts @@ -14,24 +14,24 @@ describe(name, () => { configs: typescriptConfig, }); - it("doesn't report property signatures in interfaces", () => { - valid(dedent` + it("doesn't report property signatures in interfaces", async () => { + await valid(dedent` interface Foo { bar: (a: number, b: string) => number; } `); }); - it("doesn't report property signatures in type literals", () => { - valid(dedent` + it("doesn't report property signatures in type literals", async () => { + await valid(dedent` type Foo = { bar: (a: number, b: string) => number; } `); }); - it("reports method signatures in interfaces", () => { - const invalidResult = invalid({ + it("reports method signatures in interfaces", async () => { + const invalidResult = await invalid({ code: dedent` interface Foo { bar(a: number, b: string): number; @@ -39,11 +39,11 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports method signatures in type literals", () => { - const invalidResult = invalid({ + it("reports method signatures in type literals", async () => { + const invalidResult = await invalid({ code: dedent` type Foo = { bar(a: number, b: string): number; @@ -51,13 +51,13 @@ describe(name, () => { `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); describe("options", () => { describe("ignoreIfReadonlyWrapped", () => { - it("doesn't report method signatures wrapped in Readonly in interfaces", () => { - valid({ + it("doesn't report method signatures wrapped in Readonly in interfaces", async () => { + await valid({ code: dedent` interface Foo extends Readonly<{ methodSignature(): void @@ -67,8 +67,8 @@ describe(name, () => { }); }); - it("doesn't report method signatures wrapped in Readonly in type literals", () => { - valid({ + it("doesn't report method signatures wrapped in Readonly in type literals", async () => { + await valid({ code: dedent` type Foo = Readonly<{ methodSignature(): void @@ -78,8 +78,8 @@ describe(name, () => { }); }); - it("doesn't report method signatures wrapped in Readonly that are intersepted", () => { - valid({ + it("doesn't report method signatures wrapped in Readonly that are intersepted", async () => { + await valid({ code: dedent` type Foo = Bar & Readonly { }); }); - it("doesn't report method signatures wrapped in Readonly that are intersepted and nested", () => { - valid({ + it("doesn't report method signatures wrapped in Readonly that are intersepted and nested", async () => { + await valid({ code: dedent` type Foo = Bar & Readonly { }); }); - it("doesn't report method signatures wrapped in Readonly that are intersepted and deeply nested", () => { - valid({ + it("doesn't report method signatures wrapped in Readonly that are intersepted and deeply nested", async () => { + await valid({ code: dedent` interface Foo extends Bar, Readonly { configs: typescriptConfig, }); - it("reports mutable array (non-generic)", () => { - const invalidResult = invalid({ + it("reports mutable array (non-generic)", async () => { + const invalidResult = await invalid({ code: dedent` function foo(a: number[], b: Promise) { console.log(a, b); @@ -23,11 +23,11 @@ describe(name, () => { `, errors: ["array", "array"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mutable arrays (generic)", () => { - const invalidResult = invalid({ + it("reports mutable arrays (generic)", async () => { + const invalidResult = await invalid({ code: dedent` function foo(a: Array, b: Promise>) { console.log(a, b); @@ -35,11 +35,11 @@ describe(name, () => { `, errors: ["type", "type"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mutable sets", () => { - const invalidResult = invalid({ + it("reports mutable sets", async () => { + const invalidResult = await invalid({ code: dedent` function foo(a: Set, b: Promise>) { console.log(a, b); @@ -47,11 +47,11 @@ describe(name, () => { `, errors: ["type", "type"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports mutable maps", () => { - const invalidResult = invalid({ + it("reports mutable maps", async () => { + const invalidResult = await invalid({ code: dedent` function foo(a: Map, b: Promise>) { console.log(a, b); @@ -59,11 +59,11 @@ describe(name, () => { `, errors: ["type", "type"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports issue inside interfaces", () => { - const invalidResult = invalid({ + it("reports issue inside interfaces", async () => { + const invalidResult = await invalid({ code: dedent` interface Foo { readonly bar: Array; @@ -72,11 +72,11 @@ describe(name, () => { `, errors: ["type", "type"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports issues with index signatures", () => { - const invalidResult = invalid({ + it("reports issues with index signatures", async () => { + const invalidResult = await invalid({ code: dedent` interface Foo { readonly [key: string]: { @@ -93,11 +93,11 @@ describe(name, () => { `, errors: ["type", "type", "property", "property", "property"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports non-readonly class properties", () => { - const invalidResult = invalid({ + it("reports non-readonly class properties", async () => { + const invalidResult = await invalid({ code: dedent` class Klass { foo: number; @@ -108,11 +108,11 @@ describe(name, () => { `, errors: ["property", "property", "property", "property"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports non-readonly class parameter properties", () => { - const invalidResult = invalid({ + it("reports non-readonly class parameter properties", async () => { + const invalidResult = await invalid({ code: dedent` class Klass { constructor ( @@ -124,11 +124,11 @@ describe(name, () => { `, errors: ["property", "property", "property"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports literals without readonly modifiers", () => { - const invalidResult = invalid({ + it("reports literals without readonly modifiers", async () => { + const invalidResult = await invalid({ code: dedent` let foo: { a: number, @@ -158,55 +158,55 @@ describe(name, () => { "property", ], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports issues with mapped types", () => { - const invalidResult = invalid({ + it("reports issues with mapped types", async () => { + const invalidResult = await invalid({ code: dedent` const func = (x: { [key in string]: number }) => {} `, errors: ["property"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report explicit readonly parameter types", () => { - valid(dedent` + it("doesn't report explicit readonly parameter types", async () => { + await valid(dedent` function foo(...a: ReadonlyArray) { console.log(a); } `); - valid(dedent` + await valid(dedent` const foo = (...a: readonly number[]) => { console.log(a); } `); - valid(dedent` + await valid(dedent` const foo = (tuple: readonly [number, string, readonly [number, string]]) => { console.log(a); } `); }); - it("doesn't report explicit readonly return types", () => { - valid(dedent` + it("doesn't report explicit readonly return types", async () => { + await valid(dedent` function foo(): ReadonlyArray { return [1, 2, 3]; } `); - valid(dedent` + await valid(dedent` const foo = (): readonly number[] => { return [1, 2, 3]; } `); }); - it("doesn't report implicit readonly arrays", () => { - valid({ + it("doesn't report implicit readonly arrays", async () => { + await valid({ code: dedent` const foo = [1, 2, 3]; function bar(param = [1, 2, 3]) {} @@ -214,22 +214,22 @@ describe(name, () => { }); }); - it("doesn't report readonly local types", () => { - valid(dedent` + it("doesn't report readonly local types", async () => { + await valid(dedent` function foo() { type Foo = ReadonlyArray; } `); }); - it("doesn't report readonly variable declarations", () => { - valid(dedent` + it("doesn't report readonly variable declarations", async () => { + await valid(dedent` const foo: ReadonlyArray = []; `); }); - it("doesn't report interfaces with readonly modifiers", () => { - valid(dedent` + it("doesn't report interfaces with readonly modifiers", async () => { + await valid(dedent` interface Foo { readonly a: number, readonly b: ReadonlyArray, @@ -239,7 +239,7 @@ describe(name, () => { } `); - valid(dedent` + await valid(dedent` interface Foo { readonly a: number, readonly b: ReadonlyArray, @@ -257,8 +257,8 @@ describe(name, () => { `); }); - it("doesn't report call signatures and method signatures", () => { - valid(dedent` + it("doesn't report call signatures and method signatures", async () => { + await valid(dedent` interface Foo { (): void foo(): void @@ -266,20 +266,20 @@ describe(name, () => { `); }); - it("doesn't report literal with readonly index signature", () => { - valid(dedent` + it("doesn't report literal with readonly index signature", async () => { + await valid(dedent` let foo: { readonly [key: string]: number }; `); }); - it("doesn't report type literals elements with a readonly modifer in an array", () => { - valid(dedent` + it("doesn't report type literals elements with a readonly modifer in an array", async () => { + await valid(dedent` type foo = ReadonlyArray<{ readonly type: string, readonly code: string }>; `); }); - it("doesn't report type literals with readonly on members", () => { - valid(dedent` + it("doesn't report type literals with readonly on members", async () => { + await valid(dedent` let foo: { readonly a: number, readonly b: ReadonlyArray, @@ -290,8 +290,8 @@ describe(name, () => { `); }); - it("doesn't report class parameter properties", () => { - valid(dedent` + it("doesn't report class parameter properties", async () => { + await valid(dedent` class Klass { constructor ( nonParameterProp: string, @@ -304,16 +304,16 @@ describe(name, () => { `); }); - it("doesn't report mapped types with readonly on members", () => { - valid(dedent` + it("doesn't report mapped types with readonly on members", async () => { + await valid(dedent` const func = (x: { readonly [key in string]: number }) => {} `); }); describe("options", () => { describe("allowMutableReturnType", () => { - it("doesn't report mutable return types", () => { - valid({ + it("doesn't report mutable return types", async () => { + await valid({ code: dedent` function foo(...numbers: ReadonlyArray): Array {} function bar(...numbers: readonly number[]): number[] {} @@ -321,7 +321,7 @@ describe(name, () => { options: [{ allowMutableReturnType: true }], }); - valid({ + await valid({ code: dedent` const foo = (...numbers: ReadonlyArray): Array => {} const bar = (...numbers: readonly number[]): number[] => {} @@ -329,7 +329,7 @@ describe(name, () => { options: [{ allowMutableReturnType: true }], }); - valid({ + await valid({ code: dedent` class Foo { foo(...numbers: ReadonlyArray): Array { @@ -343,7 +343,7 @@ describe(name, () => { options: [{ allowMutableReturnType: true }], }); - valid({ + await valid({ code: dedent` function foo(...numbers: ReadonlyArray): Promise> {} function foo(...numbers: ReadonlyArray): Promise {} @@ -351,7 +351,7 @@ describe(name, () => { options: [{ allowMutableReturnType: true }], }); - valid({ + await valid({ code: dedent` function foo(...numbers: ReadonlyArray): Promise>> {} function foo(...numbers: ReadonlyArray): Promise> {} @@ -359,14 +359,14 @@ describe(name, () => { options: [{ allowMutableReturnType: true }], }); - valid({ + await valid({ code: dedent` function foo(...numbers: ReadonlyArray): { readonly a: Array } | { readonly b: string[] } {} `, options: [{ allowMutableReturnType: true }], }); - valid({ + await valid({ code: dedent` type Foo = { readonly x: T; }; function foo(...numbers: ReadonlyArray): Promise>> {} @@ -375,21 +375,21 @@ describe(name, () => { options: [{ allowMutableReturnType: true }], }); - valid({ + await valid({ code: dedent` function foo(...numbers: ReadonlyArray): { readonly a: Array } & { readonly b: string[] } {} `, options: [{ allowMutableReturnType: true }], }); - valid({ + await valid({ code: dedent` function foo(x: T): T extends Array ? string : number[] {} `, options: [{ allowMutableReturnType: true }], }); - valid({ + await valid({ code: dedent` function foo(bar: string): { baz: number } { return 1 as any; @@ -401,8 +401,8 @@ describe(name, () => { }); describe("checkImplicit", () => { - it("doesn't report implicit readonly array variables", () => { - valid({ + it("doesn't report implicit readonly array variables", async () => { + await valid({ code: dedent` const numbers = [1, 2, 3] as const; `, @@ -410,8 +410,8 @@ describe(name, () => { }); }); - it("doesn't report implicit readonly return types", () => { - valid({ + it("doesn't report implicit readonly return types", async () => { + await valid({ code: dedent` function foo() { return [1, 2, 3] as const; @@ -420,9 +420,9 @@ describe(name, () => { options: [{ checkImplicit: true }], }); - valid({ + await valid({ code: dedent` - const foo = () => { + const foo = async () => { return [1, 2, 3] as const; }; `, @@ -432,8 +432,8 @@ describe(name, () => { }); describe("ignoreClass", () => { - it("doesn't report classes", () => { - valid({ + it("doesn't report classes", async () => { + await valid({ code: dedent` class Klass { foo: number; @@ -446,8 +446,8 @@ describe(name, () => { }); }); - it("doesn't report classes - fieldsOnly", () => { - valid({ + it("doesn't report classes - fieldsOnly", async () => { + await valid({ code: dedent` class Klass { foo: number; @@ -460,8 +460,8 @@ describe(name, () => { }); }); - it("reports non-field issues in classes - fieldsOnly", () => { - const invalidResult = invalid({ + it("reports non-field issues in classes - fieldsOnly", async () => { + const invalidResult = await invalid({ code: dedent` class Klass { foo: number; @@ -479,13 +479,13 @@ describe(name, () => { options: [{ ignoreClass: "fieldsOnly" }], errors: ["property"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); describe("ignoreInterface", () => { - it("doesn't report interfaces", () => { - valid({ + it("doesn't report interfaces", async () => { + await valid({ code: dedent` interface Foo { foo: number, @@ -500,8 +500,8 @@ describe(name, () => { }); describe("ignorePattern", () => { - it("doesn't report matching identifiers", () => { - valid({ + it("doesn't report matching identifiers", async () => { + await valid({ code: dedent` let mutableFoo: string[] = []; `, @@ -511,8 +511,8 @@ describe(name, () => { }); describe("allowLocalMutation", () => { - it("doesn't report mutable local variables", () => { - valid({ + it("doesn't report mutable local variables", async () => { + await valid({ code: dedent` function foo() { let foo: { @@ -537,22 +537,22 @@ describe(name, () => { }); describe("ignoreCollections", () => { - it("doesn't report mutable arrays", () => { - valid({ + it("doesn't report mutable arrays", async () => { + await valid({ code: dedent` type Foo = Array; `, options: [{ ignoreCollections: true }], }); - valid({ + await valid({ code: dedent` const foo: number[] = []; `, options: [{ ignoreCollections: true }], }); - valid({ + await valid({ code: dedent` const foo = []; `, @@ -560,22 +560,22 @@ describe(name, () => { }); }); - it("doesn't report mutable tuples", () => { - valid({ + it("doesn't report mutable tuples", async () => { + await valid({ code: dedent` type Foo = [string, string]; `, options: [{ ignoreCollections: true }], }); - valid({ + await valid({ code: dedent` const foo: [string, string] = ['foo', 'bar']; `, options: [{ ignoreCollections: true }], }); - valid({ + await valid({ code: dedent` const foo = ['foo', 'bar']; `, @@ -583,15 +583,15 @@ describe(name, () => { }); }); - it("doesn't report mutable sets", () => { - valid({ + it("doesn't report mutable sets", async () => { + await valid({ code: dedent` type Foo = Set; `, options: [{ ignoreCollections: true }], }); - valid({ + await valid({ code: dedent` const foo: Set = new Set(); `, @@ -599,15 +599,15 @@ describe(name, () => { }); }); - it("doesn't report mutable maps", () => { - valid({ + it("doesn't report mutable maps", async () => { + await valid({ code: dedent` type Foo = Map; `, options: [{ ignoreCollections: true }], }); - valid({ + await valid({ code: dedent` const foo: Map = new Map(); `, diff --git a/tests/rules/prefer-tacit.test.ts b/tests/rules/prefer-tacit.test.ts index 802f747a1..11aeefb6c 100644 --- a/tests/rules/prefer-tacit.test.ts +++ b/tests/rules/prefer-tacit.test.ts @@ -14,84 +14,84 @@ describe(name, () => { configs: typescriptConfig, }); - it('reports functions that can "safely" be changed', () => { - const invalidResult1 = invalid({ + it('reports functions that can "safely" be changed', async () => { + const invalidResult1 = await invalid({ code: dedent` function f(x) {} const foo = x => f(x); `, errors: ["generic"], }); - expect(invalidResult1.messages).toMatchSnapshot(); + expect(invalidResult1.result).toMatchSnapshot(); - const invalidResult2 = invalid({ + const invalidResult2 = await invalid({ code: dedent` const foo = [1, 2, 3].map(x => Boolean(x)); `, errors: ["generic"], }); - expect(invalidResult2.messages).toMatchSnapshot(); + expect(invalidResult2.result).toMatchSnapshot(); }); - it("reports functions that are just instantiations", () => { - const invalidResult = invalid({ + it("reports functions that are just instantiations", async () => { + const invalidResult = await invalid({ code: dedent` function f(x: T): T {} function foo(x) { return f(x); } `, errors: ["generic"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("doesn't report functions without type defs", () => { - valid(dedent` + it("doesn't report functions without type defs", async () => { + await valid(dedent` function foo(x) { f(x); } `); - valid(dedent` + await valid(dedent` const foo = function (x) { f(x); }; `); - valid(dedent` + await valid(dedent` const foo = (x) => { f(x); }; `); }); - it("doesn't report functions using a different number of parameters", () => { - valid(dedent` + it("doesn't report functions using a different number of parameters", async () => { + await valid(dedent` function f(x, y) {} const foo = x => f(x); `); - valid(dedent` + await valid(dedent` const a = ['1', '2']; a.map((x) => Number.parseInt(x)); `); }); - it("doesn't report functions using default parameters", () => { - valid(dedent` + it("doesn't report functions using default parameters", async () => { + await valid(dedent` function f(x, y = 1) {} const foo = x => f(x); `); }); - it("doesn't report functions with optional parameters", () => { - valid(dedent` + it("doesn't report functions with optional parameters", async () => { + await valid(dedent` function f(x: number, y?: nunber) {} const foo = x => f(x); `); }); - it("doesn't report instantiation expressions", () => { - valid(dedent` + it("doesn't report instantiation expressions", async () => { + await valid(dedent` function f(x) {} const foo = f; `); @@ -99,8 +99,8 @@ describe(name, () => { describe("options", () => { describe("checkMemberExpressions", () => { - it("doesn't report member expressions when disabled", () => { - valid({ + it("doesn't report member expressions when disabled", async () => { + await valid({ code: dedent` declare const a: { b(arg: string): string; }; function foo(x) { return a.b(x); } @@ -108,7 +108,7 @@ describe(name, () => { options: [{ checkMemberExpressions: false }], }); - valid({ + await valid({ code: dedent` [''].filter(str => /a/.test(str)) `, @@ -116,8 +116,8 @@ describe(name, () => { }); }); - it("report member expressions when enabled", () => { - const invalidResult1 = invalid({ + it("report member expressions when enabled", async () => { + const invalidResult1 = await invalid({ code: dedent` declare const a: { b(arg: string): string; }; function foo(x) { return a.b(x); } @@ -125,16 +125,16 @@ describe(name, () => { options: [{ checkMemberExpressions: true }], errors: ["generic"], }); - expect(invalidResult1.messages).toMatchSnapshot(); + expect(invalidResult1.result).toMatchSnapshot(); - const invalidResult2 = invalid({ + const invalidResult2 = await invalid({ code: dedent` [''].filter(str => /a/.test(str)) `, options: [{ checkMemberExpressions: true }], errors: ["generic"], }); - expect(invalidResult2.messages).toMatchSnapshot(); + expect(invalidResult2.result).toMatchSnapshot(); }); }); }); diff --git a/tests/rules/type-declaration-immutability.test.ts b/tests/rules/type-declaration-immutability.test.ts index 74273a6e0..5406e3726 100644 --- a/tests/rules/type-declaration-immutability.test.ts +++ b/tests/rules/type-declaration-immutability.test.ts @@ -42,8 +42,8 @@ describe(name, () => { configs: typescriptConfig, }); - it("reports invalid shallow records", () => { - const invalidResult1 = invalid({ + it("reports invalid shallow records", async () => { + const invalidResult1 = await invalid({ code: dedent` type ReadonlyFoo = { foo: number; @@ -52,9 +52,9 @@ describe(name, () => { options, errors: ["AtLeast"], }); - expect(invalidResult1.messages).toMatchSnapshot(); + expect(invalidResult1.result).toMatchSnapshot(); - const invalidResult2 = invalid({ + const invalidResult2 = await invalid({ code: dedent` type ReadonlyFoo = { readonly foo: number; @@ -66,11 +66,11 @@ describe(name, () => { options, errors: ["AtLeast"], }); - expect(invalidResult2.messages).toMatchSnapshot(); + expect(invalidResult2.result).toMatchSnapshot(); }); - it("reports invalid immutable/deep records", () => { - const invalidResult1 = invalid({ + it("reports invalid immutable/deep records", async () => { + const invalidResult1 = await invalid({ code: dedent` type ReadonlyDeepFoo = { readonly foo: number; @@ -82,9 +82,9 @@ describe(name, () => { options, errors: ["AtLeast"], }); - expect(invalidResult1.messages).toMatchSnapshot(); + expect(invalidResult1.result).toMatchSnapshot(); - const invalidResult2 = invalid({ + const invalidResult2 = await invalid({ code: dedent` type ImmutableFoo = { readonly foo: number; @@ -96,101 +96,101 @@ describe(name, () => { options, errors: ["AtLeast"], }); - expect(invalidResult2.messages).toMatchSnapshot(); + expect(invalidResult2.result).toMatchSnapshot(); }); - it("reports invalid shallow arrays", () => { - const invalidResult = invalid({ + it("reports invalid shallow arrays", async () => { + const invalidResult = await invalid({ code: `type ReadonlyMyArray = Array;`, options, errors: ["AtLeast"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports invalid deep arrays", () => { - const invalidResult = invalid({ + it("reports invalid deep arrays", async () => { + const invalidResult = await invalid({ code: `type ReadonlyDeepMyArray = ReadonlyArray<{ foo: string; }>;`, options, errors: ["AtLeast"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports invalid immutable arrays", () => { - const invalidResult = invalid({ + it("reports invalid immutable arrays", async () => { + const invalidResult = await invalid({ code: `type ImmutableMyArray = ReadonlyArray<{ readonly foo: string; }>;`, options, errors: ["AtLeast"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports invalid shallow sets", () => { - const invalidResult = invalid({ + it("reports invalid shallow sets", async () => { + const invalidResult = await invalid({ code: `type ReadonlyMySet = Set;`, options, errors: ["AtLeast"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports invalid deep sets", () => { - const invalidResult = invalid({ + it("reports invalid deep sets", async () => { + const invalidResult = await invalid({ code: `type ReadonlyDeepMySet = ReadonlySet<{ foo: string; }>;`, options, errors: ["AtLeast"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports invalid immutable sets", () => { - const invalidResult = invalid({ + it("reports invalid immutable sets", async () => { + const invalidResult = await invalid({ code: `type ImmutableMySet = ReadonlySet<{ readonly foo: string; }>;`, options, errors: ["AtLeast"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports invalid shallow maps", () => { - const invalidResult = invalid({ + it("reports invalid shallow maps", async () => { + const invalidResult = await invalid({ code: `type ReadonlyMyMap = Map;`, options, errors: ["AtLeast"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports invalid deep maps", () => { - const invalidResult = invalid({ + it("reports invalid deep maps", async () => { + const invalidResult = await invalid({ code: `type ReadonlyDeepMyMap = ReadonlyMap;`, options, errors: ["AtLeast"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports invalid immutable maps", () => { - const invalidResult = invalid({ + it("reports invalid immutable maps", async () => { + const invalidResult = await invalid({ code: `type ImmutableMyMap = ReadonlyMap;`, options, errors: ["AtLeast"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports non-mutable primitives", () => { - const invalidResult = invalid({ + it("reports non-mutable primitives", async () => { + const invalidResult = await invalid({ code: "type MutableString = string;", options, errors: ["AtMost"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); - it("reports non-mutable records", () => { - const invalidResult1 = invalid({ + it("reports non-mutable records", async () => { + const invalidResult1 = await invalid({ code: dedent` type MutableFoo = { readonly foo: number; @@ -199,9 +199,9 @@ describe(name, () => { options, errors: ["AtMost"], }); - expect(invalidResult1.messages).toMatchSnapshot(); + expect(invalidResult1.result).toMatchSnapshot(); - const invalidResult2 = invalid({ + const invalidResult2 = await invalid({ code: dedent` type MutableFoo = Readonly<{ foo: number; @@ -210,109 +210,109 @@ describe(name, () => { options, errors: ["AtMost"], }); - expect(invalidResult2.messages).toMatchSnapshot(); + expect(invalidResult2.result).toMatchSnapshot(); }); - it("doesn't report mutable sets", () => { - valid({ + it("doesn't report mutable sets", async () => { + await valid({ code: `type MutableSet = Set;`, options, }); }); - it("doesn't report mutable map", () => { - valid({ + it("doesn't report mutable map", async () => { + await valid({ code: `type MutableMap = Map;`, options, }); }); - it("doesn't report valid types", () => { - valid({ + it("doesn't report valid types", async () => { + await valid({ code: "type ReadonlyString = string;", options, }); - valid({ + await valid({ code: "type ReadonlyFoo = { readonly foo: number };", options, }); - valid({ + await valid({ code: "type ReadonlyFoo = Readonly<{ foo: number }>;", options, }); - valid({ + await valid({ code: "type ReadonlyFoo = { readonly foo: number; readonly bar: { baz: string; }; };", options, }); - valid({ + await valid({ code: "type ReadonlySet = ReadonlySet;", options, }); - valid({ + await valid({ code: "type ReadonlyMap = ReadonlyMap;", options, }); - valid({ + await valid({ code: "type ReadonlyDeepString = string;", options, }); - valid({ + await valid({ code: "type ReadonlyDeepFoo = { readonly foo: number; readonly bar: { readonly baz: string; }; };", options, }); - valid({ + await valid({ code: "type ReadonlyDeepSet = ReadonlySet;", options, }); - valid({ + await valid({ code: "type ReadonlyDeepMap = ReadonlyMap;", options, }); - valid({ + await valid({ code: "type ReadonlyDeepSet = ReadonlySet<{ readonly foo: string; }>;", options, }); - valid({ + await valid({ code: "type ReadonlyDeepMap = ReadonlyMap;", options, }); - valid({ + await valid({ code: "type ImmutableString = string;", options, }); - valid({ + await valid({ code: "type ImmutableFoo = { readonly foo: number; readonly bar: { readonly baz: string; }; };", options, }); - valid({ + await valid({ code: "type ImmutableSet = Readonly>;", options, }); - valid({ + await valid({ code: "type ImmutableMap = Readonly>;", options, }); - valid({ + await valid({ code: "type MutableFoo = { foo: number };", options, }); - valid({ + await valid({ code: "type MutableFoo = { readonly foo: number; bar: { readonly baz: string; }; };", options, }); - valid({ + await valid({ code: "type MutableSet = Set<{ readonly foo: string; }>;", options, }); - valid({ + await valid({ code: "type MutableMap = Map;", options, }); }); describe("ignoreInterfaces", () => { - it("doesn't report interfaces when enabled", () => { - valid({ + it("doesn't report interfaces when enabled", async () => { + await valid({ code: dedent` interface ReadonlyFoo { foo: number; @@ -324,8 +324,8 @@ describe(name, () => { }); describe("ignoreIdentifierPattern", () => { - it("doesn't report interfaces when enabled", () => { - valid({ + it("doesn't report interfaces when enabled", async () => { + await valid({ code: dedent` type ReadonlyFoo = { foo: number; @@ -340,8 +340,8 @@ describe(name, () => { }); }); - it("respects override settings", () => { - valid({ + it("respects override settings", async () => { + await valid({ code: dedent` type ReadonlyDeepFoo = ReadonlyDeep<{ foo: { bar: string; }; }>; type ReadonlyDeep = T; @@ -359,7 +359,7 @@ describe(name, () => { }, }); - const invalidResult = invalid({ + const invalidResult = await invalid({ code: `type MutableSet = Set;`, options, settings: { @@ -377,7 +377,7 @@ describe(name, () => { }, errors: ["AtMost"], }); - expect(invalidResult.messages).toMatchSnapshot(); + expect(invalidResult.result).toMatchSnapshot(); }); }); });