Skip to content

Commit e9f2f90

Browse files
fix(type-declaration-immutability): only allow strings to be given for identifiers (#573)
1 parent b9aeed5 commit e9f2f90

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

src/configs/recommended.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ const overrides: Linter.Config = {
6060
{
6161
rules: [
6262
{
63-
identifiers: [/^I?Immutable.+/u],
63+
identifiers: ["^I?Immutable.+"],
6464
immutability: Immutability.Immutable,
6565
comparator: RuleEnforcementComparator.AtLeast,
6666
},
6767
{
68-
identifiers: [/^I?ReadonlyDeep.+/u],
68+
identifiers: ["^I?ReadonlyDeep.+"],
6969
immutability: Immutability.ReadonlyDeep,
7070
comparator: RuleEnforcementComparator.AtLeast,
7171
},
7272
{
73-
identifiers: [/^I?Readonly.+/u],
73+
identifiers: ["^I?Readonly.+"],
7474
immutability: Immutability.ReadonlyShallow,
7575
comparator: RuleEnforcementComparator.AtLeast,
7676
fixer: [
@@ -85,7 +85,7 @@ const overrides: Linter.Config = {
8585
],
8686
},
8787
{
88-
identifiers: [/^I?Mutable.+/u],
88+
identifiers: ["^I?Mutable.+"],
8989
immutability: Immutability.Mutable,
9090
comparator: RuleEnforcementComparator.AtMost,
9191
fixer: [

src/rules/type-declaration-immutability.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type FixerConfig = {
4343
type Options = [
4444
IgnorePatternOption & {
4545
rules: Array<{
46-
identifiers: (string | RegExp) | Array<string | RegExp>;
46+
identifiers: string | string[];
4747
immutability: Exclude<
4848
Immutability | keyof typeof Immutability,
4949
"Unknown"
@@ -101,9 +101,9 @@ const schema: JSONSchema4 = [
101101
type: "object",
102102
properties: {
103103
identifiers: {
104-
type: ["string", "object", "array"],
104+
type: ["string", "array"],
105105
items: {
106-
type: ["string", "object"],
106+
type: ["string"],
107107
},
108108
},
109109
immutability: {
@@ -139,7 +139,7 @@ const defaultOptions: Options = [
139139
{
140140
rules: [
141141
{
142-
identifiers: [/^(?!I?Mutable).+/u],
142+
identifiers: ["^(?!I?Mutable).+"],
143143
immutability: Immutability.Immutable,
144144
comparator: RuleEnforcementComparator.AtLeast,
145145
},
@@ -201,14 +201,8 @@ function getRules(options: Options): ImmutabilityRule[] {
201201

202202
return rulesOptions.map((rule): ImmutabilityRule => {
203203
const identifiers = Array.isArray(rule.identifiers)
204-
? rule.identifiers.map((id) =>
205-
id instanceof RegExp ? id : new RegExp(id, "u")
206-
)
207-
: [
208-
rule.identifiers instanceof RegExp
209-
? rule.identifiers
210-
: new RegExp(rule.identifiers, "u"),
211-
];
204+
? rule.identifiers.map((id) => new RegExp(id, "u"))
205+
: [new RegExp(rule.identifiers, "u")];
212206

213207
const immutability =
214208
typeof rule.immutability === "string"

tests/rules/type-declaration-immutability/ts/invalid.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import type { InvalidTestCase } from "~/tests/helpers/util";
66
const recommended = {
77
rules: [
88
{
9-
identifiers: [/^I?Immutable.+/u],
9+
identifiers: ["^I?Immutable.+"],
1010
immutability: Immutability.Immutable,
1111
comparator: "AtLeast",
1212
},
1313
{
14-
identifiers: [/^I?ReadonlyDeep.+/u],
14+
identifiers: ["^I?ReadonlyDeep.+"],
1515
immutability: Immutability.ReadonlyDeep,
1616
comparator: "AtLeast",
1717
},
1818
{
19-
identifiers: [/^I?Readonly.+/u],
19+
identifiers: ["^I?Readonly.+"],
2020
immutability: Immutability.ReadonlyShallow,
2121
comparator: "AtLeast",
2222
fixer: [
@@ -31,7 +31,7 @@ const recommended = {
3131
],
3232
},
3333
{
34-
identifiers: [/^I?Mutable.+/u],
34+
identifiers: ["^I?Mutable.+"],
3535
immutability: Immutability.Mutable,
3636
comparator: "AtMost",
3737
fixer: [

tests/rules/type-declaration-immutability/ts/valid.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import type { ValidTestCase } from "~/tests/helpers/util";
66
const recommended = {
77
rules: [
88
{
9-
identifiers: [/^I?Immutable.+/u],
9+
identifiers: ["^I?Immutable.+"],
1010
immutability: Immutability.Immutable,
1111
comparator: "AtLeast",
1212
},
1313
{
14-
identifiers: [/^I?ReadonlyDeep.+/u],
14+
identifiers: ["^I?ReadonlyDeep.+"],
1515
immutability: Immutability.ReadonlyDeep,
1616
comparator: "AtLeast",
1717
},
1818
{
19-
identifiers: [/^I?Readonly.+/u],
19+
identifiers: ["^I?Readonly.+"],
2020
immutability: Immutability.ReadonlyShallow,
2121
comparator: "AtLeast",
2222
},
2323
{
24-
identifiers: [/^I?Mutable.+/u],
24+
identifiers: ["^I?Mutable.+"],
2525
immutability: Immutability.Mutable,
2626
comparator: "AtMost",
2727
},

0 commit comments

Comments
 (0)