Skip to content

Commit

Permalink
feat(experimental-utils): expose ast utility functions (#1670)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Mar 20, 2020
1 parent 2b9603d commit 3eb5d45
Show file tree
Hide file tree
Showing 50 changed files with 835 additions and 537 deletions.
3 changes: 2 additions & 1 deletion .cspell.json
Expand Up @@ -10,7 +10,8 @@
"**/**/CONTRIBUTORS.md",
"**/**/ROADMAP.md",
"**/*.{json,snap}",
".cspell.json"
".cspell.json",
"yarn.lock"
],
"dictionaries": [
"typescript",
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-plugin/package.json
Expand Up @@ -42,7 +42,6 @@
},
"dependencies": {
"@typescript-eslint/experimental-utils": "2.24.0",
"eslint-utils": "^1.4.3",
"functional-red-black-tree": "^1.0.1",
"regexpp": "^3.0.0",
"tsutils": "^3.17.1"
Expand Down
8 changes: 6 additions & 2 deletions packages/eslint-plugin/src/rules/comma-spacing.ts
Expand Up @@ -2,8 +2,12 @@ import {
TSESTree,
AST_TOKEN_TYPES,
} from '@typescript-eslint/experimental-utils';
import { isClosingParenToken, isCommaToken } from 'eslint-utils';
import { isTokenOnSameLine, createRule } from '../util';
import {
isClosingParenToken,
isCommaToken,
isTokenOnSameLine,
createRule,
} from '../util';

type Options = [
{
Expand Down
5 changes: 2 additions & 3 deletions packages/eslint-plugin/src/rules/func-call-spacing.ts
@@ -1,5 +1,4 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { isOpeningParenToken } from 'eslint-utils';
import * as util from '../util';

export type Options = [
Expand Down Expand Up @@ -79,7 +78,7 @@ export default util.createRule<Options, MessageIds>({
| TSESTree.OptionalCallExpression
| TSESTree.NewExpression,
): void {
const isOptionalCall = util.isOptionalOptionalChain(node);
const isOptionalCall = util.isOptionalOptionalCallExpression(node);

const closingParenToken = sourceCode.getLastToken(node)!;
const lastCalleeTokenWithoutPossibleParens = sourceCode.getLastToken(
Expand All @@ -88,7 +87,7 @@ export default util.createRule<Options, MessageIds>({
const openingParenToken = sourceCode.getFirstTokenBetween(
lastCalleeTokenWithoutPossibleParens,
closingParenToken,
isOpeningParenToken,
util.isOpeningParenToken,
);
if (!openingParenToken || openingParenToken.range[1] >= node.range[1]) {
// new expression with no parens...
Expand Down
13 changes: 8 additions & 5 deletions packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts
Expand Up @@ -8,7 +8,13 @@ import {
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';

import { TokenOrComment } from './BinarySearchTree';
import { OffsetStorage } from './OffsetStorage';
import { TokenInfo } from './TokenInfo';
import {
createRule,
ExcludeKeys,
isClosingBraceToken,
isClosingBracketToken,
isClosingParenToken,
Expand All @@ -19,11 +25,8 @@ import {
isOpeningBraceToken,
isOpeningParenToken,
isSemicolonToken,
} from 'eslint-utils';
import { TokenOrComment } from './BinarySearchTree';
import { OffsetStorage } from './OffsetStorage';
import { TokenInfo } from './TokenInfo';
import { createRule, ExcludeKeys, RequireKeys } from '../../util';
RequireKeys,
} from '../../util';

const GLOBAL_LINEBREAK_REGEX = /\r\n|[\r\n\u2028\u2029]/gu;
const WHITESPACE_REGEX = /\s*$/u;
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-array-constructor.ts
Expand Up @@ -37,7 +37,7 @@ export default util.createRule({
node.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.name === 'Array' &&
!node.typeParameters &&
!util.isOptionalOptionalChain(node)
!util.isOptionalOptionalCallExpression(node)
) {
context.report({
node,
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/prefer-includes.ts
Expand Up @@ -2,10 +2,9 @@ import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { getStaticValue } from 'eslint-utils';
import { AST as RegExpAST, parseRegExpLiteral } from 'regexpp';
import * as ts from 'typescript';
import { createRule, getParserServices } from '../util';
import { createRule, getParserServices, getStaticValue } from '../util';

export default createRule({
name: 'prefer-includes',
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/prefer-optional-chain.ts
Expand Up @@ -2,7 +2,6 @@ import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { isOpeningParenToken } from 'eslint-utils';
import * as util from '../util';

type ValidChainTarget =
Expand Down Expand Up @@ -203,7 +202,7 @@ export default util.createRule({
sourceCode.getFirstTokenBetween(
node.callee,
closingParenToken,
isOpeningParenToken,
util.isOpeningParenToken,
),
util.NullThrowsReasons.MissingToken('opening parenthesis', node.type),
);
Expand Down
8 changes: 6 additions & 2 deletions packages/eslint-plugin/src/rules/prefer-regexp-exec.ts
@@ -1,6 +1,10 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { getStaticValue } from 'eslint-utils';
import { createRule, getParserServices, getTypeName } from '../util';
import {
createRule,
getParserServices,
getStaticValue,
getTypeName,
} from '../util';

export default createRule({
name: 'prefer-regexp-exec',
Expand Down
Expand Up @@ -3,13 +3,15 @@ import {
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { AST as RegExpAST, RegExpParser } from 'regexpp';
import {
createRule,
getParserServices,
getPropertyName,
getStaticValue,
getTypeName,
isNotClosingParenToken,
} from 'eslint-utils';
import { AST as RegExpAST, RegExpParser } from 'regexpp';
import { createRule, getParserServices, getTypeName } from '../util';
} from '../util';

const EQ_OPERATORS = /^[=!]=/;
const regexpp = new RegExpParser();
Expand Down
13 changes: 4 additions & 9 deletions packages/eslint-plugin/src/rules/require-await.ts
Expand Up @@ -3,11 +3,6 @@ import {
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
isArrowToken,
getFunctionNameWithKind,
isOpeningParenToken,
} from 'eslint-utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import * as util from '../util';
Expand Down Expand Up @@ -73,7 +68,7 @@ export default util.createRule({
loc: getFunctionHeadLoc(node, sourceCode),
messageId: 'missingAwait',
data: {
name: util.upperCaseFirst(getFunctionNameWithKind(node)),
name: util.upperCaseFirst(util.getFunctionNameWithKind(node)),
},
});
}
Expand Down Expand Up @@ -157,8 +152,8 @@ function getOpeningParenOfParams(
): TSESTree.Token {
return util.nullThrows(
node.id
? sourceCode.getTokenAfter(node.id, isOpeningParenToken)
: sourceCode.getFirstToken(node, isOpeningParenToken),
? sourceCode.getTokenAfter(node.id, util.isOpeningParenToken)
: sourceCode.getFirstToken(node, util.isOpeningParenToken),
util.NullThrowsReasons.MissingToken('(', node.type),
);
}
Expand All @@ -180,7 +175,7 @@ function getFunctionHeadLoc(

if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
const arrowToken = util.nullThrows(
sourceCode.getTokenBefore(node.body, isArrowToken),
sourceCode.getTokenBefore(node.body, util.isArrowToken),
util.NullThrowsReasons.MissingToken('=>', node.type),
);

Expand Down
Expand Up @@ -2,7 +2,6 @@ import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { isOpeningParenToken } from 'eslint-utils';
import * as util from '../util';

type Option = 'never' | 'always';
Expand Down Expand Up @@ -106,7 +105,7 @@ export default util.createRule<Options, MessageIds>({
// Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar
if (
node.async &&
isOpeningParenToken(sourceCode.getFirstToken(node, { skip: 1 })!)
util.isOpeningParenToken(sourceCode.getFirstToken(node, { skip: 1 })!)
) {
return overrideConfig.asyncArrow ?? baseConfig;
}
Expand Down Expand Up @@ -143,7 +142,7 @@ export default util.createRule<Options, MessageIds>({
leftToken = sourceCode.getLastToken(node.typeParameters)!;
rightToken = sourceCode.getTokenAfter(leftToken)!;
} else {
rightToken = sourceCode.getFirstToken(node, isOpeningParenToken)!;
rightToken = sourceCode.getFirstToken(node, util.isOpeningParenToken)!;
leftToken = sourceCode.getTokenBefore(rightToken)!;
}
const hasSpacing = sourceCode.isSpaceBetweenTokens(leftToken, rightToken);
Expand Down
Expand Up @@ -2,11 +2,12 @@ import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils';
import * as ts from 'typescript';
import {
createRule,
getParserServices,
getConstrainedTypeAtLocation,
getParserServices,
isClosingBraceToken,
isOpeningBraceToken,
} from '../util';
import { isTypeFlagSet, unionTypeParts } from 'tsutils';
import { isClosingBraceToken, isOpeningBraceToken } from 'eslint-utils';

export default createRule({
name: 'switch-exhaustiveness-check',
Expand Down

0 comments on commit 3eb5d45

Please sign in to comment.