mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-22 22:55:36 -05:00
treat ambient non-aliased 'require' as commonjs 'require'
This commit is contained in:
@@ -12819,16 +12819,41 @@ namespace ts {
|
||||
}
|
||||
|
||||
// In JavaScript files, calls to any identifier 'require' are treated as external module imports
|
||||
if (isInJavaScriptFile(node) &&
|
||||
isRequireCall(node, /*checkArgumentIsStringLiteral*/true) &&
|
||||
// Make sure require is not a local function
|
||||
!resolveName(node.expression, (<Identifier>node.expression).text, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)) {
|
||||
if (isInJavaScriptFile(node) && isCommonJsRequire(node)) {
|
||||
return resolveExternalModuleTypeByLiteral(<StringLiteral>node.arguments[0]);
|
||||
}
|
||||
|
||||
return getReturnTypeOfSignature(signature);
|
||||
}
|
||||
|
||||
function isCommonJsRequire(node: Node) {
|
||||
if (!isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) {
|
||||
return false;
|
||||
}
|
||||
// Make sure require is not a local function
|
||||
const resolvedRequire = resolveName(node.expression, (<Identifier>node.expression).text, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
|
||||
if (!resolvedRequire) {
|
||||
// project does not contain symbol named 'require' - assume commonjs require
|
||||
return true;
|
||||
}
|
||||
// project includes symbol named 'require' - make sure that it it ambient and local non-alias
|
||||
if (resolvedRequire.flags & SymbolFlags.Alias) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const targetDeclarationKind = resolvedRequire.flags & SymbolFlags.Function
|
||||
? SyntaxKind.FunctionDeclaration
|
||||
: resolvedRequire.flags & SymbolFlags.Variable
|
||||
? SyntaxKind.VariableDeclaration
|
||||
: SyntaxKind.Unknown;
|
||||
if (targetDeclarationKind !== SyntaxKind.Unknown) {
|
||||
const decl = getDeclarationOfKind(resolvedRequire, targetDeclarationKind);
|
||||
// function/variable declaration should be ambient
|
||||
return isInAmbientContext(decl);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function checkTaggedTemplateExpression(node: TaggedTemplateExpression): Type {
|
||||
return getReturnTypeOfSignature(getResolvedSignature(node));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user