FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix crash in decorator metadata emit for decorated object literal members by Andarist · Pull Request #64633 · microsoft/TypeScript · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (1) .js  (1) .symbols  (1) .ts  (1) .txt  (1) .types  (1) All 6 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 10 additions & 0 deletions tsc/internal/transformers/tstransforms/metadata.go
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
Expand Up @@ -41,6 +41,8 @@ func (tx *MetadataTransformer) visit(node *ast.Node) *ast.Node {
return tx.visitClassDeclaration(node.AsClassDeclaration())
case ast.KindClassExpression:
return tx.visitClassExpression(node.AsClassExpression())
case ast.KindObjectLiteralExpression:
return tx.visitObjectLiteralExpression(node)
case ast.KindPropertyDeclaration:
return tx.visitPropertyDeclaration(node.AsPropertyDeclaration())
case ast.KindMethodDeclaration:
Expand Down Expand Up @@ -76,6 +78,14 @@ func (tx *MetadataTransformer) setCurrentLexicalScope(node *ast.Node) {
tx.currentLexicalScope = node
}

func (tx *MetadataTransformer) visitObjectLiteralExpression(node *ast.Node) *ast.Node {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
  1. test code is intentionally invalid but parsing-wise permitted, it only has grammar errors
  2. the transforms/emitting is traditionally OK with such inputs, doing its best to emit "something"

The fix here matches Strada conceptually. Strada passed down parent (also in the case of object literal expressions). This patch just reuses the neighboring pattern (the same thing as in visitClassExpression) for doing the equivalent here in Corsa

oldParent := tx.parent
tx.parent = node
defer tx.setParent(oldParent)

return tx.Visitor().VisitEachChild(node)
}

func (tx *MetadataTransformer) visitClassExpression(node *ast.ClassExpression) *ast.Node {
oldParent := tx.parent
tx.parent = node.AsNode()
Expand Down
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,30 @@
decoratorMetadataObjectLiteralMethodNoCrash.ts(4,7): error TS1206: Decorators are not valid here.
decoratorMetadataObjectLiteralMethodNoCrash.ts(13,15): error TS1206: Decorators are not valid here.
decoratorMetadataObjectLiteralMethodNoCrash.ts(14,19): error TS1206: Decorators are not valid here.


==== decoratorMetadataObjectLiteralMethodNoCrash.ts (3 errors) ====
declare function dec(...args: any[]): any;

const o = {
m(@dec x: string) {}
~
!!! error TS1206: Decorators are not valid here.
};

class C {
@dec
prop!: number;

method() {
return {
m(@dec x: string) {},
~
!!! error TS1206: Decorators are not valid here.
set s(@dec v: number) {}
~
!!! error TS1206: Decorators are not valid here.
};
}
}

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,49 @@
//// [tests/cases/compiler/decoratorMetadataObjectLiteralMethodNoCrash.ts] ////

//// [decoratorMetadataObjectLiteralMethodNoCrash.ts]
declare function dec(...args: any[]): any;

const o = {
m(@dec x: string) {}
};

class C {
@dec
prop!: number;

method() {
return {
m(@dec x: string) {},
set s(@dec v: number) {}
};
}
}


//// [decoratorMetadataObjectLiteralMethodNoCrash.js]
"use strict";
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);
};
const o = {
m(x) { }
};
class C {
prop;
method() {
return {
m(x) { },
set s(v) { }
};
}
}
__decorate([
dec,
__metadata("design:type", Number)
], C.prototype, "prop", void 0);
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,44 @@
//// [tests/cases/compiler/decoratorMetadataObjectLiteralMethodNoCrash.ts] ////

=== decoratorMetadataObjectLiteralMethodNoCrash.ts ===
declare function dec(...args: any[]): any;
>dec : Symbol(dec, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 0, 0))
>args : Symbol(args, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 0, 21))

const o = {
>o : Symbol(o, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 2, 5))

m(@dec x: string) {}
>m : Symbol(m, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 2, 11))
>dec : Symbol(dec, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 0, 0))
>x : Symbol(x, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 3, 6))

};

class C {
>C : Symbol(C, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 4, 2))

@dec
>dec : Symbol(dec, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 0, 0))

prop!: number;
>prop : Symbol(C.prop, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 6, 9))

method() {
>method : Symbol(C.method, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 8, 18))

return {
m(@dec x: string) {},
>m : Symbol(m, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 11, 16))
>dec : Symbol(dec, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 0, 0))
>x : Symbol(x, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 12, 14))

set s(@dec v: number) {}
>s : Symbol(s, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 12, 33))
>dec : Symbol(dec, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 0, 0))
>v : Symbol(v, Decl(decoratorMetadataObjectLiteralMethodNoCrash.ts, 13, 18))

};
}
}

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,47 @@
//// [tests/cases/compiler/decoratorMetadataObjectLiteralMethodNoCrash.ts] ////

=== decoratorMetadataObjectLiteralMethodNoCrash.ts ===
declare function dec(...args: any[]): any;
>dec : (...args: any[]) => any
>args : any[]

const o = {
>o : { m(x: string): void; }
>{ m(@dec x: string) {}} : { m(x: string): void; }

m(@dec x: string) {}
>m : (x: string) => void
>dec : (...args: any[]) => any
>x : string

};

class C {
>C : C

@dec
>dec : (...args: any[]) => any

prop!: number;
>prop : number

method() {
>method : () => { m(x: string): void; s: number; }

return {
>{ m(@dec x: string) {}, set s(@dec v: number) {} } : { m(x: string): void; s: number; }

m(@dec x: string) {},
>m : (x: string) => void
>dec : (...args: any[]) => any
>x : string

set s(@dec v: number) {}
>s : number
>dec : (...args: any[]) => any
>v : number

};
}
}

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,20 @@
// @experimentalDecorators: true
// @emitDecoratorMetadata: true
Comment thread
Andarist marked this conversation as resolved.

declare function dec(...args: any[]): any;

const o = {
m(@dec x: string) {}
};

class C {
@dec
prop!: number;

method() {
return {
m(@dec x: string) {},
set s(@dec v: number) {}
};
}
}
Loading

Back | FazBrowse Home | New Git URL