Skip to content

Perform usage analysis on refenced binding aliases in function signatures. #55683

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
merged 6 commits into from
Dec 13, 2023
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/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6184,12 +6184,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function hasVisibleDeclarations(symbol: Symbol, shouldComputeAliasToMakeVisible: boolean): SymbolVisibilityResult | undefined {
let aliasesToMakeVisible: LateVisibilityPaintedStatement[] | undefined;
let bindingElementToMakeVisible: BindingElement | undefined;
if (!every(filter(symbol.declarations, d => d.kind !== SyntaxKind.Identifier), getIsDeclarationVisible)) {
return undefined;
}
return { accessibility: SymbolAccessibility.Accessible, aliasesToMakeVisible };
return { accessibility: SymbolAccessibility.Accessible, aliasesToMakeVisible, bindingElementToMakeVisible };

function getIsDeclarationVisible(declaration: Declaration) {
if (isBindingElement(declaration) && findAncestor(declaration, isParameter)) {
bindingElementToMakeVisible = declaration;
}
if (!isDeclarationVisible(declaration)) {
// Mark the unexported alias as visible if its parent is visible
// because these kind of aliases can be used to name types in declaration file
Expand Down
330 changes: 278 additions & 52 deletions src/compiler/transformers/declarations.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5578,6 +5578,7 @@ export type LateVisibilityPaintedStatement =
/** @internal */
export interface SymbolVisibilityResult {
accessibility: SymbolAccessibility;
bindingElementToMakeVisible?: BindingElement;
aliasesToMakeVisible?: LateVisibilityPaintedStatement[]; // aliases that need to have this symbol visible
errorSymbolName?: string; // Optional symbol name that results in error
errorNode?: Node; // optional node that results in error
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
declarationEmitBindingPatternsUnused.ts(85,35): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
declarationEmitBindingPatternsUnused.ts(89,41): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
declarationEmitBindingPatternsUnused.ts(95,11): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
declarationEmitBindingPatternsUnused.ts(96,15): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
declarationEmitBindingPatternsUnused.ts(97,16): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
declarationEmitBindingPatternsUnused.ts(98,12): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?


==== declarationEmitBindingPatternsUnused.ts (6 errors) ====
type Named = { name: string }
function notReferenced({ name: alias }: Named) {

}
function notReferencedNestedAlias({ p: { name: alias } }: { p: Named }) {
}
function notReferencedArrayAlias([a, b, { name: alias }]: Named[]) {
}



function referencedInCode({ name: alias }: Named) {
return alias;
}

function referencedInSignarture({ name: alias }: Named): typeof alias {
return alias;
}

function referencedInSignartureKeyword({ function: alias }: { function: string }): typeof alias {
return null!;
}

function referencedInInferredType({ name: alias }: Named) {
type Named2 = { name: typeof alias }
return null! as Named2
}

function referencedInNestedFunction({ name: alias }: Named) {
return function(p: typeof alias) {

}
}

function referencedNestedAlias({ p: { name: alias } }: { p: Named }): typeof alias {
return alias;
}

function referencedArrayAlias([a, b, { name: alias }]: Named[]): typeof alias {
return alias;
}


class NotReferencedClass {
constructor({ name: alias }: Named) {
}
set x({ name: alias }: Named) {
console.log(alias);
}
m({ name: alias }: Named) {
console.log(alias);
}
}

class ReferencedInCodeClas {
constructor({ name: alias }: Named) {
console.log(alias);
}
set x({ name: alias }: Named) {
console.log(alias);
}
m({ name: alias }: Named) {
console.log(alias);
}
}

class ReferencedInSignartureClass {
constructor({ name: alias }: Named, p: typeof alias) {
console.log(alias);
}
set x({ name: alias }: Named & { o: typeof alias }) {

}
mReturnType({ name: alias }: Named): typeof alias {
return null!
}
mRerturnTypeNested({ name: alias }: Named): NonNullable<typeof alias> {
return null!
}
mParameter({ name: alias }: Named, p: typeof alias) {
return null!
}
}

let notReferencedFnType: ({ name: alias }: Named) => void;
~~~~~
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
let referencedInSignartureReturnTypeFnType: ({ name: alias }: Named) => typeof alias;
let referencedInSignartureParamTypeFnType: ({ name: alias }: Named, p: typeof alias) => void;

let notReferencedCtorType: new ({ name: alias }: Named) => void;
~~~~~
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
let referencedInSignartureReturnTypeCtorType: new ({ name: alias }: Named) => typeof alias;
let referencedInSignartureParamTypeCtorType: new ({ name: alias }: Named, p: typeof alias) => void;


interface NotReferencedInterface {
({ name: alias }: Named): void
~~~~~
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
new ({ name: alias }: Named): void
~~~~~
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
set x({ name: alias }: Named);
~~~~~
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
m({ name: alias }: Named);
~~~~~
!!! error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
}

interface ReferencedInSignartureInterface {
({ name: alias }: Named, p: typeof alias): void
({ name: alias }: Named): typeof alias

new ({ name: alias }: Named, p: typeof alias): void
new ({ name: alias }: Named): typeof alias
set x({ name: alias }: Named & { o: typeof alias })
mReturnType({ name: alias }: Named): typeof alias;
mRerturnTypeNested({ name: alias }: Named): NonNullable<typeof alias>;
mParameter({ name: alias }: Named, p: typeof alias);
}
Loading