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 2, 2021
1 parent ad0efec commit 2d047c1
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 29 deletions.
8 changes: 3 additions & 5 deletions packages/eslint-plugin-tslint/src/rules/config.ts
Expand Up @@ -106,11 +106,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 baseRule from 'eslint/lib/rules/init-declarations';
import {
InferOptionsTypeFromRule,
InferMessageIdsTypeFromRule,
createRule,
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../util';

export type Options = InferOptionsTypeFromRule<typeof baseRule>;
Expand All @@ -32,9 +32,8 @@ export default createRule<Options, MessageIds>({
},
},
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 @@ -44,10 +44,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-unused-expressions.ts
Expand Up @@ -31,9 +31,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 @@ -95,7 +95,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 @@ -29,7 +29,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 @@ -42,9 +43,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 @@ -49,10 +49,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 @@ -59,12 +59,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 2d047c1

Please sign in to comment.