-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Add new special assignment kinds for recognizing Object.defineProperty calls #27208
Changes from 1 commit
66a5531
f91debf
a1780fe
66d2bb7
01589fb
71e8d8c
8da77c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4841,7 +4841,7 @@ namespace ts { | |
let jsdocType: Type | undefined; | ||
let types: Type[] | undefined; | ||
for (const declaration of symbol.declarations) { | ||
const expression = isBinaryExpression(declaration) ? declaration : | ||
const expression = (isBinaryExpression(declaration) || isCallExpression(declaration)) ? declaration : | ||
isPropertyAccessExpression(declaration) ? isBinaryExpression(declaration.parent) ? declaration.parent : declaration : | ||
undefined; | ||
if (!expression) { | ||
|
@@ -4857,9 +4857,11 @@ namespace ts { | |
definedInMethod = true; | ||
} | ||
} | ||
jsdocType = getJSDocTypeFromAssignmentDeclaration(jsdocType, expression, symbol, declaration); | ||
if (!isCallExpression(expression)) { | ||
jsdocType = getJSDocTypeFromAssignmentDeclaration(jsdocType, expression, symbol, declaration); | ||
} | ||
if (!jsdocType) { | ||
(types || (types = [])).push(isBinaryExpression(expression) ? getInitializerTypeFromAssignmentDeclaration(symbol, resolvedSymbol, expression, kind) : neverType); | ||
(types || (types = [])).push((isBinaryExpression(expression) || isCallExpression(expression)) ? getInitializerTypeFromAssignmentDeclaration(symbol, resolvedSymbol, expression, kind) : neverType); | ||
} | ||
} | ||
let type = jsdocType; | ||
|
@@ -4922,7 +4924,32 @@ namespace ts { | |
} | ||
|
||
/** If we don't have an explicit JSDoc type, get the type from the initializer. */ | ||
function getInitializerTypeFromAssignmentDeclaration(symbol: Symbol, resolvedSymbol: Symbol | undefined, expression: BinaryExpression, kind: AssignmentDeclarationKind) { | ||
function getInitializerTypeFromAssignmentDeclaration(symbol: Symbol, resolvedSymbol: Symbol | undefined, expression: BinaryExpression | CallExpression, kind: AssignmentDeclarationKind) { | ||
if (isCallExpression(expression)) { | ||
if (resolvedSymbol) { | ||
return getTypeOfSymbol(resolvedSymbol); // This shouldn't happen except under some hopefully forbidden merges of export assignments and object define assignments | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a test for this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK I can't construct something that can actually hit this codepath currently (since something like |
||
const objectLitType = checkExpressionCached((expression as BindableObjectDefinePropertyCall).arguments[2]); | ||
const valueType = getTypeOfPropertyOfType(objectLitType, "value" as __String); | ||
if (valueType) { | ||
return valueType; | ||
} | ||
const getFunc = getTypeOfPropertyOfType(objectLitType, "get" as __String); | ||
if (getFunc) { | ||
const getSig = getSingleCallSignature(getFunc); | ||
if (getSig) { | ||
return getReturnTypeOfSignature(getSig); | ||
} | ||
} | ||
const setFunc = getTypeOfPropertyOfType(objectLitType, "set" as __String); | ||
if (setFunc) { | ||
const setSig = getSingleCallSignature(setFunc); | ||
if (setSig) { | ||
return getTypeOfFirstParameterOfSignature(setSig); | ||
} | ||
} | ||
return anyType; | ||
} | ||
const type = resolvedSymbol ? getTypeOfSymbol(resolvedSymbol) : getWidenedLiteralType(checkExpressionCached(expression.right)); | ||
if (type.flags & TypeFlags.Object && | ||
kind === AssignmentDeclarationKind.ModuleExports && | ||
|
@@ -5176,7 +5203,7 @@ namespace ts { | |
} | ||
let type: Type | undefined; | ||
if (isInJSFile(declaration) && | ||
(isBinaryExpression(declaration) || isPropertyAccessExpression(declaration) && isBinaryExpression(declaration.parent))) { | ||
(isCallExpression(declaration) || isBinaryExpression(declaration) || isPropertyAccessExpression(declaration) && isBinaryExpression(declaration.parent))) { | ||
type = getWidenedTypeFromAssignmentDeclaration(symbol); | ||
} | ||
else if (isJSDocPropertyLikeTag(declaration) | ||
|
@@ -16535,6 +16562,9 @@ namespace ts { | |
} | ||
const thisType = checkThisExpression(thisAccess.expression); | ||
return thisType && getTypeOfPropertyOfContextualType(thisType, thisAccess.name.escapedText) || false; | ||
case AssignmentDeclarationKind.ObjectDefinePropertyValue: | ||
case AssignmentDeclarationKind.ObjectDefinePropertyExports: | ||
return Debug.fail("Unimplemented"); | ||
default: | ||
return Debug.assertNever(kind); | ||
} | ||
|
@@ -21206,18 +21236,52 @@ namespace ts { | |
return true; | ||
} | ||
|
||
function isReadonlyAssignmentDeclaration(d: Declaration) { | ||
if (!isCallExpression(d)) { | ||
return false; | ||
} | ||
if (!isBindableObjectDefinePropertyCall(d)) { | ||
return false; | ||
} | ||
const objectLitType = checkExpressionCached(d.arguments[2]); | ||
const valueType = getTypeOfPropertyOfType(objectLitType, "value" as __String); | ||
if (valueType) { | ||
const writableType = getTypeOfPropertyOfType(objectLitType, "writable" as __String); | ||
if (!writableType || writableType === falseType || writableType === regularFalseType) { | ||
return true; | ||
} | ||
// We include this definition whereupon we walk back and check the type at the declaration because | ||
// The usual definition of `Object.defineProperty` will _not_ cause literal type to be preserved in the | ||
// argument types, should the type be contextualized by the call itself. | ||
const writableProp = getPropertyOfType(objectLitType, "writable" as __String); | ||
if (writableProp!.valueDeclaration && isPropertyAssignment(writableProp!.valueDeclaration)) { | ||
const initializer = (writableProp!.valueDeclaration as PropertyAssignment).initializer; | ||
const rawOriginalType = checkExpression(initializer); | ||
if (rawOriginalType === falseType || rawOriginalType === regularFalseType) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
const setProp = getPropertyOfType(objectLitType, "set" as __String); | ||
return !setProp; | ||
} | ||
|
||
function isReadonlySymbol(symbol: Symbol): boolean { | ||
// The following symbols are considered read-only: | ||
// Properties with a 'readonly' modifier | ||
// Variables declared with 'const' | ||
// Get accessors without matching set accessors | ||
// Enum members | ||
// JS Assignments with writable false or no setter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pedantically (since I spent a day thinking about terms), it's better to say |
||
// Unions and intersections of the above (unions and intersections eagerly set isReadonly on creation) | ||
return !!(getCheckFlags(symbol) & CheckFlags.Readonly || | ||
symbol.flags & SymbolFlags.Property && getDeclarationModifierFlagsFromSymbol(symbol) & ModifierFlags.Readonly || | ||
symbol.flags & SymbolFlags.Variable && getDeclarationNodeFlagsFromSymbol(symbol) & NodeFlags.Const || | ||
symbol.flags & SymbolFlags.Accessor && !(symbol.flags & SymbolFlags.SetAccessor) || | ||
symbol.flags & SymbolFlags.EnumMember); | ||
symbol.flags & SymbolFlags.EnumMember || | ||
some(symbol.declarations, isReadonlyAssignmentDeclaration) | ||
); | ||
} | ||
|
||
function isReferenceToReadonlyEntity(expr: Expression, symbol: Symbol): boolean { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -713,7 +713,7 @@ namespace ts { | |
|
||
export type PropertyName = Identifier | StringLiteral | NumericLiteral | ComputedPropertyName; | ||
|
||
export type DeclarationName = Identifier | StringLiteral | NumericLiteral | ComputedPropertyName | BindingPattern; | ||
export type DeclarationName = Identifier | StringLiteralLike | NumericLiteral | ComputedPropertyName | BindingPattern; | ||
|
||
export interface Declaration extends Node { | ||
_declarationBrand: any; | ||
|
@@ -1697,6 +1697,10 @@ namespace ts { | |
arguments: NodeArray<Expression>; | ||
} | ||
|
||
/** @internal */ | ||
export type BindableObjectDefinePropertyCall = CallExpression & { arguments: { 0: EntityNameExpression, 1: StringLiteralLike | NumericLiteral, 2: ObjectLiteralExpression } }; | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra newline |
||
// see: https://tc39.github.io/ecma262/#prod-SuperCall | ||
export interface SuperCall extends CallExpression { | ||
expression: SuperExpression; | ||
|
@@ -4252,6 +4256,13 @@ namespace ts { | |
Property, | ||
// F.prototype = { ... } | ||
Prototype, | ||
// Object.defineProperty(x, 'name', { value: any, writable?: boolean (false by default) }); | ||
// Object.defineProperty(x, 'name', { get: Function, set: Function }); | ||
// Object.defineProperty(x, 'name', { get: Function }); | ||
// Object.defineProperty(x, 'name', { set: Function }); | ||
ObjectDefinePropertyValue, | ||
// Object.defineProperty(exports || module.exports, 'name', ...); | ||
ObjectDefinePropertyExports, | ||
} | ||
|
||
/** @deprecated Use FileExtensionInfo instead. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug.assertNever is better here I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assignmentKind
has all the non-object-define values still "possible" (they're not, because it's a call expression and not a binary expression) and so isn't never; so not quite?