Skip to content

fix(experimental-utils): support immutable members #3844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/no-empty-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;

const schema = util.deepMerge(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- https://github.com/microsoft/TypeScript/issues/17002
Array.isArray(baseRule.meta.schema)
? baseRule.meta.schema[0]
: baseRule.meta.schema,
Expand Down
44 changes: 20 additions & 24 deletions packages/eslint-plugin/src/rules/no-magic-numbers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import {
TSESTree,
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { getESLintCoreRule } from '../util/getESLintCoreRule';
import * as util from '../util';
import { getESLintCoreRule } from '../util/getESLintCoreRule';

const baseRule = getESLintCoreRule('no-magic-numbers');

type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;

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<Options, MessageIds>({
name: 'no-magic-numbers',
Expand All @@ -23,25 +37,7 @@ export default util.createRule<Options, MessageIds>({
recommended: false,
extendsBaseRule: true,
},
hasSuggestions: baseRule.meta.hasSuggestions,
// 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,
},
defaultOptions: [
Expand Down
16 changes: 8 additions & 8 deletions packages/experimental-utils/src/ts-eslint/Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ interface RuleMetaData<TMessageIds extends string> {
/**
* 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<AST.Range>;
text: string;
}

Expand All @@ -87,25 +87,25 @@ interface RuleFixer {
text: string,
): RuleFix;

insertTextAfterRange(range: AST.Range, text: string): RuleFix;
insertTextAfterRange(range: Readonly<AST.Range>, text: string): RuleFix;

insertTextBefore(
nodeOrToken: TSESTree.Node | TSESTree.Token,
text: string,
): RuleFix;

insertTextBeforeRange(range: AST.Range, text: string): RuleFix;
insertTextBeforeRange(range: Readonly<AST.Range>, text: string): RuleFix;

remove(nodeOrToken: TSESTree.Node | TSESTree.Token): RuleFix;

removeRange(range: AST.Range): RuleFix;
removeRange(range: Readonly<AST.Range>): RuleFix;

replaceText(
nodeOrToken: TSESTree.Node | TSESTree.Token,
text: string,
): RuleFix;

replaceTextRange(range: AST.Range, text: string): RuleFix;
replaceTextRange(range: Readonly<AST.Range>, text: string): RuleFix;
}

interface SuggestionReportDescriptor<TMessageIds extends string>
Expand Down Expand Up @@ -216,7 +216,7 @@ interface RuleContext<
* 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.
Expand Down
4 changes: 2 additions & 2 deletions packages/experimental-utils/src/ts-eslint/RuleTester.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RuleTester as ESLintRuleTester } from 'eslint';
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '../ts-estree';
import { ParserOptions } from './ParserOptions';
import { Linter } from './Linter';
import { ParserOptions } from './ParserOptions';
import {
RuleCreateFunction,
RuleModule,
Expand Down Expand Up @@ -112,7 +112,7 @@ interface TestCaseError<TMessageIds extends string> {
/**
* Reported suggestions.
*/
readonly suggestions?: SuggestionOutput<TMessageIds>[] | null;
readonly suggestions?: readonly SuggestionOutput<TMessageIds>[] | null;
/**
* The type of the reported AST node.
*/
Expand Down