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 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
17 changes: 11 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 @@ -6657,6 +6659,8 @@ namespace ts {
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 +6670,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: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3537,6 +3537,10 @@
"category": "Error",
"code": 4083
},
"Exported type alias '{0}' has or is using private name '{1}' from module {2}.": {
"category": "Error",
"code": 4084
},
"Conflicting definitions for '{0}' found at '{1}' and '{2}'. Consider installing a specific version of this library to resolve the conflict.": {
"category": "Error",
"code": 4090
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
22 changes: 14 additions & 8 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 @@ -124,7 +128,7 @@ namespace ts {
}
}

export function createGetSymbolAccessibilityDiagnosticForNode(node: DeclarationDiagnosticProducing): (symbolAccessibilityResult: SymbolAccessibilityResult) => SymbolAccessibilityDiagnostic | undefined {
export function createGetSymbolAccessibilityDiagnosticForNode(node: DeclarationDiagnosticProducing): GetSymbolAccessibilityDiagnostic {
if (isVariableDeclaration(node) || isPropertyDeclaration(node) || isPropertySignature(node) || isPropertyAccessExpression(node) || isBindingElement(node) || isConstructorDeclaration(node)) {
return getVariableDeclarationTypeVisibilityError;
}
Expand All @@ -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 Expand Up @@ -474,11 +478,13 @@ namespace ts {
};
}

function getTypeAliasDeclarationVisibilityError(): SymbolAccessibilityDiagnostic {
function getTypeAliasDeclarationVisibilityError(symbolAccessibilityResult: SymbolAccessibilityResult): SymbolAccessibilityDiagnostic {
return {
diagnosticMessage: Diagnostics.Exported_type_alias_0_has_or_is_using_private_name_1,
errorNode: (node as TypeAliasDeclaration).type,
typeName: (node as TypeAliasDeclaration).name
diagnosticMessage: symbolAccessibilityResult.errorModuleName
? Diagnostics.Exported_type_alias_0_has_or_is_using_private_name_1_from_module_2
: Diagnostics.Exported_type_alias_0_has_or_is_using_private_name_1,
errorNode: isJSDocTypeAlias(node) ? Debug.checkDefined(node.typeExpression) : (node as TypeAliasDeclaration).type,
typeName: isJSDocTypeAlias(node) ? getNameOfDeclaration(node) : (node as TypeAliasDeclaration).name,
};
}
}
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,6 @@
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 TS4084: Exported type alias 'BaseFactory' has or is using private name 'Base' from module "tests/cases/conformance/jsdoc/declarations/base".
tests/cases/conformance/jsdoc/declarations/file.js(3,4): error TS4084: Exported type alias 'BaseFactoryFactory' has or is using private name 'Base' from module "tests/cases/conformance/jsdoc/declarations/base".
tests/cases/conformance/jsdoc/declarations/file.js(6,5): error TS4084: Exported type alias 'couldntThinkOfAny' has or is using private name 'Base' from module "tests/cases/conformance/jsdoc/declarations/base".


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

module.exports = BaseFactory;

==== tests/cases/conformance/jsdoc/declarations/file.js (1 errors) ====
==== tests/cases/conformance/jsdoc/declarations/file.js (3 errors) ====
/** @typedef {import('./base')} BaseFactory */
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS4084: Exported type alias 'BaseFactory' has or is using private name 'Base' from module "tests/cases/conformance/jsdoc/declarations/base".
/**
* @callback BaseFactoryFactory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* @param {import('./base')} factory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
~
!!! error TS4084: Exported type alias 'BaseFactoryFactory' has or is using private name 'Base' from module "tests/cases/conformance/jsdoc/declarations/base".
/** @enum {import('./base')} */
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS4084: Exported type alias 'couldntThinkOfAny' has or is using private name 'Base' from module "tests/cases/conformance/jsdoc/declarations/base".
const couldntThinkOfAny = {}

/**
*
* @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;
};

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ module.exports = BaseFactory;

//// [file.js]
/** @typedef {import('./base')} BaseFactory */
/**
* @callback BaseFactoryFactory
* @param {import('./base')} factory
*/
/** @enum {import('./base')} */
const couldntThinkOfAny = {}

/**
*
Expand All @@ -37,6 +43,12 @@ BaseFactory.Base = Base;
module.exports = BaseFactory;
//// [file.js]
/** @typedef {import('./base')} BaseFactory */
/**
* @callback BaseFactoryFactory
* @param {import('./base')} factory
*/
/** @enum {import('./base')} */
const couldntThinkOfAny = {};
/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,25 @@ module.exports = BaseFactory;

=== tests/cases/conformance/jsdoc/declarations/file.js ===
/** @typedef {import('./base')} BaseFactory */
/**
* @callback BaseFactoryFactory
* @param {import('./base')} factory
*/
/** @enum {import('./base')} */
const couldntThinkOfAny = {}
>couldntThinkOfAny : Symbol(couldntThinkOfAny, Decl(file.js, 6, 5), Decl(file.js, 5, 4))

/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
const test = (base) => {
>test : Symbol(test, Decl(file.js, 7, 5))
>base : Symbol(base, Decl(file.js, 7, 14))
>test : Symbol(test, Decl(file.js, 13, 5))
>base : Symbol(base, Decl(file.js, 13, 14))

return base;
>base : Symbol(base, Decl(file.js, 7, 14))
>base : Symbol(base, Decl(file.js, 13, 14))

};

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ module.exports = BaseFactory;

=== tests/cases/conformance/jsdoc/declarations/file.js ===
/** @typedef {import('./base')} BaseFactory */
/**
* @callback BaseFactoryFactory
* @param {import('./base')} factory
*/
/** @enum {import('./base')} */
const couldntThinkOfAny = {}
>couldntThinkOfAny : {}
>{} : {}

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ module.exports = BaseFactory;

// @filename: file.js
/** @typedef {import('./base')} BaseFactory */
/**
* @callback BaseFactoryFactory
* @param {import('./base')} factory
*/
/** @enum {import('./base')} */
const couldntThinkOfAny = {}

/**
*
Expand Down