Skip to content

Commit

Permalink
Add getWrappedNodeAtPosition utility
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff committed Jan 16, 2018
1 parent 02b1427 commit 76ef784
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions util/util.ts
@@ -1,4 +1,5 @@
import * as ts from 'typescript';
import { NodeWrap } from './convert-ast';
import {
isBlockLike, isLiteralExpression, isPropertyDeclaration, isJsDoc, isImportDeclaration,
isTextualLiteral, isImportEqualsDeclaration, isModuleDeclaration, isCallExpression, isExportDeclaration,
Expand Down Expand Up @@ -196,6 +197,26 @@ export function isPositionInComment(sourceFile: ts.SourceFile, pos: number, pare
return getCommentAtPosition(sourceFile, pos, parent) !== undefined;
}

/**
* Returns the NodeWrap of deepest AST node that contains `pos` between its `pos` and `end`.
* Only returns undefined if pos is outside of `wrap`
*/
export function getWrappedNodeAtPosition(wrap: NodeWrap, pos: number): NodeWrap | undefined {
if (wrap.node.pos > pos || wrap.node.end <= pos)
return;
outer: while (true) {
for (const child of wrap.children) {
if (child.node.pos > pos)
return wrap;
if (child.node.end > pos) {
wrap = child;
continue outer;
}
}
return wrap;
}
}

export function getPropertyName(propertyName: ts.PropertyName): string | undefined {
if (propertyName.kind === ts.SyntaxKind.ComputedPropertyName) {
if (!isLiteralExpression(propertyName.expression))
Expand Down

0 comments on commit 76ef784

Please sign in to comment.