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

Fix visibility lookup for cjs require aliases #41257

Merged
merged 2 commits into from
Oct 27, 2020
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
8 changes: 8 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4164,6 +4164,14 @@ namespace ts {
&& isDeclarationVisible(declaration.parent)) {
return addVisibleAlias(declaration, declaration);
}
else if (symbol.flags & SymbolFlags.Alias && isBindingElement(declaration) && isInJSFile(declaration) && declaration.parent?.parent // exported import-like top-level JS require statement
&& isVariableDeclaration(declaration.parent.parent)
&& declaration.parent.parent.parent?.parent && isVariableStatement(declaration.parent.parent.parent.parent)
&& !hasSyntacticModifier(declaration.parent.parent.parent.parent, ModifierFlags.Export)
Copy link
Member

Choose a reason for hiding this comment

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

should we care about checking for const or require here? Probably doesn't matter because I don't think there are any other declarations that look like const { x } = require('y').

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe. Might just be worth checking for symbol.flags & SymbolFlags.Alias.

&& declaration.parent.parent.parent.parent.parent // check if the thing containing the variable statement is visible (ie, the file)
&& isDeclarationVisible(declaration.parent.parent.parent.parent.parent)) {
return addVisibleAlias(declaration, declaration.parent.parent.parent.parent);
}

// Declaration is not visible
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,6 @@ export const testFnTypes: {
[x: string]: any;
};
export namespace testFnTypes {
type input = boolean | Function | {
/**
* - Prop 1.
*/
prop1: string | RegExp | (string | RegExp)[];
/**
* - Prop 2.
*/
prop2: string;
};
type input = boolean | Function | myTypes.typeB;
}
import { myTypes } from "./file.js";
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ const testFnTypes = {
*/
function testFn(input) {
>testFn : (input: testFnTypes.input) => number | null
>input : boolean | Function | { prop1: string | RegExp | (string | RegExp)[]; prop2: string; }
>input : boolean | Function | myTypes.typeB

if (typeof input === 'number') {
>typeof input === 'number' : boolean
>typeof input : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>input : boolean | Function | { prop1: string | RegExp | (string | RegExp)[]; prop2: string; }
>input : boolean | Function | myTypes.typeB
>'number' : "number"

return 2 * input;
Expand Down