From 538223b44835061cc13733f6a05dfe8f7c715b3e Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 27 Jan 2019 07:06:40 +0100 Subject: [PATCH] refactor(ts-estree): simplify methods location calculation --- packages/typescript-estree/src/convert.ts | 66 ++++++-------------- packages/typescript-estree/src/node-utils.ts | 56 +++++++---------- 2 files changed, 39 insertions(+), 83 deletions(-) diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index ff365b187c7e..bc9e209f23ed 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -23,10 +23,10 @@ import { isComma, getBinaryExpressionType, isOptional, - findFirstMatchingToken, unescapeStringLiteralText, getDeclarationKind, - getLastModifier + getLastModifier, + getLineAndCharacterFor } from './node-utils'; import { AST_NODE_TYPES } from './ast-node-types'; import { ESTreeNode } from './temp-types-based-on-js-source'; @@ -858,38 +858,19 @@ export default function convert(config: ConvertConfig): ESTreeNode | null { case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: case SyntaxKind.MethodDeclaration: { - const openingParen = findFirstMatchingToken( - node.name, - ast, - (token: any) => { - if (!token || !token.kind) { - return false; - } - return getTextForTokenKind(token.kind) === '('; - }, - ast - ); - - const methodLoc = ast.getLineAndCharacterOfPosition( - (openingParen as any).getStart(ast) - ), - nodeIsMethod = node.kind === SyntaxKind.MethodDeclaration, - method: ESTreeNode = { - type: AST_NODE_TYPES.FunctionExpression, - id: null, - generator: !!node.asteriskToken, - expression: false, // ESTreeNode as ESTreeNode here - async: hasModifier(SyntaxKind.AsyncKeyword, node), - body: convertChild(node.body), - range: [node.parameters.pos - 1, result.range[1]], - loc: { - start: { - line: methodLoc.line + 1, - column: methodLoc.character - }, - end: result.loc.end - } - } as any; + const method: ESTreeNode = { + type: AST_NODE_TYPES.FunctionExpression, + id: null, + generator: !!node.asteriskToken, + expression: false, // ESTreeNode as ESTreeNode here + async: hasModifier(SyntaxKind.AsyncKeyword, node), + body: convertChild(node.body), + range: [node.parameters.pos - 1, result.range[1]], + loc: { + start: getLineAndCharacterFor(node.parameters.pos - 1, ast), + end: result.loc.end + } + } as any; if (node.type) { (method as any).returnType = convertTypeAnnotation(node.type); @@ -903,7 +884,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null { key: convertChild(node.name), value: method, computed: isComputedProperty(node.name), - method: nodeIsMethod, + method: node.kind === SyntaxKind.MethodDeclaration, shorthand: false, kind: 'init' }); @@ -992,10 +973,6 @@ export default function convert(config: ConvertConfig): ESTreeNode | null { constructorToken.end ]; - const constructorLoc = ast.getLineAndCharacterOfPosition( - node.parameters.pos - 1 - ); - const constructor: ESTreeNode = { type: AST_NODE_TYPES.FunctionExpression, id: null, @@ -1006,10 +983,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null { body: convertChild(node.body), range: [node.parameters.pos - 1, result.range[1]], loc: { - start: { - line: constructorLoc.line + 1, - column: constructorLoc.character - }, + start: getLineAndCharacterFor(node.parameters.pos - 1, ast), end: result.loc.end } } as any; @@ -1906,16 +1880,12 @@ export default function convert(config: ConvertConfig): ESTreeNode | null { break; case SyntaxKind.JsxExpression: { - const eloc = ast.getLineAndCharacterOfPosition(result.range[0] + 1); const expression = node.expression ? convertChild(node.expression) : { type: AST_NODE_TYPES.JSXEmptyExpression, loc: { - start: { - line: eloc.line + 1, - column: eloc.character - }, + start: getLineAndCharacterFor(result.range[0] + 1, ast), end: { line: result.loc.end.line, column: result.loc.end.column - 1 diff --git a/packages/typescript-estree/src/node-utils.ts b/packages/typescript-estree/src/node-utils.ts index c844cf7ed20d..d79353f755e6 100644 --- a/packages/typescript-estree/src/node-utils.ts +++ b/packages/typescript-estree/src/node-utils.ts @@ -9,7 +9,8 @@ import unescape from 'lodash.unescape'; import { ESTreeNodeLoc, ESTreeNode, - ESTreeToken + ESTreeToken, + LineAndColumnData } from './temp-types-based-on-js-source'; import { AST_NODE_TYPES } from './ast-node-types'; @@ -219,6 +220,23 @@ export function getBinaryExpressionType( return AST_NODE_TYPES.BinaryExpression; } +/** + * Returns line and column data for the given positions, + * @param pos position to check + * @param ast the AST object + * @returns line and column + */ +export function getLineAndCharacterFor( + pos: number, + ast: ts.SourceFile +): LineAndColumnData { + const loc = ast.getLineAndCharacterOfPosition(pos); + return { + line: loc.line + 1, + column: loc.character + }; +} + /** * Returns line and column data for the given start and end positions, * for the given AST @@ -232,18 +250,9 @@ export function getLocFor( end: number, ast: ts.SourceFile ): ESTreeNodeLoc { - const startLoc = ast.getLineAndCharacterOfPosition(start), - endLoc = ast.getLineAndCharacterOfPosition(end); - return { - start: { - line: startLoc.line + 1, - column: startLoc.character - }, - end: { - line: endLoc.line + 1, - column: endLoc.character - } + start: getLineAndCharacterFor(start, ast), + end: getLineAndCharacterFor(end, ast) }; } @@ -389,29 +398,6 @@ export function findNextToken( } } -/** - * Find the first matching token based on the given predicate function. - * @param {ts.Node} previousToken The previous ts.Token - * @param {ts.Node} parent The parent ts.Node - * @param {Function} predicate The predicate function to apply to each checked token - * @param {ts.SourceFile} ast The TS AST - * @returns {ts.Node|undefined} a matching ts.Token - */ -export function findFirstMatchingToken( - previousToken: ts.Node | undefined, - parent: ts.Node, - predicate: (node: ts.Node) => boolean, - ast: ts.SourceFile -): ts.Node | undefined { - while (previousToken) { - if (predicate(previousToken)) { - return previousToken; - } - previousToken = findNextToken(previousToken, parent, ast); - } - return undefined; -} - /** * Find the first matching ancestor based on the given predicate function. * @param {ts.Node} node The current ts.Node pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy