Skip to content

Commit

Permalink
refactor: normalize options indexes access
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelss95 committed Oct 12, 2021
1 parent 1b99c67 commit 612f02d
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 31 deletions.
8 changes: 3 additions & 5 deletions packages/eslint-plugin-tslint/src/rules/config.ts
Expand Up @@ -104,11 +104,9 @@ export default createRule<Options, MessageIds>({
/**
* The TSLint rules configuration passed in by the user
*/
const {
rules: tslintRules,
rulesDirectory: tslintRulesDirectory,
lintFile,
} = context.options[0];
const [
{ rules: tslintRules, rulesDirectory: tslintRulesDirectory, lintFile },
] = context.options;

const program = parserServices.program;

Expand Down
9 changes: 4 additions & 5 deletions packages/eslint-plugin/src/rules/init-declarations.ts
@@ -1,12 +1,12 @@
import {
TSESTree,
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { getESLintCoreRule } from '../util/getESLintCoreRule';
import {
InferOptionsTypeFromRule,
InferMessageIdsTypeFromRule,
createRule,
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../util';

const baseRule = getESLintCoreRule('init-declarations');
Expand All @@ -29,9 +29,8 @@ export default createRule<Options, MessageIds>({
messages: baseRule.meta.messages,
},
defaultOptions: ['always'],
create(context) {
create(context, [mode]) {
const rules = baseRule.create(context);
const mode = context.options[0] || 'always';

return {
'VariableDeclaration:exit'(node: TSESTree.VariableDeclaration): void {
Expand Down
Expand Up @@ -43,10 +43,10 @@ export default util.createRule<Options, MessageIds>({
exceptAfterSingleLine: false,
},
],
create(context, options) {
create(context, [firstOption, secondOption]) {
const rules = baseRule.create(context);
const exceptAfterOverload =
options[1]?.exceptAfterOverload && options[0] === 'always';
secondOption?.exceptAfterOverload && firstOption === 'always';

function isOverload(node: TSESTree.Node): boolean {
return (
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/no-duplicate-imports.ts
Expand Up @@ -34,9 +34,8 @@ export default util.createRule<Options, MessageIds>({
includeExports: false,
},
],
create(context, [option]) {
create(context, [{ includeExports }]) {
const rules = baseRule.create(context);
const includeExports = option.includeExports;
const typeMemberImports = new Set();
const typeDefaultImports = new Set();
const typeExports = new Set();
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/no-unused-expressions.ts
Expand Up @@ -34,9 +34,8 @@ export default util.createRule<Options, MessageIds>({
allowTaggedTemplates: false,
},
],
create(context, options) {
create(context, [{ allowShortCircuit = false, allowTernary = false }]) {
const rules = baseRule.create(context);
const { allowShortCircuit = false, allowTernary = false } = options[0];

function isValidExpression(node: TSESTree.Node): boolean {
if (allowShortCircuit && node.type === AST_NODE_TYPES.LogicalExpression) {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-unused-vars.ts
Expand Up @@ -94,7 +94,7 @@ export default util.createRule<Options, MessageIds>({
caughtErrors: 'none',
};

const firstOption = context.options[0];
const [firstOption] = context.options;

if (firstOption) {
if (typeof firstOption === 'string') {
Expand Down
7 changes: 3 additions & 4 deletions packages/eslint-plugin/src/rules/object-curly-spacing.ts
Expand Up @@ -30,7 +30,8 @@ export default createRule<Options, MessageIds>({
},
defaultOptions: ['never'],
create(context) {
const spaced = context.options[0] === 'always';
const [firstOption, secondOption] = context.options;
const spaced = firstOption === 'always';
const sourceCode = context.getSourceCode();

/**
Expand All @@ -43,9 +44,7 @@ export default createRule<Options, MessageIds>({
function isOptionSet(
option: 'arraysInObjects' | 'objectsInObjects',
): boolean {
return context.options[1]
? context.options[1][option] === !spaced
: false;
return secondOption ? secondOption[option] === !spaced : false;
}

const options = {
Expand Down
@@ -1,6 +1,6 @@
import {
TSESTree,
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';

Expand Down Expand Up @@ -48,10 +48,10 @@ export default util.createRule<Options, MessageIds>({
...util.readonlynessOptionsDefaults,
},
],
create(context, options) {
const [
{ checkParameterProperties, ignoreInferredTypes, treatMethodsAsReadonly },
] = options;
create(
context,
[{ checkParameterProperties, ignoreInferredTypes, treatMethodsAsReadonly }],
) {
const { esTreeNodeToTSNodeMap, program } = util.getParserServices(context);
const checker = program.getTypeChecker();

Expand Down
Expand Up @@ -58,12 +58,10 @@ export default util.createRule<Options, MessageIds>({
},
defaultOptions: ['always'],

create(context) {
create(context, [firstOption]) {
const sourceCode = context.getSourceCode();
const baseConfig =
typeof context.options[0] === 'string' ? context.options[0] : 'always';
const overrideConfig =
typeof context.options[0] === 'object' ? context.options[0] : {};
const baseConfig = typeof firstOption === 'string' ? firstOption : 'always';
const overrideConfig = typeof firstOption === 'object' ? firstOption : {};

/**
* Determines whether a function has a name.
Expand Down

0 comments on commit 612f02d

Please sign in to comment.