Skip to content

Commit

Permalink
Fix error where node in tsconfig file was missing a source file
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hanson committed May 8, 2018
1 parent 19d7309 commit 7b9cbfe
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/services/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace ts.BreakpointResolver {
}

function textSpanEndingAtNextToken(startNode: Node, previousTokenToFindNextEndToken: Node): TextSpan {
return textSpan(startNode, findNextToken(previousTokenToFindNextEndToken, previousTokenToFindNextEndToken.parent));
return textSpan(startNode, findNextToken(previousTokenToFindNextEndToken, previousTokenToFindNextEndToken.parent, sourceFile));
}

function spanInNodeIfStartsOnSameLine(node: Node, otherwiseOnNode?: Node): TextSpan {
Expand All @@ -60,7 +60,7 @@ namespace ts.BreakpointResolver {
}

function spanInNextNode(node: Node): TextSpan {
return spanInNode(findNextToken(node, node.parent));
return spanInNode(findNextToken(node, node.parent, sourceFile));
}

function spanInNode(node: Node): TextSpan {
Expand Down
2 changes: 1 addition & 1 deletion src/services/formatting/smartIndenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ namespace ts.formatting {
}

function nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken: Node, current: Node, lineAtPosition: number, sourceFile: SourceFile): NextTokenKind {
const nextToken = findNextToken(precedingToken, current);
const nextToken = findNextToken(precedingToken, current, sourceFile);
if (!nextToken) {
return NextTokenKind.Unknown;
}
Expand Down
3 changes: 2 additions & 1 deletion src/services/refactors/moveToNewFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ namespace ts.refactor {
const newFileAbsolutePath = normalizePath(combinePaths(oldFileName, "..", newFileNameWithExtension));
const newFilePath = getRelativePathFromFile(cfg.fileName, newFileAbsolutePath, getCanonicalFileName);

const filesProp = cfg.jsonObject && find(cfg.jsonObject.properties, (prop): prop is PropertyAssignment =>
const cfgObject = cfg.statements[0] && tryCast(cfg.statements[0].expression, isObjectLiteralExpression);
const filesProp = cfgObject && find(cfgObject.properties, (prop): prop is PropertyAssignment =>
isPropertyAssignment(prop) && isStringLiteral(prop.name) && prop.name.text === "files");
if (filesProp && isArrayLiteralExpression(filesProp.initializer)) {
changes.insertNodeInListAfter(cfg, last(filesProp.initializer.elements), createLiteral(newFilePath), filesProp.initializer.elements);
Expand Down
2 changes: 1 addition & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ts {
getStart(sourceFile?: SourceFileLike, includeJsDocComment?: boolean): number;
getFullStart(): number;
getEnd(): number;
getWidth(sourceFile?: SourceFile): number;
getWidth(sourceFile?: SourceFileLike): number;
getFullWidth(): number;
getLeadingTriviaWidth(sourceFile?: SourceFile): number;
getFullText(sourceFile?: SourceFile): string;
Expand Down
20 changes: 10 additions & 10 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ namespace ts {
return findPrecedingToken(position, file);
}

export function findNextToken(previousToken: Node, parent: Node): Node {
export function findNextToken(previousToken: Node, parent: Node, sourceFile: SourceFile): Node {
return find(parent);

function find(n: Node): Node {
Expand All @@ -724,7 +724,7 @@ namespace ts {
// previous token ends exactly at the beginning of child
(child.pos === previousToken.end);

if (shouldDiveInChildNode && nodeHasTokens(child)) {
if (shouldDiveInChildNode && nodeHasTokens(child, sourceFile)) {
return find(child);
}
}
Expand Down Expand Up @@ -759,12 +759,12 @@ namespace ts {
const start = child.getStart(sourceFile, includeJsDoc);
const lookInPreviousChild =
(start >= position) || // cursor in the leading trivia
!nodeHasTokens(child) ||
!nodeHasTokens(child, sourceFile) ||
isWhiteSpaceOnlyJsxText(child);

if (lookInPreviousChild) {
// actual start of the node is past the position - previous token should be at the end of previous child
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ i);
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ i, sourceFile);
return candidate && findRightmostToken(candidate, sourceFile);
}
else {
Expand All @@ -781,7 +781,7 @@ namespace ts {
// Try to find the rightmost token in the file without filtering.
// Namely we are skipping the check: 'position < node.end'
if (children.length) {
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length);
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length, sourceFile);
return candidate && findRightmostToken(candidate, sourceFile);
}
}
Expand All @@ -797,21 +797,21 @@ namespace ts {
}

const children = n.getChildren(sourceFile);
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length);
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length, sourceFile);
return candidate && findRightmostToken(candidate, sourceFile);
}

/**
* Finds the rightmost child to the left of `children[exclusiveStartPosition]` which is a non-all-whitespace token or has constituent tokens.
*/
function findRightmostChildNodeWithTokens(children: Node[], exclusiveStartPosition: number): Node | undefined {
function findRightmostChildNodeWithTokens(children: Node[], exclusiveStartPosition: number, sourceFile: SourceFile): Node | undefined {
for (let i = exclusiveStartPosition - 1; i >= 0; i--) {
const child = children[i];

if (isWhiteSpaceOnlyJsxText(child)) {
Debug.assert(i > 0, "`JsxText` tokens should not be the first child of `JsxElement | JsxSelfClosingElement`");
}
else if (nodeHasTokens(children[i])) {
else if (nodeHasTokens(children[i], sourceFile)) {
return children[i];
}
}
Expand Down Expand Up @@ -1022,10 +1022,10 @@ namespace ts {
}
}

function nodeHasTokens(n: Node): boolean {
function nodeHasTokens(n: Node, sourceFile: SourceFileLike): boolean {
// If we have a token or node that has a non-zero width, it must have tokens.
// Note: getWidth() does not take trivia into account.
return n.getWidth() !== 0;
return n.getWidth(sourceFile) !== 0;
}

export function getNodeModifiers(node: Node): string {
Expand Down

0 comments on commit 7b9cbfe

Please sign in to comment.