本文整理汇总了TypeScript中ts-simple-ast.Node类的典型用法代码示例。如果您正苦于以下问题:TypeScript Node类的具体用法?TypeScript Node怎么用?TypeScript Node使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Node类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getExtra
function getExtra(node: Node) {
const extras = []
if (TypeGuards.isJsxTagNamedNode(node)) {
extras.push(node.getTagNameNode().getText().match(/^[a-z]/) ? 'JSXIntrinsicElement' : 'JSXNonIntrinsicElement')
}
const parent = node.getParent()
if (parent && TypeGuards.isJsxTagNamedNode(parent)) {
extras.push(parent.getTagNameNode().getText().match(/^[a-z]/) ? 'JSXIntrinsicElementChild' : 'JSXNonIntrinsicElementChild')
}
return extras.length ? extras : undefined
}
示例2: getParentRanges
function getParentRanges(node: Node) {
const ranges = []
const [start, end] = [node.getStart(), node.getEnd()]
let lastEnd = start
node.forEachChild(child => {
const [start, end] = [child.getStart(), child.getEnd()]
ranges.push({
start: lastEnd,
end: start
})
lastEnd = end
})
if (lastEnd !== end) {
ranges.push({
start: lastEnd,
end
})
}
return ranges
}
示例3: buildJsxAstNode
function buildJsxAstNode(n: tsNode, config: CodeWorkerRequestJsxAst): CodeWorkerResponseJsxAsNode {
let text = n.getText().trim()
const children = config.mode === 'forEachChild' ? getChildrenForEachChild(n) : n.getChildren()
text = text.substring(0, Math.max(config.nodeTextLength || 20, text.length))
const type = tryTo(() => n.getType().getApparentType().getText() || n.getType().getText()) || 'TODO'
const node: CodeWorkerResponseJsxAsNode = {
kind: n.getKindName(),
type,
text,
start: n.getStart(),
end: n.getEnd(),
startColumn: ts.getLineAndCharacterOfPosition(n.getSourceFile().compilerNode, n.compilerNode.getStart()).character + 1,
startLineNumber: ts.getLineAndCharacterOfPosition(n.getSourceFile().compilerNode, n.compilerNode.getStart()).line + 1,
endColumn: ts.getLineAndCharacterOfPosition(n.getSourceFile().compilerNode, n.compilerNode.getEnd()).character + 1,
endLineNumber: ts.getLineAndCharacterOfPosition(n.getSourceFile().compilerNode, n.compilerNode.getEnd()).line + 1,
children: children.map(c => buildJsxAstNode(c, config))
}
return node
}
示例4: filterNonJsxRelatedNodes
function filterNonJsxRelatedNodes(n: Node) {
// this is faster - we just dont want syntax list since they pollute a lot the JSX.
return n.getKindName() !== 'SyntaxList'
// But these are other more elegant ways:
// // only pass those with ancestors or with first-level children which are JSX :
// if (n.getKindName()!.toLowerCase().includes('jsx')) {
// return true
// }
// else if(n.getFirstAncestor(a=>a.getKindName()!.toLowerCase().includes('jsx'))){
// return true
// }
// else {
// return n.getFirstChild(a=>a.getKindName()!.toLowerCase().includes('jsx')))
// }
}
示例5: addChildNodes
function addChildNodes(node: Node, classifications: Classification[], sourceFile: SourceFile, project: Project) {
const lines = sourceFile.getFullText().split('\n').map(line => line.length)
node.getDescendants()
.filter(filterNonJsxRelatedNodes)
.forEach(node => {
const parent = node.getParent()
const parentKind = parent && parent.getKindName()
// const type = tryTo(() => buildParentShipKind({ node: node, project })[0]) || undefined
const kind = node.getKindName()
const extra = getExtra(node)
getNodeRangesForMonaco(node, lines).forEach(r => {
classifications.push(
{
...r,
kind,
parentKind,
// type,
extra,
}
)
})
})
}
示例6: getChildrenForEachChild
export function getChildrenForEachChild(n: Node): Node[] {
const result: Node[] = []
n.forEachChild(n => result.push(n))
return result
}
示例7: getLineNumberAndOffset
.map(({ start, end }) => {
const { offset, line: startLineNumber } = getLineNumberAndOffset(start, lines, node)
const { line: endLineNumber } = getLineNumberAndOffset(end, lines, node)
return {
startLineNumber,
// Heads up : following sum fixes an error of original implementation when JSXText has multiple lines:
endLineNumber: endLineNumber + (TypeGuards.isJsxText(node) && node.getText().includes('\n') ? -1 : 0),
startColumn: start + 1 - offset,
endColumn: end + 1 - offset,
}
})
示例8: tryTo
const type = tryTo(() => n.getType().getApparentType().getText() || n.getType().getText()) || 'TODO'