Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

refactor(ts-estree): simplify calculation of method location#152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JamesHenry merged 2 commits intotypescript-eslint:masterfromarmano2:function-expression-loc
Jan 27, 2019
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 18 additions & 48 deletionspackages/typescript-estree/src/convert.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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';
Expand DownExpand Up@@ -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);
Expand All@@ -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'
});
Expand DownExpand Up@@ -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,
Expand All@@ -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;
Expand DownExpand Up@@ -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
Expand Down
56 changes: 21 additions & 35 deletionspackages/typescript-estree/src/node-utils.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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';

Expand DownExpand Up@@ -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
Expand All@@ -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)
};
}

Expand DownExpand Up@@ -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
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp