-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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 serialization of typenode for null/undefined/never as part of metadata type #13034
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
5940379
Add serialization of typenode for null/undefined/never as part of met…
sheetalkamat 73a8292
Support union of non identifier serialized type node with null/undefi…
sheetalkamat 62426de
Nit: Moving type aliases for serialized type nodes as per feedback
sheetalkamat 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 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 |
---|---|---|
|
@@ -1662,6 +1662,8 @@ namespace ts { | |
|
||
switch (node.kind) { | ||
case SyntaxKind.VoidKeyword: | ||
case SyntaxKind.UndefinedKeyword: | ||
case SyntaxKind.NullKeyword: | ||
return createVoidZero(); | ||
|
||
case SyntaxKind.ParenthesizedType: | ||
|
@@ -1715,27 +1717,35 @@ namespace ts { | |
case SyntaxKind.UnionType: | ||
{ | ||
const unionOrIntersection = <UnionOrIntersectionTypeNode>node; | ||
let serializedUnion: Identifier; | ||
let serializedUnion: Identifier | VoidExpression; | ||
for (const typeNode of unionOrIntersection.types) { | ||
const serializedIndividual = serializeTypeNode(typeNode) as Identifier; | ||
// Non identifier | ||
if (serializedIndividual.kind !== SyntaxKind.Identifier) { | ||
serializedUnion = undefined; | ||
break; | ||
} | ||
const serializedIndividual = serializeTypeNode(typeNode); | ||
|
||
// One of the individual is global object, return immediately | ||
if (serializedIndividual.text === "Object") { | ||
return serializedIndividual; | ||
} | ||
if (isIdentifier(serializedIndividual)) { | ||
// One of the individual is global object, return immediately | ||
if (serializedIndividual.text === "Object") { | ||
return serializedIndividual; | ||
} | ||
|
||
// Different types | ||
if (serializedUnion && isIdentifier(serializedUnion) && serializedUnion.text !== serializedIndividual.text) { | ||
serializedUnion = undefined; | ||
break; | ||
} | ||
|
||
// Different types | ||
if (serializedUnion && serializedUnion.text !== serializedIndividual.text) { | ||
serializedUnion = serializedIndividual; | ||
} | ||
else if (isVoidExpression(serializedIndividual)) { | ||
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. it could also return 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. or |
||
// If we dont have any other type already set, set the initial type | ||
if (!serializedUnion) { | ||
serializedUnion = serializedIndividual; | ||
} | ||
} | ||
else { | ||
// Non identifier and undefined/null | ||
serializedUnion = undefined; | ||
break; | ||
} | ||
|
||
serializedUnion = serializedIndividual; | ||
} | ||
|
||
// If we were able to find common type | ||
|
@@ -1751,6 +1761,7 @@ namespace ts { | |
case SyntaxKind.TypeLiteral: | ||
case SyntaxKind.AnyKeyword: | ||
case SyntaxKind.ThisType: | ||
case SyntaxKind.NeverKeyword: | ||
break; | ||
|
||
default: | ||
|
This file contains 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 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,78 @@ | ||
//// [metadataOfUnionWithNull.ts] | ||
function PropDeco(target: Object, propKey: string | symbol) { } | ||
|
||
class A { | ||
} | ||
|
||
class B { | ||
@PropDeco | ||
x: "foo" | null; | ||
|
||
@PropDeco | ||
y: true | never; | ||
|
||
@PropDeco | ||
z: "foo" | undefined; | ||
|
||
@PropDeco | ||
a: null; | ||
|
||
@PropDeco | ||
b: never; | ||
|
||
@PropDeco | ||
c: undefined; | ||
|
||
@PropDeco | ||
d: undefined | null; | ||
} | ||
|
||
//// [metadataOfUnionWithNull.js] | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
function PropDeco(target, propKey) { } | ||
var A = (function () { | ||
function A() { | ||
} | ||
return A; | ||
}()); | ||
var B = (function () { | ||
function B() { | ||
} | ||
return B; | ||
}()); | ||
__decorate([ | ||
PropDeco, | ||
__metadata("design:type", String) | ||
], B.prototype, "x"); | ||
__decorate([ | ||
PropDeco, | ||
__metadata("design:type", Object) | ||
], B.prototype, "y"); | ||
__decorate([ | ||
PropDeco, | ||
__metadata("design:type", String) | ||
], B.prototype, "z"); | ||
__decorate([ | ||
PropDeco, | ||
__metadata("design:type", void 0) | ||
], B.prototype, "a"); | ||
__decorate([ | ||
PropDeco, | ||
__metadata("design:type", Object) | ||
], B.prototype, "b"); | ||
__decorate([ | ||
PropDeco, | ||
__metadata("design:type", void 0) | ||
], B.prototype, "c"); | ||
__decorate([ | ||
PropDeco, | ||
__metadata("design:type", void 0) | ||
], B.prototype, "d"); |
This file contains 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,56 @@ | ||
=== tests/cases/compiler/metadataOfUnionWithNull.ts === | ||
function PropDeco(target: Object, propKey: string | symbol) { } | ||
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0)) | ||
>target : Symbol(target, Decl(metadataOfUnionWithNull.ts, 0, 18)) | ||
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) | ||
>propKey : Symbol(propKey, Decl(metadataOfUnionWithNull.ts, 0, 33)) | ||
|
||
class A { | ||
>A : Symbol(A, Decl(metadataOfUnionWithNull.ts, 0, 63)) | ||
} | ||
|
||
class B { | ||
>B : Symbol(B, Decl(metadataOfUnionWithNull.ts, 3, 1)) | ||
|
||
@PropDeco | ||
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0)) | ||
|
||
x: "foo" | null; | ||
>x : Symbol(B.x, Decl(metadataOfUnionWithNull.ts, 5, 9)) | ||
|
||
@PropDeco | ||
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0)) | ||
|
||
y: true | never; | ||
>y : Symbol(B.y, Decl(metadataOfUnionWithNull.ts, 7, 20)) | ||
|
||
@PropDeco | ||
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0)) | ||
|
||
z: "foo" | undefined; | ||
>z : Symbol(B.z, Decl(metadataOfUnionWithNull.ts, 10, 20)) | ||
|
||
@PropDeco | ||
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0)) | ||
|
||
a: null; | ||
>a : Symbol(B.a, Decl(metadataOfUnionWithNull.ts, 13, 25)) | ||
|
||
@PropDeco | ||
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0)) | ||
|
||
b: never; | ||
>b : Symbol(B.b, Decl(metadataOfUnionWithNull.ts, 16, 12)) | ||
|
||
@PropDeco | ||
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0)) | ||
|
||
c: undefined; | ||
>c : Symbol(B.c, Decl(metadataOfUnionWithNull.ts, 19, 13)) | ||
|
||
@PropDeco | ||
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0)) | ||
|
||
d: undefined | null; | ||
>d : Symbol(B.d, Decl(metadataOfUnionWithNull.ts, 22, 17)) | ||
} |
This file contains 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,60 @@ | ||
=== tests/cases/compiler/metadataOfUnionWithNull.ts === | ||
function PropDeco(target: Object, propKey: string | symbol) { } | ||
>PropDeco : (target: Object, propKey: string | symbol) => void | ||
>target : Object | ||
>Object : Object | ||
>propKey : string | symbol | ||
|
||
class A { | ||
>A : A | ||
} | ||
|
||
class B { | ||
>B : B | ||
|
||
@PropDeco | ||
>PropDeco : (target: Object, propKey: string | symbol) => void | ||
|
||
x: "foo" | null; | ||
>x : "foo" | ||
>null : null | ||
|
||
@PropDeco | ||
>PropDeco : (target: Object, propKey: string | symbol) => void | ||
|
||
y: true | never; | ||
>y : true | ||
>true : true | ||
|
||
@PropDeco | ||
>PropDeco : (target: Object, propKey: string | symbol) => void | ||
|
||
z: "foo" | undefined; | ||
>z : "foo" | ||
|
||
@PropDeco | ||
>PropDeco : (target: Object, propKey: string | symbol) => void | ||
|
||
a: null; | ||
>a : null | ||
>null : null | ||
|
||
@PropDeco | ||
>PropDeco : (target: Object, propKey: string | symbol) => void | ||
|
||
b: never; | ||
>b : never | ||
|
||
@PropDeco | ||
>PropDeco : (target: Object, propKey: string | symbol) => void | ||
|
||
c: undefined; | ||
>c : undefined | ||
|
||
@PropDeco | ||
>PropDeco : (target: Object, propKey: string | symbol) => void | ||
|
||
d: undefined | null; | ||
>d : null | ||
>null : null | ||
} |
This file contains 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,29 @@ | ||
// @experimentalDecorators: true | ||
// @emitDecoratorMetadata: true | ||
function PropDeco(target: Object, propKey: string | symbol) { } | ||
|
||
class A { | ||
} | ||
|
||
class B { | ||
@PropDeco | ||
x: "foo" | null; | ||
|
||
@PropDeco | ||
y: true | never; | ||
|
||
@PropDeco | ||
z: "foo" | undefined; | ||
|
||
@PropDeco | ||
a: null; | ||
|
||
@PropDeco | ||
b: never; | ||
|
||
@PropDeco | ||
c: undefined; | ||
|
||
@PropDeco | ||
d: undefined | null; | ||
} |
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.
We should make the serializeType* family strongly typed when it comes to return value. today it returns
Expression