From 5b7c9528a8b6185519d3a784f1efbcea34859509 Mon Sep 17 00:00:00 2001 From: Rafael Santana Date: Tue, 21 Sep 2021 12:26:57 -0300 Subject: [PATCH] fix(experimental-utils): support immutable members --- .../src/rules/no-magic-numbers.ts | 41 +++++++++---------- .../experimental-utils/src/ts-eslint/Rule.ts | 18 ++++---- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-magic-numbers.ts b/packages/eslint-plugin/src/rules/no-magic-numbers.ts index 1f45a489d35..ac079f62d43 100644 --- a/packages/eslint-plugin/src/rules/no-magic-numbers.ts +++ b/packages/eslint-plugin/src/rules/no-magic-numbers.ts @@ -1,6 +1,6 @@ import { - TSESTree, AST_NODE_TYPES, + TSESTree, } from '@typescript-eslint/experimental-utils'; import baseRule from 'eslint/lib/rules/no-magic-numbers'; import * as util from '../util'; @@ -8,9 +8,23 @@ import * as util from '../util'; type Options = util.InferOptionsTypeFromRule; type MessageIds = util.InferMessageIdsTypeFromRule; -const baseRuleSchema = Array.isArray(baseRule.meta.schema) - ? baseRule.meta.schema[0] - : baseRule.meta.schema; +// Extend base schema with additional property to ignore TS numeric literal types +const schema = util.deepMerge( + { ...baseRule.meta.schema }, + { + properties: { + ignoreNumericLiteralTypes: { + type: 'boolean', + }, + ignoreEnums: { + type: 'boolean', + }, + ignoreReadonlyClassProperties: { + type: 'boolean', + }, + }, + }, +); export default util.createRule({ name: 'no-magic-numbers', @@ -22,24 +36,7 @@ export default util.createRule({ recommended: false, extendsBaseRule: true, }, - // Extend base schema with additional property to ignore TS numeric literal types - schema: [ - { - ...baseRuleSchema, - properties: { - ...baseRuleSchema.properties, - ignoreNumericLiteralTypes: { - type: 'boolean', - }, - ignoreEnums: { - type: 'boolean', - }, - ignoreReadonlyClassProperties: { - type: 'boolean', - }, - }, - }, - ], + schema: [schema], messages: baseRule.meta.messages ?? { useConst: "Number constants declarations must use 'const'.", noMagic: 'No magic number: {{raw}}.', diff --git a/packages/experimental-utils/src/ts-eslint/Rule.ts b/packages/experimental-utils/src/ts-eslint/Rule.ts index 24b8fd4fa11..bdabc3fe883 100644 --- a/packages/experimental-utils/src/ts-eslint/Rule.ts +++ b/packages/experimental-utils/src/ts-eslint/Rule.ts @@ -73,15 +73,15 @@ interface RuleMetaData { /** * The name of the rule this rule was replaced by, if it was deprecated. */ - replacedBy?: string[]; + replacedBy?: readonly string[]; /** * The options schema. Supply an empty array if there are no options. */ - schema: JSONSchema4 | JSONSchema4[]; + schema: JSONSchema4 | readonly JSONSchema4[]; } interface RuleFix { - range: AST.Range; + range: Readonly; text: string; } @@ -91,25 +91,25 @@ interface RuleFixer { text: string, ): RuleFix; - insertTextAfterRange(range: AST.Range, text: string): RuleFix; + insertTextAfterRange(range: Readonly, text: string): RuleFix; insertTextBefore( nodeOrToken: TSESTree.Node | TSESTree.Token, text: string, ): RuleFix; - insertTextBeforeRange(range: AST.Range, text: string): RuleFix; + insertTextBeforeRange(range: Readonly, text: string): RuleFix; remove(nodeOrToken: TSESTree.Node | TSESTree.Token): RuleFix; - removeRange(range: AST.Range): RuleFix; + removeRange(range: Readonly): RuleFix; replaceText( nodeOrToken: TSESTree.Node | TSESTree.Token, text: string, ): RuleFix; - replaceTextRange(range: AST.Range, text: string): RuleFix; + replaceTextRange(range: Readonly, text: string): RuleFix; } type ReportFixFunction = ( @@ -209,13 +209,13 @@ interface RuleContext< * the root of the AST and continuing through the direct parent of the current node. * This array does not include the currently-traversed node itself. */ - getAncestors(): TSESTree.Node[]; + getAncestors(): readonly TSESTree.Node[]; /** * Returns a list of variables declared by the given node. * This information can be used to track references to variables. */ - getDeclaredVariables(node: TSESTree.Node): Scope.Variable[]; + getDeclaredVariables(node: TSESTree.Node): readonly Scope.Variable[]; /** * Returns the current working directory passed to Linter.