Skip to content

Handle parentless nodes in nodeIsDecorated #20314

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13043,7 +13043,7 @@ namespace ts {
// must instead be rewritten to point to a temporary variable to avoid issues with the double-bind
// behavior of class names in ES6.
if (declaration.kind === SyntaxKind.ClassDeclaration
&& nodeIsDecorated(declaration)) {
&& nodeIsDecorated(declaration as ClassDeclaration)) {
let container = getContainingClass(node);
while (container !== undefined) {
if (container === declaration && container.name !== node) {
Expand Down Expand Up @@ -20685,7 +20685,7 @@ namespace ts {

// skip this check for nodes that cannot have decorators. These should have already had an error reported by
// checkGrammarDecorators.
if (!nodeCanBeDecorated(node)) {
if (!nodeCanBeDecorated(node, node.parent, node.parent.parent)) {
return;
}

Expand Down Expand Up @@ -25191,7 +25191,7 @@ namespace ts {
if (!node.decorators) {
return false;
}
if (!nodeCanBeDecorated(node)) {
if (!nodeCanBeDecorated(node, node.parent, node.parent.parent)) {
if (node.kind === SyntaxKind.MethodDeclaration && !nodeIsPresent((<MethodDeclaration>node).body)) {
return grammarErrorOnFirstToken(node, Diagnostics.A_decorator_can_only_decorate_a_method_implementation_not_an_overload);
}
Expand Down
14 changes: 7 additions & 7 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ namespace ts {
* the class.
*/
function getDecoratedClassElements(node: ClassExpression | ClassDeclaration, isStatic: boolean): ReadonlyArray<ClassElement> {
return filter(node.members, isStatic ? isStaticDecoratedClassElement : isInstanceDecoratedClassElement);
return filter(node.members, isStatic ? m => isStaticDecoratedClassElement(m, node) : m => isInstanceDecoratedClassElement(m, node));
}

/**
Expand All @@ -1270,8 +1270,8 @@ namespace ts {
*
* @param member The class member.
*/
function isStaticDecoratedClassElement(member: ClassElement) {
return isDecoratedClassElement(member, /*isStatic*/ true);
function isStaticDecoratedClassElement(member: ClassElement, parent: ClassLikeDeclaration) {
return isDecoratedClassElement(member, /*isStatic*/ true, parent);
}

/**
Expand All @@ -1280,8 +1280,8 @@ namespace ts {
*
* @param member The class member.
*/
function isInstanceDecoratedClassElement(member: ClassElement) {
return isDecoratedClassElement(member, /*isStatic*/ false);
function isInstanceDecoratedClassElement(member: ClassElement, parent: ClassLikeDeclaration) {
return isDecoratedClassElement(member, /*isStatic*/ false, parent);
}

/**
Expand All @@ -1290,8 +1290,8 @@ namespace ts {
*
* @param member The class member.
*/
function isDecoratedClassElement(member: ClassElement, isStatic: boolean) {
return nodeOrChildIsDecorated(member)
function isDecoratedClassElement(member: ClassElement, isStatic: boolean, parent: ClassLikeDeclaration) {
return nodeOrChildIsDecorated(member, parent)
&& isStatic === hasModifier(member, ModifierFlags.Static);
}

Expand Down
41 changes: 26 additions & 15 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1205,51 +1205,62 @@ namespace ts {
return (<CallExpression | Decorator>node).expression;
}

export function nodeCanBeDecorated(node: Node): boolean {
export function nodeCanBeDecorated(node: ClassDeclaration): true;
export function nodeCanBeDecorated(node: ClassElement, parent: Node): boolean;
export function nodeCanBeDecorated(node: Node, parent: Node, grandparent: Node): boolean;
export function nodeCanBeDecorated(node: Node, parent?: Node, grandparent?: Node): boolean {
switch (node.kind) {
case SyntaxKind.ClassDeclaration:
// classes are valid targets
return true;

case SyntaxKind.PropertyDeclaration:
// property declarations are valid if their parent is a class declaration.
return node.parent.kind === SyntaxKind.ClassDeclaration;
return parent.kind === SyntaxKind.ClassDeclaration;

case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.MethodDeclaration:
// if this method has a body and its parent is a class declaration, this is a valid target.
return (<FunctionLikeDeclaration>node).body !== undefined
&& node.parent.kind === SyntaxKind.ClassDeclaration;
&& parent.kind === SyntaxKind.ClassDeclaration;

case SyntaxKind.Parameter:
// if the parameter's parent has a body and its grandparent is a class declaration, this is a valid target;
return (<FunctionLikeDeclaration>node.parent).body !== undefined
&& (node.parent.kind === SyntaxKind.Constructor
|| node.parent.kind === SyntaxKind.MethodDeclaration
|| node.parent.kind === SyntaxKind.SetAccessor)
&& node.parent.parent.kind === SyntaxKind.ClassDeclaration;
return (<FunctionLikeDeclaration>parent).body !== undefined
&& (parent.kind === SyntaxKind.Constructor
|| parent.kind === SyntaxKind.MethodDeclaration
|| parent.kind === SyntaxKind.SetAccessor)
&& grandparent.kind === SyntaxKind.ClassDeclaration;
}

return false;
}

export function nodeIsDecorated(node: Node): boolean {
export function nodeIsDecorated(node: ClassDeclaration): boolean;
export function nodeIsDecorated(node: ClassElement, parent: Node): boolean;
export function nodeIsDecorated(node: Node, parent: Node, grandparent: Node): boolean;
export function nodeIsDecorated(node: Node, parent?: Node, grandparent?: Node): boolean {
return node.decorators !== undefined
&& nodeCanBeDecorated(node);
&& nodeCanBeDecorated(node, parent, grandparent);
}

export function nodeOrChildIsDecorated(node: Node): boolean {
return nodeIsDecorated(node) || childIsDecorated(node);
export function nodeOrChildIsDecorated(node: ClassDeclaration): boolean;
export function nodeOrChildIsDecorated(node: ClassElement, parent: Node): boolean;
export function nodeOrChildIsDecorated(node: Node, parent: Node, grandparent: Node): boolean;
export function nodeOrChildIsDecorated(node: Node, parent?: Node, grandparent?: Node): boolean {
return nodeIsDecorated(node, parent, grandparent) || childIsDecorated(node, parent);
}

export function childIsDecorated(node: Node): boolean {
export function childIsDecorated(node: ClassDeclaration): boolean;
export function childIsDecorated(node: Node, parent: Node): boolean;
export function childIsDecorated(node: Node, parent?: Node): boolean {
switch (node.kind) {
case SyntaxKind.ClassDeclaration:
return forEach((<ClassDeclaration>node).members, nodeOrChildIsDecorated);
return forEach((<ClassDeclaration>node).members, m => nodeOrChildIsDecorated(m, node, parent));
case SyntaxKind.MethodDeclaration:
case SyntaxKind.SetAccessor:
return forEach((<FunctionLikeDeclaration>node).parameters, nodeIsDecorated);
return forEach((<FunctionLikeDeclaration>node).parameters, p => nodeIsDecorated(p, node, parent));
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/harness/unittests/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,32 @@ namespace ts {
}
}
});

// https://github.com/Microsoft/TypeScript/issues/17384
testBaseline("transformAddDecoratedNode", () => {
return ts.transpileModule("", {
transformers: {
before: [transformAddDecoratedNode],
},
compilerOptions: {
target: ts.ScriptTarget.ES5,
newLine: NewLineKind.CarriageReturnLineFeed,
}
}).outputText;

function transformAddDecoratedNode(_context: ts.TransformationContext) {
return (sourceFile: ts.SourceFile): ts.SourceFile => {
return visitNode(sourceFile);
};
function visitNode(sf: ts.SourceFile) {
// produce `class Foo { @Bar baz() {} }`;
const classDecl = ts.createClassDeclaration([], [], "Foo", /*typeParameters*/ undefined, /*heritageClauses*/ undefined, [
ts.createMethod([ts.createDecorator(ts.createIdentifier("Bar"))], [], /**/ undefined, "baz", /**/ undefined, /**/ undefined, [], /**/ undefined, ts.createBlock([]))
]);
return ts.updateSourceFileNode(sf, [classDecl]);
}
}
});
});
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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 Foo = /** @class */ (function () {
function Foo() {
}
Foo.prototype.baz = function () { };
__decorate([
Bar
], Foo.prototype, "baz", null);
Foo = __decorate([], Foo);
return Foo;
}());