Skip to content

Resolve the decorator type as type and check if the symbol has value. #4386

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 7 commits into from
Aug 24, 2015
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
17 changes: 14 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11384,9 +11384,16 @@ namespace ts {
// serialize the type metadata.
if (node && node.kind === SyntaxKind.TypeReference) {
let root = getFirstIdentifier((<TypeReferenceNode>node).typeName);
let rootSymbol = resolveName(root, root.text, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
if (rootSymbol && rootSymbol.flags & SymbolFlags.Alias && !isInTypeQuery(node) && !isConstEnumOrConstEnumOnlyModule(resolveAlias(rootSymbol))) {
markAliasSymbolAsReferenced(rootSymbol);
let meaning = root.parent.kind === SyntaxKind.TypeReference ? SymbolFlags.Type : SymbolFlags.Namespace;
// Resolve type so we know which symbol is referenced
let rootSymbol = resolveName(root, root.text, meaning | SymbolFlags.Alias, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
// Resolved symbol is alias
if (rootSymbol && rootSymbol.flags & SymbolFlags.Alias) {
let aliasTarget = resolveAlias(rootSymbol);
// If alias has value symbol - mark alias as referenced
if (aliasTarget.flags & SymbolFlags.Value && !isConstEnumOrConstEnumOnlyModule(resolveAlias(rootSymbol))) {
markAliasSymbolAsReferenced(rootSymbol);
}
}
}
}
Expand Down Expand Up @@ -14402,6 +14409,10 @@ namespace ts {

// Resolve the symbol as a type so that we can provide a more useful hint for the type serializer.
let typeSymbol = resolveEntityName(typeName, SymbolFlags.Type, /*ignoreErrors*/ true);
// We might not be able to resolve type symbol so use unknown type in that case (eg error case)
if (!typeSymbol) {
return TypeReferenceSerializationKind.ObjectType;
}
let type = getDeclaredTypeOfSymbol(typeSymbol);
if (type === unknownType) {
return TypeReferenceSerializationKind.Unknown;
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
else if (isNameOfNestedRedeclaration(node)) {
write(getGeneratedNameForNode(node));
}
else if (nodeIsSynthesized(node)) {
write(node.text);
}
else {
writeTextOfNode(currentSourceFile, node);
}
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/decoratorMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var service_1 = require("./service");
var MyComponent = (function () {
function MyComponent(Service) {
this.Service = Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//// [tests/cases/compiler/decoratorMetadataWithImportDeclarationNameCollision.ts] ////

//// [db.ts]
export class db {
public doSomething() {
}
}

//// [service.ts]
import {db} from './db';
function someDecorator(target) {
return target;
}
@someDecorator
class MyClass {
db: db;

constructor(db: db) {
this.db = db;
this.db.doSomething();
}
}
export {MyClass};


//// [db.js]
var db = (function () {
function db() {
}
db.prototype.doSomething = function () {
};
return db;
})();
exports.db = db;
//// [service.js]
var db_1 = require('./db');
function someDecorator(target) {
return target;
}
var MyClass = (function () {
function MyClass(db) {
this.db = db;
this.db.doSomething();
}
MyClass = __decorate([
someDecorator,
__metadata('design:paramtypes', [db_1.db])
], MyClass);
return MyClass;
})();
exports.MyClass = MyClass;
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=== tests/cases/compiler/db.ts ===
export class db {
>db : Symbol(db, Decl(db.ts, 0, 0))

public doSomething() {
>doSomething : Symbol(doSomething, Decl(db.ts, 0, 17))
}
}

=== tests/cases/compiler/service.ts ===
import {db} from './db';
>db : Symbol(db, Decl(service.ts, 0, 8))

function someDecorator(target) {
>someDecorator : Symbol(someDecorator, Decl(service.ts, 0, 24))
>target : Symbol(target, Decl(service.ts, 1, 23))

return target;
>target : Symbol(target, Decl(service.ts, 1, 23))
}
@someDecorator
>someDecorator : Symbol(someDecorator, Decl(service.ts, 0, 24))

class MyClass {
>MyClass : Symbol(MyClass, Decl(service.ts, 3, 1))

db: db;
>db : Symbol(db, Decl(service.ts, 5, 15))
>db : Symbol(db, Decl(service.ts, 0, 8))

constructor(db: db) {
>db : Symbol(db, Decl(service.ts, 8, 16))
>db : Symbol(db, Decl(service.ts, 0, 8))

this.db = db;
>this.db : Symbol(db, Decl(service.ts, 5, 15))
>this : Symbol(MyClass, Decl(service.ts, 3, 1))
>db : Symbol(db, Decl(service.ts, 5, 15))
>db : Symbol(db, Decl(service.ts, 8, 16))

this.db.doSomething();
>this.db.doSomething : Symbol(db.doSomething, Decl(db.ts, 0, 17))
>this.db : Symbol(db, Decl(service.ts, 5, 15))
>this : Symbol(MyClass, Decl(service.ts, 3, 1))
>db : Symbol(db, Decl(service.ts, 5, 15))
>doSomething : Symbol(db.doSomething, Decl(db.ts, 0, 17))
}
}
export {MyClass};
>MyClass : Symbol(MyClass, Decl(service.ts, 13, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
=== tests/cases/compiler/db.ts ===
export class db {
>db : db

public doSomething() {
>doSomething : () => void
}
}

=== tests/cases/compiler/service.ts ===
import {db} from './db';
>db : typeof db

function someDecorator(target) {
>someDecorator : (target: any) => any
>target : any

return target;
>target : any
}
@someDecorator
>someDecorator : (target: any) => any

class MyClass {
>MyClass : MyClass

db: db;
>db : db
>db : db

constructor(db: db) {
>db : db
>db : db

this.db = db;
>this.db = db : db
>this.db : db
>this : MyClass
>db : db
>db : db

this.db.doSomething();
>this.db.doSomething() : void
>this.db.doSomething : () => void
>this.db : db
>this : MyClass
>db : db
>doSomething : () => void
}
}
export {MyClass};
>MyClass : typeof MyClass

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//// [tests/cases/compiler/decoratorMetadataWithImportDeclarationNameCollision2.ts] ////

//// [db.ts]
export class db {
public doSomething() {
}
}

//// [service.ts]
import {db as Database} from './db';
function someDecorator(target) {
return target;
}
@someDecorator
class MyClass {
db: Database;

constructor(db: Database) { // no collision
this.db = db;
this.db.doSomething();
}
}
export {MyClass};


//// [db.js]
var db = (function () {
function db() {
}
db.prototype.doSomething = function () {
};
return db;
})();
exports.db = db;
//// [service.js]
var db_1 = require('./db');
function someDecorator(target) {
return target;
}
var MyClass = (function () {
function MyClass(db) {
this.db = db;
this.db.doSomething();
}
MyClass = __decorate([
someDecorator,
__metadata('design:paramtypes', [db_1.db])
], MyClass);
return MyClass;
})();
exports.MyClass = MyClass;
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
=== tests/cases/compiler/db.ts ===
export class db {
>db : Symbol(db, Decl(db.ts, 0, 0))

public doSomething() {
>doSomething : Symbol(doSomething, Decl(db.ts, 0, 17))
}
}

=== tests/cases/compiler/service.ts ===
import {db as Database} from './db';
>db : Symbol(Database, Decl(service.ts, 0, 8))
>Database : Symbol(Database, Decl(service.ts, 0, 8))

function someDecorator(target) {
>someDecorator : Symbol(someDecorator, Decl(service.ts, 0, 36))
>target : Symbol(target, Decl(service.ts, 1, 23))

return target;
>target : Symbol(target, Decl(service.ts, 1, 23))
}
@someDecorator
>someDecorator : Symbol(someDecorator, Decl(service.ts, 0, 36))

class MyClass {
>MyClass : Symbol(MyClass, Decl(service.ts, 3, 1))

db: Database;
>db : Symbol(db, Decl(service.ts, 5, 15))
>Database : Symbol(Database, Decl(service.ts, 0, 8))

constructor(db: Database) { // no collision
>db : Symbol(db, Decl(service.ts, 8, 16))
>Database : Symbol(Database, Decl(service.ts, 0, 8))

this.db = db;
>this.db : Symbol(db, Decl(service.ts, 5, 15))
>this : Symbol(MyClass, Decl(service.ts, 3, 1))
>db : Symbol(db, Decl(service.ts, 5, 15))
>db : Symbol(db, Decl(service.ts, 8, 16))

this.db.doSomething();
>this.db.doSomething : Symbol(Database.doSomething, Decl(db.ts, 0, 17))
>this.db : Symbol(db, Decl(service.ts, 5, 15))
>this : Symbol(MyClass, Decl(service.ts, 3, 1))
>db : Symbol(db, Decl(service.ts, 5, 15))
>doSomething : Symbol(Database.doSomething, Decl(db.ts, 0, 17))
}
}
export {MyClass};
>MyClass : Symbol(MyClass, Decl(service.ts, 13, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
=== tests/cases/compiler/db.ts ===
export class db {
>db : db

public doSomething() {
>doSomething : () => void
}
}

=== tests/cases/compiler/service.ts ===
import {db as Database} from './db';
>db : typeof Database
>Database : typeof Database

function someDecorator(target) {
>someDecorator : (target: any) => any
>target : any

return target;
>target : any
}
@someDecorator
>someDecorator : (target: any) => any

class MyClass {
>MyClass : MyClass

db: Database;
>db : Database
>Database : Database

constructor(db: Database) { // no collision
>db : Database
>Database : Database

this.db = db;
>this.db = db : Database
>this.db : Database
>this : MyClass
>db : Database
>db : Database

this.db.doSomething();
>this.db.doSomething() : void
>this.db.doSomething : () => void
>this.db : Database
>this : MyClass
>db : Database
>doSomething : () => void
}
}
export {MyClass};
>MyClass : typeof MyClass

Loading