Skip to content

Commit

Permalink
Only keep binding element aliases if they are used in the signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirtitian committed Sep 8, 2023
1 parent 0055c91 commit aad9692
Show file tree
Hide file tree
Showing 12 changed files with 1,262 additions and 84 deletions.
6 changes: 5 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6121,12 +6121,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
141 changes: 122 additions & 19 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 @@ -5517,6 +5517,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,128 @@
declarationEmitBindingPatternsUnused.ts(81,35): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
declarationEmitBindingPatternsUnused.ts(85,41): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
declarationEmitBindingPatternsUnused.ts(91,11): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
declarationEmitBindingPatternsUnused.ts(92,15): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
declarationEmitBindingPatternsUnused.ts(93,16): error TS2842: 'alias' is an unused renaming of 'name'. Did you intend to use it as a type annotation?
declarationEmitBindingPatternsUnused.ts(94,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 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);
}

0 comments on commit aad9692

Please sign in to comment.