mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-11-18 20:11:14 +00:00
Clean up js decl code in checker+utilities
This commit is contained in:
1 parent
bad155f072
commit
0cadfcf6df
6 files changed
+200
-181
No files matched your search
+12
-18
@@ -4174,6 +4174,11 @@ namespace ts {
|
||||
}
|
||||
|
||||
function getWidenedTypeFromJSSpecialPropertyDeclarations(symbol: Symbol) {
|
||||
// function/class/{} assignments are fresh declarations, not property assignments:
|
||||
const specialDeclaration = getAssignedJavascriptInitializer(symbol.valueDeclaration);
|
||||
if (specialDeclaration) {
|
||||
return getWidenedLiteralType(checkExpressionCached(specialDeclaration));
|
||||
}
|
||||
const types: Type[] = [];
|
||||
let definedInConstructor = false;
|
||||
let definedInMethod = false;
|
||||
@@ -4367,18 +4372,6 @@ namespace ts {
|
||||
if (isInJavaScriptFile(declaration) && isJSDocPropertyLikeTag(declaration) && declaration.typeExpression) {
|
||||
return links.type = getTypeFromTypeNode(declaration.typeExpression.type);
|
||||
}
|
||||
if (isInJavaScriptFile(declaration) && isDeclarationOfDefaultedJavascriptContainerExpression(declaration)) {
|
||||
// !!! (probably out of place, probably not the right function to call)
|
||||
return links.type = checkExpression(((declaration as VariableDeclaration).initializer as BinaryExpression).right);
|
||||
}
|
||||
if (isInJavaScriptFile(declaration) && isAssignmentOfDefaultedJavascriptContainerExpression(declaration.parent)) {
|
||||
// !!! (probably out of place, probably not the right function to call)
|
||||
return links.type = checkExpression(((declaration.parent as BinaryExpression).right as BinaryExpression).right);
|
||||
}
|
||||
// TODO: Not sure this is needed (probably, but I need to write a test case for it)
|
||||
if (isInJavaScriptFile(declaration) && isAssignmentOfJavascriptContainerExpression(declaration)) {
|
||||
return links.type = checkExpression((declaration.parent as BinaryExpression).right);
|
||||
}
|
||||
// Handle variable, parameter or property
|
||||
if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) {
|
||||
return unknownType;
|
||||
@@ -4399,7 +4392,6 @@ namespace ts {
|
||||
|| isIdentifier(declaration)
|
||||
|| (isMethodDeclaration(declaration) && !isObjectLiteralMethod(declaration))
|
||||
|| isMethodSignature(declaration)) {
|
||||
// TODO: Might need to add a case here?
|
||||
// Symbol is property of some kind that is merged with something - should use `getTypeOfFuncClassEnumModule` and not `getTypeOfVariableOrParameterOrProperty`
|
||||
if (symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) {
|
||||
return getTypeOfFuncClassEnumModule(symbol);
|
||||
@@ -14120,7 +14112,8 @@ namespace ts {
|
||||
// expression has no contextual type, the right operand is contextually typed by the type of the left operand,
|
||||
// except for the special case of Javascript declarations of the form `namespace.prop = namespace.prop || {}`
|
||||
const type = getContextualType(binaryExpression);
|
||||
return !type && node === right && !isDeclarationOfDefaultedJavascriptContainerExpression(binaryExpression.parent) && !isAssignmentOfDefaultedJavascriptContainerExpression(binaryExpression.parent) ? getTypeOfExpression(left, /*cache*/ true) : type;
|
||||
return !type && node === right && !getDeclaredJavascriptInitializer(binaryExpression.parent) && !getAssignedJavascriptInitializer(binaryExpression) ?
|
||||
getTypeOfExpression(left, /*cache*/ true) : type;
|
||||
case SyntaxKind.AmpersandAmpersandToken:
|
||||
case SyntaxKind.CommaToken:
|
||||
return node === right ? getContextualType(binaryExpression) : undefined;
|
||||
@@ -18994,7 +18987,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
function checkBinaryExpression(node: BinaryExpression, checkMode?: CheckMode) {
|
||||
if (node.operatorToken.kind === SyntaxKind.BarBarToken && isInJavaScriptFile(node) && isAssignmentOfDefaultedJavascriptContainerExpression(node.parent)) {
|
||||
if (node.operatorToken.kind === SyntaxKind.BarBarToken && isInJavaScriptFile(node) && getAssignedJavascriptInitializer(node)) {
|
||||
return checkExpression(node.right, checkMode);
|
||||
}
|
||||
return checkBinaryLikeExpression(node.left, node.operatorToken, node.right, checkMode, node);
|
||||
@@ -19352,10 +19345,11 @@ namespace ts {
|
||||
}
|
||||
|
||||
function checkDeclarationInitializer(declaration: HasExpressionInitializer) {
|
||||
const type = getTypeOfExpression(declaration.initializer, /*cache*/ true);
|
||||
const initializer = isInJavaScriptFile(declaration) && getDeclaredJavascriptInitializer(declaration) || declaration.initializer;
|
||||
const type = getTypeOfExpression(initializer, /*cache*/ true);
|
||||
return getCombinedNodeFlags(declaration) & NodeFlags.Const ||
|
||||
(getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration)) ||
|
||||
isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type);
|
||||
isTypeAssertion(initializer) ? type : getWidenedLiteralType(type);
|
||||
}
|
||||
|
||||
function isLiteralOfContextualType(candidateType: Type, contextualType: Type): boolean {
|
||||
@@ -21872,7 +21866,7 @@ namespace ts {
|
||||
// Node is the primary declaration of the symbol, just validate the initializer
|
||||
// Don't validate for-in initializer as it is already an error
|
||||
if (node.initializer && node.parent.parent.kind !== SyntaxKind.ForInStatement) {
|
||||
const initializer = isInJavaScriptFile(node) && isDeclarationOfDefaultedJavascriptContainerExpression(node) ? (node.initializer as BinaryExpression).right : node.initializer;
|
||||
const initializer = isInJavaScriptFile(node) && getDeclaredJavascriptInitializer(node) || node.initializer;
|
||||
checkTypeAssignableTo(checkExpressionCached(initializer), type, node, /*headMessage*/ undefined);
|
||||
checkParameterInitializer(node);
|
||||
}
|
||||
|
||||
+39
-42
@@ -1487,60 +1487,57 @@ namespace ts {
|
||||
}
|
||||
|
||||
export function follow(symbol: Symbol) {
|
||||
return symbol && isDeclarationOfJavascriptContainerExpression(symbol.valueDeclaration) ? (symbol.valueDeclaration as VariableDeclaration).initializer.symbol :
|
||||
symbol && isDeclarationOfDefaultedJavascriptContainerExpression(symbol.valueDeclaration) ? ((symbol.valueDeclaration as VariableDeclaration).initializer as BinaryExpression).right.symbol :
|
||||
symbol && symbol.valueDeclaration && isAssignmentOfDefaultedJavascriptContainerExpression(symbol.valueDeclaration.parent) ? (((symbol.valueDeclaration.parent as BinaryExpression).right as BinaryExpression).right as BinaryExpression).symbol :
|
||||
symbol && isAssignmentOfJavascriptContainerExpression(symbol.valueDeclaration) ? ((symbol.valueDeclaration.parent as BinaryExpression).right as BinaryExpression).symbol :
|
||||
symbol;
|
||||
if (!symbol || !symbol.valueDeclaration) {
|
||||
return symbol;
|
||||
}
|
||||
const declaration = symbol.valueDeclaration;
|
||||
const e = getDeclaredJavascriptInitializer(declaration) || getAssignedJavascriptInitializer(declaration);
|
||||
return e ? e.symbol : symbol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the node is a variable declaration whose initializer is a function or class expression, or an empty object literal.
|
||||
* This function does not test if the node is in a JavaScript file or not.
|
||||
*/
|
||||
export function isDeclarationOfJavascriptContainerExpression(node: Node): node is VariableDeclaration {
|
||||
return node &&
|
||||
isVariableDeclaration(node) &&
|
||||
node.initializer &&
|
||||
isJavascriptContainerExpression(node.initializer);
|
||||
export function getDeclaredJavascriptInitializer(node: Node) {
|
||||
if (node && isVariableDeclaration(node) && node.initializer) {
|
||||
return getJavascriptInitializer(node.initializer) ||
|
||||
isIdentifier(node.name) && getDefaultedJavascriptInitializer(node.name, node.initializer);
|
||||
}
|
||||
}
|
||||
|
||||
function isJavascriptContainerExpression(e: Expression) {
|
||||
return e.kind === SyntaxKind.FunctionExpression ||
|
||||
e.kind === SyntaxKind.ClassExpression ||
|
||||
isObjectLiteralExpression(e) && e.properties.length === 0;
|
||||
export function getAssignedJavascriptInitializer(node: Node) {
|
||||
return (isBinaryExpression(node) && node.operatorToken.kind === SyntaxKind.BarBarToken || isPropertyAccessExpression(node)) &&
|
||||
node.parent && isBinaryExpression(node.parent) &&
|
||||
node.parent.operatorToken.kind === SyntaxKind.EqualsToken &&
|
||||
(getJavascriptInitializer(node.parent.right) || getDefaultedJavascriptInitializer(node.parent.left as EntityNameExpression, node.parent.right));
|
||||
}
|
||||
|
||||
export function isDeclarationOfDefaultedJavascriptContainerExpression(node: Node): node is VariableDeclaration {
|
||||
return node &&
|
||||
isVariableDeclaration(node) &&
|
||||
node.initializer &&
|
||||
isBinaryExpression(node.initializer) &&
|
||||
isIdentifier(node.name) &&
|
||||
isSameName(node.name, node.initializer.left as EntityNameExpression) &&
|
||||
isJavascriptContainerExpression(node.initializer.right);
|
||||
function getJavascriptInitializer(e: Expression) {
|
||||
if(e.kind === SyntaxKind.FunctionExpression ||
|
||||
e.kind === SyntaxKind.ClassExpression ||
|
||||
isObjectLiteralExpression(e) && e.properties.length === 0) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
export function isAssignmentOfJavascriptContainerExpression(node: Node) {
|
||||
return node &&
|
||||
isPropertyAccessExpression(node) &&
|
||||
isBinaryExpression(node.parent) &&
|
||||
node.parent.right &&
|
||||
isJavascriptContainerExpression(node.parent.right);
|
||||
function getDefaultedJavascriptInitializer(name: EntityNameExpression, initializer: Expression) {
|
||||
const e = isBinaryExpression(initializer) && getJavascriptInitializer(initializer.right)
|
||||
if (e && isSameEntityName(name, (initializer as BinaryExpression).left as EntityNameExpression)) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
export function isAssignmentOfDefaultedJavascriptContainerExpression(node: Node) {
|
||||
// or ... uh ... maybe not? usage is pretty inconvenient
|
||||
// (changing what is the declaration might instead be the right answer)
|
||||
return node &&
|
||||
isBinaryExpression(node) &&
|
||||
node.right &&
|
||||
isBinaryExpression(node.right) &&
|
||||
isSameName(node.left as EntityNameExpression, node.right.left as EntityNameExpression) &&
|
||||
isJavascriptContainerExpression(node.right.right);
|
||||
}
|
||||
|
||||
function isSameName(name: EntityNameExpression, initializer: EntityNameExpression): boolean {
|
||||
/**
|
||||
* Is the 'declared' name the same as the one in the initializer?
|
||||
* @return true for identical entity names, as well as ones where the initializer is prefixed with
|
||||
* 'window', 'self' or 'global'. For example:
|
||||
*
|
||||
* var my = my || {}
|
||||
* var min = window.min || {}
|
||||
* my.app = self.my.app || class { }
|
||||
*/
|
||||
function isSameEntityName(name: EntityNameExpression, initializer: EntityNameExpression): boolean {
|
||||
if (isIdentifier(name) && isIdentifier(initializer)) {
|
||||
return name.escapedText === initializer.escapedText;
|
||||
}
|
||||
@@ -1550,10 +1547,10 @@ namespace ts {
|
||||
(initializer.expression.escapedText === "window" as __String ||
|
||||
initializer.expression.escapedText === "self" as __String ||
|
||||
initializer.expression.escapedText === "global" as __String)) &&
|
||||
isSameName(name, initializer.name);
|
||||
isSameEntityName(name, initializer.name);
|
||||
}
|
||||
if (isPropertyAccessExpression(name) && isPropertyAccessExpression(initializer)) {
|
||||
return name.name.escapedText === initializer.name.escapedText && isSameName(name.expression, initializer.expression);
|
||||
return name.name.escapedText === initializer.name.escapedText && isSameEntityName(name.expression, initializer.expression);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user