Skip to content

Nested assignment to a require alias isn't a declaration #40186

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 1 commit into from
Sep 1, 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
4 changes: 4 additions & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2974,6 +2974,10 @@ namespace ts {
if (!isInJSFile(node) && !isFunctionSymbol(parentSymbol)) {
return;
}
const rootExpr = getLeftmostAccessExpression(node.left);
if (isIdentifier(rootExpr) && lookupSymbolForName(container, rootExpr.escapedText)!?.flags & SymbolFlags.Alias) {
return;
}
// Fix up parent pointers since we're going to use these nodes before we bind into them
setParent(node.left, node);
setParent(node.right, node);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,7 @@ namespace ts {

function getTargetOfImportEqualsDeclaration(node: ImportEqualsDeclaration | VariableDeclaration, dontResolveAlias: boolean): Symbol | undefined {
if (isVariableDeclaration(node) && node.initializer && isPropertyAccessExpression(node.initializer)) {
const name = (getLeftmostPropertyAccessExpression(node.initializer.expression) as CallExpression).arguments[0] as StringLiteral;
const name = (getLeftmostAccessExpression(node.initializer.expression) as CallExpression).arguments[0] as StringLiteral;
return isIdentifier(node.initializer.name)
? getPropertyOfType(resolveExternalModuleTypeByLiteral(name), node.initializer.name.escapedText)
: undefined;
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,7 @@ namespace ts {

export function getExternalModuleRequireArgument(node: Node) {
return isRequireVariableDeclaration(node, /*requireStringLiteralLikeArgument*/ true)
&& (getLeftmostPropertyAccessExpression(node.initializer) as CallExpression).arguments[0] as StringLiteral;
&& (getLeftmostAccessExpression(node.initializer) as CallExpression).arguments[0] as StringLiteral;
}

export function isInternalModuleImportEqualsDeclaration(node: Node): node is ImportEqualsDeclaration {
Expand Down Expand Up @@ -1929,7 +1929,7 @@ namespace ts {
export function isRequireVariableDeclaration(node: Node, requireStringLiteralLikeArgument: boolean): node is VariableDeclaration;
export function isRequireVariableDeclaration(node: Node, requireStringLiteralLikeArgument: boolean): node is VariableDeclaration {
node = getRootDeclaration(node);
return isVariableDeclaration(node) && !!node.initializer && isRequireCall(getLeftmostPropertyAccessExpression(node.initializer), requireStringLiteralLikeArgument);
return isVariableDeclaration(node) && !!node.initializer && isRequireCall(getLeftmostAccessExpression(node.initializer), requireStringLiteralLikeArgument);
}

export function isRequireVariableStatement(node: Node, requireStringLiteralLikeArgument = true): node is RequireVariableStatement {
Expand Down Expand Up @@ -5452,8 +5452,8 @@ namespace ts {
return node.kind === SyntaxKind.NamedImports || node.kind === SyntaxKind.NamedExports;
}

export function getLeftmostPropertyAccessExpression(expr: Expression): Expression {
while (isPropertyAccessExpression(expr)) {
export function getLeftmostAccessExpression(expr: Expression): Expression {
while (isAccessExpression(expr)) {
expr = expr.expression;
}
return expr;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/conformance/salsa/bug40140.js(1,19): error TS7016: Could not find a declaration file for module 'untyped'. 'tests/cases/conformance/salsa/node_modules/untyped/index.js' implicitly has an 'any' type.


==== tests/cases/conformance/salsa/bug40140.js (1 errors) ====
const u = require('untyped');
~~~~~~~~~
!!! error TS7016: Could not find a declaration file for module 'untyped'. 'tests/cases/conformance/salsa/node_modules/untyped/index.js' implicitly has an 'any' type.
u.assignment.nested = true
u.noError()


==== tests/cases/conformance/salsa/node_modules/untyped/index.js (0 errors) ====
module.exports = {}

21 changes: 21 additions & 0 deletions tests/baselines/reference/namespaceAssignmentToRequireAlias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/conformance/salsa/namespaceAssignmentToRequireAlias.ts] ////

//// [index.js]
module.exports = {}

//// [bug40140.js]
const u = require('untyped');
u.assignment.nested = true
u.noError()



//// [bug40140.js]
"use strict";
var u = require('untyped');
u.assignment.nested = true;
u.noError();


//// [bug40140.d.ts]
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/conformance/salsa/bug40140.js ===
const u = require('untyped');
>u : Symbol(u, Decl(bug40140.js, 0, 5))
>require : Symbol(require)

u.assignment.nested = true
>u : Symbol(u, Decl(bug40140.js, 0, 5))

u.noError()
>u : Symbol(u, Decl(bug40140.js, 0, 5))


23 changes: 23 additions & 0 deletions tests/baselines/reference/namespaceAssignmentToRequireAlias.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/salsa/bug40140.js ===
const u = require('untyped');
>u : any
>require('untyped') : any
>require : any
>'untyped' : "untyped"

u.assignment.nested = true
>u.assignment.nested = true : true
>u.assignment.nested : any
>u.assignment : any
>u : any
>assignment : any
>nested : any
>true : true

u.noError()
>u.noError() : any
>u.noError : any
>u : any
>noError : any


13 changes: 13 additions & 0 deletions tests/cases/conformance/salsa/namespaceAssignmentToRequireAlias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @allowJs: true
// @checkJs: true
// @strict: true
// @outDir: out
// @declaration: true
// @filename: node_modules/untyped/index.js
module.exports = {}

// @filename: bug40140.js
const u = require('untyped');
u.assignment.nested = true
u.noError()