Skip to content

Commit

Permalink
fix(prefer-immutable-types): inheriting of config options
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Feb 6, 2023
1 parent 8efc7dd commit b528bc7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
71 changes: 30 additions & 41 deletions src/rules/prefer-immutable-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,21 @@ function getParameterTypeViolations(
const { parameters: rawOption, fixer: rawFixerConfig } = optionsObject;
const {
enforcement: rawEnforcement,
ignoreInferredTypes: rawIgnoreInferredTypes,
ignoreInferredTypes,
ignoreClasses,
ignoreNamePattern,
ignoreTypePattern,
} = typeof rawOption === "object"
? rawOption
: {
enforcement: rawOption,
ignoreInferredTypes: optionsObject.ignoreInferredTypes,
ignoreClasses: optionsObject.ignoreClasses,
ignoreNamePattern: optionsObject.ignoreNamePattern,
ignoreTypePattern: optionsObject.ignoreTypePattern,
};
} = {
ignoreInferredTypes: optionsObject.ignoreInferredTypes,
ignoreClasses: optionsObject.ignoreClasses,
ignoreNamePattern: optionsObject.ignoreNamePattern,
ignoreTypePattern: optionsObject.ignoreTypePattern,
...(typeof rawOption === "object"
? rawOption
: {
enforcement: rawOption,
}),
};

const enforcement = parseEnforcement(
rawEnforcement ?? optionsObject.enforcement
Expand All @@ -375,9 +377,6 @@ function getParameterTypeViolations(

const fixerConfigs = parseFixerConfigs(rawFixerConfig, enforcement);

const ignoreInferredTypes =
rawIgnoreInferredTypes ?? optionsObject.ignoreInferredTypes;

return node.params
.map((param): Descriptor | undefined => {
if (shouldIgnorePattern(param, context, ignoreNamePattern)) {
Expand Down Expand Up @@ -463,27 +462,22 @@ function getReturnTypeViolations(
const { returnTypes: rawOption, fixer: rawFixerConfig } = optionsObject;
const {
enforcement: rawEnforcement,
ignoreInferredTypes: rawIgnoreInferredTypes,
ignoreInferredTypes,
ignoreClasses,
ignoreNamePattern,
ignoreTypePattern,
} = typeof rawOption === "object"
? rawOption
: {
enforcement: rawOption,
ignoreInferredTypes: optionsObject.ignoreInferredTypes,
ignoreClasses: optionsObject.ignoreClasses,
ignoreNamePattern: optionsObject.ignoreNamePattern,
ignoreTypePattern: optionsObject.ignoreTypePattern,
};
} = {
ignoreInferredTypes: optionsObject.ignoreInferredTypes,
ignoreClasses: optionsObject.ignoreClasses,
ignoreNamePattern: optionsObject.ignoreNamePattern,
ignoreTypePattern: optionsObject.ignoreTypePattern,
...(typeof rawOption === "object" ? rawOption : { enforcement: rawOption }),
};

const enforcement = parseEnforcement(
rawEnforcement ?? optionsObject.enforcement
);

const ignoreInferredTypes =
rawIgnoreInferredTypes ?? optionsObject.ignoreInferredTypes;

if (
enforcement === false ||
(ignoreInferredTypes && node.returnType?.typeAnnotation === undefined) ||
Expand Down Expand Up @@ -612,26 +606,23 @@ function checkVarible(
const { variables: rawOption, fixer: rawFixerConfig } = optionsObject;
const {
enforcement: rawEnforcement,
ignoreInferredTypes: rawIgnoreInferredTypes,
ignoreInferredTypes,
ignoreClasses,
ignoreNamePattern,
ignoreTypePattern,
ignoreInFunctions: rawIgnoreInFunctions,
} = typeof rawOption === "object"
? rawOption
: {
enforcement: rawOption,
ignoreInferredTypes: optionsObject.ignoreInferredTypes,
ignoreClasses: optionsObject.ignoreClasses,
ignoreNamePattern: optionsObject.ignoreNamePattern,
ignoreTypePattern: optionsObject.ignoreTypePattern,
ignoreInFunctions: false,
};
ignoreInFunctions,
} = {
ignoreInferredTypes: optionsObject.ignoreInferredTypes,
ignoreClasses: optionsObject.ignoreClasses,
ignoreNamePattern: optionsObject.ignoreNamePattern,
ignoreTypePattern: optionsObject.ignoreTypePattern,
ignoreInFunctions: false,
...(typeof rawOption === "object" ? rawOption : { enforcement: rawOption }),
};

const enforcement = parseEnforcement(
rawEnforcement ?? optionsObject.enforcement
);
const ignoreInFunctions = rawIgnoreInFunctions ?? false;

if (
enforcement === false ||
Expand Down Expand Up @@ -663,8 +654,6 @@ function checkVarible(
};
}

const ignoreInferredTypes =
rawIgnoreInferredTypes ?? optionsObject.ignoreInferredTypes;
const nodeWithTypeAnnotation = propertyDefinition ? node : node.id;

if (
Expand Down
16 changes: 16 additions & 0 deletions tests/rules/prefer-immutable-types/ts/parameters/valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ const tests: ReadonlyArray<ValidTestCase> = [
`,
optionsSet: [[{ ignoreNamePattern: "^mutable" }]],
},
// Inherit Ignore Name Prefix.
{
code: dedent`
function foo(mutableArg: string[]) {}
`,
optionsSet: [
[
{
ignoreNamePattern: "^mutable",
parameters: {
enforcement: "Immutable",
},
},
],
],
},
// Ignore Name Suffix.
{
code: dedent`
Expand Down

0 comments on commit b528bc7

Please sign in to comment.