Skip to content

getTokenAtPosition: default includeJsDocComment to true #25015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
7 commits merged into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,11 @@ namespace ts {
bindJSDocTypeAlias(node as JSDocTypedefTag | JSDocCallbackTag);
break;
// In source files and blocks, bind functions first to match hoisting that occurs at runtime
case SyntaxKind.SourceFile:
case SyntaxKind.SourceFile: {
bindEachFunctionsFirst((node as SourceFile).statements);
bind((node as SourceFile).endOfFileToken);
break;
}
case SyntaxKind.Block:
case SyntaxKind.ModuleBlock:
bindEachFunctionsFirst((node as Block).statements);
Expand Down Expand Up @@ -1975,7 +1976,10 @@ namespace ts {
}
else if (!skipTransformFlagAggregation && (node.transformFlags & TransformFlags.HasComputedFlags) === 0) {
subtreeTransformFlags |= computeTransformFlagsForNode(node, 0);
const saveParent = parent;
if (node.kind === SyntaxKind.EndOfFileToken) parent = node;
bindJSDoc(node);
parent = saveParent;
}
inStrictMode = saveInStrictMode;
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ts.BreakpointResolver {
return undefined;
}

let tokenAtLocation = getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false);
let tokenAtLocation = getTokenAtPosition(sourceFile, position);
const lineOfPosition = sourceFile.getLineAndCharacterOfPosition(position).line;
if (sourceFile.getLineAndCharacterOfPosition(tokenAtLocation.getStart(sourceFile)).line > lineOfPosition) {
// Get previous token if the token is returned starts on new line
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/addMissingInvocationForDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ts.codefix {
});

function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, pos);
const decorator = findAncestor(token, isDecorator)!;
Debug.assert(!!decorator, "Expected position to be owned by a decorator.");
const replacement = createCall(decorator.expression, /*typeArguments*/ undefined, /*argumentsArray*/ undefined);
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/annotateWithTypeFromJSDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace ts.codefix {
});

function getDeclaration(file: SourceFile, pos: number): DeclarationWithType | undefined {
const name = getTokenAtPosition(file, pos, /*includeJsDocComment*/ false);
const name = getTokenAtPosition(file, pos);
// For an arrow function with no name, 'name' lands on the first parameter.
return tryCast(isParameter(name.parent) ? name.parent.parent : name.parent, parameterShouldGetTypeFromJSDoc);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/convertFunctionToEs6Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ts.codefix {

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, position: number, checker: TypeChecker): void {
const deletedNodes: { node: Node, inList: boolean }[] = [];
const ctorSymbol = checker.getSymbolAtLocation(getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false))!;
const ctorSymbol = checker.getSymbolAtLocation(getTokenAtPosition(sourceFile, position))!;

if (!ctorSymbol || !(ctorSymbol.flags & (SymbolFlags.Function | SymbolFlags.Variable))) {
// Bad input
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/convertToMappedObjectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace ts.codefix {

interface Info { readonly indexSignature: IndexSignatureDeclaration; readonly container: FixableDeclaration; }
function getInfo(sourceFile: SourceFile, pos: number): Info | undefined {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, pos);
const indexSignature = cast(token.parent.parent, isIndexSignatureDeclaration);
if (isClassDeclaration(indexSignature.parent)) return undefined;
const container = isInterfaceDeclaration(indexSignature.parent) ? indexSignature.parent : cast(indexSignature.parent.parent, isTypeAliasDeclaration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace ts.codefix {
});

function getQualifiedName(sourceFile: SourceFile, pos: number): QualifiedName & { left: Identifier } | undefined {
const qualifiedName = findAncestor(getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ true), isQualifiedName)!;
const qualifiedName = findAncestor(getTokenAtPosition(sourceFile, pos), isQualifiedName)!;
Debug.assert(!!qualifiedName, "Expected position to be owned by a qualified name.");
return isIdentifier(qualifiedName.left) ? qualifiedName as QualifiedName & { left: Identifier } : undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixAddMissingMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace ts.codefix {
// The identifier of the missing property. eg:
// this.missing = 1;
// ^^^^^^^
const token = getTokenAtPosition(tokenSourceFile, tokenPos, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(tokenSourceFile, tokenPos);
if (!isIdentifier(token)) {
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ts.codefix {
});

function getImportTypeNode(sourceFile: SourceFile, pos: number): ImportTypeNode {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, pos);
Debug.assert(token.kind === SyntaxKind.ImportKeyword);
Debug.assert(token.parent.kind === SyntaxKind.ImportType);
return <ImportTypeNode>token.parent;
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixAwaitInSyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace ts.codefix {
}

function getNodes(sourceFile: SourceFile, start: number): { insertBefore: Node, returnType: TypeNode | undefined } | undefined {
const token = getTokenAtPosition(sourceFile, start, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, start);
const containingFunction = getContainingFunction(token);
if (!containingFunction) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixCannotFindModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace ts.codefix {
}

function getTypesPackageNameToInstall(host: LanguageServiceHost, sourceFile: SourceFile, pos: number, diagCode: number): string | undefined {
const moduleName = cast(getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false), isStringLiteral).text;
const moduleName = cast(getTokenAtPosition(sourceFile, pos), isStringLiteral).text;
const { packageName } = getPackageName(moduleName);
return diagCode === errorCodeCannotFindModule
? (JsTyping.nodeCoreModules.has(packageName) ? "@types/node" : undefined)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace ts.codefix {
function getClass(sourceFile: SourceFile, pos: number): ClassLikeDeclaration {
// Token is the identifier in the case of a class declaration
// or the class keyword token in the case of a class expression.
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, pos);
return cast(token.parent, isClassLike);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ts.codefix {
});

function getClass(sourceFile: SourceFile, pos: number): ClassLikeDeclaration {
return Debug.assertDefined(getContainingClass(getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false)));
return Debug.assertDefined(getContainingClass(getTokenAtPosition(sourceFile, pos)));
}

function symbolPointsToNonPrivateMember (symbol: Symbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace ts.codefix {
}

function getNodes(sourceFile: SourceFile, pos: number): { readonly constructor: ConstructorDeclaration, readonly superCall: ExpressionStatement } | undefined {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, pos);
if (token.kind !== SyntaxKind.ThisKeyword) return undefined;
const constructor = getContainingFunction(token) as ConstructorDeclaration;
const superCall = findSuperCall(constructor.body!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ts.codefix {
});

function getNode(sourceFile: SourceFile, pos: number): ConstructorDeclaration {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, pos);
Debug.assert(token.kind === SyntaxKind.ConstructorKeyword);
return token.parent as ConstructorDeclaration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace ts.codefix {
});

function getNodes(sourceFile: SourceFile, pos: number) {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, pos);
const heritageClauses = getContainingClass(token)!.heritageClauses!;
const extendsToken = heritageClauses[0].getFirstToken()!;
return extendsToken.kind === SyntaxKind.ExtendsKeyword ? { extendsToken, heritageClauses } : undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixForgottenThisPropertyAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace ts.codefix {

interface Info { readonly node: Identifier; readonly className: string | undefined; }
function getInfo(sourceFile: SourceFile, pos: number, diagCode: number): Info | undefined {
const node = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const node = getTokenAtPosition(sourceFile, pos);
if (!isIdentifier(node)) return undefined;
return { node, className: diagCode === didYouMeanStaticMemberCode ? getContainingClass(node)!.name!.text : undefined };
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/fixInvalidImportSyntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace ts.codefix {
function getActionsForUsageOfInvalidImport(context: CodeFixContext): CodeFixAction[] | undefined {
const sourceFile = context.sourceFile;
const targetKind = Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures.code === context.errorCode ? SyntaxKind.CallExpression : SyntaxKind.NewExpression;
const node = findAncestor(getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false), a => a.kind === targetKind && a.getStart() === context.span.start && a.getEnd() === (context.span.start + context.span.length)) as CallExpression | NewExpression;
const node = findAncestor(getTokenAtPosition(sourceFile, context.span.start), a => a.kind === targetKind && a.getStart() === context.span.start && a.getEnd() === (context.span.start + context.span.length)) as CallExpression | NewExpression;
if (!node) {
return [];
}
Expand Down Expand Up @@ -69,7 +69,7 @@ namespace ts.codefix {

function getActionsForInvalidImportLocation(context: CodeFixContext): CodeFixAction[] | undefined {
const sourceFile = context.sourceFile;
const node = findAncestor(getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false), a => a.getStart() === context.span.start && a.getEnd() === (context.span.start + context.span.length));
const node = findAncestor(getTokenAtPosition(sourceFile, context.span.start), a => a.getStart() === context.span.start && a.getEnd() === (context.span.start + context.span.length));
if (!node) {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixJSDocTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace ts.codefix {
}

function getInfo(sourceFile: SourceFile, pos: number, checker: TypeChecker): { readonly typeNode: TypeNode, readonly type: Type } | undefined {
const decl = findAncestor(getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false), isTypeContainer);
const decl = findAncestor(getTokenAtPosition(sourceFile, pos), isTypeContainer);
const typeNode = decl && decl.type;
return typeNode && { typeNode, type: checker.getTypeFromTypeNode(typeNode) };
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixSpelling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ts.codefix {
// This is the identifier of the misspelled word. eg:
// this.speling = 1;
// ^^^^^^^
const node = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false); // TODO: GH#15852
const node = getTokenAtPosition(sourceFile, pos);
const checker = context.program.getTypeChecker();

let suggestion: string | undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixStrictClassInitialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace ts.codefix {
});

function getPropertyDeclaration (sourceFile: SourceFile, pos: number): PropertyDeclaration | undefined {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, pos);
return isIdentifier(token) ? cast(token.parent, isPropertyDeclaration) : undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixUnreachableCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ts.codefix {
});

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, start: number): void {
const token = getTokenAtPosition(sourceFile, start, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, start);
const statement = findAncestor(token, isStatement)!;
Debug.assert(statement.getStart(sourceFile) === token.getStart(sourceFile));

Expand Down
12 changes: 6 additions & 6 deletions src/services/codefixes/fixUnusedIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ts.codefix {
getCodeActions(context) {
const { errorCode, sourceFile, program } = context;
const checker = program.getTypeChecker();
const startToken = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false);
const startToken = getTokenAtPosition(sourceFile, context.span.start);

const importDecl = tryGetFullImport(startToken);
if (importDecl) {
Expand Down Expand Up @@ -54,7 +54,7 @@ namespace ts.codefix {
const { sourceFile, program } = context;
const checker = program.getTypeChecker();
return codeFixAll(context, errorCodes, (changes, diag) => {
const startToken = getTokenAtPosition(sourceFile, diag.start, /*includeJsDocComment*/ false);
const startToken = getTokenAtPosition(sourceFile, diag.start);
const token = findPrecedingToken(textSpanEnd(diag), diag.file)!;
switch (context.fixId) {
case fixIdPrefix:
Expand Down Expand Up @@ -173,8 +173,8 @@ namespace ts.codefix {
const typeParameters = getEffectiveTypeParameterDeclarations(<DeclarationWithTypeParameters>parent.parent);
if (typeParameters.length === 1) {
const { pos, end } = cast(typeParameters, isNodeArray);
const previousToken = getTokenAtPosition(sourceFile, pos - 1, /*includeJsDocComment*/ true);
const nextToken = getTokenAtPosition(sourceFile, end, /*includeJsDocComment*/ true);
const previousToken = getTokenAtPosition(sourceFile, pos - 1);
const nextToken = getTokenAtPosition(sourceFile, end);
Debug.assert(previousToken.kind === SyntaxKind.LessThanToken);
Debug.assert(nextToken.kind === SyntaxKind.GreaterThanToken);

Expand Down Expand Up @@ -251,7 +251,7 @@ namespace ts.codefix {
else {
// import |d,| * as ns from './file'
const start = importClause.name!.getStart(sourceFile);
const nextToken = getTokenAtPosition(sourceFile, importClause.name!.end, /*includeJsDocComment*/ false);
const nextToken = getTokenAtPosition(sourceFile, importClause.name!.end);
if (nextToken && nextToken.kind === SyntaxKind.CommaToken) {
// shift first non-whitespace position after comma to the start position of the node
const end = skipTrivia(sourceFile.text, nextToken.end, /*stopAfterLineBreaks*/ false, /*stopAtComments*/ true);
Expand Down Expand Up @@ -285,7 +285,7 @@ namespace ts.codefix {
// Delete named imports while preserving the default import
// import d|, * as ns| from './file'
// import d|, { a }| from './file'
const previousToken = getTokenAtPosition(sourceFile, namedBindings.pos - 1, /*includeJsDocComment*/ false);
const previousToken = getTokenAtPosition(sourceFile, namedBindings.pos - 1);
if (previousToken && previousToken.kind === SyntaxKind.CommaToken) {
changes.deleteRange(sourceFile, { pos: previousToken.getStart(), end: namedBindings.end });
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixUnusedLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ts.codefix {
});

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, start: number): void {
const token = getTokenAtPosition(sourceFile, start, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, start);
const labeledStatement = cast(token.parent, isLabeledStatement);
const pos = token.getStart(sourceFile);
const statementPos = labeledStatement.statement.getStart(sourceFile);
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ namespace ts.codefix {
}

function getActionsForUMDImport(context: CodeFixContext): CodeFixAction[] | undefined {
const token = getTokenAtPosition(context.sourceFile, context.span.start, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(context.sourceFile, context.span.start);
const checker = context.program.getTypeChecker();

let umdSymbol: Symbol | undefined;
Expand Down Expand Up @@ -385,7 +385,7 @@ namespace ts.codefix {
// This will always be an Identifier, since the diagnostics we fix only fail on identifiers.
const { sourceFile, span, program, cancellationToken } = context;
const checker = program.getTypeChecker();
const symbolToken = getTokenAtPosition(sourceFile, span.start, /*includeJsDocComment*/ false);
const symbolToken = getTokenAtPosition(sourceFile, span.start);
// If we're at `<Foo/>`, we must check if `Foo` is already in scope, and if so, get an import for `React` instead.
const symbolName = isJsxOpeningLikeElement(symbolToken.parent)
&& symbolToken.parent.tagName === symbolToken
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/inferFromUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace ts.codefix {
return undefined; // TODO: GH#20113
}

const token = getTokenAtPosition(sourceFile, start, /*includeJsDocComment*/ false);
const token = getTokenAtPosition(sourceFile, start);
let declaration!: Declaration | undefined;
const changes = textChanges.ChangeTracker.with(context, changes => { declaration = doChange(changes, sourceFile, token, errorCode, program, cancellationToken, /*markSeenseen*/ returnTrue); });
const name = getNameOfDeclaration(declaration!);
Expand All @@ -42,7 +42,7 @@ namespace ts.codefix {
const { sourceFile, program, cancellationToken } = context;
const markSeen = nodeSeenTracker();
return codeFixAll(context, errorCodes, (changes, err) => {
doChange(changes, sourceFile, getTokenAtPosition(err.file, err.start, /*includeJsDocComment*/ false), err.code, program, cancellationToken, markSeen);
doChange(changes, sourceFile, getTokenAtPosition(err.file, err.start), err.code, program, cancellationToken, markSeen);
});
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/requireInTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace ts.codefix {

interface Info { readonly statement: VariableStatement; readonly name: Identifier; readonly required: StringLiteralLike; }
function getInfo(sourceFile: SourceFile, pos: number): Info {
const { parent } = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const { parent } = getTokenAtPosition(sourceFile, pos);
if (!isRequireCall(parent, /*checkArgumentIsStringLiteralLike*/ true)) throw Debug.failBadSyntaxKind(parent);
const decl = cast(parent.parent, isVariableDeclaration);
return { statement: cast(decl.parent.parent, isVariableStatement), name: cast(decl.name, isIdentifier), required: parent.arguments[0] };
Expand Down
Loading