From fba140797d9f2cc944ee2609cd3362e0c1acd083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Sat, 22 May 2021 00:50:29 +0200 Subject: [PATCH 1/2] chore(eslint-plugin): remove useless union types --- .../eslint-plugin/src/rules/comma-spacing.ts | 4 +-- .../src/rules/consistent-type-imports.ts | 4 +-- .../indent-new-do-not-use/BinarySearchTree.ts | 3 +- .../indent-new-do-not-use/OffsetStorage.ts | 28 ++++++++----------- .../rules/indent-new-do-not-use/TokenInfo.ts | 9 +++--- .../src/rules/indent-new-do-not-use/index.ts | 15 +++++----- .../src/rules/object-curly-spacing.ts | 4 +-- 7 files changed, 30 insertions(+), 37 deletions(-) diff --git a/packages/eslint-plugin/src/rules/comma-spacing.ts b/packages/eslint-plugin/src/rules/comma-spacing.ts index e0b206221162..019a82a2e76e 100644 --- a/packages/eslint-plugin/src/rules/comma-spacing.ts +++ b/packages/eslint-plugin/src/rules/comma-spacing.ts @@ -108,8 +108,8 @@ export default createRule({ */ function validateCommaSpacing( commaToken: TSESTree.PunctuatorToken, - prevToken: TSESTree.Token | TSESTree.Comment | null, - nextToken: TSESTree.Token | TSESTree.Comment | null, + prevToken: TSESTree.Token | null, + nextToken: TSESTree.Token | null, ): void { if ( prevToken && diff --git a/packages/eslint-plugin/src/rules/consistent-type-imports.ts b/packages/eslint-plugin/src/rules/consistent-type-imports.ts index 6a3067270a45..f15a27a75308 100644 --- a/packages/eslint-plugin/src/rules/consistent-type-imports.ts +++ b/packages/eslint-plugin/src/rules/consistent-type-imports.ts @@ -31,13 +31,13 @@ interface ReportValueImport { } function isImportToken( - token: TSESTree.Token | TSESTree.Comment, + token: TSESTree.Token, ): token is TSESTree.KeywordToken & { value: 'import' } { return token.type === AST_TOKEN_TYPES.Keyword && token.value === 'import'; } function isTypeToken( - token: TSESTree.Token | TSESTree.Comment, + token: TSESTree.Token, ): token is TSESTree.IdentifierToken & { value: 'type' } { return token.type === AST_TOKEN_TYPES.Identifier && token.value === 'type'; } diff --git a/packages/eslint-plugin/src/rules/indent-new-do-not-use/BinarySearchTree.ts b/packages/eslint-plugin/src/rules/indent-new-do-not-use/BinarySearchTree.ts index 0010750ebfd8..7c4b78f5b7b5 100644 --- a/packages/eslint-plugin/src/rules/indent-new-do-not-use/BinarySearchTree.ts +++ b/packages/eslint-plugin/src/rules/indent-new-do-not-use/BinarySearchTree.ts @@ -4,10 +4,9 @@ import { TSESTree } from '@typescript-eslint/experimental-utils'; import createTree from 'functional-red-black-tree'; -export type TokenOrComment = TSESTree.Token | TSESTree.Comment; export interface TreeValue { offset: number; - from: TokenOrComment | null; + from: TSESTree.Token | null; force: boolean; } diff --git a/packages/eslint-plugin/src/rules/indent-new-do-not-use/OffsetStorage.ts b/packages/eslint-plugin/src/rules/indent-new-do-not-use/OffsetStorage.ts index d19334672630..c673a7d5af4b 100644 --- a/packages/eslint-plugin/src/rules/indent-new-do-not-use/OffsetStorage.ts +++ b/packages/eslint-plugin/src/rules/indent-new-do-not-use/OffsetStorage.ts @@ -2,11 +2,7 @@ // License: https://github.com/eslint/eslint/blob/48700fc8408f394887cdedd071b22b757700fdcb/LICENSE import { TSESTree } from '@typescript-eslint/experimental-utils'; -import { - BinarySearchTree, - TokenOrComment, - TreeValue, -} from './BinarySearchTree'; +import { BinarySearchTree, TreeValue } from './BinarySearchTree'; import { TokenInfo } from './TokenInfo'; /** @@ -17,9 +13,9 @@ export class OffsetStorage { private readonly indentSize: number; private readonly indentType: string; private readonly tree: BinarySearchTree; - private readonly lockedFirstTokens: WeakMap; - private readonly desiredIndentCache: WeakMap; - private readonly ignoredTokens: WeakSet; + private readonly lockedFirstTokens: WeakMap; + private readonly desiredIndentCache: WeakMap; + private readonly ignoredTokens: WeakSet; /** * @param tokenInfo a TokenInfo instance * @param indentSize The desired size of each indentation level @@ -38,7 +34,7 @@ export class OffsetStorage { this.ignoredTokens = new WeakSet(); } - private getOffsetDescriptor(token: TokenOrComment): TreeValue { + private getOffsetDescriptor(token: TSESTree.Token): TreeValue { return this.tree.findLe(token.range[0]).value; } @@ -50,8 +46,8 @@ export class OffsetStorage { * @param offsetToken The second token, whose offset should be matched to the first token */ public matchOffsetOf( - baseToken: TokenOrComment, - offsetToken: TokenOrComment, + baseToken: TSESTree.Token, + offsetToken: TSESTree.Token, ): void { /* * lockedFirstTokens is a map from a token whose indentation is controlled by the "first" option to @@ -120,8 +116,8 @@ export class OffsetStorage { * @param offset The desired indent level */ public setDesiredOffset( - token: TokenOrComment, - fromToken: TokenOrComment | null, + token: TSESTree.Token, + fromToken: TSESTree.Token | null, offset: number, ): void { this.setDesiredOffsets(token.range, fromToken, offset); @@ -154,7 +150,7 @@ export class OffsetStorage { */ public setDesiredOffsets( range: [number, number], - fromToken: TokenOrComment | null, + fromToken: TSESTree.Token | null, offset = 0, force = false, ): void { @@ -212,7 +208,7 @@ export class OffsetStorage { * Gets the desired indent of a token * @returns The desired indent of the token */ - public getDesiredIndent(token: TokenOrComment): string { + public getDesiredIndent(token: TSESTree.Token): string { if (!this.desiredIndentCache.has(token)) { if (this.ignoredTokens.has(token)) { /* @@ -263,7 +259,7 @@ export class OffsetStorage { /** * Ignores a token, preventing it from being reported. */ - ignoreToken(token: TokenOrComment): void { + ignoreToken(token: TSESTree.Token): void { if (this.tokenInfo.isFirstTokenOfLine(token)) { this.ignoredTokens.add(token); } diff --git a/packages/eslint-plugin/src/rules/indent-new-do-not-use/TokenInfo.ts b/packages/eslint-plugin/src/rules/indent-new-do-not-use/TokenInfo.ts index 16d15c4ae5f1..2e5c737e0c30 100644 --- a/packages/eslint-plugin/src/rules/indent-new-do-not-use/TokenInfo.ts +++ b/packages/eslint-plugin/src/rules/indent-new-do-not-use/TokenInfo.ts @@ -2,7 +2,6 @@ // License: https://github.com/eslint/eslint/blob/48700fc8408f394887cdedd071b22b757700fdcb/LICENSE import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils'; -import { TokenOrComment } from './BinarySearchTree'; /** * A helper class to get token-based info related to indentation @@ -37,8 +36,8 @@ export class TokenInfo { * @returns The first token on the given line */ public getFirstTokenOfLine( - token: TokenOrComment | TSESTree.Node, - ): TokenOrComment { + token: TSESTree.Token | TSESTree.Node, + ): TSESTree.Token { return this.firstTokensByLineNumber.get(token.loc.start.line)!; } @@ -46,7 +45,7 @@ export class TokenInfo { * Determines whether a token is the first token in its line * @returns `true` if the token is the first on its line */ - public isFirstTokenOfLine(token: TokenOrComment): boolean { + public isFirstTokenOfLine(token: TSESTree.Token): boolean { return this.getFirstTokenOfLine(token) === token; } @@ -55,7 +54,7 @@ export class TokenInfo { * @param token Token to examine. This should be the first token on its line. * @returns The indentation characters that precede the token */ - public getTokenIndent(token: TokenOrComment): string { + public getTokenIndent(token: TSESTree.Token): string { return this.sourceCode.text.slice( token.range[0] - token.loc.start.column, token.range[0], diff --git a/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts b/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts index 7b6bf6d35b99..ec06ae9f3e35 100644 --- a/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts +++ b/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts @@ -9,7 +9,6 @@ import { TSESTree, } from '@typescript-eslint/experimental-utils'; -import { TokenOrComment } from './BinarySearchTree'; import { OffsetStorage } from './OffsetStorage'; import { TokenInfo } from './TokenInfo'; import { @@ -474,7 +473,7 @@ export default createRule({ * @param token Token violating the indent rule * @param neededIndent Expected indentation string */ - function report(token: TokenOrComment, neededIndent: string): void { + function report(token: TSESTree.Token, neededIndent: string): void { const actualIndent = Array.from(tokenInfo.getTokenIndent(token)); const numSpaces = actualIndent.filter(char => char === ' ').length; const numTabs = actualIndent.filter(char => char === '\t').length; @@ -503,7 +502,7 @@ export default createRule({ * @returns `true` if the token's indentation is correct */ function validateTokenIndent( - token: TokenOrComment, + token: TSESTree.Token, desiredIndent: string, ): boolean { const indentation = tokenInfo.getTokenIndent(token); @@ -1653,20 +1652,20 @@ export default createRule({ addParensIndent(sourceCode.ast.tokens); /* - * Create a Map from (tokenOrComment) => (precedingToken). + * Create a Map from (token) => (precedingToken). * This is necessary because sourceCode.getTokenBefore does not handle a comment as an argument correctly. */ const precedingTokens = sourceCode.ast.comments.reduce( (commentMap, comment) => { - const tokenOrCommentBefore = sourceCode.getTokenBefore(comment, { + const tokenBefore = sourceCode.getTokenBefore(comment, { includeComments: true, })!; return commentMap.set( comment, - commentMap.has(tokenOrCommentBefore) - ? commentMap.get(tokenOrCommentBefore) - : tokenOrCommentBefore, + commentMap.has(tokenBefore) + ? commentMap.get(tokenBefore) + : tokenBefore, ); }, new WeakMap(), diff --git a/packages/eslint-plugin/src/rules/object-curly-spacing.ts b/packages/eslint-plugin/src/rules/object-curly-spacing.ts index 74161246a31a..256057c4fdfa 100644 --- a/packages/eslint-plugin/src/rules/object-curly-spacing.ts +++ b/packages/eslint-plugin/src/rules/object-curly-spacing.ts @@ -164,8 +164,8 @@ export default createRule({ function validateBraceSpacing( node: TSESTree.TSMappedType | TSESTree.TSTypeLiteral, first: TSESTree.Token, - second: TSESTree.Token | TSESTree.Comment, - penultimate: TSESTree.Token | TSESTree.Comment, + second: TSESTree.Token, + penultimate: TSESTree.Token, last: TSESTree.Token, ): void { if (isTokenOnSameLine(first, second)) { From 406ccc3ddb60852da3519085502df87a0843e3b1 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Fri, 28 May 2021 16:36:33 -0700 Subject: [PATCH 2/2] fix type --- packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts b/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts index 3362563d6a2a..352fcd17256a 100644 --- a/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts +++ b/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts @@ -1666,7 +1666,7 @@ export default createRule({ commentMap.get(tokenBefore) ?? tokenBefore, ); }, - new WeakMap(), + new WeakMap(), ); sourceCode.lines.forEach((_, lineIndex) => {