Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Feb 5, 2021
1 parent de698f9 commit 77b1902
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4520,16 +4520,18 @@ namespace ts {
return false;
}

// If nodes haven't been transformed, they should always be real siblings
if (!previousNode.original && !nextNode.original) {
return true;
}

if (!previousNode.parent || !nextNode.parent) {
const previousParent = getOriginalNode(previousNode).parent;
return previousParent && previousParent === getOriginalNode(nextNode).parent;
}

if (!nodeIsFirstNodeAtOrAfterPosition(currentSourceFile!, getOriginalNode(nextNode), previousNode.end)) {
return false;
}

return true;
// This check is most expensive, so everything preceding is avoiding it when possible
return nodeIsFirstNodeAtOrAfterPosition(getOriginalNode(nextNode), previousNode.end);
}

function getClosingLineTerminatorCount(parentNode: TextRange, children: readonly Node[], format: ListFormat): number {
Expand Down
9 changes: 6 additions & 3 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7119,10 +7119,11 @@ namespace ts {
}
}

export function nodeIsFirstNodeAtOrAfterPosition(sourceFile: SourceFile, node: Node, position: number): boolean {
export function nodeIsFirstNodeAtOrAfterPosition(node: Node, position: number): boolean {
if (node.pos === position) return true;
if (node.pos < position) return false;
let current: Node = sourceFile;
let current = findAncestor(node.parent, p => textRangeContainsPositionInclusive(p, position));
if (!current) return false;
let next: Node | undefined;
const getContainingChild = (child: Node) => {
if (child.pos <= position && (position < child.end || (position === child.end && (child.kind === SyntaxKind.EndOfFileToken)))) {
Expand All @@ -7133,7 +7134,9 @@ namespace ts {
}
};
while (true) {
const child = isSourceFileJS(sourceFile) && hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) || forEachChild(current, getContainingChild);
const child: Node | undefined =
isInJSFile(current) && hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) ||
forEachChild(current, getContainingChild);
if (child === node || next === node) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ namespace ts {
// All node tests in the following list should *not* reference parent pointers so that
// they may be used with transformations.
/* @internal */
export function isNode(node: Node) {
export function isNode(node: any): node is Node {
return isNodeKind(node.kind);
}

Expand Down

0 comments on commit 77b1902

Please sign in to comment.