Skip to content
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

@typedef: Improve error spans from declaration emit #42501

Merged
merged 5 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 12 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4183,7 +4183,8 @@ namespace ts {
return {
accessibility: SymbolAccessibility.CannotBeNamed,
errorSymbolName: symbolToString(symbol, enclosingDeclaration, meaning),
errorModuleName: symbolToString(symbolExternalModule)
errorModuleName: symbolToString(symbolExternalModule),
errorNode: isInJSFile(enclosingDeclaration) ? enclosingDeclaration : undefined,
};
}
}
Expand Down Expand Up @@ -4699,7 +4700,7 @@ namespace ts {

function createMappedTypeNodeFromType(type: MappedType) {
Debug.assert(!!(type.flags & TypeFlags.Object));
const readonlyToken = type.declaration.readonlyToken ? <ReadonlyToken | PlusToken | MinusToken>factory.createToken(type.declaration.readonlyToken.kind) : undefined;
const readonlyToken = type.declaration.readonlyToken ? <ReadonlyKeyword | PlusToken | MinusToken>factory.createToken(type.declaration.readonlyToken.kind) : undefined;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by deprecation fix

const questionToken = type.declaration.questionToken ? <QuestionToken | PlusToken | MinusToken>factory.createToken(type.declaration.questionToken.kind) : undefined;
let appropriateConstraintTypeNode: TypeNode;
if (isMappedTypeWithKeyofConstraintDeclaration(type)) {
Expand Down Expand Up @@ -6183,7 +6184,7 @@ namespace ts {
tracker: {
...oldcontext.tracker,
trackSymbol: (sym, decl, meaning) => {
const accessibleResult = isSymbolAccessible(sym, decl, meaning, /*computeALiases*/ false);
const accessibleResult = isSymbolAccessible(sym, decl, meaning, /*computeAliases*/ false);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by case fix

if (accessibleResult.accessibility === SymbolAccessibility.Accessible) {
// Lookup the root symbol of the chain of refs we'll use to access it and serialize it
const chain = lookupSymbolChainWorker(sym, context, meaning);
Expand Down Expand Up @@ -6625,16 +6626,17 @@ namespace ts {
function addResult(node: Statement, additionalModifierFlags: ModifierFlags) {
if (canHaveModifiers(node)) {
let newModifierFlags: ModifierFlags = ModifierFlags.None;
const enclosingDeclaration = context.enclosingDeclaration &&
(isJSDocTypeAlias(context.enclosingDeclaration) ? getSourceFileOfNode(context.enclosingDeclaration) : context.enclosingDeclaration);
if (additionalModifierFlags & ModifierFlags.Export &&
context.enclosingDeclaration &&
(isExportingScope(context.enclosingDeclaration) || isModuleDeclaration(context.enclosingDeclaration)) &&
enclosingDeclaration && (isExportingScope(enclosingDeclaration) || isModuleDeclaration(enclosingDeclaration)) &&
canHaveExportModifier(node)
) {
// Classes, namespaces, variables, functions, interfaces, and types should all be `export`ed in a module context if not private
newModifierFlags |= ModifierFlags.Export;
}
if (addingDeclare && !(newModifierFlags & ModifierFlags.Export) &&
(!context.enclosingDeclaration || !(context.enclosingDeclaration.flags & NodeFlags.Ambient)) &&
(!enclosingDeclaration || !(enclosingDeclaration.flags & NodeFlags.Ambient)) &&
(isEnumDeclaration(node) || isVariableStatement(node) || isFunctionDeclaration(node) || isClassDeclaration(node) || isModuleDeclaration(node))) {
// Classes, namespaces, variables, enums, and functions all need `declare` modifiers to be valid in a declaration file top-level scope
newModifierFlags |= ModifierFlags.Ambient;
Expand All @@ -6654,9 +6656,12 @@ namespace ts {
const typeParams = getSymbolLinks(symbol).typeParameters;
const typeParamDecls = map(typeParams, p => typeParameterToDeclaration(p, context));
const jsdocAliasDecl = find(symbol.declarations, isJSDocTypeAlias);

const commentText = jsdocAliasDecl ? jsdocAliasDecl.comment || jsdocAliasDecl.parent.comment : undefined;
const oldFlags = context.flags;
context.flags |= NodeBuilderFlags.InTypeAlias;
const oldEnclosingDecl = context.enclosingDeclaration;
context.enclosingDeclaration = jsdocAliasDecl;
const typeNode = jsdocAliasDecl && jsdocAliasDecl.typeExpression
&& isJSDocTypeExpression(jsdocAliasDecl.typeExpression)
&& serializeExistingTypeNode(context, jsdocAliasDecl.typeExpression.type, includePrivateSymbol, bundled)
Expand All @@ -6666,6 +6671,7 @@ namespace ts {
!commentText ? [] : [{ kind: SyntaxKind.MultiLineCommentTrivia, text: "*\n * " + commentText.replace(/\n/g, "\n * ") + "\n ", pos: -1, end: -1, hasTrailingNewLine: true }]
), modifierFlags);
context.flags = oldFlags;
context.enclosingDeclaration = oldEnclosingDecl;
}

function serializeInterface(symbol: Symbol, symbolName: string, modifierFlags: ModifierFlags) {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ namespace ts {

function transformDeclarationsForJS(sourceFile: SourceFile, bundled?: boolean) {
const oldDiag = getSymbolAccessibilityDiagnostic;
getSymbolAccessibilityDiagnostic = (s) => ({
getSymbolAccessibilityDiagnostic = (s) => (s.errorNode && canProduceDiagnostics(s.errorNode) ? createGetSymbolAccessibilityDiagnosticForNode(s.errorNode)(s) : ({
diagnosticMessage: s.errorModuleName
? Diagnostics.Declaration_emit_for_this_file_requires_using_private_name_0_from_module_1_An_explicit_type_annotation_may_unblock_declaration_emit
: Diagnostics.Declaration_emit_for_this_file_requires_using_private_name_0_An_explicit_type_annotation_may_unblock_declaration_emit,
errorNode: s.errorNode || sourceFile
});
}));
const result = resolver.getDeclarationStatementsForSourceFile(sourceFile, declarationEmitNodeBuilderFlags, symbolTracker, bundled);
getSymbolAccessibilityDiagnostic = oldDiag;
return result;
Expand Down
10 changes: 7 additions & 3 deletions src/compiler/transformers/declarations/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ namespace ts {
| TypeAliasDeclaration
| ConstructorDeclaration
| IndexSignatureDeclaration
| PropertyAccessExpression;
| PropertyAccessExpression
| JSDocTypedefTag
| JSDocCallbackTag
| JSDocEnumTag;

export function canProduceDiagnostics(node: Node): node is DeclarationDiagnosticProducing {
return isVariableDeclaration(node) ||
Expand All @@ -48,7 +51,8 @@ namespace ts {
isTypeAliasDeclaration(node) ||
isConstructorDeclaration(node) ||
isIndexSignatureDeclaration(node) ||
isPropertyAccessExpression(node);
isPropertyAccessExpression(node) ||
isJSDocTypeAlias(node);
}

export function createGetSymbolAccessibilityDiagnosticForNodeName(node: DeclarationDiagnosticProducing) {
Expand Down Expand Up @@ -149,7 +153,7 @@ namespace ts {
else if (isImportEqualsDeclaration(node)) {
return getImportEntityNameVisibilityError;
}
else if (isTypeAliasDeclaration(node)) {
else if (isTypeAliasDeclaration(node) || isJSDocTypeAlias(node)) {
return getTypeAliasDeclarationVisibilityError;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ export class Encoder<T> implements IEncoder<T> {
*/
encode(value: T): Uint8Array;
}
export type IEncoder<T> = import("./interface").Encoder<T>;
export type IEncoder<T> = import('./interface').Encoder<T>;
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export namespace myTypes {
/**
* - Prop 1.
*/
prop1: typeA;
prop1: myTypes.typeA;
/**
* - Prop 2.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export namespace myTypes {
/**
* - Prop 1.
*/
prop1: typeA;
prop1: myTypes.typeA;
/**
* - Prop 2.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/jsdoc/declarations/file.js(8,1): error TS9006: Declaration emit for this file requires using private name 'Base' from module '"tests/cases/conformance/jsdoc/declarations/base"'. An explicit type annotation may unblock declaration emit.
tests/cases/conformance/jsdoc/declarations/file.js(1,5): error TS4081: Exported type alias 'BaseFactory' has or is using private name 'Base'.


==== tests/cases/conformance/jsdoc/declarations/base.js (0 errors) ====
Expand All @@ -16,15 +16,15 @@ tests/cases/conformance/jsdoc/declarations/file.js(8,1): error TS9006: Declarati

==== tests/cases/conformance/jsdoc/declarations/file.js (1 errors) ====
/** @typedef {import('./base')} BaseFactory */
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS4081: Exported type alias 'BaseFactory' has or is using private name 'Base'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should add a Exported type alias 'BaseFactory' has or is using private name 'Base' from module "tests/cases/conformance/jsdoc/declarations/base". formatted error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the general case, yes -- in this case, with the import('./base') sitting right there, it's not so valuable.


/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
const test = (base) => {
~~~~~
!!! error TS9006: Declaration emit for this file requires using private name 'Base' from module '"tests/cases/conformance/jsdoc/declarations/base"'. An explicit type annotation may unblock declaration emit.
return base;
};