Skip to content

Commit 99d5058

Browse files
author
Andy
authored
Add 'isPrototypeAccess' helper (microsoft#22648)
* Add 'isPrototypeAccess' helper * Fix type error
1 parent 2d01d76 commit 99d5058

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

src/compiler/binder.ts

+9-14
Original file line numberDiff line numberDiff line change
@@ -2336,7 +2336,7 @@ namespace ts {
23362336
// For `f.prototype.m = function() { this.x = 0; }`, `this.x = 0` should modify `f`'s members, not the function expression.
23372337
if (isBinaryExpression(thisContainer.parent) && thisContainer.parent.operatorToken.kind === SyntaxKind.EqualsToken) {
23382338
const l = thisContainer.parent.left;
2339-
if (isPropertyAccessExpression(l) && isPropertyAccessExpression(l.expression) && l.expression.name.escapedText === "prototype" && isEntityNameExpression(l.expression.expression)) {
2339+
if (isPropertyAccessEntityNameExpression(l) && isPrototypeAccess(l.expression)) {
23402340
constructorSymbol = getJSInitializerSymbolFromName(l.expression.expression, containerContainer);
23412341
}
23422342
}
@@ -2368,12 +2368,12 @@ namespace ts {
23682368
if (node.expression.kind === SyntaxKind.ThisKeyword) {
23692369
bindThisPropertyAssignment(node);
23702370
}
2371-
else if (isEntityNameExpression(node) && node.parent.parent.kind === SyntaxKind.SourceFile) {
2372-
if (isPropertyAccessExpression(node.expression) && node.expression.name.escapedText === "prototype") {
2373-
bindPrototypePropertyAssignment(node as PropertyAccessEntityNameExpression, node.parent);
2371+
else if (isPropertyAccessEntityNameExpression(node) && node.parent.parent.kind === SyntaxKind.SourceFile) {
2372+
if (isPrototypeAccess(node.expression)) {
2373+
bindPrototypePropertyAssignment(node, node.parent);
23742374
}
23752375
else {
2376-
bindStaticPropertyAssignment(node as PropertyAccessEntityNameExpression);
2376+
bindStaticPropertyAssignment(node);
23772377
}
23782378
}
23792379
}
@@ -2436,15 +2436,10 @@ namespace ts {
24362436

24372437
function bindPropertyAssignment(name: EntityNameExpression, propertyAccess: PropertyAccessEntityNameExpression, isPrototypeProperty: boolean) {
24382438
let symbol = getJSInitializerSymbolFromName(name);
2439-
let isToplevelNamespaceableInitializer: boolean;
2440-
if (isBinaryExpression(propertyAccess.parent)) {
2441-
const isPrototypeAssignment = isPropertyAccessExpression(propertyAccess.parent.left) && propertyAccess.parent.left.name.escapedText === "prototype";
2442-
isToplevelNamespaceableInitializer = propertyAccess.parent.parent.parent.kind === SyntaxKind.SourceFile &&
2443-
!!getJavascriptInitializer(propertyAccess.parent.right, isPrototypeAssignment);
2444-
}
2445-
else {
2446-
isToplevelNamespaceableInitializer = propertyAccess.parent.parent.kind === SyntaxKind.SourceFile;
2447-
}
2439+
const isToplevelNamespaceableInitializer = isBinaryExpression(propertyAccess.parent)
2440+
? propertyAccess.parent.parent.parent.kind === SyntaxKind.SourceFile &&
2441+
!!getJavascriptInitializer(propertyAccess.parent.right, isPrototypeAccess(propertyAccess.parent.left))
2442+
: propertyAccess.parent.parent.kind === SyntaxKind.SourceFile;
24482443
if (!isPrototypeProperty && (!symbol || !(symbol.flags & SymbolFlags.Namespace)) && isToplevelNamespaceableInitializer) {
24492444
// make symbols or add declarations for intermediate containers
24502445
const flags = SymbolFlags.Module | SymbolFlags.JSContainer;

src/compiler/checker.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -18069,8 +18069,7 @@ namespace ts {
1806918069
parent = parent.parent;
1807018070
}
1807118071
return parent && isBinaryExpression(parent) &&
18072-
isPropertyAccessExpression(parent.left) &&
18073-
parent.left.name.escapedText === "prototype" &&
18072+
isPrototypeAccess(parent.left) &&
1807418073
parent.operatorToken.kind === SyntaxKind.EqualsToken &&
1807518074
isObjectLiteralExpression(parent.right) &&
1807618075
parent.right;

src/compiler/utilities.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ namespace ts {
15091509
*/
15101510
export function getAssignedJavascriptInitializer(node: Node) {
15111511
if (node && node.parent && isBinaryExpression(node.parent) && node.parent.operatorToken.kind === SyntaxKind.EqualsToken) {
1512-
const isPrototypeAssignment = isPropertyAccessExpression(node.parent.left) && node.parent.left.name.escapedText === "prototype";
1512+
const isPrototypeAssignment = isPrototypeAccess(node.parent.left);
15131513
return getJavascriptInitializer(node.parent.right, isPrototypeAssignment) ||
15141514
getDefaultedJavascriptInitializer(node.parent.left as EntityNameExpression, node.parent.right, isPrototypeAssignment);
15151515
}
@@ -1629,7 +1629,7 @@ namespace ts {
16291629
// F.prototype = { ... }
16301630
return SpecialPropertyAssignmentKind.Prototype;
16311631
}
1632-
else if (isPropertyAccessExpression(lhs.expression) && lhs.expression.name.escapedText === "prototype") {
1632+
else if (isPrototypeAccess(lhs.expression)) {
16331633
// F.G....prototype.x = expr
16341634
return SpecialPropertyAssignmentKind.PrototypeProperty;
16351635
}
@@ -3336,8 +3336,15 @@ namespace ts {
33363336
}
33373337

33383338
export function isEntityNameExpression(node: Node): node is EntityNameExpression {
3339-
return node.kind === SyntaxKind.Identifier ||
3340-
node.kind === SyntaxKind.PropertyAccessExpression && isEntityNameExpression((<PropertyAccessExpression>node).expression);
3339+
return node.kind === SyntaxKind.Identifier || isPropertyAccessEntityNameExpression(node);
3340+
}
3341+
3342+
export function isPropertyAccessEntityNameExpression(node: Node): node is PropertyAccessEntityNameExpression {
3343+
return isPropertyAccessExpression(node) && isEntityNameExpression(node.expression);
3344+
}
3345+
3346+
export function isPrototypeAccess(node: Node): node is PropertyAccessExpression {
3347+
return isPropertyAccessExpression(node) && node.name.escapedText === "prototype";
33413348
}
33423349

33433350
export function isRightSideOfQualifiedNameOrPropertyAccess(node: Node) {

0 commit comments

Comments
 (0)