@rehearsal/ts-utils
Advanced tools
Comparing version 1.0.15-beta to 1.0.16-beta
@@ -6,7 +6,2 @@ /** | ||
/** | ||
* Find the diagnosed node and passes it to `transformer` function. | ||
* The `transform` function have to return modified node or `undefined` to remove node from AST. | ||
*/ | ||
export declare function transformDiagnosedNode(diagnostic: DiagnosticWithLocation, transformer: (node: Node) => Node | undefined): string; | ||
/** | ||
* Checks if node starts with `start` position and its length equals to `length`. | ||
@@ -13,0 +8,0 @@ */ |
@@ -5,24 +5,4 @@ /** | ||
import ts from 'typescript'; | ||
const { createPrinter, findAncestor, forEachChild, getLineAndCharacterOfPosition, isCatchClause, isIdentifier, isJsxElement, isJsxFragment, isSourceFile, NewLineKind, transform, visitEachChild, visitNode, } = ts; | ||
const { findAncestor, getLineAndCharacterOfPosition, isCatchClause, isIdentifier, isJsxElement, isJsxFragment, isSourceFile, } = ts; | ||
/** | ||
* Find the diagnosed node and passes it to `transformer` function. | ||
* The `transform` function have to return modified node or `undefined` to remove node from AST. | ||
*/ | ||
export function transformDiagnosedNode(diagnostic, transformer) { | ||
const result = transform(diagnostic.file, [ | ||
(context) => { | ||
const visit = (node) => { | ||
return isNodeDiagnosed(node, diagnostic) | ||
? transformer(node) | ||
: visitEachChild(node, visit, context); | ||
}; | ||
return (node) => visitNode(node, visit); | ||
}, | ||
]); | ||
return createPrinter({ | ||
newLine: NewLineKind.LineFeed, | ||
removeComments: false, | ||
}).printFile(result.transformed[0]); | ||
} | ||
/** | ||
* Checks if node starts with `start` position and its length equals to `length`. | ||
@@ -57,3 +37,11 @@ */ | ||
export function findNodeAtPosition(sourceFile, start, length) { | ||
const visitor = (node) => isNodeAtPosition(node, start, length) ? node : forEachChild(node, visitor); | ||
const visitor = (node) => { | ||
if (isNodeAtPosition(node, start, length)) { | ||
return node; | ||
} | ||
if (node.getStart() <= start && node.getEnd() >= start + length) { | ||
return ts.forEachChild(node, visitor); | ||
} | ||
return undefined; | ||
}; | ||
return visitor(sourceFile); | ||
@@ -60,0 +48,0 @@ } |
{ | ||
"name": "@rehearsal/ts-utils", | ||
"version": "1.0.15-beta", | ||
"version": "1.0.16-beta", | ||
"description": "Rehearsal TypeScript Utils", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
134610
25
337