Skip to content

Commit

Permalink
fix(type-declaration-immutability): don't strip whitespace formatting…
Browse files Browse the repository at this point in the history
… from node text
  • Loading branch information
RebeccaStevens committed Jan 28, 2023
1 parent b5bd427 commit 6d429f3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/rules/type-declaration-immutability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function getRules(options: Options): ImmutabilityRule[] {
? false
: (Array.isArray(rule.fixer) ? rule.fixer : [rule.fixer]).map((r) => ({
...r,
pattern: new RegExp(r.pattern, "u"),
pattern: new RegExp(r.pattern, "us"),
}));

return {
Expand Down Expand Up @@ -273,7 +273,7 @@ function getConfiuredFixer<T extends TSESTree.Node>(
context: TSESLint.RuleContext<keyof typeof errorMessages, Options>,
configs: FixerConfig[]
): NonNullable<Descriptor["fix"]> | null {
const text = context.getSourceCode().getText(node).replaceAll(/\s+/gmu, " ");
const text = context.getSourceCode().getText(node);
const config = configs.find((c) => c.pattern.test(text));
if (config === undefined) {
return null;
Expand Down
116 changes: 92 additions & 24 deletions tests/rules/type-declaration-immutability/ts/invalid.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dedent from "dedent";
import { Immutability } from "is-immutable-type";

import type { InvalidTestCase } from "~/tests/helpers/util";
Expand Down Expand Up @@ -49,9 +50,13 @@ const recommended = {

const tests: InvalidTestCase[] = [
{
code: "type ReadonlyFoo = { foo: number }",
code: dedent`
type ReadonlyFoo = { foo: number }
`,
optionsSet: [[recommended]],
output: "type ReadonlyFoo = Readonly<{ foo: number }>",
output: dedent`
type ReadonlyFoo = Readonly<{ foo: number }>
`,
errors: [
{
messageId: "AtLeast",
Expand All @@ -66,10 +71,23 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type ReadonlyFoo = { readonly foo: number; bar: { baz: string; }; }",
code: dedent`
type ReadonlyFoo = {
readonly foo: number;
bar: {
baz: string;
};
}
`,
optionsSet: [[recommended]],
output:
"type ReadonlyFoo = Readonly<{ readonly foo: number; bar: { baz: string; }; }>",
output: dedent`
type ReadonlyFoo = Readonly<{
readonly foo: number;
bar: {
baz: string;
};
}>
`,
errors: [
{
messageId: "AtLeast",
Expand All @@ -84,9 +102,13 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type ReadonlyMySet = Set<string>;",
code: dedent`
type ReadonlyMySet = Set<string>;
`,
optionsSet: [[recommended]],
output: "type ReadonlyMySet = ReadonlySet<string>;",
output: dedent`
type ReadonlyMySet = ReadonlySet<string>;
`,
errors: [
{
messageId: "AtLeast",
Expand All @@ -101,8 +123,12 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type ReadonlyMyMap = Map<string, string>;",
output: "type ReadonlyMyMap = ReadonlyMap<string, string>;",
code: dedent`
type ReadonlyMyMap = Map<string, string>;
`,
output: dedent`
type ReadonlyMyMap = ReadonlyMap<string, string>;
`,
optionsSet: [[recommended]],
errors: [
{
Expand All @@ -118,7 +144,12 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type ReadonlyDeepFoo = { readonly foo: number; readonly bar: { baz: string; }; }",
code: dedent`
type ReadonlyDeepFoo = {
readonly foo: number;
readonly bar: { baz: string; };
}
`,
optionsSet: [[recommended]],
errors: [
{
Expand All @@ -134,7 +165,9 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type ReadonlyDeepSet = ReadonlySet<{ foo: string; }>;",
code: dedent`
type ReadonlyDeepSet = ReadonlySet<{ foo: string; }>;
`,
optionsSet: [[recommended]],
errors: [
{
Expand All @@ -150,7 +183,9 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type ReadonlyDeepMap = ReadonlyMap<string, { foo: string; }>;",
code: dedent`
type ReadonlyDeepMap = ReadonlyMap<string, { foo: string; }>;
`,
optionsSet: [[recommended]],
errors: [
{
Expand All @@ -166,7 +201,12 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type ImmutableFoo = { readonly foo: number; readonly bar: { baz: string; }; }",
code: dedent`
type ImmutableFoo = {
readonly foo: number;
readonly bar: { baz: string; };
}
`,
optionsSet: [[recommended]],
errors: [
{
Expand All @@ -182,7 +222,9 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type ImmutableSet = ReadonlySet<{ readonly foo: string; }>;",
code: dedent`
type ImmutableSet = ReadonlySet<{ readonly foo: string; }>;
`,
optionsSet: [[recommended]],
errors: [
{
Expand All @@ -198,7 +240,9 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type ImmutableMap = ReadonlyMap<string, { readonly foo: string; }>;",
code: dedent`
type ImmutableMap = ReadonlyMap<string, { readonly foo: string; }>;
`,
optionsSet: [[recommended]],
errors: [
{
Expand All @@ -214,7 +258,9 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type MutableString = string",
code: dedent`
type MutableString = string
`,
optionsSet: [[recommended]],
errors: [
{
Expand All @@ -230,7 +276,9 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type MutableFoo = { readonly foo: number }",
code: dedent`
type MutableFoo = { readonly foo: number }
`,
optionsSet: [[recommended]],
errors: [
{
Expand All @@ -246,9 +294,13 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type MutableFoo = Readonly<{ foo: number }>",
code: dedent`
type MutableFoo = Readonly<{ foo: number }>
`,
optionsSet: [[recommended]],
output: "type MutableFoo = { foo: number }",
output: dedent`
type MutableFoo = { foo: number }
`,
errors: [
{
messageId: "AtMost",
Expand All @@ -263,7 +315,12 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type MutableFoo = { readonly foo: number; readonly bar: { baz: string; }; }",
code: dedent`
type MutableFoo = {
readonly foo: number;
readonly bar: { baz: string; };
}
`,
optionsSet: [[recommended]],
errors: [
{
Expand All @@ -279,7 +336,9 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type Foo = { foo: number }",
code: dedent`
type Foo = { foo: number }
`,
optionsSet: [
[
{
Expand All @@ -306,7 +365,12 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type ReadonlyFoo = { readonly foo: number; readonly bar: { baz: string; }; };",
code: dedent`
type ReadonlyFoo = {
readonly foo: number;
readonly bar: { baz: string; };
};
`,
optionsSet: [
[
{
Expand All @@ -333,7 +397,9 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type MutableSet = Set<string>;",
code: dedent`
type MutableSet = Set<string>;
`,
optionsSet: [[recommended]],
settingsSet: [
{
Expand All @@ -358,7 +424,9 @@ const tests: InvalidTestCase[] = [
],
},
{
code: "type MutableSet = Set<string>;",
code: dedent`
type MutableSet = Set<string>;
`,
optionsSet: [[recommended]],
settingsSet: [
{
Expand Down

0 comments on commit 6d429f3

Please sign in to comment.