Skip to content

Commit 86e0cd2

Browse files
committed
Evaluation tests and bug fixes
1 parent 50c7e00 commit 86e0cd2

File tree

53 files changed

+2086
-109
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2086
-109
lines changed

src/compiler/checker.ts

+45-11
Original file line numberDiff line numberDiff line change
@@ -1263,10 +1263,11 @@ namespace ts {
12631263
return addDeprecatedSuggestionWorker(declaration, diagnostic);
12641264
}
12651265

1266-
function createSymbol(flags: SymbolFlags, name: __String, checkFlags?: CheckFlags) {
1266+
function createSymbol(flags: SymbolFlags, name: __String, checkFlags?: CheckFlags, type?: Type) {
12671267
symbolCount++;
12681268
const symbol = (new Symbol(flags | SymbolFlags.Transient, name) as TransientSymbol);
12691269
symbol.checkFlags = checkFlags || 0;
1270+
if (type) symbol.type = type;
12701271
return symbol;
12711272
}
12721273

@@ -37371,6 +37372,30 @@ namespace ts {
3737137372
break;
3737237373

3737337374
case SyntaxKind.PropertyDeclaration:
37375+
if (!legacyDecorators) {
37376+
headMessage = Diagnostics.Decorator_function_return_type_0_is_not_assignable_to_type_1;
37377+
const expectedType = getTypeOfNode(node.parent);
37378+
const param = createSymbol(SymbolFlags.FunctionScopedVariable, "value" as __String, /*checkFlags*/ undefined, expectedType);
37379+
const initializerSignatureType = createFunctionType(/*typeParameters*/ undefined, /*thisParameter*/ undefined, [param], expectedType);
37380+
if (hasAccessorModifier(node.parent)) {
37381+
const getSignatureType = createFunctionType(/*typeParameters*/ undefined, /*thisParameter*/ undefined, emptyArray, expectedType);
37382+
const setSignatureType = createFunctionType(/*typeParameters*/ undefined, /*thisParameter*/ undefined, [param], voidType);
37383+
const members = createSymbolTable([
37384+
createSymbol(SymbolFlags.Property | SymbolFlags.Optional, "get" as __String, /*checkFlags*/ undefined, getSignatureType),
37385+
createSymbol(SymbolFlags.Property | SymbolFlags.Optional, "set" as __String, /*checkFlags*/ undefined, setSignatureType),
37386+
createSymbol(SymbolFlags.Property | SymbolFlags.Optional, "init" as __String, /*checkFlags*/ undefined, initializerSignatureType)
37387+
]);
37388+
37389+
const descriptorType = createAnonymousType(/*symbol*/ undefined, members, emptyArray, emptyArray, emptyArray);
37390+
expectedReturnType = getUnionType([descriptorType, voidType]);
37391+
}
37392+
else {
37393+
expectedReturnType = getUnionType([initializerSignatureType, voidType]);
37394+
}
37395+
break;
37396+
}
37397+
// falls through
37398+
3737437399
case SyntaxKind.Parameter:
3737537400
headMessage = Diagnostics.Decorator_function_return_type_is_0_but_is_expected_to_be_void_or_any;
3737637401
expectedReturnType = voidType;
@@ -37380,22 +37405,31 @@ namespace ts {
3738037405
case SyntaxKind.GetAccessor:
3738137406
case SyntaxKind.SetAccessor:
3738237407
headMessage = Diagnostics.Decorator_function_return_type_0_is_not_assignable_to_type_1;
37383-
const methodType = getTypeOfNode(node.parent);
37384-
expectedReturnType = getUnionType([
37385-
legacyDecorators ? createTypedPropertyDescriptorType(methodType) : methodType,
37386-
voidType
37387-
]);
37408+
const expectedType = legacyDecorators ?
37409+
createTypedPropertyDescriptorType(getTypeOfNode(node.parent)) :
37410+
getOrCreateTypeFromSignature(getSignatureFromDeclaration(node.parent as MethodDeclaration | GetAccessorDeclaration | SetAccessorDeclaration));
37411+
expectedReturnType = getUnionType([expectedType, voidType]);
3738837412
break;
3738937413

3739037414
default:
3739137415
return Debug.failBadSyntaxKind(node.parent);
3739237416
}
3739337417

37394-
checkTypeAssignableTo(
37395-
returnType,
37396-
expectedReturnType,
37397-
node,
37398-
headMessage);
37418+
checkTypeAssignableTo(returnType, expectedReturnType, node, headMessage);
37419+
}
37420+
37421+
function createFunctionType(
37422+
typeParameters: readonly TypeParameter[] | undefined,
37423+
thisParameter: Symbol | undefined,
37424+
parameters: readonly Symbol[],
37425+
returnType: Type,
37426+
typePredicate?: TypePredicate,
37427+
minArgumentCount: number = parameters.length,
37428+
flags: SignatureFlags = SignatureFlags.None
37429+
) {
37430+
const decl = factory.createFunctionTypeNode(/*typeParameters*/ undefined, emptyArray, factory.createKeywordTypeNode(SyntaxKind.AnyKeyword));
37431+
const signature = createSignature(decl, typeParameters, thisParameter, parameters, returnType, typePredicate, minArgumentCount, flags);
37432+
return getOrCreateTypeFromSignature(signature);
3739937433
}
3740037434

3740137435
/**

src/compiler/factory/emitHelpers.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -646,19 +646,20 @@ namespace ts {
646646
priority: 2,
647647
text: `
648648
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
649-
function accept(f) { if (f && typeof f !== "function") throw new TypeError("Function expected"); return f; }
649+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
650650
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
651651
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
652652
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
653-
var _;
653+
var _, done = false;
654654
for (var i = decorators.length - 1; i >= 0; i--) {
655655
var context = {};
656656
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
657657
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
658-
context.addInitializer = function (f) { extraInitializers.push(f); };
658+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
659659
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
660660
if (kind === "accessor") {
661-
if (!result) continue;
661+
if (result === void 0) continue;
662+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
662663
if (_ = accept(result.get)) descriptor.get = _;
663664
if (_ = accept(result.set)) descriptor.set = _;
664665
if (_ = accept(result.init)) initializers.push(_);
@@ -668,7 +669,8 @@ namespace ts {
668669
else descriptor[key] = _;
669670
}
670671
}
671-
if (target) Object.defineProperty(target, contextIn.key, descriptor);
672+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
673+
done = true;
672674
};`
673675
};
674676

src/compiler/transformers/esDecorators.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -704,9 +704,10 @@ namespace ts {
704704
const classExpression = factory.createClassExpression(/*modifiers*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, heritageClauses, members);
705705
const classReferenceDeclaration = factory.createVariableDeclaration(classReference, /*exclamationToken*/ undefined, /*type*/ undefined, classExpression);
706706
const classReferenceVarDeclList = factory.createVariableDeclarationList([classReferenceDeclaration]);
707+
const returnExpr = classInfo.classThis ? factory.createAssignment(classReference, classInfo.classThis) : classReference;
707708
classDefinitionStatements.push(
708709
factory.createVariableStatement(/*modifiers*/ undefined, classReferenceVarDeclList),
709-
factory.createReturnStatement(classReference),
710+
factory.createReturnStatement(returnExpr),
710711
);
711712
}
712713
else {
@@ -928,12 +929,12 @@ namespace ts {
928929
propertyName = { computed: false, name: member.name };
929930
}
930931
else if (isPropertyNameLiteral(member.name)) {
931-
propertyName = { computed: true, name: member.name };
932+
propertyName = { computed: true, name: factory.createStringLiteralFromNode(member.name) };
932933
}
933934
else {
934935
const expression = member.name.expression;
935936
if (isPropertyNameLiteral(expression) && !isIdentifier(expression)) {
936-
propertyName = { computed: true, name: expression };
937+
propertyName = { computed: true, name: factory.createStringLiteralFromNode(expression) };
937938
}
938939
else {
939940
enterName();

src/testRunner/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"unittests/evaluation/autoAccessors.ts",
9393
"unittests/evaluation/awaiter.ts",
9494
"unittests/evaluation/destructuring.ts",
95+
"unittests/evaluation/esDecorators.ts",
9596
"unittests/evaluation/externalModules.ts",
9697
"unittests/evaluation/forAwaitOf.ts",
9798
"unittests/evaluation/forOf.ts",

0 commit comments

Comments
 (0)