Skip to content

Commit

Permalink
Separate our usages of utilities that expect variables initialized to…
Browse files Browse the repository at this point in the history
… require(...) and require(...).foo.
  • Loading branch information
DanielRosenwasser committed Jan 14, 2022
1 parent d954948 commit f476e84
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/compiler/binder.ts
Expand Up @@ -3274,15 +3274,15 @@ namespace ts {
return isEnumConst(node)
? bindBlockScopedDeclaration(node, SymbolFlags.ConstEnum, SymbolFlags.ConstEnumExcludes)
: bindBlockScopedDeclaration(node, SymbolFlags.RegularEnum, SymbolFlags.RegularEnumExcludes);
}
}

function bindVariableDeclarationOrBindingElement(node: VariableDeclaration | BindingElement) {
if (inStrictMode) {
checkStrictModeEvalOrArguments(node, node.name);
}

if (!isBindingPattern(node.name)) {
if (isInJSFile(node) && isRequireVariableDeclaration(node) && !getJSDocTypeTag(node)) {
if (isInJSFile(node) && isAccessedOrBareRequireVariableDeclaration(node) && !getJSDocTypeTag(node)) {
declareSymbolAndAddToSymbolTable(node as Declaration, SymbolFlags.Alias, SymbolFlags.AliasExcludes);
}
else if (isBlockOrCatchScoped(node)) {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Expand Up @@ -2596,7 +2596,7 @@ namespace ts {
&& isAliasableOrJsExpression(node.parent.right)
|| node.kind === SyntaxKind.ShorthandPropertyAssignment
|| node.kind === SyntaxKind.PropertyAssignment && isAliasableOrJsExpression((node as PropertyAssignment).initializer)
|| isRequireVariableDeclaration(node);
|| isAccessedOrBareRequireVariableDeclaration(node);
}

function isAliasableOrJsExpression(e: Expression) {
Expand Down Expand Up @@ -36984,7 +36984,7 @@ namespace ts {
}
// For a commonjs `const x = require`, validate the alias and exit
const symbol = getSymbolOfNode(node);
if (symbol.flags & SymbolFlags.Alias && isRequireVariableDeclaration(node)) {
if (symbol.flags & SymbolFlags.Alias && isAccessedOrBareRequireVariableDeclaration(node)) {
checkAliasSymbol(node);
return;
}
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/types.ts
Expand Up @@ -4668,6 +4668,11 @@ namespace ts {
readonly initializer: RequireOrImportCall;
}

/* @internal */
export interface AccessedOrBareRequireVariableDeclaration extends VariableDeclaration {
readonly initializer: RequireOrImportCall | AccessExpression;
}

/* @internal */
export interface RequireVariableStatement extends VariableStatement {
readonly declarationList: RequireVariableDeclarationList;
Expand Down
17 changes: 15 additions & 2 deletions src/compiler/utilities.ts
Expand Up @@ -2046,7 +2046,7 @@ namespace ts {
}

export function getExternalModuleRequireArgument(node: Node) {
return isRequireVariableDeclaration(node) && (getLeftmostAccessExpression(node.initializer) as CallExpression).arguments[0] as StringLiteral;
return isAccessedOrBareRequireVariableDeclaration(node) && (getLeftmostAccessExpression(node.initializer) as CallExpression).arguments[0] as StringLiteral;
}

export function isInternalModuleImportEqualsDeclaration(node: Node): node is ImportEqualsDeclaration {
Expand Down Expand Up @@ -2114,10 +2114,23 @@ namespace ts {
* This function does not test if the node is in a JavaScript file or not.
*/
export function isRequireVariableDeclaration(node: Node): node is RequireVariableDeclaration {
return isVariableDeclarationInitializedWithRequireHelper(node, /*allowAccessedRequire*/ false);
}

/**
* Like `isRequireVariableDeclaration` but allows things like `require("...").foo.bar` or `require("...")["baz"]`.
*/
export function isAccessedOrBareRequireVariableDeclaration(node: Node): node is AccessedOrBareRequireVariableDeclaration {
return isVariableDeclarationInitializedWithRequireHelper(node, /*allowAccessedRequire*/ true);
}

function isVariableDeclarationInitializedWithRequireHelper(node: Node, allowAccessedRequire: boolean) {
if (node.kind === SyntaxKind.BindingElement) {
node = node.parent.parent;
}
return isVariableDeclaration(node) && !!node.initializer && isRequireCall(getLeftmostAccessExpression(node.initializer), /*requireStringLiteralLikeArgument*/ true);
return isVariableDeclaration(node) &&
!!node.initializer &&
isRequireCall(allowAccessedRequire ? getLeftmostAccessExpression(node.initializer) : node.initializer, /*requireStringLiteralLikeArgument*/ true);
}

export function isRequireVariableStatement(node: Node): node is RequireVariableStatement {
Expand Down
2 changes: 1 addition & 1 deletion src/services/findAllReferences.ts
Expand Up @@ -1531,7 +1531,7 @@ namespace ts.FindAllReferences {
// Use the parent symbol if the location is commonjs require syntax on javascript files only.
if (isInJSFile(referenceLocation)
&& referenceLocation.parent.kind === SyntaxKind.BindingElement
&& isRequireVariableDeclaration(referenceLocation.parent)) {
&& isAccessedOrBareRequireVariableDeclaration(referenceLocation.parent)) {
referenceSymbol = referenceLocation.parent.symbol;
// The parent will not have a symbol if it's an ObjectBindingPattern (when destructuring is used). In
// this case, just skip it, since the bound identifiers are not an alias of the import.
Expand Down
2 changes: 1 addition & 1 deletion src/services/goToDefinition.ts
Expand Up @@ -290,7 +290,7 @@ namespace ts.GoToDefinition {
return declaration.parent.kind === SyntaxKind.NamedImports;
case SyntaxKind.BindingElement:
case SyntaxKind.VariableDeclaration:
return isInJSFile(declaration) && isRequireVariableDeclaration(declaration);
return isInJSFile(declaration) && isAccessedOrBareRequireVariableDeclaration(declaration);
default:
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/importTracker.ts
Expand Up @@ -621,7 +621,7 @@ namespace ts.FindAllReferences {
Debug.assert((parent as ImportClause | NamespaceImport).name === node);
return true;
case SyntaxKind.BindingElement:
return isInJSFile(node) && isRequireVariableDeclaration(parent);
return isInJSFile(node) && isAccessedOrBareRequireVariableDeclaration(parent);
default:
return false;
}
Expand Down

0 comments on commit f476e84

Please sign in to comment.