-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix default property assigned prototype #40836
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
175c394
Fix default-property-assignment decls+prototype property decls
sandersn 6b8a2df
small cleanup
sandersn e9a52e7
Merge branch 'master' into fix-default-property-assigned-prototype
sandersn 3ee18b5
make allowDeclaration parameter required
sandersn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -507,6 +507,7 @@ namespace ts { | |
}, | ||
getAugmentedPropertiesOfType, | ||
getRootSymbols, | ||
getSymbolOfExpando, | ||
getContextualType: (nodeIn: Expression, contextFlags?: ContextFlags) => { | ||
const node = getParseTreeNode(nodeIn, isExpression); | ||
if (!node) { | ||
|
@@ -8728,9 +8729,9 @@ namespace ts { | |
let links = getSymbolLinks(symbol); | ||
const originalLinks = links; | ||
if (!links.type) { | ||
const jsDeclaration = symbol.valueDeclaration && getDeclarationOfExpando(symbol.valueDeclaration); | ||
if (jsDeclaration) { | ||
const merged = mergeJSSymbols(symbol, getSymbolOfNode(jsDeclaration)); | ||
const expando = symbol.valueDeclaration && getSymbolOfExpando(symbol.valueDeclaration, /*allowDeclaration*/ false); | ||
if (expando) { | ||
const merged = mergeJSSymbols(symbol, expando); | ||
if (merged) { | ||
// note:we overwrite links because we just cloned the symbol | ||
symbol = links = merged; | ||
|
@@ -24828,7 +24829,7 @@ namespace ts { | |
const exprType = checkJsxAttribute(attributeDecl, checkMode); | ||
objectFlags |= getObjectFlags(exprType) & ObjectFlags.PropagatingFlags; | ||
|
||
const attributeSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient | member.flags, member.escapedName); | ||
const attributeSymbol = createSymbol(SymbolFlags.Property | member.flags, member.escapedName); | ||
attributeSymbol.declarations = member.declarations; | ||
attributeSymbol.parent = member.parent; | ||
if (member.valueDeclaration) { | ||
|
@@ -24887,7 +24888,7 @@ namespace ts { | |
const contextualType = getApparentTypeOfContextualType(openingLikeElement.attributes); | ||
const childrenContextualType = contextualType && getTypeOfPropertyOfContextualType(contextualType, jsxChildrenPropertyName); | ||
// If there are children in the body of JSX element, create dummy attribute "children" with the union of children types so that it will pass the attribute checking process | ||
const childrenPropSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, jsxChildrenPropertyName); | ||
const childrenPropSymbol = createSymbol(SymbolFlags.Property, jsxChildrenPropertyName); | ||
childrenPropSymbol.type = childrenTypes.length === 1 ? childrenTypes[0] : | ||
childrenContextualType && forEachType(childrenContextualType, isTupleLikeType) ? createTupleType(childrenTypes) : | ||
createArrayType(getUnionType(childrenTypes)); | ||
|
@@ -28076,15 +28077,59 @@ namespace ts { | |
} | ||
|
||
function getAssignedClassSymbol(decl: Declaration): Symbol | undefined { | ||
const assignmentSymbol = decl && decl.parent && | ||
(isFunctionDeclaration(decl) && getSymbolOfNode(decl) || | ||
isBinaryExpression(decl.parent) && getSymbolOfNode(decl.parent.left) || | ||
isVariableDeclaration(decl.parent) && getSymbolOfNode(decl.parent)); | ||
const prototype = assignmentSymbol && assignmentSymbol.exports && assignmentSymbol.exports.get("prototype" as __String); | ||
const init = prototype && prototype.valueDeclaration && getAssignedJSPrototype(prototype.valueDeclaration); | ||
const assignmentSymbol = decl && getSymbolOfExpando(decl, /*allowDeclaration*/ true); | ||
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. this is the actual fix |
||
const prototype = assignmentSymbol?.exports?.get("prototype" as __String); | ||
const init = prototype?.valueDeclaration && getAssignedJSPrototype(prototype.valueDeclaration); | ||
return init ? getSymbolOfNode(init) : undefined; | ||
} | ||
|
||
function getSymbolOfExpando(node: Node, allowDeclaration: boolean): Symbol | undefined { | ||
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. changes from the move:
|
||
if (!node.parent) { | ||
return undefined; | ||
} | ||
let name: Expression | BindingName | undefined; | ||
let decl: Node | undefined; | ||
if (isVariableDeclaration(node.parent) && node.parent.initializer === node) { | ||
if (!isInJSFile(node) && !isVarConst(node.parent)) { | ||
return undefined; | ||
} | ||
name = node.parent.name; | ||
decl = node.parent; | ||
} | ||
else if (isBinaryExpression(node.parent)) { | ||
const parentNode = node.parent; | ||
const parentNodeOperator = node.parent.operatorToken.kind; | ||
if (parentNodeOperator === SyntaxKind.EqualsToken && (allowDeclaration || parentNode.right === node)) { | ||
name = parentNode.left; | ||
decl = name; | ||
} | ||
else if (parentNodeOperator === SyntaxKind.BarBarToken || parentNodeOperator === SyntaxKind.QuestionQuestionToken) { | ||
if (isVariableDeclaration(parentNode.parent) && parentNode.parent.initializer === parentNode) { | ||
name = parentNode.parent.name; | ||
decl = parentNode.parent; | ||
} | ||
else if (isBinaryExpression(parentNode.parent) && parentNode.parent.operatorToken.kind === SyntaxKind.EqualsToken && (allowDeclaration || parentNode.parent.right === parentNode)) { | ||
name = parentNode.parent.left; | ||
decl = name; | ||
} | ||
|
||
if (!name || !isBindableStaticNameExpression(name) || !isSameEntityName(name, parentNode.left)) { | ||
return undefined; | ||
} | ||
} | ||
} | ||
else if (allowDeclaration && isFunctionDeclaration(node)) { | ||
name = node.name; | ||
decl = node; | ||
} | ||
|
||
if (!decl || !name || (!allowDeclaration && !getExpandoInitializer(node, isPrototypeAccess(name)))) { | ||
return undefined; | ||
} | ||
return getSymbolOfNode(decl); | ||
} | ||
|
||
|
||
function getAssignedJSPrototype(node: Node) { | ||
if (!node.parent) { | ||
return false; | ||
|
@@ -28161,14 +28206,11 @@ namespace ts { | |
} | ||
|
||
if (isInJSFile(node)) { | ||
const decl = getDeclarationOfExpando(node); | ||
if (decl) { | ||
const jsSymbol = getSymbolOfNode(decl); | ||
if (jsSymbol?.exports?.size) { | ||
const jsAssignmentType = createAnonymousType(jsSymbol, jsSymbol.exports, emptyArray, emptyArray, undefined, undefined); | ||
jsAssignmentType.objectFlags |= ObjectFlags.JSLiteral; | ||
return getIntersectionType([returnType, jsAssignmentType]); | ||
} | ||
const jsSymbol = getSymbolOfExpando(node, /*allowDeclaration*/ false); | ||
if (jsSymbol?.exports?.size) { | ||
const jsAssignmentType = createAnonymousType(jsSymbol, jsSymbol.exports, emptyArray, emptyArray, undefined, undefined); | ||
jsAssignmentType.objectFlags |= ObjectFlags.JSLiteral; | ||
return getIntersectionType([returnType, jsAssignmentType]); | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/baselines/reference/defaultPropertyAssignedClassWithPrototype.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
=== tests/cases/conformance/salsa/bug39167.js === | ||
var test = {}; | ||
>test : Symbol(test, Decl(bug39167.js, 0, 3), Decl(bug39167.js, 0, 14), Decl(bug39167.js, 2, 18)) | ||
|
||
test.K = test.K || | ||
>test.K : Symbol(test.K, Decl(bug39167.js, 0, 14), Decl(bug39167.js, 4, 5)) | ||
>test : Symbol(test, Decl(bug39167.js, 0, 3), Decl(bug39167.js, 0, 14), Decl(bug39167.js, 2, 18)) | ||
>K : Symbol(test.K, Decl(bug39167.js, 0, 14), Decl(bug39167.js, 4, 5)) | ||
>test.K : Symbol(test.K, Decl(bug39167.js, 0, 14), Decl(bug39167.js, 4, 5)) | ||
>test : Symbol(test, Decl(bug39167.js, 0, 3), Decl(bug39167.js, 0, 14), Decl(bug39167.js, 2, 18)) | ||
>K : Symbol(test.K, Decl(bug39167.js, 0, 14), Decl(bug39167.js, 4, 5)) | ||
|
||
function () {} | ||
|
||
test.K.prototype = { | ||
>test.K.prototype : Symbol(test.K.prototype, Decl(bug39167.js, 2, 18)) | ||
>test.K : Symbol(test.K, Decl(bug39167.js, 0, 14), Decl(bug39167.js, 4, 5)) | ||
>test : Symbol(test, Decl(bug39167.js, 0, 3), Decl(bug39167.js, 0, 14), Decl(bug39167.js, 2, 18)) | ||
>K : Symbol(test.K, Decl(bug39167.js, 0, 14), Decl(bug39167.js, 4, 5)) | ||
>prototype : Symbol(test.K.prototype, Decl(bug39167.js, 2, 18)) | ||
|
||
add() {} | ||
>add : Symbol(add, Decl(bug39167.js, 4, 20)) | ||
|
||
}; | ||
|
||
new test.K().add; | ||
>new test.K().add : Symbol(add, Decl(bug39167.js, 4, 20)) | ||
>test.K : Symbol(test.K, Decl(bug39167.js, 0, 14), Decl(bug39167.js, 4, 5)) | ||
>test : Symbol(test, Decl(bug39167.js, 0, 3), Decl(bug39167.js, 0, 14), Decl(bug39167.js, 2, 18)) | ||
>K : Symbol(test.K, Decl(bug39167.js, 0, 14), Decl(bug39167.js, 4, 5)) | ||
>add : Symbol(add, Decl(bug39167.js, 4, 20)) | ||
|
40 changes: 40 additions & 0 deletions
40
tests/baselines/reference/defaultPropertyAssignedClassWithPrototype.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
=== tests/cases/conformance/salsa/bug39167.js === | ||
var test = {}; | ||
>test : typeof test | ||
>{} : {} | ||
|
||
test.K = test.K || | ||
>test.K = test.K || function () {} : typeof K | ||
>test.K : typeof K | ||
>test : typeof test | ||
>K : typeof K | ||
>test.K || function () {} : typeof K | ||
>test.K : typeof K | ||
>test : typeof test | ||
>K : typeof K | ||
|
||
function () {} | ||
>function () {} : typeof K | ||
|
||
test.K.prototype = { | ||
>test.K.prototype = { add() {}} : { add(): void; } | ||
>test.K.prototype : { add(): void; } | ||
>test.K : typeof K | ||
>test : typeof test | ||
>K : typeof K | ||
>prototype : { add(): void; } | ||
>{ add() {}} : { add(): void; } | ||
|
||
add() {} | ||
>add : () => void | ||
|
||
}; | ||
|
||
new test.K().add; | ||
>new test.K().add : () => void | ||
>new test.K() : K | ||
>test.K : typeof K | ||
>test : typeof test | ||
>K : typeof K | ||
>add : () => void | ||
|
13 changes: 13 additions & 0 deletions
13
tests/cases/conformance/salsa/defaultPropertyAssignedClassWithPrototype.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @noEmit: true | ||
// @allowJs: true | ||
// @checkJs: true | ||
// @Filename: bug39167.js | ||
var test = {}; | ||
test.K = test.K || | ||
function () {} | ||
|
||
test.K.prototype = { | ||
add() {} | ||
}; | ||
|
||
new test.K().add; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this is a drive-by fix; createSymbol always adds
Transient
.