From f9479006bb4a60146601e8fc89620a6a31e7c78e Mon Sep 17 00:00:00 2001 From: Yanxing Wang Date: Thu, 22 Oct 2015 22:32:58 -0700 Subject: [PATCH 01/43] prim type and struct backend --- .gitignore | 1 + src/compiler/.idea/.name | 1 + src/compiler/.idea/compiler.iml | 8 + src/compiler/.idea/misc.xml | 14 + src/compiler/.idea/modules.xml | 8 + src/compiler/.idea/vcs.xml | 6 + src/compiler/.idea/workspace.xml | 125 +++ src/compiler/binder.ts | 54 +- src/compiler/checker.ts | 927 ++++++++++++++++-- src/compiler/commandLineParser.ts | 97 +- src/compiler/core.ts | 22 +- src/compiler/diagnosticMessages.json | 4 - src/compiler/emitter.ts | 268 ++++- src/compiler/parser.ts | 239 ++++- src/compiler/scanner.ts | 13 +- src/compiler/sys.ts | 2 +- src/compiler/tsc.ts | 3 +- src/compiler/types.ts | 112 ++- src/compiler/utilities.ts | 36 +- src/harness/harness.ts | 4 +- src/lib/core.d.ts | 2 +- src/server/editorServices.ts | 3 +- src/services/formatting/formatting.ts | 8 +- src/services/services.ts | 14 +- tests/baselines/reference/APISample_linter.js | 118 --- .../reference/arrayBufferIsViewNarrowsType.js | 13 - .../arrayBufferIsViewNarrowsType.symbols | 17 - .../arrayBufferIsViewNarrowsType.types | 18 - .../reference/assignmentCompat1.errors.txt | 16 +- .../baselines/reference/assignmentCompat1.js | 7 - .../reference/assignmentLHSIsValue.errors.txt | 46 +- ...nmentToParenthesizedExpression1.errors.txt | 8 - .../assignmentToParenthesizedExpression1.js | 7 - ...nmentToParenthesizedIdentifiers.errors.txt | 14 +- .../compoundAssignmentLHSIsValue.errors.txt | 55 +- ...onentiationAssignmentLHSIsValue.errors.txt | 11 +- .../decrementAndIncrementOperators.errors.txt | 38 +- ...thAnyOtherTypeInvalidOperations.errors.txt | 20 +- ...orWithEnumTypeInvalidOperations.errors.txt | 8 +- ...WithNumberTypeInvalidOperations.errors.txt | 8 +- tests/baselines/reference/for-of47.errors.txt | 13 - tests/baselines/reference/for-of47.js | 20 - tests/baselines/reference/for-of48.errors.txt | 13 - tests/baselines/reference/for-of48.js | 20 - .../functionConstraintSatisfaction.types | 8 +- .../functionConstraintSatisfaction3.types | 8 +- ...thAnyOtherTypeInvalidOperations.errors.txt | 20 +- ...WithNumberTypeInvalidOperations.errors.txt | 8 +- .../reference/indexTypeCheck.errors.txt | 5 +- .../inferentialTypingUsingApparentType1.types | 2 +- .../inferentialTypingUsingApparentType2.types | 2 +- .../reference/inferringAnyFunctionType1.types | 4 +- .../reference/inferringAnyFunctionType2.types | 4 +- .../reference/inferringAnyFunctionType3.types | 4 +- .../reference/inferringAnyFunctionType4.types | 4 +- .../reference/inferringAnyFunctionType5.types | 4 +- .../reference/intTypeCheck.errors.txt | 24 +- tests/baselines/reference/intTypeCheck.js | 6 +- .../interfaceDeclaration1.errors.txt | 5 +- .../jsxEsprimaFbTestSuite.errors.txt | 9 +- .../reference/jsxEsprimaFbTestSuite.js | 4 +- .../jsxInvalidEsprimaTestSuite.errors.txt | 30 +- .../reference/jsxInvalidEsprimaTestSuite.js | 10 +- .../reference/nestedRedeclarationInES6AMD.js | 15 - .../nestedRedeclarationInES6AMD.symbols | 11 - .../nestedRedeclarationInES6AMD.types | 14 - .../objectLiteralExcessProperties.errors.txt | 66 -- .../objectLiteralExcessProperties.js | 32 - .../parserAssignmentExpression1.errors.txt | 5 +- .../reference/promisePermutations3.errors.txt | 4 +- .../reference/recursiveInheritance.errors.txt | 5 +- ...pertyAssignmentsInDestructuring.errors.txt | 164 ---- ...thandPropertyAssignmentsInDestructuring.js | 242 ----- ...yAssignmentsInDestructuring_ES6.errors.txt | 164 ---- ...dPropertyAssignmentsInDestructuring_ES6.js | 213 ---- .../reference/tsxErrorRecovery2.errors.txt | 18 - .../baselines/reference/tsxErrorRecovery2.js | 19 - .../reference/tsxErrorRecovery3.errors.txt | 30 - .../baselines/reference/tsxErrorRecovery3.js | 19 - .../baselines/reference/underscoreTest1.types | 8 +- tests/cases/compiler/APISample_linter.ts | 67 -- .../compiler/arrayBufferIsViewNarrowsType.ts | 5 - tests/cases/compiler/assignmentCompat1.ts | 4 - .../assignmentToParenthesizedExpression1.ts | 2 - .../compiler/nestedRedeclarationInES6AMD.ts | 8 - .../compiler/objectLiteralExcessProperties.ts | 21 - ...thandPropertyAssignmentsInDestructuring.ts | 118 --- ...dPropertyAssignmentsInDestructuring_ES6.ts | 118 --- .../es6/for-ofStatements/for-of47.ts | 8 - .../es6/for-ofStatements/for-of48.ts | 8 - .../conformance/jsx/tsxErrorRecovery2.tsx | 10 - .../conformance/jsx/tsxErrorRecovery3.tsx | 10 - .../fourslash/formatAfterMultilineComment.ts | 7 - tests/webTestServer.ts | 16 +- 94 files changed, 1821 insertions(+), 2212 deletions(-) create mode 100644 src/compiler/.idea/.name create mode 100644 src/compiler/.idea/compiler.iml create mode 100644 src/compiler/.idea/misc.xml create mode 100644 src/compiler/.idea/modules.xml create mode 100644 src/compiler/.idea/vcs.xml create mode 100644 src/compiler/.idea/workspace.xml delete mode 100644 tests/baselines/reference/APISample_linter.js delete mode 100644 tests/baselines/reference/arrayBufferIsViewNarrowsType.js delete mode 100644 tests/baselines/reference/arrayBufferIsViewNarrowsType.symbols delete mode 100644 tests/baselines/reference/arrayBufferIsViewNarrowsType.types delete mode 100644 tests/baselines/reference/assignmentToParenthesizedExpression1.errors.txt delete mode 100644 tests/baselines/reference/assignmentToParenthesizedExpression1.js delete mode 100644 tests/baselines/reference/for-of47.errors.txt delete mode 100644 tests/baselines/reference/for-of47.js delete mode 100644 tests/baselines/reference/for-of48.errors.txt delete mode 100644 tests/baselines/reference/for-of48.js delete mode 100644 tests/baselines/reference/nestedRedeclarationInES6AMD.js delete mode 100644 tests/baselines/reference/nestedRedeclarationInES6AMD.symbols delete mode 100644 tests/baselines/reference/nestedRedeclarationInES6AMD.types delete mode 100644 tests/baselines/reference/objectLiteralExcessProperties.errors.txt delete mode 100644 tests/baselines/reference/objectLiteralExcessProperties.js delete mode 100644 tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring.errors.txt delete mode 100644 tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring.js delete mode 100644 tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring_ES6.errors.txt delete mode 100644 tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring_ES6.js delete mode 100644 tests/baselines/reference/tsxErrorRecovery2.errors.txt delete mode 100644 tests/baselines/reference/tsxErrorRecovery2.js delete mode 100644 tests/baselines/reference/tsxErrorRecovery3.errors.txt delete mode 100644 tests/baselines/reference/tsxErrorRecovery3.js delete mode 100644 tests/cases/compiler/APISample_linter.ts delete mode 100644 tests/cases/compiler/arrayBufferIsViewNarrowsType.ts delete mode 100644 tests/cases/compiler/assignmentToParenthesizedExpression1.ts delete mode 100644 tests/cases/compiler/nestedRedeclarationInES6AMD.ts delete mode 100644 tests/cases/compiler/objectLiteralExcessProperties.ts delete mode 100644 tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts delete mode 100644 tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts delete mode 100644 tests/cases/conformance/es6/for-ofStatements/for-of47.ts delete mode 100644 tests/cases/conformance/es6/for-ofStatements/for-of48.ts delete mode 100644 tests/cases/conformance/jsx/tsxErrorRecovery2.tsx delete mode 100644 tests/cases/conformance/jsx/tsxErrorRecovery3.tsx delete mode 100644 tests/cases/fourslash/formatAfterMultilineComment.ts diff --git a/.gitignore b/.gitignore index 8d05f8f3337b1..2a546cc069cf9 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ rwc-report.html *.swp build.json *.actual +mytests tests/webhost/*.d.ts tests/webhost/webtsc.js tests/*.js diff --git a/src/compiler/.idea/.name b/src/compiler/.idea/.name new file mode 100644 index 0000000000000..7f246c0118ebb --- /dev/null +++ b/src/compiler/.idea/.name @@ -0,0 +1 @@ +compiler \ No newline at end of file diff --git a/src/compiler/.idea/compiler.iml b/src/compiler/.idea/compiler.iml new file mode 100644 index 0000000000000..c956989b29ad0 --- /dev/null +++ b/src/compiler/.idea/compiler.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/compiler/.idea/misc.xml b/src/compiler/.idea/misc.xml new file mode 100644 index 0000000000000..19f74da8ea2dc --- /dev/null +++ b/src/compiler/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/compiler/.idea/modules.xml b/src/compiler/.idea/modules.xml new file mode 100644 index 0000000000000..b67420d2e33b3 --- /dev/null +++ b/src/compiler/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/compiler/.idea/vcs.xml b/src/compiler/.idea/vcs.xml new file mode 100644 index 0000000000000..6564d52db279c --- /dev/null +++ b/src/compiler/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/compiler/.idea/workspace.xml b/src/compiler/.idea/workspace.xml new file mode 100644 index 0000000000000..632469ad277f7 --- /dev/null +++ b/src/compiler/.idea/workspace.xml @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1445578175689 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index c12a9afad62ae..961720d08f9e9 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -166,6 +166,7 @@ namespace ts { return (node).isExportEquals ? "export=" : "default"; case SyntaxKind.FunctionDeclaration: case SyntaxKind.ClassDeclaration: + case SyntaxKind.StructDeclaration: return node.flags & NodeFlags.Default ? "default" : undefined; } } @@ -356,6 +357,8 @@ namespace ts { switch (node.kind) { case SyntaxKind.ClassExpression: case SyntaxKind.ClassDeclaration: + case SyntaxKind.StructExpression: + case SyntaxKind.StructDeclaration: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.EnumDeclaration: case SyntaxKind.TypeLiteral: @@ -439,6 +442,10 @@ namespace ts { case SyntaxKind.ClassDeclaration: return declareClassMember(node, symbolFlags, symbolExcludes); + case SyntaxKind.StructExpression: + case SyntaxKind.StructDeclaration: + return declareStructMember(node, symbolFlags, symbolExcludes); + case SyntaxKind.EnumDeclaration: return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); @@ -482,6 +489,12 @@ namespace ts { : declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); } + function declareStructMember(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags) { + return node.flags & NodeFlags.Static + ? declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes) + : declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); + } + function declareSourceFileMember(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags) { return isExternalModule(file) ? declareModuleMember(node, symbolFlags, symbolExcludes) @@ -534,7 +547,7 @@ namespace ts { } else { declareSymbolAndAddToSymbolTable(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes); - if (node.symbol.flags & (SymbolFlags.Function | SymbolFlags.Class | SymbolFlags.RegularEnum)) { + if (node.symbol.flags & (SymbolFlags.Function | SymbolFlags.Class | SymbolFlags.Struct | SymbolFlags.RegularEnum)) { // if module was already merged with some function, class or non-const enum // treat is a non-const-enum-only node.symbol.constEnumOnlyModule = false; @@ -922,6 +935,9 @@ namespace ts { case SyntaxKind.ClassExpression: case SyntaxKind.ClassDeclaration: return bindClassLikeDeclaration(node); + case SyntaxKind.StructExpression: + case SyntaxKind.StructDeclaration: + return bindStructLikeDeclaration(node); case SyntaxKind.InterfaceDeclaration: return bindBlockScopedDeclaration(node, SymbolFlags.Interface, SymbolFlags.InterfaceExcludes); case SyntaxKind.TypeAliasDeclaration: @@ -985,6 +1001,42 @@ namespace ts { } } + function bindStructLikeDeclaration(node: StructLikeDeclaration) { + if (node.kind === SyntaxKind.StructDeclaration) { + bindBlockScopedDeclaration(node, SymbolFlags.Struct, SymbolFlags.StructExcludes); + } + else { + let bindingName = node.name ? node.name.text : "__struct"; + bindAnonymousDeclaration(node, SymbolFlags.Struct, bindingName); + // Add name of class expression into the map for semantic classifier + if (node.name) { + classifiableNames[node.name.text] = node.name.text; + } + } + + let symbol = node.symbol; + + // TypeScript 1.0 spec (April 2014): 8.4 + // Every class automatically contains a static property member named 'prototype', the + // type of which is an instantiation of the class type with type Any supplied as a type + // argument for each type parameter. It is an error to explicitly declare a static + // property member with the name 'prototype'. + // + // Note: we check for this here because this class may be merging into a module. The + // module might have an exported variable called 'prototype'. We can't allow that as + // that would clash with the built-in 'prototype' for the class. + let prototypeSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Prototype, "prototype"); + if (hasProperty(symbol.exports, prototypeSymbol.name)) { + if (node.name) { + node.name.parent = node; + } + file.bindDiagnostics.push(createDiagnosticForNode(symbol.exports[prototypeSymbol.name].declarations[0], + Diagnostics.Duplicate_identifier_0, prototypeSymbol.name)); + } + symbol.exports[prototypeSymbol.name] = prototypeSymbol; + prototypeSymbol.parent = symbol; + } + function bindClassLikeDeclaration(node: ClassLikeDeclaration) { if (node.kind === SyntaxKind.ClassDeclaration) { bindBlockScopedDeclaration(node, SymbolFlags.Class, SymbolFlags.ClassExcludes); diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b3fa607b0ea49..6901caeb80a57 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -111,6 +111,14 @@ namespace ts { let unknownType = createIntrinsicType(TypeFlags.Any, "unknown"); let circularType = createIntrinsicType(TypeFlags.Any, "__circular__"); + var i8Type = createIntrinsicType(TypeFlags.I8, "i8"); + var u8Type = createIntrinsicType(TypeFlags.U8, "u8"); + var i16Type = createIntrinsicType(TypeFlags.I16, "i16"); + var u16Type = createIntrinsicType(TypeFlags.U16, "u16"); + var i32Type = createIntrinsicType(TypeFlags.I32, "i32"); + var u32Type = createIntrinsicType(TypeFlags.U32, "u32"); + var floatType = createIntrinsicType(TypeFlags.Float, "float"); + let emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); let emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); emptyGenericType.instantiations = {}; @@ -118,7 +126,7 @@ namespace ts { let anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); // The anyFunctionType contains the anyFunctionType by definition. The flag is further propagated // in getPropagatingFlagsOfTypes, and it is checked in inferFromTypes. - anyFunctionType.flags |= TypeFlags.ContainsAnyFunctionType; + //anyFunctionType.flags |= TypeFlags.ContainsAnyFunctionType; let noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); @@ -147,6 +155,17 @@ namespace ts { let globalIteratorType: GenericType; let globalIterableIteratorType: GenericType; + var globalIntType : ObjectType; + var globalUintType : ObjectType; + var globalI8Type : ObjectType; + var globalU8Type : ObjectType; + var globalI16Type : ObjectType; + var globalU16Type : ObjectType; + var globalI32Type : ObjectType; + var globalU32Type : ObjectType; + var globalFloatType : ObjectType; + var globalDoubleType : ObjectType; + let anyArrayType: Type; let getGlobalClassDecoratorType: () => ObjectType; let getGlobalParameterDecoratorType: () => ObjectType; @@ -169,6 +188,7 @@ namespace ts { let emitParam = false; let emitAwaiter = false; let emitGenerator = false; + let emitMathFround = false; let resolutionTargets: TypeSystemEntity[] = []; let resolutionResults: boolean[] = []; @@ -191,6 +211,34 @@ namespace ts { type: numberType, flags: TypeFlags.NumberLike }, + "i8": { + type: i8Type, + flags: TypeFlags.I8 + }, + "u8": { + type: u8Type, + flags: TypeFlags.U8 + }, + "i16": { + type: i16Type, + flags: TypeFlags.I16 + }, + "u16": { + type: u16Type, + flags: TypeFlags.U16 + }, + "i32": { + type: i32Type, + flags: TypeFlags.I32 + }, + "u32": { + type: u32Type, + flags: TypeFlags.U32 + }, + "float": { + type: floatType, + flags: TypeFlags.Float + }, "boolean": { type: booleanType, flags: TypeFlags.Boolean @@ -256,6 +304,7 @@ namespace ts { if (flags & SymbolFlags.EnumMember) result |= SymbolFlags.EnumMemberExcludes; if (flags & SymbolFlags.Function) result |= SymbolFlags.FunctionExcludes; if (flags & SymbolFlags.Class) result |= SymbolFlags.ClassExcludes; + if (flags & SymbolFlags.Struct) result |= SymbolFlags.StructExcludes; if (flags & SymbolFlags.Interface) result |= SymbolFlags.InterfaceExcludes; if (flags & SymbolFlags.RegularEnum) result |= SymbolFlags.RegularEnumExcludes; if (flags & SymbolFlags.ConstEnum) result |= SymbolFlags.ConstEnumExcludes; @@ -557,6 +606,26 @@ namespace ts { } } break; + case SyntaxKind.StructDeclaration: + case SyntaxKind.StructExpression: + if (result = getSymbol(getSymbolOfNode(location).members, name, meaning & SymbolFlags.Type)) { + if (lastLocation && lastLocation.flags & NodeFlags.Static) { + // TypeScript 1.0 spec (April 2014): 3.4.1 + // The scope of a type parameter extends over the entire declaration with which the type + // parameter list is associated, with the exception of static member declarations in classes. + error(errorLocation, Diagnostics.Static_members_cannot_reference_class_type_parameters); + return undefined; + } + break loop; + } + if (location.kind === SyntaxKind.StructExpression && meaning & SymbolFlags.Struct) { + let className = (location).name; + if (className && name === className.text) { + result = location.symbol; + break loop; + } + } + break; // It is not legal to reference a class's own type parameters from a computed property name that // belongs to the class. For example: @@ -1212,6 +1281,7 @@ namespace ts { } break; case SyntaxKind.ClassDeclaration: + case SyntaxKind.StructDeclaration: case SyntaxKind.InterfaceDeclaration: if (result = callback(getSymbolOfNode(location).members)) { return result; @@ -1519,6 +1589,7 @@ namespace ts { } switch (declaration.kind) { case SyntaxKind.ClassExpression: + case SyntaxKind.StructExpression: return "(Anonymous class)"; case SyntaxKind.FunctionExpression: case SyntaxKind.ArrowFunction: @@ -2124,6 +2195,7 @@ namespace ts { // Otherwise fall through case SyntaxKind.ModuleDeclaration: case SyntaxKind.ClassDeclaration: + case SyntaxKind.StructDeclaration: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.TypeAliasDeclaration: case SyntaxKind.FunctionDeclaration: @@ -2751,6 +2823,7 @@ namespace ts { return typeParameters; } if (node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression || + node.kind === SyntaxKind.StructDeclaration || node.kind === SyntaxKind.StructExpression || node.kind === SyntaxKind.FunctionDeclaration || node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.ArrowFunction) { let declarations = (node).typeParameters; @@ -2773,7 +2846,8 @@ namespace ts { let result: TypeParameter[]; for (let node of symbol.declarations) { if (node.kind === SyntaxKind.InterfaceDeclaration || node.kind === SyntaxKind.ClassDeclaration || - node.kind === SyntaxKind.ClassExpression || node.kind === SyntaxKind.TypeAliasDeclaration) { + node.kind === SyntaxKind.ClassExpression || node.kind === SyntaxKind.StructDeclaration || + node.kind === SyntaxKind.StructExpression || node.kind === SyntaxKind.TypeAliasDeclaration) { let declaration = node; if (declaration.typeParameters) { result = appendTypeParameters(result, declaration.typeParameters); @@ -2797,6 +2871,10 @@ namespace ts { return getClassExtendsHeritageClauseElement(type.symbol.valueDeclaration); } + function getBaseTypeNodeOfStruct(type: InterfaceType): ExpressionWithTypeArguments { + return getStructExtendsHeritageClauseElement(type.symbol.valueDeclaration); + } + function getConstructorsForTypeArguments(type: ObjectType, typeArgumentNodes: TypeNode[]): Signature[] { let typeArgCount = typeArgumentNodes ? typeArgumentNodes.length : 0; return filter(getSignaturesOfType(type, SignatureKind.Construct), @@ -2845,6 +2923,34 @@ namespace ts { return type.resolvedBaseConstructorType; } + function getBaseConstructorTypeOfStruct(type: InterfaceType): ObjectType { + if (!type.resolvedBaseConstructorType) { + let baseTypeNode = getBaseTypeNodeOfStruct(type); + if (!baseTypeNode) { + return type.resolvedBaseConstructorType = undefinedType; + } + if (!pushTypeResolution(type, TypeSystemPropertyName.ResolvedBaseConstructorType)) { + return unknownType; + } + let baseConstructorType = checkExpression(baseTypeNode.expression); + if (baseConstructorType.flags & TypeFlags.ObjectType) { + // Resolving the members of a class requires us to resolve the base class of that class. + // We force resolution here such that we catch circularities now. + resolveStructuredTypeMembers(baseConstructorType); + } + if (!popTypeResolution()) { + error(type.symbol.valueDeclaration, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_base_expression, symbolToString(type.symbol)); + return type.resolvedBaseConstructorType = unknownType; + } + if (baseConstructorType !== unknownType && baseConstructorType !== nullType && !isConstructorType(baseConstructorType)) { + error(baseTypeNode.expression, Diagnostics.Type_0_is_not_a_constructor_function_type, typeToString(baseConstructorType)); + return type.resolvedBaseConstructorType = unknownType; + } + type.resolvedBaseConstructorType = baseConstructorType; + } + return type.resolvedBaseConstructorType; + } + function getBaseTypes(type: InterfaceType): ObjectType[] { if (!type.resolvedBaseTypes) { if (type.symbol.flags & SymbolFlags.Class) { @@ -3077,6 +3183,13 @@ namespace ts { case SyntaxKind.AnyKeyword: case SyntaxKind.StringKeyword: case SyntaxKind.NumberKeyword: + case SyntaxKind.I8Keyword: + case SyntaxKind.U8Keyword: + case SyntaxKind.I16Keyword: + case SyntaxKind.U16Keyword: + case SyntaxKind.I32Keyword: + case SyntaxKind.U32Keyword: + case SyntaxKind.FloatKeyword: case SyntaxKind.BooleanKeyword: case SyntaxKind.SymbolKeyword: case SyntaxKind.VoidKeyword: @@ -4129,6 +4242,7 @@ namespace ts { for (let declaration of declarations) { switch (declaration.kind) { case SyntaxKind.ClassDeclaration: + case SyntaxKind.StructDeclaration: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.EnumDeclaration: return declaration; @@ -4434,6 +4548,20 @@ namespace ts { return esSymbolType; case SyntaxKind.VoidKeyword: return voidType; + case SyntaxKind.I32Keyword: + return i32Type; + case SyntaxKind.U32Keyword: + return u32Type; + case SyntaxKind.I16Keyword: + return i16Type; + case SyntaxKind.U16Keyword: + return u16Type; + case SyntaxKind.I8Keyword: + return i8Type; + case SyntaxKind.U8Keyword: + return u8Type; + case SyntaxKind.FloatKeyword: + return floatType; case SyntaxKind.ThisKeyword: return getTypeFromThisTypeNode(node); case SyntaxKind.StringLiteral: @@ -4823,6 +4951,31 @@ namespace ts { if (source === numberType && target.flags & TypeFlags.Enum) return Ternary.True; } + if (target.flags & TypeFlags.I32 && + source.flags & (TypeFlags.I16 | TypeFlags.U16 | TypeFlags.I8 | TypeFlags.U8)) { + return Ternary.True; + } + if (target.flags & TypeFlags.U32 && + source.flags & (TypeFlags.U16 | TypeFlags.U8)) { + return Ternary.True; + } + if (target.flags & TypeFlags.I16 && + source.flags & (TypeFlags.I8 | TypeFlags.U8)) { + return Ternary.True; + } + if (target.flags & TypeFlags.U16 && + source.flags & TypeFlags.U8) { + return Ternary.True; + } + if (target.flags & TypeFlags.RealNumber && + source.flags & (TypeFlags.PrimitiveType)) { + return Ternary.True; + } + if (target.flags & TypeFlags.PrimitiveType && + source.flags & (TypeFlags.RealNumber)) { + return Ternary.True; + } + if (source.flags & TypeFlags.FreshObjectLiteral) { if (hasExcessProperties(source, target, reportErrors)) { if (reportErrors) { @@ -4897,7 +5050,7 @@ namespace ts { if (apparentType.flags & (TypeFlags.ObjectType | TypeFlags.Intersection) && target.flags & TypeFlags.ObjectType) { // Report structural errors only if we haven't reported any errors yet let reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo; - if (result = objectTypeRelatedTo(apparentType, source, target, reportStructuralErrors)) { + if (result = objectTypeRelatedTo(apparentType, target, reportStructuralErrors)) { errorInfo = saveErrorInfo; return result; } @@ -4919,7 +5072,7 @@ namespace ts { return result; } } - return objectTypeRelatedTo(source, source, target, /*reportErrors*/ false); + return objectTypeRelatedTo(source, target, /*reportErrors*/ false); } if (source.flags & TypeFlags.TypeParameter && target.flags & TypeFlags.TypeParameter) { return typeParameterIdenticalTo(source, target); @@ -4946,34 +5099,34 @@ namespace ts { resolved.stringIndexType || resolved.numberIndexType || getPropertyOfType(type, name)) { return true; } + return false; } - else if (type.flags & TypeFlags.UnionOrIntersection) { + if (type.flags & TypeFlags.UnionOrIntersection) { for (let t of (type).types) { if (isKnownProperty(t, name)) { return true; } } + return false; } - return false; + return true; } function hasExcessProperties(source: FreshObjectLiteralType, target: Type, reportErrors: boolean): boolean { - if (someConstituentTypeHasKind(target, TypeFlags.ObjectType)) { - for (let prop of getPropertiesOfObjectType(source)) { - if (!isKnownProperty(target, prop.name)) { - if (reportErrors) { - // We know *exactly* where things went wrong when comparing the types. - // Use this property as the error node as this will be more helpful in - // reasoning about what went wrong. - errorNode = prop.valueDeclaration; - reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, - symbolToString(prop), typeToString(target)); - } - return true; + for (let prop of getPropertiesOfObjectType(source)) { + if (!isKnownProperty(target, prop.name)) { + if (reportErrors) { + // We know *exactly* where things went wrong when comparing the types. + // Use this property as the error node as this will be more helpful in + // reasoning about what went wrong. + errorNode = prop.valueDeclaration; + reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, + symbolToString(prop), + typeToString(target)); } + return true; } } - return false; } function eachTypeRelatedToSomeType(source: UnionOrIntersectionType, target: UnionOrIntersectionType): Ternary { @@ -5073,11 +5226,11 @@ namespace ts { // Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are // equal and infinitely expanding. Fourth, if we have reached a depth of 100 nested comparisons, assume we have runaway recursion // and issue an error. Otherwise, actually compare the structure of the two types. - function objectTypeRelatedTo(apparentSource: Type, originalSource: Type, target: Type, reportErrors: boolean): Ternary { + function objectTypeRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary { if (overflow) { return Ternary.False; } - let id = relation !== identityRelation || apparentSource.id < target.id ? apparentSource.id + "," + target.id : target.id + "," + apparentSource.id; + let id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id; let related = relation[id]; if (related !== undefined) { // If we computed this relation already and it was failed and reported, or if we're not being asked to elaborate @@ -5104,28 +5257,28 @@ namespace ts { maybeStack = []; expandingFlags = 0; } - sourceStack[depth] = apparentSource; + sourceStack[depth] = source; targetStack[depth] = target; maybeStack[depth] = {}; maybeStack[depth][id] = RelationComparisonResult.Succeeded; depth++; let saveExpandingFlags = expandingFlags; - if (!(expandingFlags & 1) && isDeeplyNestedGeneric(apparentSource, sourceStack, depth)) expandingFlags |= 1; + if (!(expandingFlags & 1) && isDeeplyNestedGeneric(source, sourceStack, depth)) expandingFlags |= 1; if (!(expandingFlags & 2) && isDeeplyNestedGeneric(target, targetStack, depth)) expandingFlags |= 2; let result: Ternary; if (expandingFlags === 3) { result = Ternary.Maybe; } else { - result = propertiesRelatedTo(apparentSource, target, reportErrors); + result = propertiesRelatedTo(source, target, reportErrors); if (result) { - result &= signaturesRelatedTo(apparentSource, target, SignatureKind.Call, reportErrors); + result &= signaturesRelatedTo(source, target, SignatureKind.Call, reportErrors); if (result) { - result &= signaturesRelatedTo(apparentSource, target, SignatureKind.Construct, reportErrors); + result &= signaturesRelatedTo(source, target, SignatureKind.Construct, reportErrors); if (result) { - result &= stringIndexTypesRelatedTo(apparentSource, originalSource, target, reportErrors); + result &= stringIndexTypesRelatedTo(source, target, reportErrors); if (result) { - result &= numberIndexTypesRelatedTo(apparentSource, originalSource, target, reportErrors); + result &= numberIndexTypesRelatedTo(source, target, reportErrors); } } } @@ -5456,17 +5609,12 @@ namespace ts { return result; } - function stringIndexTypesRelatedTo(source: Type, originalSource: Type, target: Type, reportErrors: boolean): Ternary { + function stringIndexTypesRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary { if (relation === identityRelation) { return indexTypesIdenticalTo(IndexKind.String, source, target); } let targetType = getIndexTypeOfType(target, IndexKind.String); - if (targetType) { - if ((targetType.flags & TypeFlags.Any) && !(originalSource.flags & TypeFlags.Primitive)) { - // non-primitive assignment to any is always allowed, eg - // `var x: { [index: string]: any } = { property: 12 };` - return Ternary.True; - } + if (targetType && !(targetType.flags & TypeFlags.Any)) { let sourceType = getIndexTypeOfType(source, IndexKind.String); if (!sourceType) { if (reportErrors) { @@ -5486,17 +5634,12 @@ namespace ts { return Ternary.True; } - function numberIndexTypesRelatedTo(source: Type, originalSource: Type, target: Type, reportErrors: boolean): Ternary { + function numberIndexTypesRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary { if (relation === identityRelation) { return indexTypesIdenticalTo(IndexKind.Number, source, target); } let targetType = getIndexTypeOfType(target, IndexKind.Number); - if (targetType) { - if ((targetType.flags & TypeFlags.Any) && !(originalSource.flags & TypeFlags.Primitive)) { - // non-primitive assignment to any is always allowed, eg - // `var x: { [index: number]: any } = { property: 12 };` - return Ternary.True; - } + if (targetType && !(targetType.flags & TypeFlags.Any)) { let sourceStringType = getIndexTypeOfType(source, IndexKind.String); let sourceNumberType = getIndexTypeOfType(source, IndexKind.Number); if (!(sourceStringType || sourceNumberType)) { @@ -5925,9 +6068,9 @@ namespace ts { // it as an inference candidate. Hopefully, a better candidate will come along that does // not contain anyFunctionType when we come back to this argument for its second round // of inference. - if (source.flags & TypeFlags.ContainsAnyFunctionType) { - return; - } + //if (source.flags & TypeFlags.ContainsAnyFunctionType) { + // return; + //} let typeParameters = context.typeParameters; for (let i = 0; i < typeParameters.length; i++) { @@ -7031,7 +7174,7 @@ namespace ts { return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined; } - function getContextualTypeForJsxExpression(expr: JsxExpression | JsxSpreadAttribute): Type { + function getContextualTypeForJsxExpression(expr: JsxExpression|JsxSpreadAttribute): Type { // Contextual type only applies to JSX expressions that are in attribute assignments (not in 'Children' positions) if (expr.parent.kind === SyntaxKind.JsxAttribute) { let attrib = expr.parent; @@ -7522,6 +7665,9 @@ namespace ts { case SyntaxKind.JsxSelfClosingElement: checkJsxSelfClosingElement(child); break; + default: + // No checks for JSX Text + Debug.assert(child.kind === SyntaxKind.JsxText); } } @@ -7539,7 +7685,7 @@ namespace ts { /** * Returns true iff React would emit this tag name as a string rather than an identifier or qualified name */ - function isJsxIntrinsicIdentifier(tagName: Identifier | QualifiedName) { + function isJsxIntrinsicIdentifier(tagName: Identifier|QualifiedName) { if (tagName.kind === SyntaxKind.QualifiedName) { return false; } @@ -7625,7 +7771,7 @@ namespace ts { /// If this is a class-based tag (otherwise returns undefined), returns the symbol of the class /// type or factory function. /// Otherwise, returns unknownSymbol. - function getJsxElementTagSymbol(node: JsxOpeningLikeElement | JsxClosingElement): Symbol { + function getJsxElementTagSymbol(node: JsxOpeningLikeElement|JsxClosingElement): Symbol { let flags: JsxFlags = JsxFlags.UnknownElement; let links = getNodeLinks(node); if (!links.resolvedSymbol) { @@ -7638,7 +7784,7 @@ namespace ts { } return links.resolvedSymbol; - function lookupIntrinsicTag(node: JsxOpeningLikeElement | JsxClosingElement): Symbol { + function lookupIntrinsicTag(node: JsxOpeningLikeElement|JsxClosingElement): Symbol { let intrinsicElementsType = getJsxIntrinsicElementsType(); if (intrinsicElementsType !== unknownType) { // Property case @@ -7666,7 +7812,7 @@ namespace ts { } } - function lookupClassTag(node: JsxOpeningLikeElement | JsxClosingElement): Symbol { + function lookupClassTag(node: JsxOpeningLikeElement|JsxClosingElement): Symbol { let valueSymbol: Symbol = resolveJsxTagName(node); // Look up the value in the current scope @@ -7680,7 +7826,7 @@ namespace ts { return valueSymbol || unknownSymbol; } - function resolveJsxTagName(node: JsxOpeningLikeElement | JsxClosingElement): Symbol { + function resolveJsxTagName(node: JsxOpeningLikeElement|JsxClosingElement): Symbol { if (node.tagName.kind === SyntaxKind.Identifier) { let tag = node.tagName; let sym = getResolvedSymbol(tag); @@ -8097,8 +8243,14 @@ namespace ts { } } + var symbol = getResolvedSymbol((node.expression)); + if (symbol && (symbol.flags & SymbolFlags.Struct)) { + node.isStructArray = true; + } + // Obtain base constraint such that we can bail out if the constraint is an unknown type - let objectType = getApparentType(checkExpression(node.expression)); + let originObjectType = checkExpression(node.expression); + let objectType = getApparentType(originObjectType); let indexType = node.argumentExpression ? checkExpression(node.argumentExpression) : unknownType; if (objectType === unknownType) { @@ -8548,6 +8700,12 @@ namespace ts { if (!checkTypeRelatedTo(argType, paramType, relation, errorNode, headMessage)) { return false; } + + if (argType.flags & TypeFlags.PrimitiveType) { + args[i] = convertToLower(argType, argType, arg, true); + } else if (paramType.flags & TypeFlags.PrimitiveType) { + args[i] = convertToLower(paramType, argType, arg); + } } } @@ -8606,6 +8764,8 @@ namespace ts { switch (node.parent.kind) { case SyntaxKind.ClassDeclaration: case SyntaxKind.ClassExpression: + case SyntaxKind.StructDeclaration: + case SyntaxKind.StructExpression: // A class decorator will have one argument (see `ClassDecorator` in core.d.ts) return 1; @@ -8655,7 +8815,7 @@ namespace ts { */ function getEffectiveDecoratorFirstArgumentType(node: Node): Type { // The first argument to a decorator is its `target`. - if (node.kind === SyntaxKind.ClassDeclaration) { + if (node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.StructDeclaration) { // For a class decorator, the `target` is the type of the class (e.g. the // "static" or "constructor" side of the class) let classSymbol = getSymbolOfNode(node); @@ -8709,6 +8869,11 @@ namespace ts { return unknownType; } + if (node.kind === SyntaxKind.StructDeclaration) { + Debug.fail("Struct decorators should not have a second synthetic argument."); + return unknownType; + } + if (node.kind === SyntaxKind.Parameter) { node = node.parent; if (node.kind === SyntaxKind.Constructor) { @@ -8769,6 +8934,11 @@ namespace ts { return unknownType; } + if (node.kind === SyntaxKind.StructDeclaration) { + Debug.fail("Struct decorators should not have a third synthetic argument."); + return unknownType; + } + if (node.kind === SyntaxKind.Parameter) { // The `parameterIndex` for a parameter decorator is always a number return numberType; @@ -9251,6 +9421,10 @@ namespace ts { case SyntaxKind.ClassExpression: return Diagnostics.Unable_to_resolve_signature_of_class_decorator_when_called_as_an_expression; + case SyntaxKind.StructDeclaration: + case SyntaxKind.StructExpression: + return Diagnostics.Unable_to_resolve_signature_of_class_decorator_when_called_as_an_expression; + case SyntaxKind.Parameter: return Diagnostics.Unable_to_resolve_signature_of_parameter_decorator_when_called_as_an_expression; @@ -9794,7 +9968,7 @@ namespace ts { // old compiler doesn't check indexed access return true; case SyntaxKind.ParenthesizedExpression: - return isReferenceOrErrorExpression((n).expression); + return true; default: return false; } @@ -9870,6 +10044,9 @@ namespace ts { function checkPrefixUnaryExpression(node: PrefixUnaryExpression): Type { let operandType = checkExpression(node.operand); + var primType : boolean = false; + if(operandType.flags & TypeFlags.PrimitiveType) primType = true; + switch (node.operator) { case SyntaxKind.PlusToken: case SyntaxKind.MinusToken: @@ -9889,13 +10066,105 @@ namespace ts { Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer, Diagnostics.The_operand_of_an_increment_or_decrement_operator_cannot_be_a_constant); } - return numberType; + if(primType) { + var newOp : SyntaxKind = node.operator === SyntaxKind.PlusPlusToken ? SyntaxKind.PlusToken : SyntaxKind.MinusToken; + var newNode = createBinaryNode(node.operand, SyntaxKind.EqualsToken, createLitNode(node.operand, newOp, "1")); + var retType = checkBinaryExpression(newNode); + node.operator = SyntaxKind.Unknown; + node.operand = createParenNode(newNode); + return retType; + } else { + return numberType; + } } return unknownType; } + function checkDuplicationOfName(name : string, block : Node) : boolean { + var symbol = resolveName(block, name, SymbolFlags.Variable, undefined, ""); + return symbol !== undefined; + } + + function getNextHelperVarName(block : Block | SourceFile) : string { + if(block.helperVarCount === undefined) block.helperVarCount = 0; + var count : Number = block.helperVarCount++; + + var name = "_$" + count.toString(); + while(checkDuplicationOfName(name, block)) { + count = block.helperVarCount++; + name = "_$" + count.toString(); + } + return name; + } + + function createVariableDeclarationStatement(varName : string, varType? : Type) : VariableStatement{ + let ret = createNode(SyntaxKind.VariableStatement); + let varDecl = createNode(SyntaxKind.VariableDeclaration); + + let declList = createNode(SyntaxKind.VariableDeclarationList); + declList.declarations = >[varDecl]; + + let varNameIdentifier = createNode(SyntaxKind.Identifier); + varNameIdentifier.text = varName; + //varNameIdentifier.parent = varDecl; + + varDecl.name = varNameIdentifier; + varDecl.parent = declList; + + ret.declarationList = declList; + return ret; + } + + function createPostIncrementExpression(tempVarIdentifier : Identifier, incExp : Node) { + //return: (t = incExp, incExp = incExp + 1, t) + + var nodeLeft = createBinaryNode(tempVarIdentifier, SyntaxKind.EqualsToken, incExp); + + var nodeMid1 = createLitNode(incExp, SyntaxKind.PlusToken, "1"); + var nodeMid0 = createBinaryNode(incExp, SyntaxKind.EqualsToken, nodeMid1); + checkExpression(nodeMid0); + + var nodeRight = tempVarIdentifier; + + var ret = createBinaryNode(nodeLeft, SyntaxKind.CommaToken, nodeMid0); + ret = createParenNode(createBinaryNode(ret, SyntaxKind.CommaToken, nodeRight)); + return ret; + } + function checkPostfixUnaryExpression(node: PostfixUnaryExpression): Type { let operandType = checkExpression(node.operand); + + var primType : boolean = false; + if(operandType.flags & TypeFlags.PrimitiveType) primType = true; + + if(primType && node.operator != SyntaxKind.Unknown) { + var block = node.parent; + while(block != undefined) { + var blockInstance : Block | SourceFile; + if (block.kind === SyntaxKind.Block) { + blockInstance = (block); + } else if(block.kind === SyntaxKind.SourceFile) { + blockInstance = (block); + } + if(blockInstance != undefined && blockInstance.statements != undefined) { + var helperVarName = getNextHelperVarName(blockInstance); + var varDecl = createVariableDeclarationStatement(helperVarName); + varDecl.parent = blockInstance; + + if(blockInstance.helperStatements === undefined) { + blockInstance.helperStatements = >[]; + } + blockInstance.helperStatements.push(varDecl); + + var nameId = (varDecl.declarationList.declarations[0].name); + node.operand = createPostIncrementExpression(nameId, node.operand); + node.operator = SyntaxKind.Unknown; + break; + } + block = block.parent; + } + } + let ok = checkArithmeticOperandType(node.operand, operandType, Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type); if (ok) { // run check only if former checks succeeded to avoid reporting cascading errors @@ -10090,17 +10359,298 @@ namespace ts { return sourceType; } + function getSizeOfPrimType(type : Type) : number { + if(type.flags & (TypeFlags.U8 | type.flags & TypeFlags.I8)) { + return 1; + } else if(type.flags & (TypeFlags.U16 | type.flags & TypeFlags.I16)) { + return 2; + } else if(type.flags & (TypeFlags.U32 | type.flags & TypeFlags.I32 | type.flags & TypeFlags.Float)) { + return 4; + } else if(type.flags & (TypeFlags.Number | TypeFlags.Enum)) { + return 8; + } else { + return 8; + } + } + + function isSigned(type : Type) { + if(type.flags & (TypeFlags.U8 | type.flags & TypeFlags.U16 | type.flags & TypeFlags.U32)) { + return false; + } else { + return true; + } + } + + function createLitNode(node : Node, op : SyntaxKind, lit : string) { + let newer = createNode(SyntaxKind.BinaryExpression); + + let literal = createNode(SyntaxKind.NumericLiteral); + literal.text = lit; + + newer.left = (node); + newer.operatorToken = createSynthesizedNode(op); + newer.right = literal; + + return newer; + } + + function createParenNode(node : Node) { + let newer = createNode(SyntaxKind.ParenthesizedExpression); + newer.expression = (node); + return newer; + } + + function createBinaryNode(node : Node, op : SyntaxKind, nodeRight : Node) : Expression{ + var newer = (new (objectAllocator.getNodeConstructor(SyntaxKind.BinaryExpression))()); + newer.pos = node.pos; + newer.end = node.end; + + newer.left = (node); + newer.operatorToken = createSynthesizedNode(op); + newer.right = (nodeRight); + + return newer; + } + + function isAssignmentOp(op : SyntaxKind) { + switch(op){ + case SyntaxKind.EqualsToken: + case SyntaxKind.PlusEqualsToken: + case SyntaxKind.MinusEqualsToken: + case SyntaxKind.AsteriskEqualsToken: + case SyntaxKind.SlashEqualsToken: + case SyntaxKind.PercentEqualsToken: + case SyntaxKind.LessThanLessThanEqualsToken: + case SyntaxKind.GreaterThanGreaterThanEqualsToken: + case SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken: + case SyntaxKind.AmpersandEqualsToken: + case SyntaxKind.BarEqualsToken: + case SyntaxKind.CaretEqualsToken: + return true; + default: + return false; + } + } + + function splitAssignmentOp(op : SyntaxKind) { + switch(op){ + case SyntaxKind.PlusEqualsToken: + return SyntaxKind.PlusToken; + case SyntaxKind.MinusEqualsToken: + return SyntaxKind.MinusToken; + case SyntaxKind.AsteriskEqualsToken: + return SyntaxKind.AsteriskToken; + case SyntaxKind.SlashEqualsToken: + return SyntaxKind.SlashToken; + case SyntaxKind.PercentEqualsToken: + return SyntaxKind.PercentToken; + case SyntaxKind.LessThanLessThanEqualsToken: + return SyntaxKind.LessThanLessThanToken; + case SyntaxKind.GreaterThanGreaterThanEqualsToken: + return SyntaxKind.GreaterThanGreaterThanToken; + case SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken: + return SyntaxKind.GreaterThanGreaterThanGreaterThanToken; + case SyntaxKind.AmpersandEqualsToken: + return SyntaxKind.AmpersandToken; + case SyntaxKind.BarEqualsToken: + return SyntaxKind.BarToken; + case SyntaxKind.CaretEqualsToken: + return SyntaxKind.CaretToken; + default: + return undefined; + } + } + + function getNumericLiteralValue(originNode : Node) : number { + var val : number; + if(originNode.kind == SyntaxKind.NumericLiteral) { + var text = ((originNode)).text; + val = parseFloat(text); + } else { + var unaryNode = originNode; + /* + if(unaryNode.operand.kind == SyntaxKind.NumericLiteral){ + var text = ((unaryNode.operand)).text; + val = parseFloat(text); + if(unaryNode.operator === SyntaxKind.PlusToken) { + val = val; + } else if(unaryNode.operator === SyntaxKind.MinusToken) { + val = -val; + } else if(unaryNode.operator === SyntaxKind.ExclamationToken) { + val = !val ? 0 : 1; + } else if(unaryNode.operator === SyntaxKind.TildeToken) { + val = ~val; + } else { + console.error("Invalid unary expression, failed to convert to lower expression"); + } + } + */ + } + return val; + } + + function canIgnoreConvertNumericLit(targetType : Type, originType : Type, originNode : Expression) { + if (originNode.kind == SyntaxKind.NumericLiteral || + originNode.kind == SyntaxKind.PostfixUnaryExpression) { + var val : number = getNumericLiteralValue(originNode); + var val2 : number; + + var sizeLeft = getSizeOfPrimType(targetType); + var signedLeft = isSigned(targetType); + var widthLeft = sizeLeft << 3; + var sizeRight = getSizeOfPrimType(originType); + var signedRight = isSigned(originType); + var widthRight = sizeRight << 3; + + if (widthLeft !== 32 && widthLeft < widthRight) { + val2 = val & ((1 << widthLeft) - 1); + if (signedLeft) val2 = (val2 << (32 - widthLeft)) >> (32 - widthLeft); + } else if (widthLeft !== widthRight || signedLeft != signedRight) { + if (signedLeft) val2 = val | 0; + else val2 = val >>> 0; + } else { + val2 = val; + } + + if (val === val2) { + return true; + } + } + return false; + } + + function convertDoubleToFloat(originNode : Expression) : Expression { + + emitMathFround = true; + + var funcName = (new (objectAllocator.getNodeConstructor(SyntaxKind.Identifier))()); + funcName.text = "Math.fround"; + funcName.pos = funcName.end = -1; + + var newer = (new (objectAllocator.getNodeConstructor(SyntaxKind.CallExpression))()); + newer.expression = funcName; + newer.arguments = >[]; + newer.arguments.push(originNode); + + funcName.parent = newer; + + return newer; + } + + function convertToLower(targetType : Type, originType : Type, originNode : Expression, forceConv : boolean = false) : Expression { + if(originType.flags & TypeFlags.RealNumber && + targetType.flags & TypeFlags.SinglePrecisionFloat) { + return convertDoubleToFloat(originNode); + } + if(!(targetType.flags & TypeFlags.PrimitiveType)) { + return originNode; + } + if(targetType.flags == originType.flags && !forceConv){ + return originNode; + } + + if(canIgnoreConvertNumericLit(targetType, originType, originNode)) { + return originNode; + } + + if(!forceConv && !isTypeAssignableTo(originType, targetType)) { + return originNode; + } + + var sizeLeft = getSizeOfPrimType(targetType); + var signedLeft = isSigned(targetType); + var widthLeft = sizeLeft << 3; + var mask = (1 << widthLeft) - 1; + var shift = 32 - widthLeft; + + var widthRight = getSizeOfPrimType(originType) << 3; + + var retNode : Expression = undefined; + + if (widthLeft !== 32 && widthLeft < widthRight) { + retNode = createLitNode(originNode, SyntaxKind.AmpersandToken, new Number(mask).toString()); + // Do we need to sign extend? + if (signedLeft) { + retNode = createParenNode(retNode); + retNode = createLitNode(retNode, SyntaxKind.LessThanLessThanToken, new Number(shift).toString()); + retNode = createLitNode(retNode, SyntaxKind.GreaterThanGreaterThanToken, new Number(shift).toString()); + } + } else { + retNode = createLitNode(originNode, + signedLeft ? SyntaxKind.BarToken : SyntaxKind.GreaterThanGreaterThanGreaterThanToken, "0"); + } + return retNode; + } + + function checkAndMarkExpression(node: Expression, contextualMapper?: TypeMapper): Type { + var result = checkExpression(node, contextualMapper); + getNodeLinks(node).flags |= NodeCheckFlags.TypeChecked; + return result; + } + function checkBinaryExpression(node: BinaryExpression, contextualMapper?: TypeMapper) { return checkBinaryLikeExpression(node.left, node.operatorToken, node.right, contextualMapper, node); } function checkBinaryLikeExpression(left: Expression, operatorToken: Node, right: Expression, contextualMapper?: TypeMapper, errorNode?: Node) { + function getBestSizeFromNumberLikes(leftType : Type, rightType : Type) : Type { + if((leftType.flags & TypeFlags.Enum) || (rightType.flags & TypeFlags.Enum)) { + return numberType; + } + if(leftType.flags & TypeFlags.Number || rightType.flags & TypeFlags.Number) { + return numberType; + } + if(leftType.flags & TypeFlags.Float || rightType.flags & TypeFlags.Float) { + return floatType; + } + if(leftType.flags & TypeFlags.U32 || rightType.flags & TypeFlags.U32) { + return u32Type; + } + return i32Type; + } + let operator = operatorToken.kind; if (operator === SyntaxKind.EqualsToken && (left.kind === SyntaxKind.ObjectLiteralExpression || left.kind === SyntaxKind.ArrayLiteralExpression)) { return checkDestructuringAssignment(left, checkExpression(right, contextualMapper), contextualMapper); } let leftType = checkExpression(left, contextualMapper); let rightType = checkExpression(right, contextualMapper); + + var node = (errorNode); + var splitedOp = splitAssignmentOp(operator); + + if(leftType.flags & TypeFlags.PrimitiveType && splitedOp != undefined) { + operator = node.operatorToken.kind = SyntaxKind.EqualsToken; + var lowerNode = convertToLower(leftType, rightType, node.right); + node.right = createBinaryNode(node.left, splitedOp, createParenNode(lowerNode)); + } + + if (isAssignmentOp(operator)) { + if (leftType.flags & TypeFlags.Reference) { + var tRef = leftType; + if (tRef.target === globalArrayType && node.right.kind === SyntaxKind.ArrayLiteralExpression + && tRef.typeArguments[0].flags & TypeFlags.PrimitiveType) { + for (var i = 0; i < (node.right).elements.length; ++i) { + var origType = checkAndMarkExpression((node.right).elements[i]); + (node.right).elements[i] = convertToLower(tRef.typeArguments[0], + origType, (node.right).elements[i]); + } + } + } else { + var lowerNode = convertToLower(leftType, rightType, node.right); + + if(lowerNode != node.right) { + node.right = lowerNode; + rightType = leftType; + } else { + if(leftType.flags & TypeFlags.PrimitiveType && + canIgnoreConvertNumericLit(leftType, rightType, node.right)) { + rightType = leftType; + } + } + } + } + switch (operator) { case SyntaxKind.AsteriskToken: case SyntaxKind.AsteriskAsteriskToken: @@ -10150,7 +10700,21 @@ namespace ts { } } - return numberType; + if (leftType.flags & TypeFlags.NumberLike && rightType.flags & TypeFlags.NumberLike) { + var leftSize = getSizeOfPrimType(leftType); + var rightSize = getSizeOfPrimType(rightType); + if (leftSize != rightSize) { + if (canIgnoreConvertNumericLit(leftType, rightType, node.right)) { + return leftType; + } else if (canIgnoreConvertNumericLit(rightType, leftType, node.left)) { + return rightType; + } else { + return leftSize < rightSize ? rightType : leftType; + } + } + } + else + return numberType; case SyntaxKind.PlusToken: case SyntaxKind.PlusEqualsToken: // TypeScript 1.0 spec (April 2014): 4.19.2 @@ -10494,6 +11058,8 @@ namespace ts { return checkExpression((node).expression, contextualMapper); case SyntaxKind.ClassExpression: return checkClassExpression(node); + case SyntaxKind.StructExpression: + return checkStructExpression(node); case SyntaxKind.FunctionExpression: case SyntaxKind.ArrowFunction: return checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper); @@ -11061,6 +11627,8 @@ namespace ts { if (n.parent.kind !== SyntaxKind.InterfaceDeclaration && n.parent.kind !== SyntaxKind.ClassDeclaration && n.parent.kind !== SyntaxKind.ClassExpression && + n.parent.kind !== SyntaxKind.StructDeclaration && + n.parent.kind !== SyntaxKind.StructExpression && isInAmbientContext(n)) { if (!(flags & NodeFlags.Ambient)) { // It is nested in an ambient context, which means it is automatically exported @@ -11368,6 +11936,7 @@ namespace ts { ? SymbolFlags.ExportNamespace | SymbolFlags.ExportValue : SymbolFlags.ExportNamespace; case SyntaxKind.ClassDeclaration: + case SyntaxKind.StructDeclaration: case SyntaxKind.EnumDeclaration: return SymbolFlags.ExportType | SymbolFlags.ExportValue; case SyntaxKind.ImportEqualsDeclaration: @@ -11649,6 +12218,7 @@ namespace ts { let errorInfo: DiagnosticMessageChain; switch (node.parent.kind) { case SyntaxKind.ClassDeclaration: + case SyntaxKind.StructDeclaration: let classSymbol = getSymbolOfNode(node.parent); let classConstructorType = getTypeOfSymbol(classSymbol); expectedReturnType = getUnionType([classConstructorType, voidType]); @@ -11765,6 +12335,13 @@ namespace ts { } break; + case SyntaxKind.StructDeclaration: + constructor = getFirstConstructorWithBody(node); + if (constructor) { + checkParameterTypeAnnotationsAsExpressions(constructor); + } + break; + case SyntaxKind.MethodDeclaration: checkParameterTypeAnnotationsAsExpressions(node); // fall-through @@ -11872,6 +12449,36 @@ namespace ts { if (isFunctionBlock(node) || node.kind === SyntaxKind.ModuleBlock) { checkFunctionAndClassExpressionBodies(node); } + + if(node.helperStatements !== undefined) { + while(node.helperStatements.length > 0){ + node.statements.unshift(node.helperStatements.pop()); + } + } + + if(isFunctionBlock(node)) { + var func = getContainingFunction(node); + + for (var i = 0, n = func.parameters.length; i < n; i++) { + var param = func.parameters[i]; + + if(param.type && param.name) { + var type = getTypeFromTypeNode(param.type); + //if(type && type.flags & TypeFlags.PrimitiveType) { + // var nameNode : Identifier; + // if (typeof param.name === 'Identifier') { + // nameNode = (param.name); + // } + // var lower = convertToLower(type, new Type(checker, TypeFlags.Number), nameNode, true); + // var ret = (new (objectAllocator.getNodeConstructor(SyntaxKind.ExpressionStatement))()); + // ret.expression = createBinaryNode(param.name, SyntaxKind.EqualsToken, lower); + // ret.expression.parent = ret; + // ret.parent = node; + // node.statements.unshift(ret); + //} + } + } + } } function checkCollisionWithArgumentsInGeneratedCode(node: SignatureDeclaration) { @@ -12128,6 +12735,17 @@ namespace ts { if (node.initializer) { checkTypeAssignableTo(checkExpressionCached(node.initializer), type, node, /*headMessage*/ undefined); checkParameterInitializer(node); + + if(type.flags & TypeFlags.PrimitiveType) { + + var initializerType = checkAndMarkExpression(node.initializer); + if(type.flags & TypeFlags.PrimitiveType) { + if(canIgnoreConvertNumericLit(type, initializerType, node.initializer)) { + initializerType = type; + } + } + node.initializer = convertToLower(type, initializerType, node.initializer); + } } } else { @@ -12627,6 +13245,11 @@ namespace ts { else { checkTypeAssignableTo(exprType, returnType, node.expression); } + + if (returnType.flags & TypeFlags.PrimitiveType) { + var returnExpType = checkExpression(node.expression); + node.expression = convertToLower(returnType, returnExpType, node.expression, true); + } } } } @@ -12847,6 +13470,16 @@ namespace ts { switch ((name).text) { case "any": case "number": + case "int": + case "uint": + case "i8": + case "u8": + case "i16": + case "u16": + case "i32": + case "u32": + case "double": + case "float": case "boolean": case "string": case "symbol": @@ -12873,11 +13506,25 @@ namespace ts { } } + function checkStructExpression(node: StructExpression): Type { + checkStructLikeDeclaration(node); + return getTypeOfSymbol(getSymbolOfNode(node)); + } + function checkClassExpression(node: ClassExpression): Type { checkClassLikeDeclaration(node); return getTypeOfSymbol(getSymbolOfNode(node)); } + function checkStructDeclaration(node: StructDeclaration) { + if (!node.name && !(node.flags & NodeFlags.Default)) { + grammarErrorOnFirstToken(node, Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); + } + checkStructLikeDeclaration(node); + + forEach(node.members, checkSourceElement); + } + function checkClassDeclaration(node: ClassDeclaration) { if (!node.name && !(node.flags & NodeFlags.Default)) { grammarErrorOnFirstToken(node, Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); @@ -12887,6 +13534,83 @@ namespace ts { forEach(node.members, checkSourceElement); } + function checkStructLikeDeclaration(node: StructLikeDeclaration) { + checkGrammarStructDeclarationHeritageClauses(node); + checkDecorators(node); + if (node.name) { + checkTypeNameIsReserved(node.name, Diagnostics.Class_name_cannot_be_0); + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + } + checkTypeParameters(node.typeParameters); + checkExportsOnMergedDeclarations(node); + let symbol = getSymbolOfNode(node); + let type = getDeclaredTypeOfSymbol(symbol); + let typeWithThis = getTypeWithThisArgument(type); + let staticType = getTypeOfSymbol(symbol); + + let baseTypeNode = getStructExtendsHeritageClauseElement(node); + if (baseTypeNode) { + emitExtends = emitExtends || !isInAmbientContext(node); + let baseTypes = getBaseTypes(type); + if (baseTypes.length && produceDiagnostics) { + let baseType = baseTypes[0]; + let staticBaseType = getBaseConstructorTypeOfStruct(type); + checkSourceElement(baseTypeNode.expression); + if (baseTypeNode.typeArguments) { + forEach(baseTypeNode.typeArguments, checkSourceElement); + for (let constructor of getConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments)) { + if (!checkTypeArgumentConstraints(constructor.typeParameters, baseTypeNode.typeArguments)) { + break; + } + } + } + checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType, type.thisType), node.name || node, Diagnostics.Class_0_incorrectly_extends_base_class_1); + checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node, + Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); + + if (!(staticBaseType.symbol && staticBaseType.symbol.flags & SymbolFlags.Struct)) { + // When the static base type is a "class-like" constructor function (but not actually a class), we verify + // that all instantiated base constructor signatures return the same type. We can simply compare the type + // references (as opposed to checking the structure of the types) because elsewhere we have already checked + // that the base type is a class or interface type (and not, for example, an anonymous object type). + let constructors = getInstantiatedConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments); + if (forEach(constructors, sig => getReturnTypeOfSignature(sig) !== baseType)) { + error(baseTypeNode.expression, Diagnostics.Base_constructors_must_all_have_the_same_return_type); + } + } + checkKindsOfPropertyMemberOverrides(type, baseType); + } + } + + let implementedTypeNodes = getStructImplementsHeritageClauseElements(node); + if (implementedTypeNodes) { + for (let typeRefNode of implementedTypeNodes) { + if (!isSupportedExpressionWithTypeArguments(typeRefNode)) { + error(typeRefNode.expression, Diagnostics.A_class_can_only_implement_an_identifier_Slashqualified_name_with_optional_type_arguments); + } + checkTypeReferenceNode(typeRefNode); + if (produceDiagnostics) { + let t = getTypeFromTypeNode(typeRefNode); + if (t !== unknownType) { + let declaredType = (t.flags & TypeFlags.Reference) ? (t).target : t; + if (declaredType.flags & (TypeFlags.Struct | TypeFlags.Interface)) { + checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(t, type.thisType), node.name || node, Diagnostics.Class_0_incorrectly_implements_interface_1); + } + else { + error(typeRefNode, Diagnostics.A_class_may_only_implement_another_class_or_interface); + } + } + } + } + } + + if (produceDiagnostics) { + checkIndexConstraints(type); + checkTypeForDuplicateIndexSignatures(node); + } + } + function checkClassLikeDeclaration(node: ClassLikeDeclaration) { checkGrammarClassDeclarationHeritageClauses(node); checkDecorators(node); @@ -13925,6 +14649,10 @@ namespace ts { forEach((node).members, checkSourceElement); forEachChild(node, checkFunctionAndClassExpressionBodies); break; + case SyntaxKind.StructExpression: + forEach((node).members, checkSourceElement); + forEachChild(node, checkFunctionAndClassExpressionBodies); + break; case SyntaxKind.MethodDeclaration: case SyntaxKind.MethodSignature: forEach(node.decorators, checkFunctionAndClassExpressionBodies); @@ -14051,10 +14779,20 @@ namespace ts { potentialThisCollisions.length = 0; } + if(node.helperStatements !== undefined) { + while(node.helperStatements.length > 0){ + node.statements.unshift(node.helperStatements.pop()); + } + } + if (emitExtends) { links.flags |= NodeCheckFlags.EmitExtends; } + if (emitMathFround) { + links.flags |= NodeCheckFlags.EmitMathFround; + } + if (emitDecorate) { links.flags |= NodeCheckFlags.EmitDecorate; } @@ -14155,6 +14893,7 @@ namespace ts { copySymbols(getSymbolOfNode(location).exports, meaning & SymbolFlags.EnumMember); break; case SyntaxKind.ClassExpression: + case SyntaxKind.StructExpression: let className = (location).name; if (className) { copySymbol(location.symbol, meaning); @@ -14162,6 +14901,7 @@ namespace ts { // fall through; this fall-through is necessary because we would like to handle // type parameter inside class expression similar to how we handle it in classDeclaration and interface Declaration case SyntaxKind.ClassDeclaration: + case SyntaxKind.StructDeclaration: case SyntaxKind.InterfaceDeclaration: // If we didn't come from static member of class or interface, // add the type parameters into the symbol table @@ -14229,6 +14969,7 @@ namespace ts { switch (node.kind) { case SyntaxKind.TypeParameter: case SyntaxKind.ClassDeclaration: + case SyntaxKind.StructDeclaration: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.TypeAliasDeclaration: case SyntaxKind.EnumDeclaration: @@ -14792,6 +15533,38 @@ namespace ts { } } + function writeStructProperty(node: ClassElement, writer: EmitTextWriter): boolean { + let symbol = getSymbolOfNode(node); + let type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature)) + ? getTypeOfSymbol(symbol) + : unknownType; + + if (type.flags & TypeFlags.I32) { + writer.write("TypedObject.int32"); + } else if (type.flags & TypeFlags.U32) { + writer.write("TypedObject.uint32"); + } else if (type.flags & TypeFlags.U16) { + writer.write("TypedObject.uint16"); + } else if (type.flags & TypeFlags.I16) { + writer.write("TypedObject.int16"); + } else if (type.flags & TypeFlags.U8) { + writer.write("TypedObject.uint8"); + } else if (type.flags & TypeFlags.I8) { + writer.write("TypedObject.int8"); + } else if (type.flags & TypeFlags.Float) { + writer.write("TypedObject.float32"); + } else if (type.flags & TypeFlags.Number) { + writer.write("TypedObject.float64"); + } else if (type.flags & TypeFlags.String) { + writer.write("TypedObject.string"); + } else if (type.flags & TypeFlags.Any) { + writer.write("TypedObject.Any"); + } else { + writer.write("TypedObject.Object"); + } + return true; + } + function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter) { // Get type of the symbol if this is the valid symbol otherwise get type at location let symbol = getSymbolOfNode(declaration); @@ -14855,6 +15628,7 @@ namespace ts { isTopLevelValueImportEqualsWithEntityName, isDeclarationVisible, isImplementationOfOverload, + writeStructProperty, writeTypeOfDeclaration, writeReturnTypeOfSignatureDeclaration, writeTypeOfExpression, @@ -14886,6 +15660,7 @@ namespace ts { getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments"); getSymbolLinks(unknownSymbol).type = unknownType; globals[undefinedSymbol.name] = undefinedSymbol; + // Initialize special types globalArrayType = getGlobalType("Array", /*arity*/ 1); globalObjectType = getGlobalType("Object"); @@ -14894,6 +15669,7 @@ namespace ts { globalNumberType = getGlobalType("Number"); globalBooleanType = getGlobalType("Boolean"); globalRegExpType = getGlobalType("RegExp"); + jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element); getGlobalClassDecoratorType = memoize(() => getGlobalType("ClassDecorator")); getGlobalPropertyDecoratorType = memoize(() => getGlobalType("PropertyDecorator")); @@ -14996,6 +15772,7 @@ namespace ts { return grammarErrorOnFirstToken(node, Diagnostics.Modifiers_cannot_appear_here); } break; + case SyntaxKind.StructDeclaration: case SyntaxKind.ClassDeclaration: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.VariableStatement: @@ -15375,6 +16152,42 @@ namespace ts { } } + function checkGrammarStructDeclarationHeritageClauses(node: StructLikeDeclaration) { + let seenExtendsClause = false; + let seenImplementsClause = false; + + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && node.heritageClauses) { + for (let heritageClause of node.heritageClauses) { + if (heritageClause.token === SyntaxKind.ExtendsKeyword) { + if (seenExtendsClause) { + return grammarErrorOnFirstToken(heritageClause, Diagnostics.extends_clause_already_seen); + } + + if (seenImplementsClause) { + return grammarErrorOnFirstToken(heritageClause, Diagnostics.extends_clause_must_precede_implements_clause); + } + + if (heritageClause.types.length > 1) { + return grammarErrorOnFirstToken(heritageClause.types[1], Diagnostics.Classes_can_only_extend_a_single_class); + } + + seenExtendsClause = true; + } + else { + Debug.assert(heritageClause.token === SyntaxKind.ImplementsKeyword); + if (seenImplementsClause) { + return grammarErrorOnFirstToken(heritageClause, Diagnostics.implements_clause_already_seen); + } + + seenImplementsClause = true; + } + + // Grammar checking heritageClause inside class declaration + checkGrammarHeritageClause(heritageClause); + } + } + } + function checkGrammarClassDeclarationHeritageClauses(node: ClassLikeDeclaration) { let seenExtendsClause = false; let seenImplementsClause = false; @@ -15547,7 +16360,7 @@ namespace ts { } } - function checkGrammarJsxElement(node: JsxOpeningLikeElement) { + function checkGrammarJsxElement(node: JsxOpeningElement|JsxSelfClosingElement) { const seen: Map = {}; for (let attr of node.attributes) { if (attr.kind === SyntaxKind.JsxSpreadAttribute) { diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 0c561ab06d881..acf0474b7bfb0 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -410,19 +410,63 @@ namespace ts { /** * Parse the contents of a config file (tsconfig.json). * @param json The contents of the config file to parse - * @param host Instance of ParseConfigHost used to enumerate files in folder. * @param basePath A root directory to resolve relative path entries in the config * file to. e.g. outDir */ export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string): ParsedCommandLine { - let { options, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath); + let errors: Diagnostic[] = []; return { - options, + options: getCompilerOptions(), fileNames: getFileNames(), errors }; + function getCompilerOptions(): CompilerOptions { + let options: CompilerOptions = {}; + let optionNameMap: Map = {}; + forEach(optionDeclarations, option => { + optionNameMap[option.name] = option; + }); + let jsonOptions = json["compilerOptions"]; + if (jsonOptions) { + for (let id in jsonOptions) { + if (hasProperty(optionNameMap, id)) { + let opt = optionNameMap[id]; + let optType = opt.type; + let value = jsonOptions[id]; + let expectedType = typeof optType === "string" ? optType : "string"; + if (typeof value === expectedType) { + if (typeof optType !== "string") { + let key = value.toLowerCase(); + if (hasProperty(optType, key)) { + value = optType[key]; + } + else { + errors.push(createCompilerDiagnostic((opt).error)); + value = 0; + } + } + if (opt.isFilePath) { + value = normalizePath(combinePaths(basePath, value)); + if (value === "") { + value = "."; + } + } + options[opt.name] = value; + } + else { + errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, id, expectedType)); + } + } + else { + errors.push(createCompilerDiagnostic(Diagnostics.Unknown_compiler_option_0, id)); + } + } + } + return options; + } + function getFileNames(): string[] { let fileNames: string[] = []; if (hasProperty(json, "files")) { @@ -457,51 +501,4 @@ namespace ts { return fileNames; } } - - export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string): { options: CompilerOptions, errors: Diagnostic[] } { - let options: CompilerOptions = {}; - let errors: Diagnostic[] = []; - - if (!jsonOptions) { - return { options, errors }; - } - - let optionNameMap = arrayToMap(optionDeclarations, opt => opt.name); - - for (let id in jsonOptions) { - if (hasProperty(optionNameMap, id)) { - let opt = optionNameMap[id]; - let optType = opt.type; - let value = jsonOptions[id]; - let expectedType = typeof optType === "string" ? optType : "string"; - if (typeof value === expectedType) { - if (typeof optType !== "string") { - let key = value.toLowerCase(); - if (hasProperty(optType, key)) { - value = optType[key]; - } - else { - errors.push(createCompilerDiagnostic((opt).error)); - value = 0; - } - } - if (opt.isFilePath) { - value = normalizePath(combinePaths(basePath, value)); - if (value === "") { - value = "."; - } - } - options[opt.name] = value; - } - else { - errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, id, expectedType)); - } - } - else { - errors.push(createCompilerDiagnostic(Diagnostics.Unknown_compiler_option_0, id)); - } - } - - return { options, errors }; - } } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 8fd92818c46bd..9a4f5452eed67 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -775,7 +775,7 @@ namespace ts { }; export interface ObjectAllocator { - getNodeConstructor(kind: SyntaxKind): new (pos?: number, end?: number) => Node; + getNodeConstructor(kind: SyntaxKind): new () => Node; getSymbolConstructor(): new (flags: SymbolFlags, name: string) => Symbol; getTypeConstructor(): new (checker: TypeChecker, flags: TypeFlags) => Type; getSignatureConstructor(): new (checker: TypeChecker) => Signature; @@ -796,13 +796,15 @@ namespace ts { export let objectAllocator: ObjectAllocator = { getNodeConstructor: kind => { - function Node(pos: number, end: number) { - this.pos = pos; - this.end = end; - this.flags = NodeFlags.None; - this.parent = undefined; + function Node() { } - Node.prototype = { kind }; + Node.prototype = { + kind: kind, + pos: -1, + end: -1, + flags: 0, + parent: undefined, + }; return Node; }, getSymbolConstructor: () => Symbol, @@ -842,9 +844,9 @@ namespace ts { export function copyListRemovingItem(item: T, list: T[]) { let copiedList: T[] = []; - for (let e of list) { - if (e !== item) { - copiedList.push(e); + for (var i = 0, len = list.length; i < len; i++) { + if (list[i] !== item) { + copiedList.push(list[i]); } } return copiedList; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 188cf23f76c9c..b2c1e6f0d5ec0 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1720,10 +1720,6 @@ "category": "Error", "code": 2656 }, - "JSX expressions must have one parent element": { - "category": "Error", - "code": 2657 - }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", "code": 4000 diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 32f40e7f18083..7a0ea3eb9ecdf 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -282,6 +282,18 @@ var __extends = (this && this.__extends) || function (d, b) { d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); };`; + const froundHelper = ` +if (Math.fround === undefined) { + Math.fround = function(x) { + if (isNaN(x)) return NaN; + x = Number(x); + if (x === 0) return x; + if (!isFinite(x)) return x; + return new Float32Array([x])[0]; + } +} + `; + // emit output for the __decorate helper function const decorateHelper = ` var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { @@ -397,6 +409,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi let computedPropertyNamesToGeneratedNames: string[]; let extendsEmitted = false; + let froundEmitted = false; let decorateEmitted = false; let paramEmitted = false; let awaiterEmitted = false; @@ -1404,10 +1417,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emit(span.literal); } - function jsxEmitReact(node: JsxElement | JsxSelfClosingElement) { + function jsxEmitReact(node: JsxElement|JsxSelfClosingElement) { /// Emit a tag name, which is either '"div"' for lower-cased names, or /// 'Div' for upper-cased or dotted names - function emitTagName(name: Identifier | QualifiedName) { + function emitTagName(name: Identifier|QualifiedName) { if (name.kind === SyntaxKind.Identifier && isIntrinsicJsxName((name).text)) { write("\""); emit(name); @@ -1557,7 +1570,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } } - function jsxEmitPreserve(node: JsxElement | JsxSelfClosingElement) { + function jsxEmitPreserve(node: JsxElement|JsxSelfClosingElement) { function emitJsxAttribute(node: JsxAttribute) { emit(node.name); if (node.initializer) { @@ -1572,7 +1585,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi write("}"); } - function emitAttributes(attribs: NodeArray) { + function emitAttributes(attribs: NodeArray) { for (let i = 0, n = attribs.length; i < n; i++) { if (i > 0) { write(" "); @@ -1588,7 +1601,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } } - function emitJsxOpeningOrSelfClosingElement(node: JsxOpeningElement | JsxSelfClosingElement) { + function emitJsxOpeningOrSelfClosingElement(node: JsxOpeningElement|JsxSelfClosingElement) { write("<"); emit(node.tagName); if (node.attributes.length > 0 || (node.kind === SyntaxKind.JsxSelfClosingElement)) { @@ -1769,40 +1782,35 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi write("."); } } - else { - if (modulekind !== ModuleKind.ES6) { - let declaration = resolver.getReferencedImportDeclaration(node); - if (declaration) { - if (declaration.kind === SyntaxKind.ImportClause) { - // Identifier references default import - write(getGeneratedNameForNode(declaration.parent)); - write(languageVersion === ScriptTarget.ES3 ? "[\"default\"]" : ".default"); - return; + else if (modulekind !== ModuleKind.ES6) { + let declaration = resolver.getReferencedImportDeclaration(node); + if (declaration) { + if (declaration.kind === SyntaxKind.ImportClause) { + // Identifier references default import + write(getGeneratedNameForNode(declaration.parent)); + write(languageVersion === ScriptTarget.ES3 ? "[\"default\"]" : ".default"); + return; + } + else if (declaration.kind === SyntaxKind.ImportSpecifier) { + // Identifier references named import + write(getGeneratedNameForNode(declaration.parent.parent.parent)); + let name = (declaration).propertyName || (declaration).name; + let identifier = getSourceTextOfNodeFromSourceFile(currentSourceFile, name); + if (languageVersion === ScriptTarget.ES3 && identifier === "default") { + write(`["default"]`); } - else if (declaration.kind === SyntaxKind.ImportSpecifier) { - // Identifier references named import - write(getGeneratedNameForNode(declaration.parent.parent.parent)); - let name = (declaration).propertyName || (declaration).name; - let identifier = getSourceTextOfNodeFromSourceFile(currentSourceFile, name); - if (languageVersion === ScriptTarget.ES3 && identifier === "default") { - write(`["default"]`); - } - else { - write("."); - write(identifier); - } - return; + else { + write("."); + write(identifier); } - } - } - - if (languageVersion !== ScriptTarget.ES6) { - let declaration = resolver.getReferencedNestedRedeclaration(node); - if (declaration) { - write(getGeneratedNameForNode(declaration.name)); return; } } + declaration = resolver.getReferencedNestedRedeclaration(node); + if (declaration) { + write(getGeneratedNameForNode(declaration.name)); + return; + } } if (nodeIsSynthesized(node)) { @@ -2454,10 +2462,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi if (tryEmitConstantValue(node)) { return; } - emit(node.expression); - write("["); - emit(node.argumentExpression); - write("]"); + + if(node.isStructArray) { + write("("); + emit(node.expression); + write("._TO.array(") + emit(node.argumentExpression); + write("))()"); + } else { + emit(node.expression); + write("["); + emit(node.argumentExpression); + write("]"); + } } function hasSpreadElement(elements: Expression[]) { @@ -3325,8 +3342,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } function nodeEndIsOnSameLineAsNodeStart(node1: Node, node2: Node) { - return getLineOfLocalPosition(currentSourceFile, node1.end) === - getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node2.pos)); + let line1 = getLineOfLocalPosition(currentSourceFile, node1.end); + let line2 = getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node2.pos)); + return (line1 === -1 || line2 === -1) ? true : line1 === line2; } function emitCaseOrDefaultClause(node: CaseOrDefaultClause) { @@ -4548,7 +4566,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitTrailingComments(property); } - function emitMemberFunctionsForES5AndLower(node: ClassLikeDeclaration) { + function emitMemberFunctionsForES5AndLower(node: ClassLikeDeclaration, isStruct?: boolean) { forEach(node.members, member => { if (member.kind === SyntaxKind.SemicolonClassElement) { writeLine(); @@ -4563,7 +4581,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitLeadingComments(member); emitStart(member); emitStart((member).name); - emitClassMemberPrefix(node, member); + + if (isStruct !== undefined) { + emitStructMemberPrefix(node, member); + } else { + emitClassMemberPrefix(node, member); + } + emitMemberAccessForPropertyName((member).name); emitEnd((member).name); write(" = "); @@ -4807,6 +4831,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } } + function emitStructExpression(node: StructExpression) { + return emitStructLikeDeclaration(node); + } + + function emitStructDeclaration(node: StructDeclaration) { + return emitStructLikeDeclaration(node); + } + + function emitStructLikeDeclaration(node: StructLikeDeclaration) { + emitStructLikeDeclarationBelowES6(node); + if (modulekind !== ModuleKind.ES6 && node.parent === currentSourceFile && node.name) { + emitExportMemberAssignments(node.name); + } + } + function emitClassLikeDeclarationForES6AndHigher(node: ClassLikeDeclaration) { let thisNodeIsDecorated = nodeIsDecorated(node); if (node.kind === SyntaxKind.ClassDeclaration) { @@ -5054,6 +5093,140 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } } + function writeStructProperty(node: ClassElement) { + emitDeclarationName(node); + write(": "); + + resolver.writeStructProperty(node, writer); + } + + function emitStructProperties(node: StructLikeDeclaration) { + for (var i = 0; i < node.members.length; i++) { + if(node.members[i].kind == SyntaxKind.PropertyDeclaration && !(node.members[i].flags & NodeFlags.Static)) { + increaseIndent(); + + writeStructProperty(node.members[i]); + + if (i < node.members.length - 1) { + write(","); + } + writeLine(); + decreaseIndent(); + } + } + } + + function emitStructLikeDeclarationBelowES6(node: StructLikeDeclaration) { + write("var "); + emitDeclarationName(node); + write(" = (function ("); + write(") {"); + writeLine(); + + increaseIndent(); + + write("var "); + write("_" + node.name.text); + write(" = new TypedObject.StructType({"); + writeLine(); + emitStructProperties(node); + writeLine(); + write("});"); + writeLine(); + + emitConstructorOfStruct(); + emitMemberFunctionsForES5AndLower(node, true); + + writeLine(); + write("return " + node.name.text + ";"); + + decreaseIndent(); + writeLine(); + write("})();"); + writeLine(); + + emitTrailingComments(node); + + function emitConstructorOfStruct() { + var ctor = getFirstConstructorWithBody(node); + if (ctor) { + emitLeadingComments(ctor); + } + + writeLine(); + + write("function _ctor"); + emitSignatureParameters(ctor); + write(" {"); + scopeEmitStart(node, "constructor"); + increaseIndent(); + if (ctor) { + emitDetachedComments((ctor.body).statements); + } + emitCaptureThisForNodeIfNecessary(node); + if (ctor) { + emitDefaultValueAssignments(ctor); + emitRestParameter(ctor); + emitParameterPropertyAssignments(ctor); + } + + //emitMemberAssignments(node, /*nonstatic*/0); + if (ctor) { + var statements: Node[] = (ctor.body).statements; + emitLines(statements); + } + writeLine(); + if (ctor) { + emitLeadingCommentsOfPosition((ctor.body).statements.end); + } + decreaseIndent(); + emitToken(SyntaxKind.CloseBraceToken, ctor ? (ctor.body).statements.end : node.members.end); + scopeEmitEnd(); + emitEnd(ctor || node); + if (ctor) { + emitTrailingComments(ctor); + } + writeLine(); + + emitStart(ctor || node); + + write("function " + node.name.text); + emitSignatureParameters(ctor); + write(" {"); + writeLine(); + increaseIndent(); + + writeLine(); + write("var obj = new _"); + write(node.name.text + "();"); + writeLine(); + + write("_ctor.call(obj"); + + if (ctor) { + if(ctor.parameters.length > 0) write(" ,"); + } + + write(");"); + writeLine(); + write("return obj;"); + writeLine(); + decreaseIndent(); + write("}"); + + writeLine(); + write(node.name.text + "._TO = _" + node.name.text + ";"); + } + } + + function emitStructMemberPrefix(node: StructLikeDeclaration, member: Node) { + write("_"); + emitDeclarationName(node); + if (!(member.flags & NodeFlags.Static)) { + write(".prototype"); + } + } + function emitClassMemberPrefix(node: ClassLikeDeclaration, member: Node) { emitDeclarationName(node); if (!(member.flags & NodeFlags.Static)) { @@ -7078,6 +7251,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi extendsEmitted = true; } + if (!froundEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitMathFround) { + writeLines(froundHelper); + froundEmitted = true; + } + if (!decorateEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitDecorate) { writeLines(decorateHelper); if (compilerOptions.emitDecoratorMetadata) { @@ -7246,7 +7424,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi return emitTemplateSpan(node); case SyntaxKind.JsxElement: case SyntaxKind.JsxSelfClosingElement: - return emitJsxElement(node); + return emitJsxElement(node); case SyntaxKind.JsxText: return emitJsxText(node); case SyntaxKind.JsxExpression: @@ -7359,6 +7537,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi return emitClassExpression(node); case SyntaxKind.ClassDeclaration: return emitClassDeclaration(node); + case SyntaxKind.StructExpression: + return emitStructExpression(node); + case SyntaxKind.StructDeclaration: + return emitStructDeclaration(node); case SyntaxKind.InterfaceDeclaration: return emitInterfaceDeclaration(node); case SyntaxKind.EnumDeclaration: diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 3e93e31340d40..8d15c78ad389b 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2,15 +2,15 @@ /// namespace ts { - let nodeConstructors = new Array Node>(SyntaxKind.Count); + let nodeConstructors = new Array Node>(SyntaxKind.Count); /* @internal */ export let parseTime = 0; - export function getNodeConstructor(kind: SyntaxKind): new (pos?: number, end?: number) => Node { + export function getNodeConstructor(kind: SyntaxKind): new () => Node { return nodeConstructors[kind] || (nodeConstructors[kind] = objectAllocator.getNodeConstructor(kind)); } - export function createNode(kind: SyntaxKind, pos?: number, end?: number): Node { - return new (getNodeConstructor(kind))(pos, end); + export function createNode(kind: SyntaxKind): Node { + return new (getNodeConstructor(kind))(); } function visitNode(cbNode: (node: Node) => T, node: Node): T { @@ -259,6 +259,14 @@ namespace ts { visitNodes(cbNodes, (node).typeParameters) || visitNodes(cbNodes, (node).heritageClauses) || visitNodes(cbNodes, (node).members); + case SyntaxKind.StructDeclaration: + case SyntaxKind.StructExpression: + return visitNodes(cbNodes, node.decorators) || + visitNodes(cbNodes, node.modifiers) || + visitNode(cbNode, (node).name) || + visitNodes(cbNodes, (node).typeParameters) || + visitNodes(cbNodes, (node).heritageClauses) || + visitNodes(cbNodes, (node).members); case SyntaxKind.InterfaceDeclaration: return visitNodes(cbNodes, node.decorators) || visitNodes(cbNodes, node.modifiers) || @@ -993,10 +1001,14 @@ namespace ts { function createNode(kind: SyntaxKind, pos?: number): Node { nodeCount++; + let node = new (nodeConstructors[kind] || (nodeConstructors[kind] = objectAllocator.getNodeConstructor(kind)))(); if (!(pos >= 0)) { pos = scanner.getStartPos(); } - return new (nodeConstructors[kind] || (nodeConstructors[kind] = objectAllocator.getNodeConstructor(kind)))(pos, pos); + + node.pos = pos; + node.end = pos; + return node; } function finishNode(node: T, end?: number): T { @@ -1151,7 +1163,7 @@ namespace ts { function nextTokenIsClassOrFunction(): boolean { nextToken(); - return token === SyntaxKind.ClassKeyword || token === SyntaxKind.FunctionKeyword; + return token === SyntaxKind.ClassKeyword || token === SyntaxKind.StructKeyword || token === SyntaxKind.FunctionKeyword; } // True if positioned at the start of a list element @@ -1176,6 +1188,8 @@ namespace ts { return token === SyntaxKind.CaseKeyword || token === SyntaxKind.DefaultKeyword; case ParsingContext.TypeMembers: return isStartOfTypeMember(); + case ParsingContext.StructMembers: + return lookAhead(isStructMemberStart); case ParsingContext.ClassMembers: // We allow semicolons as class elements (as specified by ES6) as long as we're // not in error recovery. If we're in error recovery, we don't want an errant @@ -1294,6 +1308,7 @@ namespace ts { case ParsingContext.SwitchClauses: case ParsingContext.TypeMembers: case ParsingContext.ClassMembers: + case ParsingContext.StructMembers: case ParsingContext.EnumMembers: case ParsingContext.ObjectLiteralMembers: case ParsingContext.ObjectBindingElements: @@ -1482,6 +1497,9 @@ namespace ts { case ParsingContext.ClassMembers: return isReusableClassMember(node); + case ParsingContext.StructMembers: + return isReusableStructMember(node); + case ParsingContext.SwitchClauses: return isReusableSwitchClause(node); @@ -1579,6 +1597,31 @@ namespace ts { return false; } + function isReusableStructMember(node: Node) { + if (node) { + switch (node.kind) { + case SyntaxKind.Constructor: + case SyntaxKind.IndexSignature: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + case SyntaxKind.PropertyDeclaration: + case SyntaxKind.SemicolonClassElement: + return true; + case SyntaxKind.MethodDeclaration: + // Method declarations are not necessarily reusable. An object-literal + // may have a method calls "constructor(...)" and we must reparse that + // into an actual .ConstructorDeclaration. + let methodDeclaration = node; + let nameIsConstructor = methodDeclaration.name.kind === SyntaxKind.Identifier && + (methodDeclaration.name).originalKeywordKind === SyntaxKind.ConstructorKeyword; + + return !nameIsConstructor; + } + } + + return false; + } + function isReusableSwitchClause(node: Node) { if (node) { switch (node.kind) { @@ -1620,6 +1663,7 @@ namespace ts { case SyntaxKind.ExportAssignment: case SyntaxKind.ModuleDeclaration: case SyntaxKind.ClassDeclaration: + case SyntaxKind.StructDeclaration: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.EnumDeclaration: case SyntaxKind.TypeAliasDeclaration: @@ -1701,6 +1745,7 @@ namespace ts { case ParsingContext.SwitchClauseStatements: return Diagnostics.Statement_expected; case ParsingContext.TypeMembers: return Diagnostics.Property_or_signature_expected; case ParsingContext.ClassMembers: return Diagnostics.Unexpected_token_A_constructor_method_accessor_or_property_was_expected; + case ParsingContext.StructMembers: return Diagnostics.Unexpected_token_A_constructor_method_accessor_or_property_was_expected; case ParsingContext.EnumMembers: return Diagnostics.Enum_member_expected; case ParsingContext.HeritageClauseElement: return Diagnostics.Expression_expected; case ParsingContext.VariableDeclarations: return Diagnostics.Variable_declaration_expected; @@ -2365,6 +2410,13 @@ namespace ts { case SyntaxKind.StringKeyword: case SyntaxKind.NumberKeyword: case SyntaxKind.BooleanKeyword: + case SyntaxKind.I8Keyword: + case SyntaxKind.I16Keyword: + case SyntaxKind.I32Keyword: + case SyntaxKind.U8Keyword: + case SyntaxKind.U16Keyword: + case SyntaxKind.U32Keyword: + case SyntaxKind.FloatKeyword: case SyntaxKind.SymbolKeyword: // If these are followed by a dot, then parse these out as a dotted type reference instead. let node = tryParse(parseKeywordAndNoDot); @@ -2390,6 +2442,13 @@ namespace ts { case SyntaxKind.AnyKeyword: case SyntaxKind.StringKeyword: case SyntaxKind.NumberKeyword: + case SyntaxKind.I8Keyword: + case SyntaxKind.I16Keyword: + case SyntaxKind.I32Keyword: + case SyntaxKind.U8Keyword: + case SyntaxKind.U16Keyword: + case SyntaxKind.U32Keyword: + case SyntaxKind.FloatKeyword: case SyntaxKind.BooleanKeyword: case SyntaxKind.SymbolKeyword: case SyntaxKind.VoidKeyword: @@ -2524,6 +2583,7 @@ namespace ts { case SyntaxKind.OpenBraceToken: case SyntaxKind.FunctionKeyword: case SyntaxKind.ClassKeyword: + case SyntaxKind.StructKeyword: case SyntaxKind.NewKeyword: case SyntaxKind.SlashToken: case SyntaxKind.SlashEqualsToken: @@ -2574,6 +2634,7 @@ namespace ts { return token !== SyntaxKind.OpenBraceToken && token !== SyntaxKind.FunctionKeyword && token !== SyntaxKind.ClassKeyword && + token !== SyntaxKind.StructKeyword && token !== SyntaxKind.AtToken && isStartOfExpression(); } @@ -2966,6 +3027,7 @@ namespace ts { if (token !== SyntaxKind.SemicolonToken && token !== SyntaxKind.FunctionKeyword && token !== SyntaxKind.ClassKeyword && + token !== SyntaxKind.StructKeyword && isStartOfStatement() && !isStartOfExpressionStatement()) { // Check if we got a plain statement (i.e. no expression-statements, no function/class expressions/declarations) @@ -3201,7 +3263,7 @@ namespace ts { /** * Parse ES7 unary expression and await expression - * + * * ES7 UnaryExpression: * 1) SimpleUnaryExpression[?yield] * 2) IncrementExpression[?yield] ** UnaryExpression[?yield] @@ -3450,43 +3512,19 @@ namespace ts { function parseJsxElementOrSelfClosingElement(inExpressionContext: boolean): JsxElement | JsxSelfClosingElement { let opening = parseJsxOpeningOrSelfClosingElement(inExpressionContext); - let result: JsxElement | JsxSelfClosingElement; if (opening.kind === SyntaxKind.JsxOpeningElement) { let node = createNode(SyntaxKind.JsxElement, opening.pos); node.openingElement = opening; node.children = parseJsxChildren(node.openingElement.tagName); node.closingElement = parseJsxClosingElement(inExpressionContext); - result = finishNode(node); + return finishNode(node); } else { Debug.assert(opening.kind === SyntaxKind.JsxSelfClosingElement); // Nothing else to do for self-closing elements - result = opening; - } - - // If the user writes the invalid code '
' in an expression context (i.e. not wrapped in - // an enclosing tag), we'll naively try to parse ^ this as a 'less than' operator and the remainder of the tag - // as garbage, which will cause the formatter to badly mangle the JSX. Perform a speculative parse of a JSX - // element if we see a < token so that we can wrap it in a synthetic binary expression so the formatter - // does less damage and we can report a better error. - // Since JSX elements are invalid < operands anyway, this lookahead parse will only occur in error scenarios - // of one sort or another. - if (inExpressionContext && token === SyntaxKind.LessThanToken) { - let invalidElement = tryParse(() => parseJsxElementOrSelfClosingElement(/*inExpressionContext*/true)); - if (invalidElement) { - parseErrorAtCurrentToken(Diagnostics.JSX_expressions_must_have_one_parent_element); - let badNode = createNode(SyntaxKind.BinaryExpression, result.pos); - badNode.end = invalidElement.end; - badNode.left = result; - badNode.right = invalidElement; - badNode.operatorToken = createMissingNode(SyntaxKind.CommaToken, /*reportAtCurrentPosition*/ false, /*diagnosticMessage*/ undefined); - badNode.operatorToken.pos = badNode.operatorToken.end = badNode.right.pos; - return badNode; - } + return opening; } - - return result; } function parseJsxText(): JsxText { @@ -3532,7 +3570,7 @@ namespace ts { return result; } - function parseJsxOpeningOrSelfClosingElement(inExpressionContext: boolean): JsxOpeningElement | JsxSelfClosingElement { + function parseJsxOpeningOrSelfClosingElement(inExpressionContext: boolean): JsxOpeningElement|JsxSelfClosingElement { let fullStart = scanner.getStartPos(); parseExpected(SyntaxKind.LessThanToken); @@ -3823,6 +3861,8 @@ namespace ts { return parseFunctionExpression(); case SyntaxKind.ClassKeyword: return parseClassExpression(); + case SyntaxKind.StructKeyword: + return parseStructExpression(); case SyntaxKind.FunctionKeyword: return parseFunctionExpression(); case SyntaxKind.NewKeyword: @@ -4295,6 +4335,7 @@ namespace ts { case SyntaxKind.ConstKeyword: case SyntaxKind.FunctionKeyword: case SyntaxKind.ClassKeyword: + case SyntaxKind.StructKeyword: case SyntaxKind.EnumKeyword: return true; @@ -4372,6 +4413,7 @@ namespace ts { case SyntaxKind.LetKeyword: case SyntaxKind.FunctionKeyword: case SyntaxKind.ClassKeyword: + case SyntaxKind.StructKeyword: case SyntaxKind.EnumKeyword: case SyntaxKind.IfKeyword: case SyntaxKind.DoKeyword: @@ -4446,6 +4488,8 @@ namespace ts { return parseFunctionDeclaration(scanner.getStartPos(), /*decorators*/ undefined, /*modifiers*/ undefined); case SyntaxKind.ClassKeyword: return parseClassDeclaration(scanner.getStartPos(), /*decorators*/ undefined, /*modifiers*/ undefined); + case SyntaxKind.StructKeyword: + return parseStructDeclaration(scanner.getStartPos(), /*decorators*/ undefined, /*modifiers*/ undefined); case SyntaxKind.IfKeyword: return parseIfStatement(); case SyntaxKind.DoKeyword: @@ -4511,6 +4555,8 @@ namespace ts { return parseFunctionDeclaration(fullStart, decorators, modifiers); case SyntaxKind.ClassKeyword: return parseClassDeclaration(fullStart, decorators, modifiers); + case SyntaxKind.StructKeyword: + return parseStructDeclaration(fullStart, decorators, modifiers); case SyntaxKind.InterfaceKeyword: return parseInterfaceDeclaration(fullStart, decorators, modifiers); case SyntaxKind.TypeKeyword: @@ -4851,6 +4897,74 @@ namespace ts { return false; } + function isStructMemberStart(): boolean { + let idToken: SyntaxKind; + + if (token === SyntaxKind.AtToken) { + return true; + } + + // Eat up all modifiers, but hold on to the last one in case it is actually an identifier. + while (isModifier(token)) { + idToken = token; + // If the idToken is a class modifier (protected, private, public, and static), it is + // certain that we are starting to parse class member. This allows better error recovery + // Example: + // public foo() ... // true + // public @dec blah ... // true; we will then report an error later + // export public ... // true; we will then report an error later + if (isClassMemberModifier(idToken)) { + return true; + } + + nextToken(); + } + + if (token === SyntaxKind.AsteriskToken) { + return true; + } + + // Try to get the first property-like token following all modifiers. + // This can either be an identifier or the 'get' or 'set' keywords. + if (isLiteralPropertyName()) { + idToken = token; + nextToken(); + } + + // Index signatures and computed properties are class members; we can parse. + if (token === SyntaxKind.OpenBracketToken) { + return true; + } + + // If we were able to get any potential identifier... + if (idToken !== undefined) { + // If we have a non-keyword identifier, or if we have an accessor, then it's safe to parse. + if (!isKeyword(idToken) || idToken === SyntaxKind.SetKeyword || idToken === SyntaxKind.GetKeyword) { + return true; + } + + // If it *is* a keyword, but not an accessor, check a little farther along + // to see if it should actually be parsed as a class member. + switch (token) { + case SyntaxKind.OpenParenToken: // Method declaration + case SyntaxKind.LessThanToken: // Generic Method declaration + case SyntaxKind.ColonToken: // Type Annotation for declaration + case SyntaxKind.EqualsToken: // Initializer for declaration + case SyntaxKind.QuestionToken: // Not valid, but permitted so that it gets caught later on. + return true; + default: + // Covers + // - Semicolons (declaration termination) + // - Closing braces (end-of-class, must be declaration) + // - End-of-files (not valid, but permitted so that it gets caught later on) + // - Line-breaks (enabling *automatic semicolon insertion*) + return canParseSemicolon(); + } + } + + return false; + } + function parseDecorators(): NodeArray { let decorators: NodeArray; while (true) { @@ -4971,10 +5085,22 @@ namespace ts { SyntaxKind.ClassExpression); } + function parseStructExpression(): StructExpression { + return parseStructDeclarationOrExpression( + /*fullStart*/ scanner.getStartPos(), + /*decorators*/ undefined, + /*modifiers*/ undefined, + SyntaxKind.ClassExpression); + } + function parseClassDeclaration(fullStart: number, decorators: NodeArray, modifiers: ModifiersArray): ClassDeclaration { return parseClassDeclarationOrExpression(fullStart, decorators, modifiers, SyntaxKind.ClassDeclaration); } + function parseStructDeclaration(fullStart: number, decorators: NodeArray, modifiers: ModifiersArray): StructDeclaration { + return parseStructDeclarationOrExpression(fullStart, decorators, modifiers, SyntaxKind.StructDeclaration); + } + function parseClassDeclarationOrExpression(fullStart: number, decorators: NodeArray, modifiers: ModifiersArray, kind: SyntaxKind): ClassLikeDeclaration { let node = createNode(kind, fullStart); node.decorators = decorators; @@ -4997,12 +5123,45 @@ namespace ts { return finishNode(node); } + function parseStructDeclarationOrExpression(fullStart: number, decorators: NodeArray, modifiers: ModifiersArray, kind: SyntaxKind): StructLikeDeclaration { + let node = createNode(kind, fullStart); + node.decorators = decorators; + setModifiers(node, modifiers); + parseExpected(SyntaxKind.StructKeyword); + node.name = parseNameOfStructDeclarationOrExpression(); + node.typeParameters = parseTypeParameters(); + node.heritageClauses = parseHeritageClauses(/*isClassHeritageClause*/ true); + + if (parseExpected(SyntaxKind.OpenBraceToken)) { + // ClassTail[Yield,Await] : (Modified) See 14.5 + // ClassHeritage[?Yield,?Await]opt { ClassBody[?Yield,?Await]opt } + node.members = parseClassMembers(); + parseExpected(SyntaxKind.CloseBraceToken); + } + else { + node.members = createMissingList(); + } + + return finishNode(node); + } + function parseNameOfClassDeclarationOrExpression(): Identifier { // implements is a future reserved word so // 'class implements' might mean either // - class expression with omitted name, 'implements' starts heritage clause - // - class with name 'implements' - // 'isImplementsClause' helps to disambiguate between these two cases + // - class with name 'implements' + // 'isImplementsClause' helps to disambiguate between these two cases + return isIdentifier() && !isImplementsClause() + ? parseIdentifier() + : undefined; + } + + function parseNameOfStructDeclarationOrExpression(): Identifier { + // implements is a future reserved word so + // 'struct implements' might mean either + // - struct expression with omitted name, 'implements' starts heritage clause + // - struct with name 'implements' + // 'isImplementsClause' helps to disambiguate between these two cases return isIdentifier() && !isImplementsClause() ? parseIdentifier() : undefined; @@ -5457,6 +5616,7 @@ namespace ts { SwitchClauseStatements, // Statements in switch clause TypeMembers, // Members in interface or type literal ClassMembers, // Members in class declaration + StructMembers, // Members in struct declaration EnumMembers, // Members in enum declaration HeritageClauseElement, // Elements in a heritage clause VariableDeclarations, // Variable declarations in variable statement @@ -5612,6 +5772,13 @@ namespace ts { case SyntaxKind.StringKeyword: case SyntaxKind.NumberKeyword: case SyntaxKind.BooleanKeyword: + case SyntaxKind.I8Keyword: + case SyntaxKind.I16Keyword: + case SyntaxKind.I32Keyword: + case SyntaxKind.U8Keyword: + case SyntaxKind.U16Keyword: + case SyntaxKind.U32Keyword: + case SyntaxKind.FloatKeyword: case SyntaxKind.SymbolKeyword: case SyntaxKind.VoidKeyword: return parseTokenNode(); diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index a827d75530282..d2f33428f45fd 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -72,10 +72,14 @@ namespace ts { "extends": SyntaxKind.ExtendsKeyword, "false": SyntaxKind.FalseKeyword, "finally": SyntaxKind.FinallyKeyword, + "float": SyntaxKind.FloatKeyword, "for": SyntaxKind.ForKeyword, "from": SyntaxKind.FromKeyword, "function": SyntaxKind.FunctionKeyword, "get": SyntaxKind.GetKeyword, + "i8" : SyntaxKind.I8Keyword, + "i16" : SyntaxKind.I16Keyword, + "i32" : SyntaxKind.I32Keyword, "if": SyntaxKind.IfKeyword, "implements": SyntaxKind.ImplementsKeyword, "import": SyntaxKind.ImportKeyword, @@ -98,6 +102,7 @@ namespace ts { "set": SyntaxKind.SetKeyword, "static": SyntaxKind.StaticKeyword, "string": SyntaxKind.StringKeyword, + "struct": SyntaxKind.StructKeyword, "super": SyntaxKind.SuperKeyword, "switch": SyntaxKind.SwitchKeyword, "symbol": SyntaxKind.SymbolKeyword, @@ -107,6 +112,9 @@ namespace ts { "try": SyntaxKind.TryKeyword, "type": SyntaxKind.TypeKeyword, "typeof": SyntaxKind.TypeOfKeyword, + "u8" : SyntaxKind.U8Keyword, + "u16" : SyntaxKind.U16Keyword, + "u32" : SyntaxKind.U32Keyword, "var": SyntaxKind.VarKeyword, "void": SyntaxKind.VoidKeyword, "while": SyntaxKind.WhileKeyword, @@ -340,7 +348,6 @@ namespace ts { // We want the index of the previous line start, so we subtract 1. // Review 2's-complement if this is confusing. lineNumber = ~lineNumber - 1; - Debug.assert(lineNumber !== -1, "position cannot precede the beginning of the file"); } return { line: lineNumber, @@ -567,12 +574,12 @@ namespace ts { } /** - * Extract comments from text prefixing the token closest following `pos`. + * Extract comments from text prefixing the token closest following `pos`. * The return value is an array containing a TextRange for each comment. * Single-line comment ranges include the beginning '//' characters but not the ending line break. * Multi - line comment ranges include the beginning '/* and ending '/' characters. * The return value is undefined if no comments were found. - * @param trailing + * @param trailing * If false, whitespace is skipped until the first line break and comments between that location * and the next token are returned. * If true, comments occurring between the given position and the next line break are returned. diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 3eeb126c065b6..0872a2e5ba51b 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -407,7 +407,7 @@ namespace ts { // (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643) return _fs.watch( path, - { persistent: true, recursive: !!recursive }, + { persisten: true, recursive: !!recursive }, (eventName: string, relativeFileName: string) => { // In watchDirectory we only care about adding and removing files (when event name is // "rename"); changes made within files are handled by corresponding fileWatchers (when diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index e51fda074c51d..9d79d435a04aa 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -360,8 +360,7 @@ namespace ts { let newFileNames = ts.map(parsedCommandLine.fileNames, compilerHost.getCanonicalFileName); let canonicalRootFileNames = ts.map(rootFileNames, compilerHost.getCanonicalFileName); - // We check if the project file list has changed. If so, we just throw away the old program and start fresh. - if (!arrayIsEqualTo(newFileNames && newFileNames.sort(), canonicalRootFileNames && canonicalRootFileNames.sort())) { + if (!arrayStructurallyIsEqualTo(newFileNames, canonicalRootFileNames)) { setCachedProgram(undefined); startTimerForRecompilation(); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 8e0338696dde9..c32444a80f307 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -167,6 +167,15 @@ namespace ts { FromKeyword, OfKeyword, // LastKeyword and LastToken + // Primitive types + I8Keyword, + U8Keyword, + I16Keyword, + U16Keyword, + I32Keyword, + U32Keyword, + FloatKeyword, + // Parse tree nodes // Names @@ -227,6 +236,7 @@ namespace ts { YieldExpression, SpreadElementExpression, ClassExpression, + StructExpression, OmittedExpression, ExpressionWithTypeArguments, AsExpression, @@ -248,6 +258,7 @@ namespace ts { ContinueStatement, BreakStatement, ReturnStatement, + StructKeyword, WithStatement, SwitchStatement, LabeledStatement, @@ -257,6 +268,7 @@ namespace ts { VariableDeclaration, VariableDeclarationList, FunctionDeclaration, + StructDeclaration, ClassDeclaration, InterfaceDeclaration, TypeAliasDeclaration, @@ -361,7 +373,6 @@ namespace ts { } export const enum NodeFlags { - None = 0, Export = 0x00000001, // Declarations Ambient = 0x00000002, // Declarations Public = 0x00000010, // Property/Method @@ -724,7 +735,7 @@ namespace ts { } export interface PostfixUnaryExpression extends IncrementExpression { - operand: LeftHandSideExpression; + operand: Expression; operator: SyntaxKind; } @@ -835,6 +846,7 @@ namespace ts { export interface ElementAccessExpression extends MemberExpression { expression: LeftHandSideExpression; argumentExpression?: Expression; + isStructArray?: boolean; } export interface CallExpression extends LeftHandSideExpression { @@ -921,6 +933,8 @@ namespace ts { export interface Block extends Statement { statements: NodeArray; + helperStatements?: NodeArray; + helperVarCount? : number; } export interface VariableStatement extends Statement { @@ -1035,6 +1049,23 @@ namespace ts { _classElementBrand: any; } + export interface StructLikeDeclaration extends Declaration { + name?: Identifier; + typeParameters?: NodeArray; + heritageClauses?: NodeArray; + members: NodeArray; + } + + export interface StructDeclaration extends StructLikeDeclaration, Statement { + } + + export interface StructExpression extends StructLikeDeclaration, PrimaryExpression { + } + + export interface StructElement extends Declaration { + _structElementBrand: any; + } + export interface InterfaceDeclaration extends Declaration, Statement { name: Identifier; typeParameters?: NodeArray; @@ -1249,6 +1280,9 @@ namespace ts { statements: NodeArray; endOfFileToken: Node; + helperStatements?: NodeArray; + helperVarCount? : number; + fileName: string; text: string; @@ -1306,7 +1340,7 @@ namespace ts { getCurrentDirectory(): string; } - export interface ParseConfigHost { + export interface ParseConfigHost extends ModuleResolutionHost { readDirectory(rootDir: string, extension: string, exclude: string[]): string[]; } @@ -1602,6 +1636,7 @@ namespace ts { isDeclarationVisible(node: Declaration): boolean; collectLinkedAliases(node: Identifier): Node[]; isImplementationOfOverload(node: FunctionLikeDeclaration): boolean; + writeStructProperty(node: ClassElement, writer: EmitTextWriter): boolean; writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; writeReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; @@ -1621,6 +1656,7 @@ namespace ts { Property = 0x00000004, // Property or enum member EnumMember = 0x00000008, // Enum member Function = 0x00000010, // Function + Struct = 0x40000000, // Struct Class = 0x00000020, // Class Interface = 0x00000040, // Interface ConstEnum = 0x00000080, // Const enum @@ -1650,8 +1686,8 @@ namespace ts { Enum = RegularEnum | ConstEnum, Variable = FunctionScopedVariable | BlockScopedVariable, - Value = Variable | Property | EnumMember | Function | Class | Enum | ValueModule | Method | GetAccessor | SetAccessor, - Type = Class | Interface | Enum | TypeLiteral | ObjectLiteral | TypeParameter | TypeAlias, + Value = Variable | Property | EnumMember | Function | Struct | Class | Enum | ValueModule | Method | GetAccessor | SetAccessor, + Type = Struct | Class | Interface | Enum | TypeLiteral | ObjectLiteral | TypeParameter | TypeAlias, Namespace = ValueModule | NamespaceModule, Module = ValueModule | NamespaceModule, Accessor = GetAccessor | SetAccessor, @@ -1668,11 +1704,12 @@ namespace ts { PropertyExcludes = Value, EnumMemberExcludes = Value, FunctionExcludes = Value & ~(Function | ValueModule), + StructExcludes = (Value | Type) & ~ValueModule, ClassExcludes = (Value | Type) & ~(ValueModule | Interface), // class-interface mergability done in checker.ts - InterfaceExcludes = Type & ~(Interface | Class), + InterfaceExcludes = Type & ~(Interface | Struct | Class), RegularEnumExcludes = (Value | Type) & ~(RegularEnum | ValueModule), // regular enums merge only with regular enums and modules ConstEnumExcludes = (Value | Type) & ~ConstEnum, // const enums merge only with const enums - ValueModuleExcludes = Value & ~(Function | Class | RegularEnum | ValueModule), + ValueModuleExcludes = Value & ~(Function | Struct | Class | RegularEnum | ValueModule), NamespaceModuleExcludes = 0, MethodExcludes = Value & ~Method, GetAccessorExcludes = Value & ~SetAccessor, @@ -1681,14 +1718,14 @@ namespace ts { TypeAliasExcludes = Type, AliasExcludes = Alias, - ModuleMember = Variable | Function | Class | Interface | Enum | Module | TypeAlias | Alias, + ModuleMember = Variable | Function | Struct | Class | Interface | Enum | Module | TypeAlias | Alias, - ExportHasLocal = Function | Class | Enum | ValueModule, + ExportHasLocal = Function | Struct | Class | Enum | ValueModule, - HasExports = Class | Enum | Module, - HasMembers = Class | Interface | TypeLiteral | ObjectLiteral, + HasExports = Struct | Class | Enum | Module, + HasMembers = Struct | Class | Interface | TypeLiteral | ObjectLiteral, - BlockScoped = BlockScopedVariable | Class | Enum, + BlockScoped = BlockScopedVariable | Struct | Class | Enum, PropertyOrAccessor = Property | Accessor, Export = ExportNamespace | ExportType | ExportValue, @@ -1696,7 +1733,7 @@ namespace ts { /* @internal */ // The set of things we consider semantically classifiable. Used to speed up the LS during // classification. - Classifiable = Class | Enum | TypeAlias | Interface | TypeParameter | Module, + Classifiable = Struct | Class | Enum | TypeAlias | Interface | TypeParameter | Module, } export interface Symbol { @@ -1752,6 +1789,7 @@ namespace ts { ContextChecked = 0x00000400, // Contextual types have been assigned LexicalArguments = 0x00000800, CaptureArguments = 0x00001000, // Lexical 'arguments' used in body (for async functions) + EmitMathFround = 0x00002000, // Emit Math.fround if using float // Values for enum members have been computed, and any errors have been reported for them. EnumValuesComputed = 0x00002000, @@ -1790,6 +1828,7 @@ namespace ts { StringLiteral = 0x00000100, // String literal type TypeParameter = 0x00000200, // Type parameter Class = 0x00000400, // Class + Struct = 0x80000000, // Struct Interface = 0x00000800, // Interface Reference = 0x00001000, // Generic type reference Tuple = 0x00002000, // Tuple @@ -1807,23 +1846,37 @@ namespace ts { /* @internal */ ContainsObjectLiteral = 0x00400000, // Type is or contains object literal type /* @internal */ - ContainsAnyFunctionType = 0x00800000, // Type is or contains object literal type - ESSymbol = 0x01000000, // Type of symbol primitive introduced in ES6 - ThisType = 0x02000000, // This type + //ContainsAnyFunctionType = 0x00800000, // Type is or contains object literal type + ESSymbol = 0x00800000, // Type of symbol primitive introduced in ES6 + ThisType = 0x01000000, // This type + + I8 = 0x02000000, + U8 = 0x04000000, + I16 = 0x08000000, + U16 = 0x10000000, + I32 = 0x20000000, + U32 = 0x40000000, + Float = 0x80000000, /* @internal */ - Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null, + Intrinsic = Any | String | Number | Boolean | I8 | U8 | I16 | U16 | I32 | U32 | Float | ESSymbol | Void | Undefined | Null, /* @internal */ - Primitive = String | Number | Boolean | ESSymbol | Void | Undefined | Null | StringLiteral | Enum, + Primitive = String | Number | I8 | U8 | I16 | U16 | I32 | U32 | Float | Boolean | ESSymbol | Void | Undefined | Null | StringLiteral | Enum, StringLike = String | StringLiteral, - NumberLike = Number | Enum, + NumberLike = Number | Enum | I8 | U8 | I16 | U16 | I32 | U32 | Float, ObjectType = Class | Interface | Reference | Tuple | Anonymous, + + PrimitiveType = I8 | U8 | I16 | U16 | I32 | U32 | Float, + DoublePrecisionFloat = Number, + SinglePrecisionFloat = Float, + RealNumber = Number | Float, + UnionOrIntersection = Union | Intersection, StructuredType = ObjectType | Union | Intersection, /* @internal */ RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral, /* @internal */ - PropagatingFlags = ContainsUndefinedOrNull | ContainsObjectLiteral | ContainsAnyFunctionType + PropagatingFlags = ContainsUndefinedOrNull | ContainsObjectLiteral, } export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression; @@ -1980,6 +2033,13 @@ namespace ts { export const enum IndexKind { String, Number, + I8, + U8, + I16, + U16, + I32, + U32, + Float, } /* @internal */ @@ -2310,7 +2370,7 @@ namespace ts { export interface ModuleResolutionHost { fileExists(fileName: string): boolean; // readFile function is used to read arbitrary text files on disk, i.e. when resolution procedure needs the content of 'package.json' - // to determine location of bundled typings for node module + // to determine location of bundled typings for node module readFile(fileName: string): string; } @@ -2318,7 +2378,7 @@ namespace ts { resolvedFileName: string; /* * Denotes if 'resolvedFileName' is isExternalLibraryImport and thus should be proper external module: - * - be a .d.ts file + * - be a .d.ts file * - use top level imports\exports * - don't use tripleslash references */ @@ -2341,11 +2401,11 @@ namespace ts { getNewLine(): string; /* - * CompilerHost must either implement resolveModuleNames (in case if it wants to be completely in charge of - * module name resolution) or provide implementation for methods from ModuleResolutionHost (in this case compiler + * CompilerHost must either implement resolveModuleNames (in case if it wants to be completely in charge of + * module name resolution) or provide implementation for methods from ModuleResolutionHost (in this case compiler * will appply built-in module resolution logic and use members of ModuleResolutionHost to ask host specific questions). - * If resolveModuleNames is implemented then implementation for members from ModuleResolutionHost can be just - * 'throw new Error("NotImplemented")' + * If resolveModuleNames is implemented then implementation for members from ModuleResolutionHost can be just + * 'throw new Error("NotImplemented")' */ resolveModuleNames?(moduleNames: string[], containingFile: string): ResolvedModule[]; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index eeca6a43d075c..36510d674d497 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -82,17 +82,17 @@ namespace ts { return node.end - node.pos; } - export function arrayIsEqualTo(array1: T[], array2: T[], equaler?: (a: T, b: T) => boolean): boolean { - if (!array1 || !array2) { - return array1 === array2; + export function arrayIsEqualTo(arr1: T[], arr2: T[], comparer?: (a: T, b: T) => boolean): boolean { + if (!arr1 || !arr2) { + return arr1 === arr2; } - if (array1.length !== array2.length) { + if (arr1.length !== arr2.length) { return false; } - for (let i = 0; i < array1.length; ++i) { - let equals = equaler ? equaler(array1[i], array2[i]) : array1[i] === array2[i]; + for (let i = 0; i < arr1.length; ++i) { + let equals = comparer ? comparer(arr1[i], arr2[i]) : arr1[i] === arr2[i]; if (!equals) { return false; } @@ -1311,6 +1311,16 @@ namespace ts { node.kind === SyntaxKind.ExportAssignment && (node).expression.kind === SyntaxKind.Identifier; } + export function getStructExtendsHeritageClauseElement(node: StructLikeDeclaration) { + let heritageClause = getHeritageClause(node.heritageClauses, SyntaxKind.ExtendsKeyword); + return heritageClause && heritageClause.types.length > 0 ? heritageClause.types[0] : undefined; + } + + export function getStructImplementsHeritageClauseElements(node: StructLikeDeclaration) { + let heritageClause = getHeritageClause(node.heritageClauses, SyntaxKind.ImplementsKeyword); + return heritageClause ? heritageClause.types : undefined; + } + export function getClassExtendsHeritageClauseElement(node: ClassLikeDeclaration) { let heritageClause = getHeritageClause(node.heritageClauses, SyntaxKind.ExtendsKeyword); return heritageClause && heritageClause.types.length > 0 ? heritageClause.types[0] : undefined; @@ -1505,7 +1515,7 @@ namespace ts { } export function createSynthesizedNode(kind: SyntaxKind, startsOnNewLine?: boolean): Node { - let node = createNode(kind, /* pos */ -1, /* end */ -1); + let node = createNode(kind); node.startsOnNewLine = startsOnNewLine; return node; } @@ -2414,4 +2424,16 @@ namespace ts { } } } + + export function arrayStructurallyIsEqualTo(array1: Array, array2: Array): boolean { + if (!array1 || !array2) { + return false; + } + + if (array1.length !== array2.length) { + return false; + } + + return arrayIsEqualTo(array1.sort(), array2.sort()); + } } diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 806f89be04d06..a37b647a12490 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -274,9 +274,7 @@ namespace Utils { case "flags": // Print out flags with their enum names. - if (n.flags) { - o[propertyName] = getNodeFlagName(n.flags); - } + o[propertyName] = getNodeFlagName(n.flags); break; case "parserContextFlags": diff --git a/src/lib/core.d.ts b/src/lib/core.d.ts index 12df449931e25..7259ff5081c31 100644 --- a/src/lib/core.d.ts +++ b/src/lib/core.d.ts @@ -1210,7 +1210,7 @@ interface ArrayBuffer { interface ArrayBufferConstructor { prototype: ArrayBuffer; new (byteLength: number): ArrayBuffer; - isView(arg: any): arg is ArrayBufferView; + isView(arg: any): boolean; } declare var ArrayBuffer: ArrayBufferConstructor; diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index efcca0e37c571..09f8a37246329 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -576,8 +576,7 @@ namespace ts.server { let newRootFiles = projectOptions.files.map((f => this.getCanonicalFileName(f))); let currentRootFiles = project.getRootFiles().map((f => this.getCanonicalFileName(f))); - // We check if the project file list has changed. If so, we update the project. - if (!arrayIsEqualTo(currentRootFiles && currentRootFiles.sort(), newRootFiles && newRootFiles.sort())) { + if (!arrayStructurallyIsEqualTo(currentRootFiles, newRootFiles)) { // For configured projects, the change is made outside the tsconfig file, and // it is not likely to affect the project for other files opened by the client. We can // just update the current project. diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 651105e9d5c34..4a7033c6f3e47 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -695,8 +695,8 @@ namespace ts.formatting { let tokenStart = sourceFile.getLineAndCharacterOfPosition(currentTokenInfo.token.pos); if (isTokenInRange) { let rangeHasError = rangeContainsError(currentTokenInfo.token); - // save previousRange since processRange will overwrite this value with current one - let savePreviousRange = previousRange; + // save prevStartLine since processRange will overwrite this value with current ones + let prevStartLine = previousRangeStartLine; lineAdded = processRange(currentTokenInfo.token, tokenStart, parent, childContextNode, dynamicIndentation); if (rangeHasError) { // do not indent comments\token if token range overlaps with some error @@ -707,9 +707,7 @@ namespace ts.formatting { indentToken = lineAdded; } else { - // indent token only if end line of previous range does not match start line of the token - const prevEndLine = savePreviousRange && sourceFile.getLineAndCharacterOfPosition(savePreviousRange.end).line; - indentToken = lastTriviaWasNewLine && tokenStart.line !== prevEndLine; + indentToken = lastTriviaWasNewLine && tokenStart.line !== prevStartLine; } } } diff --git a/src/services/services.ts b/src/services/services.ts index ec29a89bdbc4c..15c6e3df56f8f 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -174,7 +174,9 @@ namespace ts { let jsDocCompletionEntries: CompletionEntry[]; function createNode(kind: SyntaxKind, pos: number, end: number, flags: NodeFlags, parent?: Node): NodeObject { - let node = new (getNodeConstructor(kind))(pos, end); + let node = new (getNodeConstructor(kind))(); + node.pos = pos; + node.end = end; node.flags = flags; node.parent = parent; return node; @@ -7965,14 +7967,14 @@ namespace ts { function initializeServices() { objectAllocator = { getNodeConstructor: kind => { - function Node(pos: number, end: number) { - this.pos = pos; - this.end = end; - this.flags = NodeFlags.None; - this.parent = undefined; + function Node() { } let proto = kind === SyntaxKind.SourceFile ? new SourceFileObject() : new NodeObject(); proto.kind = kind; + proto.pos = -1; + proto.end = -1; + proto.flags = 0; + proto.parent = undefined; Node.prototype = proto; return Node; }, diff --git a/tests/baselines/reference/APISample_linter.js b/tests/baselines/reference/APISample_linter.js deleted file mode 100644 index aeff3a327f557..0000000000000 --- a/tests/baselines/reference/APISample_linter.js +++ /dev/null @@ -1,118 +0,0 @@ -//// [APISample_linter.ts] - -/* - * Note: This test is a public API sample. The sample sources can be found - at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter - * Please log a "breaking change" issue for any API breaking change affecting this issue - */ - -declare var process: any; -declare var console: any; -declare var readFileSync: any; - -import * as ts from "typescript"; - -export function delint(sourceFile: ts.SourceFile) { - delintNode(sourceFile); - - function delintNode(node: ts.Node) { - switch (node.kind) { - case ts.SyntaxKind.ForStatement: - case ts.SyntaxKind.ForInStatement: - case ts.SyntaxKind.WhileStatement: - case ts.SyntaxKind.DoStatement: - if ((node).statement.kind !== ts.SyntaxKind.Block) { - report(node, "A looping statement's contents should be wrapped in a block body."); - } - break; - - case ts.SyntaxKind.IfStatement: - let ifStatement = (node); - if (ifStatement.thenStatement.kind !== ts.SyntaxKind.Block) { - report(ifStatement.thenStatement, "An if statement's contents should be wrapped in a block body."); - } - if (ifStatement.elseStatement && - ifStatement.elseStatement.kind !== ts.SyntaxKind.Block && - ifStatement.elseStatement.kind !== ts.SyntaxKind.IfStatement) { - report(ifStatement.elseStatement, "An else statement's contents should be wrapped in a block body."); - } - break; - - case ts.SyntaxKind.BinaryExpression: - let op = (node).operatorToken.kind; - if (op === ts.SyntaxKind.EqualsEqualsToken || op == ts.SyntaxKind.ExclamationEqualsToken) { - report(node, "Use '===' and '!=='.") - } - break; - } - - ts.forEachChild(node, delintNode); - } - - function report(node: ts.Node, message: string) { - let { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart()); - console.log(`${sourceFile.fileName} (${line + 1},${character + 1}): ${message}`); - } -} - -const fileNames = process.argv.slice(2); -fileNames.forEach(fileName => { - // Parse a file - let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true); - - // delint it - delint(sourceFile); -}); - -//// [APISample_linter.js] -/* - * Note: This test is a public API sample. The sample sources can be found - at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter - * Please log a "breaking change" issue for any API breaking change affecting this issue - */ -var ts = require("typescript"); -function delint(sourceFile) { - delintNode(sourceFile); - function delintNode(node) { - switch (node.kind) { - case 199 /* ForStatement */: - case 200 /* ForInStatement */: - case 198 /* WhileStatement */: - case 197 /* DoStatement */: - if (node.statement.kind !== 192 /* Block */) { - report(node, "A looping statement's contents should be wrapped in a block body."); - } - break; - case 196 /* IfStatement */: - var ifStatement = node; - if (ifStatement.thenStatement.kind !== 192 /* Block */) { - report(ifStatement.thenStatement, "An if statement's contents should be wrapped in a block body."); - } - if (ifStatement.elseStatement && - ifStatement.elseStatement.kind !== 192 /* Block */ && - ifStatement.elseStatement.kind !== 196 /* IfStatement */) { - report(ifStatement.elseStatement, "An else statement's contents should be wrapped in a block body."); - } - break; - case 181 /* BinaryExpression */: - var op = node.operatorToken.kind; - if (op === 30 /* EqualsEqualsToken */ || op == 31 /* ExclamationEqualsToken */) { - report(node, "Use '===' and '!=='."); - } - break; - } - ts.forEachChild(node, delintNode); - } - function report(node, message) { - var _a = sourceFile.getLineAndCharacterOfPosition(node.getStart()), line = _a.line, character = _a.character; - console.log(sourceFile.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message); - } -} -exports.delint = delint; -var fileNames = process.argv.slice(2); -fileNames.forEach(function (fileName) { - // Parse a file - var sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), 2 /* ES6 */, /*setParentNodes */ true); - // delint it - delint(sourceFile); -}); diff --git a/tests/baselines/reference/arrayBufferIsViewNarrowsType.js b/tests/baselines/reference/arrayBufferIsViewNarrowsType.js deleted file mode 100644 index fcda4d5976bd3..0000000000000 --- a/tests/baselines/reference/arrayBufferIsViewNarrowsType.js +++ /dev/null @@ -1,13 +0,0 @@ -//// [arrayBufferIsViewNarrowsType.ts] -var obj: Object; -if (ArrayBuffer.isView(obj)) { - // isView should be a guard that narrows type to ArrayBufferView. - var ab: ArrayBufferView = obj; -} - -//// [arrayBufferIsViewNarrowsType.js] -var obj; -if (ArrayBuffer.isView(obj)) { - // isView should be a guard that narrows type to ArrayBufferView. - var ab = obj; -} diff --git a/tests/baselines/reference/arrayBufferIsViewNarrowsType.symbols b/tests/baselines/reference/arrayBufferIsViewNarrowsType.symbols deleted file mode 100644 index eb7ffe7479eb2..0000000000000 --- a/tests/baselines/reference/arrayBufferIsViewNarrowsType.symbols +++ /dev/null @@ -1,17 +0,0 @@ -=== tests/cases/compiler/arrayBufferIsViewNarrowsType.ts === -var obj: Object; ->obj : Symbol(obj, Decl(arrayBufferIsViewNarrowsType.ts, 0, 3)) ->Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) - -if (ArrayBuffer.isView(obj)) { ->ArrayBuffer.isView : Symbol(ArrayBufferConstructor.isView, Decl(lib.d.ts, --, --)) ->ArrayBuffer : Symbol(ArrayBuffer, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) ->isView : Symbol(ArrayBufferConstructor.isView, Decl(lib.d.ts, --, --)) ->obj : Symbol(obj, Decl(arrayBufferIsViewNarrowsType.ts, 0, 3)) - - // isView should be a guard that narrows type to ArrayBufferView. - var ab: ArrayBufferView = obj; ->ab : Symbol(ab, Decl(arrayBufferIsViewNarrowsType.ts, 3, 7)) ->ArrayBufferView : Symbol(ArrayBufferView, Decl(lib.d.ts, --, --)) ->obj : Symbol(obj, Decl(arrayBufferIsViewNarrowsType.ts, 0, 3)) -} diff --git a/tests/baselines/reference/arrayBufferIsViewNarrowsType.types b/tests/baselines/reference/arrayBufferIsViewNarrowsType.types deleted file mode 100644 index 129b7d601d863..0000000000000 --- a/tests/baselines/reference/arrayBufferIsViewNarrowsType.types +++ /dev/null @@ -1,18 +0,0 @@ -=== tests/cases/compiler/arrayBufferIsViewNarrowsType.ts === -var obj: Object; ->obj : Object ->Object : Object - -if (ArrayBuffer.isView(obj)) { ->ArrayBuffer.isView(obj) : boolean ->ArrayBuffer.isView : (arg: any) => arg is ArrayBufferView ->ArrayBuffer : ArrayBufferConstructor ->isView : (arg: any) => arg is ArrayBufferView ->obj : Object - - // isView should be a guard that narrows type to ArrayBufferView. - var ab: ArrayBufferView = obj; ->ab : ArrayBufferView ->ArrayBufferView : ArrayBufferView ->obj : ArrayBufferView -} diff --git a/tests/baselines/reference/assignmentCompat1.errors.txt b/tests/baselines/reference/assignmentCompat1.errors.txt index 7539af9288718..edb8d28eeccb1 100644 --- a/tests/baselines/reference/assignmentCompat1.errors.txt +++ b/tests/baselines/reference/assignmentCompat1.errors.txt @@ -2,13 +2,9 @@ tests/cases/compiler/assignmentCompat1.ts(4,1): error TS2322: Type '{ [index: st Property 'one' is missing in type '{ [index: string]: any; }'. tests/cases/compiler/assignmentCompat1.ts(6,1): error TS2322: Type '{ [index: number]: any; }' is not assignable to type '{ one: number; }'. Property 'one' is missing in type '{ [index: number]: any; }'. -tests/cases/compiler/assignmentCompat1.ts(8,1): error TS2322: Type 'string' is not assignable to type '{ [index: string]: any; }'. - Index signature is missing in type 'String'. -tests/cases/compiler/assignmentCompat1.ts(10,1): error TS2322: Type 'boolean' is not assignable to type '{ [index: number]: any; }'. - Index signature is missing in type 'Boolean'. -==== tests/cases/compiler/assignmentCompat1.ts (4 errors) ==== +==== tests/cases/compiler/assignmentCompat1.ts (2 errors) ==== var x = { one: 1 }; var y: { [index: string]: any }; var z: { [index: number]: any }; @@ -22,14 +18,4 @@ tests/cases/compiler/assignmentCompat1.ts(10,1): error TS2322: Type 'boolean' is !!! error TS2322: Type '{ [index: number]: any; }' is not assignable to type '{ one: number; }'. !!! error TS2322: Property 'one' is missing in type '{ [index: number]: any; }'. z = x; // Ok because index signature type is any - y = "foo"; // Error - ~ -!!! error TS2322: Type 'string' is not assignable to type '{ [index: string]: any; }'. -!!! error TS2322: Index signature is missing in type 'String'. - z = "foo"; // OK, string has numeric indexer - z = false; // Error - ~ -!!! error TS2322: Type 'boolean' is not assignable to type '{ [index: number]: any; }'. -!!! error TS2322: Index signature is missing in type 'Boolean'. - \ No newline at end of file diff --git a/tests/baselines/reference/assignmentCompat1.js b/tests/baselines/reference/assignmentCompat1.js index 7451d342fa14b..740a44cced47f 100644 --- a/tests/baselines/reference/assignmentCompat1.js +++ b/tests/baselines/reference/assignmentCompat1.js @@ -6,10 +6,6 @@ x = y; // Error y = x; // Ok because index signature type is any x = z; // Error z = x; // Ok because index signature type is any -y = "foo"; // Error -z = "foo"; // OK, string has numeric indexer -z = false; // Error - //// [assignmentCompat1.js] @@ -20,6 +16,3 @@ x = y; // Error y = x; // Ok because index signature type is any x = z; // Error z = x; // Ok because index signature type is any -y = "foo"; // Error -z = "foo"; // OK, string has numeric indexer -z = false; // Error diff --git a/tests/baselines/reference/assignmentLHSIsValue.errors.txt b/tests/baselines/reference/assignmentLHSIsValue.errors.txt index cf747b44b3e94..d7fce6b8f1c4e 100644 --- a/tests/baselines/reference/assignmentLHSIsValue.errors.txt +++ b/tests/baselines/reference/assignmentLHSIsValue.errors.txt @@ -22,23 +22,9 @@ tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(4 tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(50,20): error TS1128: Declaration or statement expected. tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(51,11): error TS1005: ';' expected. tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(54,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(57,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(58,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(59,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(60,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(61,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(62,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(63,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(64,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(65,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(66,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(67,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(68,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(69,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(70,1): error TS2364: Invalid left-hand side of assignment expression. -==== tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts (38 errors) ==== +==== tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts (24 errors) ==== // expected error for all the LHS of assignments var value; @@ -144,44 +130,16 @@ tests/cases/conformance/expressions/assignmentOperator/assignmentLHSIsValue.ts(7 // parentheses, the containted expression is value (this) = value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (M) = value; - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (C) = value; - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (E) = value; - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (foo) = value; - ~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (null) = value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (true) = value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (0) = value; - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. ('') = value; - ~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (/d+/) = value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. ({}) = value; - ~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. ([]) = value; - ~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (function baz() { }) = value; - ~~~~~~~~~~~~~~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. - (foo()) = value; - ~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. \ No newline at end of file + (foo()) = value; \ No newline at end of file diff --git a/tests/baselines/reference/assignmentToParenthesizedExpression1.errors.txt b/tests/baselines/reference/assignmentToParenthesizedExpression1.errors.txt deleted file mode 100644 index 0b1b1e04de883..0000000000000 --- a/tests/baselines/reference/assignmentToParenthesizedExpression1.errors.txt +++ /dev/null @@ -1,8 +0,0 @@ -tests/cases/compiler/assignmentToParenthesizedExpression1.ts(2,1): error TS2364: Invalid left-hand side of assignment expression. - - -==== tests/cases/compiler/assignmentToParenthesizedExpression1.ts (1 errors) ==== - var x; - (1, x)=0; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. \ No newline at end of file diff --git a/tests/baselines/reference/assignmentToParenthesizedExpression1.js b/tests/baselines/reference/assignmentToParenthesizedExpression1.js deleted file mode 100644 index 8d8589b5f50aa..0000000000000 --- a/tests/baselines/reference/assignmentToParenthesizedExpression1.js +++ /dev/null @@ -1,7 +0,0 @@ -//// [assignmentToParenthesizedExpression1.ts] -var x; -(1, x)=0; - -//// [assignmentToParenthesizedExpression1.js] -var x; -(1, x) = 0; diff --git a/tests/baselines/reference/assignmentToParenthesizedIdentifiers.errors.txt b/tests/baselines/reference/assignmentToParenthesizedIdentifiers.errors.txt index a50ffdf72d71a..13e1f889abc1e 100644 --- a/tests/baselines/reference/assignmentToParenthesizedIdentifiers.errors.txt +++ b/tests/baselines/reference/assignmentToParenthesizedIdentifiers.errors.txt @@ -4,7 +4,6 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(14,1): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(15,1): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(17,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(18,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(25,5): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(31,1): error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'. Types of property 'x' are incompatible. @@ -16,7 +15,6 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize Types of property 'x' are incompatible. Type 'string' is not assignable to type 'number'. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(37,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(38,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(43,5): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(44,5): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(48,5): error TS2322: Type 'string' is not assignable to type 'number'. @@ -25,12 +23,10 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(55,5): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(56,5): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(62,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(63,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(69,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(70,1): error TS2364: Invalid left-hand side of assignment expression. -==== tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts (24 errors) ==== +==== tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts (20 errors) ==== var x: number; x = 3; // OK (x) = 3; // OK @@ -61,8 +57,6 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize ~ !!! error TS2364: Invalid left-hand side of assignment expression. (M) = { y: 3 }; // Error - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. module M2 { export module M3 { @@ -99,8 +93,6 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize ~~ !!! error TS2364: Invalid left-hand side of assignment expression. (fn) = () => 3; // Should be error - ~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. function fn2(x: number, y: { t: number }) { x = 3; @@ -142,8 +134,6 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize ~ !!! error TS2364: Invalid left-hand side of assignment expression. (E) = undefined; // Error - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. class C { @@ -153,6 +143,4 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize ~ !!! error TS2364: Invalid left-hand side of assignment expression. (C) = undefined; // Error - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. \ No newline at end of file diff --git a/tests/baselines/reference/compoundAssignmentLHSIsValue.errors.txt b/tests/baselines/reference/compoundAssignmentLHSIsValue.errors.txt index 7b26fc4bf9e04..cd25ab692ee70 100644 --- a/tests/baselines/reference/compoundAssignmentLHSIsValue.errors.txt +++ b/tests/baselines/reference/compoundAssignmentLHSIsValue.errors.txt @@ -44,37 +44,20 @@ tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsVa tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(88,11): error TS1005: ';' expected. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(91,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(92,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(95,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(96,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(97,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(98,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(99,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(100,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(101,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(102,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(103,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(104,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(105,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(106,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(107,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(108,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(109,1): error TS2364: Invalid left-hand side of assignment expression. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(110,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(111,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(112,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(113,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(114,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(115,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(116,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(117,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(118,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(119,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(120,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(121,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts(122,1): error TS2364: Invalid left-hand side of assignment expression. -==== tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts (74 errors) ==== +==== tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsValue.ts (57 errors) ==== // expected error for all the LHS of compound assignments (arithmetic and addition) var value; @@ -262,86 +245,52 @@ tests/cases/conformance/expressions/assignmentOperator/compoundAssignmentLHSIsVa // parentheses, the containted expression is value (this) *= value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (this) += value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (M) *= value; ~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. (M) += value; - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (C) *= value; ~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. (C) += value; - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (E) *= value; ~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. (E) += value; - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (foo) *= value; ~~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. (foo) += value; - ~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (null) *= value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (null) += value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (true) *= value; ~~~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. (true) += value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (0) *= value; - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (0) += value; - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. ('') *= value; ~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. ('') += value; - ~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (/d+/) *= value; ~~~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. (/d+/) += value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. ({}) *= value; ~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. ({}) += value; - ~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. ([]) *= value; ~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. ([]) += value; - ~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (function baz1() { }) *= value; ~~~~~~~~~~~~~~~~~~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. (function baz2() { }) += value; - ~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (foo()) *= value; ~~~~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. - (foo()) += value; - ~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. \ No newline at end of file + (foo()) += value; \ No newline at end of file diff --git a/tests/baselines/reference/compoundExponentiationAssignmentLHSIsValue.errors.txt b/tests/baselines/reference/compoundExponentiationAssignmentLHSIsValue.errors.txt index bee6f75ff5cbe..fd47943a90d41 100644 --- a/tests/baselines/reference/compoundExponentiationAssignmentLHSIsValue.errors.txt +++ b/tests/baselines/reference/compoundExponentiationAssignmentLHSIsValue.errors.txt @@ -21,14 +21,11 @@ tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignm tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(65,21): error TS1128: Declaration or statement expected. tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(66,11): error TS1005: ';' expected. tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(69,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(72,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(73,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(74,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(75,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(76,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(77,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(78,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(79,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(80,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(81,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(82,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. @@ -37,7 +34,7 @@ tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignm tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts(85,1): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -==== tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts (37 errors) ==== +==== tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignmentLHSIsValue.ts (34 errors) ==== // expected error for all the LHS of compound assignments (arithmetic and addition) var value; @@ -156,8 +153,6 @@ tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignm // parentheses, the containted expression is value (this) **= value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (M) **= value; ~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. @@ -171,14 +166,10 @@ tests/cases/conformance/es7/exponentiationOperator/compoundExponentiationAssignm ~~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. (null) **= value; - ~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. (true) **= value; ~~~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. (0) **= value; - ~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. ('') **= value; ~~~~ !!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. diff --git a/tests/baselines/reference/decrementAndIncrementOperators.errors.txt b/tests/baselines/reference/decrementAndIncrementOperators.errors.txt index ec58db836a631..f8824baebd1d5 100644 --- a/tests/baselines/reference/decrementAndIncrementOperators.errors.txt +++ b/tests/baselines/reference/decrementAndIncrementOperators.errors.txt @@ -1,19 +1,7 @@ tests/cases/compiler/decrementAndIncrementOperators.ts(4,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(6,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(7,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(9,3): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(10,3): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(12,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(13,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(15,3): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(16,3): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(18,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(19,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(21,3): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/compiler/decrementAndIncrementOperators.ts(22,3): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -==== tests/cases/compiler/decrementAndIncrementOperators.ts (13 errors) ==== +==== tests/cases/compiler/decrementAndIncrementOperators.ts (1 errors) ==== var x = 0; // errors @@ -22,46 +10,22 @@ tests/cases/compiler/decrementAndIncrementOperators.ts(22,3): error TS2357: The !!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. (1)++; - ~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. (1)--; - ~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ++(1); - ~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. --(1); - ~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. (1 + 2)++; - ~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. (1 + 2)--; - ~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ++(1 + 2); - ~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. --(1 + 2); - ~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. (x + x)++; - ~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. (x + x)--; - ~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ++(x + x); - ~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. --(x + x); - ~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. //OK x++; diff --git a/tests/baselines/reference/decrementOperatorWithAnyOtherTypeInvalidOperations.errors.txt b/tests/baselines/reference/decrementOperatorWithAnyOtherTypeInvalidOperations.errors.txt index 46e5515633a09..638ef082d49f3 100644 --- a/tests/baselines/reference/decrementOperatorWithAnyOtherTypeInvalidOperations.errors.txt +++ b/tests/baselines/reference/decrementOperatorWithAnyOtherTypeInvalidOperations.errors.txt @@ -16,21 +16,15 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(43,24): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(46,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(47,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(48,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(48,27): error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. -tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(49,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(49,27): error TS2365: Operator '+' cannot be applied to types 'null' and 'null'. -tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(50,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(50,27): error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(51,26): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(52,26): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(54,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(55,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(56,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(56,25): error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. -tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(57,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(57,25): error TS2365: Operator '+' cannot be applied to types 'null' and 'null'. -tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(58,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(58,25): error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(59,24): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(60,24): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. @@ -50,7 +44,7 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts(72,12): error TS1109: Expression expected. -==== tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts (50 errors) ==== +==== tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithAnyOtherTypeInvalidOperations.ts (44 errors) ==== // -- operator on any type var ANY1; var ANY2: any[] = ["", ""]; @@ -135,18 +129,12 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp ~~~~~~~ !!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. var ResultIsNumber19 = --(null + undefined); - ~~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. var ResultIsNumber20 = --(null + null); - ~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'null' and 'null'. var ResultIsNumber21 = --(undefined + undefined); - ~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. var ResultIsNumber22 = --obj1.x; @@ -163,18 +151,12 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp ~~~~~~~ !!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. var ResultIsNumber26 = (null + undefined)--; - ~~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. var ResultIsNumber27 = (null + null)--; - ~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'null' and 'null'. var ResultIsNumber28 = (undefined + undefined)--; - ~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. var ResultIsNumber29 = obj1.x--; diff --git a/tests/baselines/reference/decrementOperatorWithEnumTypeInvalidOperations.errors.txt b/tests/baselines/reference/decrementOperatorWithEnumTypeInvalidOperations.errors.txt index 079bb2a6b640c..7a6fd9a12b91b 100644 --- a/tests/baselines/reference/decrementOperatorWithEnumTypeInvalidOperations.errors.txt +++ b/tests/baselines/reference/decrementOperatorWithEnumTypeInvalidOperations.errors.txt @@ -2,9 +2,7 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts(8,25): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts(10,23): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts(11,23): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts(14,25): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts(14,43): error TS2339: Property 'B' does not exist on type 'typeof ENUM'. -tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts(15,23): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts(15,29): error TS2339: Property 'A' does not exist on type 'typeof ENUM'. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts(18,3): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts(19,3): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. @@ -12,7 +10,7 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts(22,1): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. -==== tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts (12 errors) ==== +==== tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithEnumTypeInvalidOperations.ts (10 errors) ==== // -- operator on enum type enum ENUM { }; @@ -35,13 +33,9 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp // enum type expressions var ResultIsNumber5 = --(ENUM["A"] + ENUM.B); - ~~~~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~ !!! error TS2339: Property 'B' does not exist on type 'typeof ENUM'. var ResultIsNumber6 = (ENUM.A + ENUM["B"])--; - ~~~~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~ !!! error TS2339: Property 'A' does not exist on type 'typeof ENUM'. diff --git a/tests/baselines/reference/decrementOperatorWithNumberTypeInvalidOperations.errors.txt b/tests/baselines/reference/decrementOperatorWithNumberTypeInvalidOperations.errors.txt index 8df7e50809961..09a817ea442de 100644 --- a/tests/baselines/reference/decrementOperatorWithNumberTypeInvalidOperations.errors.txt +++ b/tests/baselines/reference/decrementOperatorWithNumberTypeInvalidOperations.errors.txt @@ -8,10 +8,8 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(28,23): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(31,25): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(32,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(33,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(35,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(36,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(37,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(40,3): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(41,3): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(42,3): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. @@ -20,7 +18,7 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts(46,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -==== tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts (20 errors) ==== +==== tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOperatorWithNumberTypeInvalidOperations.ts (18 errors) ==== // -- operator on number type var NUMBER: number; var NUMBER1: number[] = [1, 2]; @@ -74,8 +72,6 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp ~~~~~~~ !!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. var ResultIsNumber11 = --(NUMBER + NUMBER); - ~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. var ResultIsNumber12 = foo()--; ~~~~~ @@ -84,8 +80,6 @@ tests/cases/conformance/expressions/unaryOperators/decrementOperator/decrementOp ~~~~~~~ !!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. var ResultIsNumber14 = (NUMBER + NUMBER)--; - ~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. // miss assignment operator --1; diff --git a/tests/baselines/reference/for-of47.errors.txt b/tests/baselines/reference/for-of47.errors.txt deleted file mode 100644 index 241dc107caa64..0000000000000 --- a/tests/baselines/reference/for-of47.errors.txt +++ /dev/null @@ -1,13 +0,0 @@ -tests/cases/conformance/es6/for-ofStatements/for-of47.ts(4,13): error TS2322: Type 'boolean' is not assignable to type 'number'. - - -==== tests/cases/conformance/es6/for-ofStatements/for-of47.ts (1 errors) ==== - var x: string, y: number; - var array = [{ x: "", y: true }] - enum E { x } - for ({x, y: y = E.x} of array) { - ~ -!!! error TS2322: Type 'boolean' is not assignable to type 'number'. - x; - y; - } \ No newline at end of file diff --git a/tests/baselines/reference/for-of47.js b/tests/baselines/reference/for-of47.js deleted file mode 100644 index d7e596d2edc14..0000000000000 --- a/tests/baselines/reference/for-of47.js +++ /dev/null @@ -1,20 +0,0 @@ -//// [for-of47.ts] -var x: string, y: number; -var array = [{ x: "", y: true }] -enum E { x } -for ({x, y: y = E.x} of array) { - x; - y; -} - -//// [for-of47.js] -var x, y; -var array = [{ x: "", y: true }]; -var E; -(function (E) { - E[E["x"] = 0] = "x"; -})(E || (E = {})); -for ({ x, y: y = E.x } of array) { - x; - y; -} diff --git a/tests/baselines/reference/for-of48.errors.txt b/tests/baselines/reference/for-of48.errors.txt deleted file mode 100644 index 8a8fd668714a0..0000000000000 --- a/tests/baselines/reference/for-of48.errors.txt +++ /dev/null @@ -1,13 +0,0 @@ -tests/cases/conformance/es6/for-ofStatements/for-of48.ts(4,10): error TS2322: Type 'boolean' is not assignable to type 'number'. - - -==== tests/cases/conformance/es6/for-ofStatements/for-of48.ts (1 errors) ==== - var x: string, y: number; - var array = [{ x: "", y: true }] - enum E { x } - for ({x, y = E.x} of array) { - ~ -!!! error TS2322: Type 'boolean' is not assignable to type 'number'. - x; - y; - } \ No newline at end of file diff --git a/tests/baselines/reference/for-of48.js b/tests/baselines/reference/for-of48.js deleted file mode 100644 index 65e23076fd952..0000000000000 --- a/tests/baselines/reference/for-of48.js +++ /dev/null @@ -1,20 +0,0 @@ -//// [for-of48.ts] -var x: string, y: number; -var array = [{ x: "", y: true }] -enum E { x } -for ({x, y = E.x} of array) { - x; - y; -} - -//// [for-of48.js] -var x, y; -var array = [{ x: "", y: true }]; -var E; -(function (E) { - E[E["x"] = 0] = "x"; -})(E || (E = {})); -for ({ x, y = E.x } of array) { - x; - y; -} diff --git a/tests/baselines/reference/functionConstraintSatisfaction.types b/tests/baselines/reference/functionConstraintSatisfaction.types index 7363fa162d3fe..631aec331374c 100644 --- a/tests/baselines/reference/functionConstraintSatisfaction.types +++ b/tests/baselines/reference/functionConstraintSatisfaction.types @@ -44,8 +44,8 @@ var r = foo(new Function()); >Function : FunctionConstructor var r1 = foo((x) => x); ->r1 : (x: any) => any ->foo((x) => x) : (x: any) => any +>r1 : {} +>foo((x) => x) : {} >foo : (x: T) => T >(x) => x : (x: any) => any >x : any @@ -60,8 +60,8 @@ var r2 = foo((x: string[]) => x); >x : string[] var r3 = foo(function (x) { return x }); ->r3 : (x: any) => any ->foo(function (x) { return x }) : (x: any) => any +>r3 : {} +>foo(function (x) { return x }) : {} >foo : (x: T) => T >function (x) { return x } : (x: any) => any >x : any diff --git a/tests/baselines/reference/functionConstraintSatisfaction3.types b/tests/baselines/reference/functionConstraintSatisfaction3.types index c58e62f69955b..06158f3ea5ca4 100644 --- a/tests/baselines/reference/functionConstraintSatisfaction3.types +++ b/tests/baselines/reference/functionConstraintSatisfaction3.types @@ -37,8 +37,8 @@ var c: { (): string; (x): string }; >x : any var r1 = foo((x) => x); ->r1 : (x: string) => string ->foo((x) => x) : (x: string) => string +>r1 : {} +>foo((x) => x) : {} >foo : string>(x: T) => T >(x) => x : (x: string) => string >x : string @@ -53,8 +53,8 @@ var r2 = foo((x: string) => x); >x : string var r3 = foo(function (x) { return x }); ->r3 : (x: string) => string ->foo(function (x) { return x }) : (x: string) => string +>r3 : {} +>foo(function (x) { return x }) : {} >foo : string>(x: T) => T >function (x) { return x } : (x: string) => string >x : string diff --git a/tests/baselines/reference/incrementOperatorWithAnyOtherTypeInvalidOperations.errors.txt b/tests/baselines/reference/incrementOperatorWithAnyOtherTypeInvalidOperations.errors.txt index 92e639df21e39..175bd8dc03e88 100644 --- a/tests/baselines/reference/incrementOperatorWithAnyOtherTypeInvalidOperations.errors.txt +++ b/tests/baselines/reference/incrementOperatorWithAnyOtherTypeInvalidOperations.errors.txt @@ -16,21 +16,15 @@ tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOp tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(43,24): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(46,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(47,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(48,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(48,27): error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. -tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(49,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(49,27): error TS2365: Operator '+' cannot be applied to types 'null' and 'null'. -tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(50,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(50,27): error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(51,26): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(52,26): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(54,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(55,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(56,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(56,25): error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. -tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(57,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(57,25): error TS2365: Operator '+' cannot be applied to types 'null' and 'null'. -tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(58,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(58,25): error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(59,24): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(60,24): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. @@ -45,7 +39,7 @@ tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOp tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts(69,12): error TS1109: Expression expected. -==== tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts (45 errors) ==== +==== tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithAnyOtherTypeInvalidOperations.ts (39 errors) ==== // ++ operator on any type var ANY1; var ANY2: any[] = [1, 2]; @@ -130,18 +124,12 @@ tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOp ~~~~~~~ !!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. var ResultIsNumber19 = ++(null + undefined); - ~~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. var ResultIsNumber20 = ++(null + null); - ~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'null' and 'null'. var ResultIsNumber21 = ++(undefined + undefined); - ~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. var ResultIsNumber22 = ++obj1.x; @@ -158,18 +146,12 @@ tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOp ~~~~~~~ !!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. var ResultIsNumber26 = (null + undefined)++; - ~~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. var ResultIsNumber27 = (null + null)++; - ~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'null' and 'null'. var ResultIsNumber28 = (undefined + undefined)++; - ~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. ~~~~~~~~~~~~~~~~~~~~~ !!! error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'. var ResultIsNumber29 = obj1.x++; diff --git a/tests/baselines/reference/incrementOperatorWithNumberTypeInvalidOperations.errors.txt b/tests/baselines/reference/incrementOperatorWithNumberTypeInvalidOperations.errors.txt index 71c3ad67c4665..77bf36c0f041a 100644 --- a/tests/baselines/reference/incrementOperatorWithNumberTypeInvalidOperations.errors.txt +++ b/tests/baselines/reference/incrementOperatorWithNumberTypeInvalidOperations.errors.txt @@ -8,10 +8,8 @@ tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOp tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(28,23): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(31,25): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(32,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(33,26): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(35,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(36,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(37,24): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(40,3): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(41,3): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(42,3): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. @@ -20,7 +18,7 @@ tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOp tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts(46,1): error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. -==== tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts (20 errors) ==== +==== tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOperatorWithNumberTypeInvalidOperations.ts (18 errors) ==== // ++ operator on number type var NUMBER: number; var NUMBER1: number[] = [1, 2]; @@ -74,8 +72,6 @@ tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOp ~~~~~~~ !!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. var ResultIsNumber11 = ++(NUMBER + NUMBER); - ~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. var ResultIsNumber12 = foo()++; ~~~~~ @@ -84,8 +80,6 @@ tests/cases/conformance/expressions/unaryOperators/incrementOperator/incrementOp ~~~~~~~ !!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. var ResultIsNumber14 = (NUMBER + NUMBER)++; - ~~~~~~~~~~~~~~~~~ -!!! error TS2357: The operand of an increment or decrement operator must be a variable, property or indexer. // miss assignment operator ++1; diff --git a/tests/baselines/reference/indexTypeCheck.errors.txt b/tests/baselines/reference/indexTypeCheck.errors.txt index 06b844f8321d1..2ea5a2f413adc 100644 --- a/tests/baselines/reference/indexTypeCheck.errors.txt +++ b/tests/baselines/reference/indexTypeCheck.errors.txt @@ -1,14 +1,13 @@ tests/cases/compiler/indexTypeCheck.ts(2,2): error TS1021: An index signature must have a type annotation. tests/cases/compiler/indexTypeCheck.ts(3,2): error TS1021: An index signature must have a type annotation. tests/cases/compiler/indexTypeCheck.ts(17,2): error TS2413: Numeric index type 'number' is not assignable to string index type 'string'. -tests/cases/compiler/indexTypeCheck.ts(22,2): error TS2413: Numeric index type 'Orange' is not assignable to string index type 'Yellow'. tests/cases/compiler/indexTypeCheck.ts(27,2): error TS2413: Numeric index type 'number' is not assignable to string index type 'string'. tests/cases/compiler/indexTypeCheck.ts(32,3): error TS1096: An index signature must have exactly one parameter. tests/cases/compiler/indexTypeCheck.ts(36,3): error TS1023: An index signature parameter type must be 'string' or 'number'. tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression argument must be of type 'string', 'number', 'symbol', or 'any'. -==== tests/cases/compiler/indexTypeCheck.ts (8 errors) ==== +==== tests/cases/compiler/indexTypeCheck.ts (7 errors) ==== interface Red { [n:number]; // ok ~~~~~~~~~~~ @@ -37,8 +36,6 @@ tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression interface Green { [n:number]: Orange; // error - ~~~~~~~~~~~~~~~~~~~ -!!! error TS2413: Numeric index type 'Orange' is not assignable to string index type 'Yellow'. [s:string]: Yellow; // ok } diff --git a/tests/baselines/reference/inferentialTypingUsingApparentType1.types b/tests/baselines/reference/inferentialTypingUsingApparentType1.types index b56f3670ddcc9..e1f0d76f39930 100644 --- a/tests/baselines/reference/inferentialTypingUsingApparentType1.types +++ b/tests/baselines/reference/inferentialTypingUsingApparentType1.types @@ -12,7 +12,7 @@ function foo number>(x: T): T { } foo(x => x.length); ->foo(x => x.length) : (x: string) => number +>foo(x => x.length) : {} >foo : number>(x: T) => T >x => x.length : (x: string) => number >x : string diff --git a/tests/baselines/reference/inferentialTypingUsingApparentType2.types b/tests/baselines/reference/inferentialTypingUsingApparentType2.types index 5841e021c3ac5..597f5885f5ae1 100644 --- a/tests/baselines/reference/inferentialTypingUsingApparentType2.types +++ b/tests/baselines/reference/inferentialTypingUsingApparentType2.types @@ -13,7 +13,7 @@ function foo(x: T): T { } foo({ m(x) { return x.length } }); ->foo({ m(x) { return x.length } }) : { m(x: string): number; } +>foo({ m(x) { return x.length } }) : { } >foo : (x: T) => T >{ m(x) { return x.length } } : { m(x: string): number; } >m : (x: string) => number diff --git a/tests/baselines/reference/inferringAnyFunctionType1.types b/tests/baselines/reference/inferringAnyFunctionType1.types index 16402cc3ced7e..e2d72d443957c 100644 --- a/tests/baselines/reference/inferringAnyFunctionType1.types +++ b/tests/baselines/reference/inferringAnyFunctionType1.types @@ -12,8 +12,8 @@ function f number }>(p: T): T { } var v = f([x => x]); ->v : [(x: number) => number] ->f([x => x]) : [(x: number) => number] +>v : [{}] +>f([x => x]) : [{}] >f : number; }>(p: T) => T >[x => x] : [(x: number) => number] >x => x : (x: number) => number diff --git a/tests/baselines/reference/inferringAnyFunctionType2.types b/tests/baselines/reference/inferringAnyFunctionType2.types index 7884bdfc6bac8..b2109ed2e4bc2 100644 --- a/tests/baselines/reference/inferringAnyFunctionType2.types +++ b/tests/baselines/reference/inferringAnyFunctionType2.types @@ -12,8 +12,8 @@ function f number]>(p: T): T { } var v = f([x => x]); ->v : [(x: number) => number] ->f([x => x]) : [(x: number) => number] +>v : [{}] +>f([x => x]) : [{}] >f : number]>(p: T) => T >[x => x] : [(x: number) => number] >x => x : (x: number) => number diff --git a/tests/baselines/reference/inferringAnyFunctionType3.types b/tests/baselines/reference/inferringAnyFunctionType3.types index 1edcfc3464301..d0097f9cc0f44 100644 --- a/tests/baselines/reference/inferringAnyFunctionType3.types +++ b/tests/baselines/reference/inferringAnyFunctionType3.types @@ -12,8 +12,8 @@ function f number)[]>(p: T): T { } var v = f([x => x]); ->v : ((x: number) => number)[] ->f([x => x]) : ((x: number) => number)[] +>v : {}[] +>f([x => x]) : {}[] >f : number)[]>(p: T) => T >[x => x] : ((x: number) => number)[] >x => x : (x: number) => number diff --git a/tests/baselines/reference/inferringAnyFunctionType4.types b/tests/baselines/reference/inferringAnyFunctionType4.types index 49d46d0433e6c..63766eb9daa96 100644 --- a/tests/baselines/reference/inferringAnyFunctionType4.types +++ b/tests/baselines/reference/inferringAnyFunctionType4.types @@ -12,8 +12,8 @@ function f number>(p: T): T { } var v = f(x => x); ->v : (x: number) => number ->f(x => x) : (x: number) => number +>v : {} +>f(x => x) : {} >f : number>(p: T) => T >x => x : (x: number) => number >x : number diff --git a/tests/baselines/reference/inferringAnyFunctionType5.types b/tests/baselines/reference/inferringAnyFunctionType5.types index 8f7ae123eac21..2d1aeffe2b8b4 100644 --- a/tests/baselines/reference/inferringAnyFunctionType5.types +++ b/tests/baselines/reference/inferringAnyFunctionType5.types @@ -13,8 +13,8 @@ function f number }>(p: T): T { } var v = f({ q: x => x }); ->v : { q: (x: number) => number; } ->f({ q: x => x }) : { q: (x: number) => number; } +>v : { q: {}; } +>f({ q: x => x }) : { q: {}; } >f : number; }>(p: T) => T >{ q: x => x } : { q: (x: number) => number; } >q : (x: number) => number diff --git a/tests/baselines/reference/intTypeCheck.errors.txt b/tests/baselines/reference/intTypeCheck.errors.txt index e48d1b1dc7303..a32b52bce320b 100644 --- a/tests/baselines/reference/intTypeCheck.errors.txt +++ b/tests/baselines/reference/intTypeCheck.errors.txt @@ -1,4 +1,5 @@ tests/cases/compiler/intTypeCheck.ts(35,6): error TS2304: Cannot find name 'p'. +tests/cases/compiler/intTypeCheck.ts(43,11): error TS2427: Interface name cannot be 'i8' tests/cases/compiler/intTypeCheck.ts(71,6): error TS2304: Cannot find name 'p'. tests/cases/compiler/intTypeCheck.ts(85,5): error TS2386: Overload signatures must all be optional or required. tests/cases/compiler/intTypeCheck.ts(99,5): error TS2322: Type 'Object' is not assignable to type 'i1'. @@ -30,8 +31,6 @@ tests/cases/compiler/intTypeCheck.ts(134,21): error TS1109: Expression expected. tests/cases/compiler/intTypeCheck.ts(134,22): error TS2304: Cannot find name 'i3'. tests/cases/compiler/intTypeCheck.ts(135,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. tests/cases/compiler/intTypeCheck.ts(142,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. -tests/cases/compiler/intTypeCheck.ts(148,5): error TS2322: Type 'boolean' is not assignable to type 'i4'. - Index signature is missing in type 'Boolean'. tests/cases/compiler/intTypeCheck.ts(148,21): error TS1109: Expression expected. tests/cases/compiler/intTypeCheck.ts(148,22): error TS2304: Cannot find name 'i4'. tests/cases/compiler/intTypeCheck.ts(149,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. @@ -67,15 +66,18 @@ tests/cases/compiler/intTypeCheck.ts(190,5): error TS2322: Type 'boolean' is not tests/cases/compiler/intTypeCheck.ts(190,21): error TS1109: Expression expected. tests/cases/compiler/intTypeCheck.ts(190,22): error TS2304: Cannot find name 'i7'. tests/cases/compiler/intTypeCheck.ts(191,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/compiler/intTypeCheck.ts(196,5): error TS2322: Type '{}' is not assignable to type 'i8'. +tests/cases/compiler/intTypeCheck.ts(197,5): error TS2322: Type 'Object' is not assignable to type 'i8'. tests/cases/compiler/intTypeCheck.ts(198,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. +tests/cases/compiler/intTypeCheck.ts(199,5): error TS2322: Type 'Base' is not assignable to type 'i8'. +tests/cases/compiler/intTypeCheck.ts(201,5): error TS2322: Type '() => void' is not assignable to type 'i8'. tests/cases/compiler/intTypeCheck.ts(204,5): error TS2322: Type 'boolean' is not assignable to type 'i8'. - Index signature is missing in type 'Boolean'. tests/cases/compiler/intTypeCheck.ts(204,21): error TS1109: Expression expected. tests/cases/compiler/intTypeCheck.ts(204,22): error TS2304: Cannot find name 'i8'. tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. -==== tests/cases/compiler/intTypeCheck.ts (63 errors) ==== +==== tests/cases/compiler/intTypeCheck.ts (67 errors) ==== interface i1 { //Property Signatures p; @@ -121,6 +123,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit interface i6 extends i2 { } interface i7 extends i3 { } interface i8 extends i4 { } + ~~ +!!! error TS2427: Interface name cannot be 'i8' interface i9 { } class Base { foo() { } } @@ -284,9 +288,6 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit //var obj40: i4 = function foo() { }; var obj41: i4 = anyVar; var obj42: i4 = new anyVar; - ~~~~~ -!!! error TS2322: Type 'boolean' is not assignable to type 'i4'. -!!! error TS2322: Index signature is missing in type 'Boolean'. ~ !!! error TS1109: Expression expected. ~~ @@ -399,19 +400,26 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit // var obj77: i8; var obj78: i8 = {}; + ~~~~~ +!!! error TS2322: Type '{}' is not assignable to type 'i8'. var obj79: i8 = new Object(); + ~~~~~ +!!! error TS2322: Type 'Object' is not assignable to type 'i8'. var obj80: i8 = new obj77; ~~~~~~~~~ !!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. var obj81: i8 = new Base; + ~~~~~ +!!! error TS2322: Type 'Base' is not assignable to type 'i8'. var obj82: i8 = null; var obj83: i8 = function () { }; + ~~~~~ +!!! error TS2322: Type '() => void' is not assignable to type 'i8'. //var obj84: i8 = function foo() { }; var obj85: i8 = anyVar; var obj86: i8 = new anyVar; ~~~~~ !!! error TS2322: Type 'boolean' is not assignable to type 'i8'. -!!! error TS2322: Index signature is missing in type 'Boolean'. ~ !!! error TS1109: Expression expected. ~~ diff --git a/tests/baselines/reference/intTypeCheck.js b/tests/baselines/reference/intTypeCheck.js index fbbe1bb5d030a..3e70212908d7f 100644 --- a/tests/baselines/reference/intTypeCheck.js +++ b/tests/baselines/reference/intTypeCheck.js @@ -322,11 +322,11 @@ var obj76 = new {}; var obj77; var obj78 = {}; var obj79 = new Object(); -var obj80 = new obj77; +var obj80 = (new obj77 & 255) << 24 >> 24; var obj81 = new Base; -var obj82 = null; +var obj82 = (null & 255) << 24 >> 24; var obj83 = function () { }; //var obj84: i8 = function foo() { }; var obj85 = anyVar; var obj86 = new < i8 > anyVar; -var obj87 = new {}; +var obj87 = (new {} & 255) << 24 >> 24; diff --git a/tests/baselines/reference/interfaceDeclaration1.errors.txt b/tests/baselines/reference/interfaceDeclaration1.errors.txt index b73cfef75dfaa..cf18ac2e785cf 100644 --- a/tests/baselines/reference/interfaceDeclaration1.errors.txt +++ b/tests/baselines/reference/interfaceDeclaration1.errors.txt @@ -6,11 +6,12 @@ tests/cases/compiler/interfaceDeclaration1.ts(22,11): error TS2310: Type 'I5' re tests/cases/compiler/interfaceDeclaration1.ts(35,7): error TS2420: Class 'C1' incorrectly implements interface 'I3'. Property 'prototype' is missing in type 'C1'. tests/cases/compiler/interfaceDeclaration1.ts(41,11): error TS2310: Type 'i8' recursively references itself as a base type. +tests/cases/compiler/interfaceDeclaration1.ts(41,11): error TS2427: Interface name cannot be 'i8' tests/cases/compiler/interfaceDeclaration1.ts(52,11): error TS2320: Interface 'i12' cannot simultaneously extend types 'i10' and 'i11'. Named property 'foo' of types 'i10' and 'i11' are not identical. -==== tests/cases/compiler/interfaceDeclaration1.ts (8 errors) ==== +==== tests/cases/compiler/interfaceDeclaration1.ts (9 errors) ==== interface I1 { item:number; ~~~~ @@ -67,6 +68,8 @@ tests/cases/compiler/interfaceDeclaration1.ts(52,11): error TS2320: Interface 'i interface i8 extends i9 { } ~~ !!! error TS2310: Type 'i8' recursively references itself as a base type. + ~~ +!!! error TS2427: Interface name cannot be 'i8' interface i9 extends i8 { } interface i10 { diff --git a/tests/baselines/reference/jsxEsprimaFbTestSuite.errors.txt b/tests/baselines/reference/jsxEsprimaFbTestSuite.errors.txt index 3a6059d8d07f1..5758db369600a 100644 --- a/tests/baselines/reference/jsxEsprimaFbTestSuite.errors.txt +++ b/tests/baselines/reference/jsxEsprimaFbTestSuite.errors.txt @@ -4,10 +4,11 @@ tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,29): error TS1005: '{' tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,57): error TS1109: Expression expected. tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,58): error TS1109: Expression expected. tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(41,1): error TS1003: Identifier expected. -tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(41,12): error TS2657: JSX expressions must have one parent element +tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(41,6): error TS1109: Expression expected. +tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(41,12): error TS1109: Expression expected. -==== tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx (7 errors) ==== +==== tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx (8 errors) ==== declare var React: any; declare var 日本語; declare var AbC_def; @@ -61,8 +62,10 @@ tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(41,12): error TS2657: JSX ; ~ !!! error TS1003: Identifier expected. + ~~ +!!! error TS1109: Expression expected. ~ -!!! error TS2657: JSX expressions must have one parent element +!!! error TS1109: Expression expected. ; diff --git a/tests/baselines/reference/jsxEsprimaFbTestSuite.js b/tests/baselines/reference/jsxEsprimaFbTestSuite.js index 465d265efb28a..1e22826f44068 100644 --- a/tests/baselines/reference/jsxEsprimaFbTestSuite.js +++ b/tests/baselines/reference/jsxEsprimaFbTestSuite.js @@ -72,8 +72,8 @@ baz
@test content
;

7x invalid-js-identifier
; } right={monkeys /> gorillas / > }/> - , - ; + < a.b > ; +a.b > ; ; (
) < x;
; diff --git a/tests/baselines/reference/jsxInvalidEsprimaTestSuite.errors.txt b/tests/baselines/reference/jsxInvalidEsprimaTestSuite.errors.txt index 578159dc6515c..02bc56e823ee7 100644 --- a/tests/baselines/reference/jsxInvalidEsprimaTestSuite.errors.txt +++ b/tests/baselines/reference/jsxInvalidEsprimaTestSuite.errors.txt @@ -12,11 +12,17 @@ tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(6,6): error TS2304: C tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(6,9): error TS1109: Expression expected. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(6,10): error TS1109: Expression expected. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(7,1): error TS1003: Identifier expected. +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(7,2): error TS2304: Cannot find name 'a'. +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(7,4): error TS1109: Expression expected. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(8,4): error TS17002: Expected corresponding JSX closing tag for 'a'. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(9,13): error TS1002: Unterminated string literal. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(10,1): error TS1003: Identifier expected. -tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(10,6): error TS17002: Expected corresponding JSX closing tag for 'a'. -tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(10,10): error TS2657: JSX expressions must have one parent element +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(10,2): error TS2304: Cannot find name 'a'. +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(10,3): error TS1005: ';' expected. +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(10,4): error TS2304: Cannot find name 'b'. +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(10,6): error TS1109: Expression expected. +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(10,8): error TS2304: Cannot find name 'b'. +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(10,10): error TS1109: Expression expected. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(11,3): error TS1003: Identifier expected. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(11,5): error TS1003: Identifier expected. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(11,11): error TS1005: '>' expected. @@ -65,7 +71,7 @@ tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(35,4): error TS1003: tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(35,21): error TS17002: Expected corresponding JSX closing tag for 'a'. -==== tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx (65 errors) ==== +==== tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx (71 errors) ==== declare var React: any; ; @@ -101,6 +107,10 @@ tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(35,21): error TS17002 ; ~ !!! error TS1003: Identifier expected. + ~ +!!! error TS2304: Cannot find name 'a'. + ~ +!!! error TS1109: Expression expected. ; ~~~~ !!! error TS17002: Expected corresponding JSX closing tag for 'a'. @@ -110,10 +120,18 @@ tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(35,21): error TS17002 ; ~ !!! error TS1003: Identifier expected. - ~~~~ -!!! error TS17002: Expected corresponding JSX closing tag for 'a'. + ~ +!!! error TS2304: Cannot find name 'a'. + ~ +!!! error TS1005: ';' expected. + ~ +!!! error TS2304: Cannot find name 'b'. + ~~ +!!! error TS1109: Expression expected. + ~ +!!! error TS2304: Cannot find name 'b'. ~ -!!! error TS2657: JSX expressions must have one parent element +!!! error TS1109: Expression expected. ; ~ !!! error TS1003: Identifier expected. diff --git a/tests/baselines/reference/jsxInvalidEsprimaTestSuite.js b/tests/baselines/reference/jsxInvalidEsprimaTestSuite.js index 7f01666bd7821..d623e127be7f6 100644 --- a/tests/baselines/reference/jsxInvalidEsprimaTestSuite.js +++ b/tests/baselines/reference/jsxInvalidEsprimaTestSuite.js @@ -41,10 +41,12 @@ var x =
one
/* intervening comment */
two
;; < ; a / > ;
}/> - , - ; -; - + < a; +b > ; +b > ; ; b.c > ; ; diff --git a/tests/baselines/reference/nestedRedeclarationInES6AMD.js b/tests/baselines/reference/nestedRedeclarationInES6AMD.js deleted file mode 100644 index bb90759dc8b9a..0000000000000 --- a/tests/baselines/reference/nestedRedeclarationInES6AMD.js +++ /dev/null @@ -1,15 +0,0 @@ -//// [nestedRedeclarationInES6AMD.ts] -function a() { - { - let status = 1; - status = 2; - } -} - -//// [nestedRedeclarationInES6AMD.js] -function a() { - { - let status = 1; - status = 2; - } -} diff --git a/tests/baselines/reference/nestedRedeclarationInES6AMD.symbols b/tests/baselines/reference/nestedRedeclarationInES6AMD.symbols deleted file mode 100644 index 65f310a7ed25a..0000000000000 --- a/tests/baselines/reference/nestedRedeclarationInES6AMD.symbols +++ /dev/null @@ -1,11 +0,0 @@ -=== tests/cases/compiler/nestedRedeclarationInES6AMD.ts === -function a() { ->a : Symbol(a, Decl(nestedRedeclarationInES6AMD.ts, 0, 0)) - { - let status = 1; ->status : Symbol(status, Decl(nestedRedeclarationInES6AMD.ts, 2, 11)) - - status = 2; ->status : Symbol(status, Decl(nestedRedeclarationInES6AMD.ts, 2, 11)) - } -} diff --git a/tests/baselines/reference/nestedRedeclarationInES6AMD.types b/tests/baselines/reference/nestedRedeclarationInES6AMD.types deleted file mode 100644 index 2b8d971e3c198..0000000000000 --- a/tests/baselines/reference/nestedRedeclarationInES6AMD.types +++ /dev/null @@ -1,14 +0,0 @@ -=== tests/cases/compiler/nestedRedeclarationInES6AMD.ts === -function a() { ->a : () => void - { - let status = 1; ->status : number ->1 : number - - status = 2; ->status = 2 : number ->status : number ->2 : number - } -} diff --git a/tests/baselines/reference/objectLiteralExcessProperties.errors.txt b/tests/baselines/reference/objectLiteralExcessProperties.errors.txt deleted file mode 100644 index 84f635bf94766..0000000000000 --- a/tests/baselines/reference/objectLiteralExcessProperties.errors.txt +++ /dev/null @@ -1,66 +0,0 @@ -tests/cases/compiler/objectLiteralExcessProperties.ts(9,18): error TS2322: Type '{ forword: string; }' is not assignable to type 'Book'. - Object literal may only specify known properties, and 'forword' does not exist in type 'Book'. -tests/cases/compiler/objectLiteralExcessProperties.ts(11,27): error TS2322: Type '{ foreward: string; }' is not assignable to type 'Book | string'. - Object literal may only specify known properties, and 'foreward' does not exist in type 'Book | string'. -tests/cases/compiler/objectLiteralExcessProperties.ts(13,53): error TS2322: Type '({ foreword: string; } | { forwards: string; })[]' is not assignable to type 'Book | Book[]'. - Type '({ foreword: string; } | { forwards: string; })[]' is not assignable to type 'Book[]'. - Type '{ foreword: string; } | { forwards: string; }' is not assignable to type 'Book'. - Type '{ forwards: string; }' is not assignable to type 'Book'. - Object literal may only specify known properties, and 'forwards' does not exist in type 'Book'. -tests/cases/compiler/objectLiteralExcessProperties.ts(15,42): error TS2322: Type '{ foreword: string; colour: string; }' is not assignable to type 'Book & Cover'. - Object literal may only specify known properties, and 'colour' does not exist in type 'Book & Cover'. -tests/cases/compiler/objectLiteralExcessProperties.ts(17,26): error TS2322: Type '{ foreward: string; color: string; }' is not assignable to type 'Book & Cover'. - Object literal may only specify known properties, and 'foreward' does not exist in type 'Book & Cover'. -tests/cases/compiler/objectLiteralExcessProperties.ts(19,57): error TS2322: Type '{ foreword: string; color: string; price: number; }' is not assignable to type 'Book & Cover'. - Object literal may only specify known properties, and 'price' does not exist in type 'Book & Cover'. -tests/cases/compiler/objectLiteralExcessProperties.ts(21,43): error TS2322: Type '{ foreword: string; price: number; }' is not assignable to type 'Book & number'. - Object literal may only specify known properties, and 'price' does not exist in type 'Book & number'. - - -==== tests/cases/compiler/objectLiteralExcessProperties.ts (7 errors) ==== - interface Book { - foreword: string; - } - - interface Cover { - color?: string; - } - - var b1: Book = { forword: "oops" }; - ~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ forword: string; }' is not assignable to type 'Book'. -!!! error TS2322: Object literal may only specify known properties, and 'forword' does not exist in type 'Book'. - - var b2: Book | string = { foreward: "nope" }; - ~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ foreward: string; }' is not assignable to type 'Book | string'. -!!! error TS2322: Object literal may only specify known properties, and 'foreward' does not exist in type 'Book | string'. - - var b3: Book | (Book[]) = [{ foreword: "hello" }, { forwards: "back" }]; - ~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '({ foreword: string; } | { forwards: string; })[]' is not assignable to type 'Book | Book[]'. -!!! error TS2322: Type '({ foreword: string; } | { forwards: string; })[]' is not assignable to type 'Book[]'. -!!! error TS2322: Type '{ foreword: string; } | { forwards: string; }' is not assignable to type 'Book'. -!!! error TS2322: Type '{ forwards: string; }' is not assignable to type 'Book'. -!!! error TS2322: Object literal may only specify known properties, and 'forwards' does not exist in type 'Book'. - - var b4: Book & Cover = { foreword: "hi", colour: "blue" }; - ~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ foreword: string; colour: string; }' is not assignable to type 'Book & Cover'. -!!! error TS2322: Object literal may only specify known properties, and 'colour' does not exist in type 'Book & Cover'. - - var b5: Book & Cover = { foreward: "hi", color: "blue" }; - ~~~~~~~~~~~~~~ -!!! error TS2322: Type '{ foreward: string; color: string; }' is not assignable to type 'Book & Cover'. -!!! error TS2322: Object literal may only specify known properties, and 'foreward' does not exist in type 'Book & Cover'. - - var b6: Book & Cover = { foreword: "hi", color: "blue", price: 10.99 }; - ~~~~~~~~~~~~ -!!! error TS2322: Type '{ foreword: string; color: string; price: number; }' is not assignable to type 'Book & Cover'. -!!! error TS2322: Object literal may only specify known properties, and 'price' does not exist in type 'Book & Cover'. - - var b7: Book & number = { foreword: "hi", price: 10.99 }; - ~~~~~~~~~~~~ -!!! error TS2322: Type '{ foreword: string; price: number; }' is not assignable to type 'Book & number'. -!!! error TS2322: Object literal may only specify known properties, and 'price' does not exist in type 'Book & number'. - \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralExcessProperties.js b/tests/baselines/reference/objectLiteralExcessProperties.js deleted file mode 100644 index 747928ccb2076..0000000000000 --- a/tests/baselines/reference/objectLiteralExcessProperties.js +++ /dev/null @@ -1,32 +0,0 @@ -//// [objectLiteralExcessProperties.ts] -interface Book { - foreword: string; -} - -interface Cover { - color?: string; -} - -var b1: Book = { forword: "oops" }; - -var b2: Book | string = { foreward: "nope" }; - -var b3: Book | (Book[]) = [{ foreword: "hello" }, { forwards: "back" }]; - -var b4: Book & Cover = { foreword: "hi", colour: "blue" }; - -var b5: Book & Cover = { foreward: "hi", color: "blue" }; - -var b6: Book & Cover = { foreword: "hi", color: "blue", price: 10.99 }; - -var b7: Book & number = { foreword: "hi", price: 10.99 }; - - -//// [objectLiteralExcessProperties.js] -var b1 = { forword: "oops" }; -var b2 = { foreward: "nope" }; -var b3 = [{ foreword: "hello" }, { forwards: "back" }]; -var b4 = { foreword: "hi", colour: "blue" }; -var b5 = { foreward: "hi", color: "blue" }; -var b6 = { foreword: "hi", color: "blue", price: 10.99 }; -var b7 = { foreword: "hi", price: 10.99 }; diff --git a/tests/baselines/reference/parserAssignmentExpression1.errors.txt b/tests/baselines/reference/parserAssignmentExpression1.errors.txt index 70eecd81b39e6..b952f510fe9b1 100644 --- a/tests/baselines/reference/parserAssignmentExpression1.errors.txt +++ b/tests/baselines/reference/parserAssignmentExpression1.errors.txt @@ -1,12 +1,9 @@ -tests/cases/conformance/parser/ecmascript5/Expressions/parserAssignmentExpression1.ts(1,1): error TS2364: Invalid left-hand side of assignment expression. tests/cases/conformance/parser/ecmascript5/Expressions/parserAssignmentExpression1.ts(1,2): error TS2304: Cannot find name 'foo'. tests/cases/conformance/parser/ecmascript5/Expressions/parserAssignmentExpression1.ts(1,11): error TS2304: Cannot find name 'bar'. -==== tests/cases/conformance/parser/ecmascript5/Expressions/parserAssignmentExpression1.ts (3 errors) ==== +==== tests/cases/conformance/parser/ecmascript5/Expressions/parserAssignmentExpression1.ts (2 errors) ==== (foo()) = bar; - ~~~~~~~ -!!! error TS2364: Invalid left-hand side of assignment expression. ~~~ !!! error TS2304: Cannot find name 'foo'. ~~~ diff --git a/tests/baselines/reference/promisePermutations3.errors.txt b/tests/baselines/reference/promisePermutations3.errors.txt index 72276745c41e2..58fe2b1f52032 100644 --- a/tests/baselines/reference/promisePermutations3.errors.txt +++ b/tests/baselines/reference/promisePermutations3.errors.txt @@ -79,7 +79,7 @@ tests/cases/compiler/promisePermutations3.ts(159,21): error TS2345: Argument of Type '(value: number) => Promise' is not assignable to type '(value: string) => any'. Types of parameters 'value' and 'value' are incompatible. Type 'number' is not assignable to type 'string'. -tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of type '{ (x: T): IPromise; (x: T, y: T): Promise; }' is not assignable to parameter of type '(value: (x: any) => any) => Promise'. +tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of type '{ (x: T): IPromise; (x: T, y: T): Promise; }' is not assignable to parameter of type '(value: {}) => Promise'. Type 'IPromise' is not assignable to type 'Promise'. Property 'done' is optional in type 'IPromise' but required in type 'Promise'. @@ -366,7 +366,7 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of var s12a = s12.then(testFunction12, testFunction12, testFunction12); // ok var s12b = s12.then(testFunction12P, testFunction12P, testFunction12P); // ok ~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '{ (x: T): IPromise; (x: T, y: T): Promise; }' is not assignable to parameter of type '(value: (x: any) => any) => Promise'. +!!! error TS2345: Argument of type '{ (x: T): IPromise; (x: T, y: T): Promise; }' is not assignable to parameter of type '(value: {}) => Promise'. !!! error TS2345: Type 'IPromise' is not assignable to type 'Promise'. !!! error TS2345: Property 'done' is optional in type 'IPromise' but required in type 'Promise'. var s12c = s12.then(testFunction12P, testFunction12, testFunction12); // ok \ No newline at end of file diff --git a/tests/baselines/reference/recursiveInheritance.errors.txt b/tests/baselines/reference/recursiveInheritance.errors.txt index c724afc5a0e6e..3950898e1d355 100644 --- a/tests/baselines/reference/recursiveInheritance.errors.txt +++ b/tests/baselines/reference/recursiveInheritance.errors.txt @@ -1,8 +1,9 @@ tests/cases/compiler/recursiveInheritance.ts(2,11): error TS2310: Type 'I5' recursively references itself as a base type. tests/cases/compiler/recursiveInheritance.ts(6,11): error TS2310: Type 'i8' recursively references itself as a base type. +tests/cases/compiler/recursiveInheritance.ts(6,11): error TS2427: Interface name cannot be 'i8' -==== tests/cases/compiler/recursiveInheritance.ts (2 errors) ==== +==== tests/cases/compiler/recursiveInheritance.ts (3 errors) ==== interface I5 extends I5 { // error ~~ @@ -13,5 +14,7 @@ tests/cases/compiler/recursiveInheritance.ts(6,11): error TS2310: Type 'i8' recu interface i8 extends i9 { } // error ~~ !!! error TS2310: Type 'i8' recursively references itself as a base type. + ~~ +!!! error TS2427: Interface name cannot be 'i8' interface i9 extends i8 { } // error \ No newline at end of file diff --git a/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring.errors.txt b/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring.errors.txt deleted file mode 100644 index 7ac5fe9a26cdb..0000000000000 --- a/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring.errors.txt +++ /dev/null @@ -1,164 +0,0 @@ -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(16,9): error TS2459: Type '{}' has no property 's1' and no string index signature. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(22,9): error TS2459: Type '{}' has no property 's1' and no string index signature. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(40,9): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(46,12): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(72,5): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(77,8): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(82,5): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(82,13): error TS2322: Type '{ x: number; }' is not assignable to type '{ x: string; }'. - Types of property 'x' are incompatible. - Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(87,8): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(87,19): error TS2322: Type '{ x: number; }' is not assignable to type '{ x: string; }'. - Types of property 'x' are incompatible. - Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(113,12): error TS2304: Cannot find name 's'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(113,14): error TS1312: '=' can only be used in an object literal property inside a destructuring assignment. - - -==== tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts (12 errors) ==== - - - (function() { - var s0; - for ({ s0 = 5 } of [{ s0: 1 }]) { - } - }); - (function() { - var s0; - for ({ s0:s0 = 5 } of [{ s0: 1 }]) { - } - }); - - (function() { - var s1; - for ({ s1 = 5 } of [{}]) { - ~~ -!!! error TS2459: Type '{}' has no property 's1' and no string index signature. - } - }); - - (function() { - var s1; - for ({ s1:s1 = 5 } of [{}]) { - ~~ -!!! error TS2459: Type '{}' has no property 's1' and no string index signature. - } - }); - - (function() { - var s2; - for ({ s2 = 5 } of [{ s2: "" }]) { - } - }); - - (function() { - var s2; - for ({ s2:s2 = 5 } of [{ s2: "" }]) { - } - }); - - (function() { - var s3: string; - for ({ s3 = 5 } of [{ s3: "" }]) { - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - } - }); - - (function() { - var s3: string; - for ({ s3:s3 = 5 } of [{ s3: "" }]) { - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - } - }); - - (function() { - let y; - ({ y = 5 } = { y: 1 }) - }); - - (function() { - let y; - ({ y:y = 5 } = { y: 1 }) - }); - - (function() { - let y0: number; - ({ y0 = 5 } = { y0: 1 }) - }); - - (function() { - let y0: number; - ({ y0:y0 = 5 } = { y0: 1 }) - }); - - (function() { - let y1: string; - ({ y1 = 5 } = {}) - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - }); - - (function() { - let y1: string; - ({ y1:y1 = 5 } = {}) - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - }); - - (function() { - let y2: string, y3: { x: string }; - ({ y2 = 5, y3 = { x: 1 } } = {}) - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - ~~ -!!! error TS2322: Type '{ x: number; }' is not assignable to type '{ x: string; }'. -!!! error TS2322: Types of property 'x' are incompatible. -!!! error TS2322: Type 'number' is not assignable to type 'string'. - }); - - (function() { - let y2: string, y3: { x: string }; - ({ y2:y2 = 5, y3:y3 = { x: 1 } } = {}) - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - ~~ -!!! error TS2322: Type '{ x: number; }' is not assignable to type '{ x: string; }'. -!!! error TS2322: Types of property 'x' are incompatible. -!!! error TS2322: Type 'number' is not assignable to type 'string'. - }); - - (function() { - let y4: number, y5: { x: number }; - ({ y4 = 5, y5 = { x: 1 } } = {}) - }); - - (function() { - let y4: number, y5: { x: number }; - ({ y4:y4 = 5, y5:y5 = { x: 1 } } = {}) - }); - - - (function() { - let z; - ({ z = { x: 5 } } = { z: { x: 1 } }); - }); - - - (function() { - let z; - ({ z:z = { x: 5 } } = { z: { x: 1 } }); - }); - - (function() { - let a = { s = 5 }; - ~ -!!! error TS2304: Cannot find name 's'. - ~ -!!! error TS1312: '=' can only be used in an object literal property inside a destructuring assignment. - }); - - function foo({a = 4, b = { x: 5 }}) { - } \ No newline at end of file diff --git a/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring.js b/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring.js deleted file mode 100644 index 981ee08e20c6a..0000000000000 --- a/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring.js +++ /dev/null @@ -1,242 +0,0 @@ -//// [shorthandPropertyAssignmentsInDestructuring.ts] - - -(function() { - var s0; - for ({ s0 = 5 } of [{ s0: 1 }]) { - } -}); -(function() { - var s0; - for ({ s0:s0 = 5 } of [{ s0: 1 }]) { - } -}); - -(function() { - var s1; - for ({ s1 = 5 } of [{}]) { - } -}); - -(function() { - var s1; - for ({ s1:s1 = 5 } of [{}]) { - } -}); - -(function() { - var s2; - for ({ s2 = 5 } of [{ s2: "" }]) { - } -}); - -(function() { - var s2; - for ({ s2:s2 = 5 } of [{ s2: "" }]) { - } -}); - -(function() { - var s3: string; - for ({ s3 = 5 } of [{ s3: "" }]) { - } -}); - -(function() { - var s3: string; - for ({ s3:s3 = 5 } of [{ s3: "" }]) { - } -}); - -(function() { - let y; - ({ y = 5 } = { y: 1 }) -}); - -(function() { - let y; - ({ y:y = 5 } = { y: 1 }) -}); - -(function() { - let y0: number; - ({ y0 = 5 } = { y0: 1 }) -}); - -(function() { - let y0: number; - ({ y0:y0 = 5 } = { y0: 1 }) -}); - -(function() { - let y1: string; - ({ y1 = 5 } = {}) -}); - -(function() { - let y1: string; - ({ y1:y1 = 5 } = {}) -}); - -(function() { - let y2: string, y3: { x: string }; - ({ y2 = 5, y3 = { x: 1 } } = {}) -}); - -(function() { - let y2: string, y3: { x: string }; - ({ y2:y2 = 5, y3:y3 = { x: 1 } } = {}) -}); - -(function() { - let y4: number, y5: { x: number }; - ({ y4 = 5, y5 = { x: 1 } } = {}) -}); - -(function() { - let y4: number, y5: { x: number }; - ({ y4:y4 = 5, y5:y5 = { x: 1 } } = {}) -}); - - -(function() { - let z; - ({ z = { x: 5 } } = { z: { x: 1 } }); -}); - - -(function() { - let z; - ({ z:z = { x: 5 } } = { z: { x: 1 } }); -}); - -(function() { - let a = { s = 5 }; -}); - -function foo({a = 4, b = { x: 5 }}) { -} - -//// [shorthandPropertyAssignmentsInDestructuring.js] -(function () { - var s0; - for (var _i = 0, _a = [{ s0: 1 }]; _i < _a.length; _i++) { - _b = _a[_i].s0, s0 = _b === void 0 ? 5 : _b; - } - var _b; -}); -(function () { - var s0; - for (var _i = 0, _a = [{ s0: 1 }]; _i < _a.length; _i++) { - _b = _a[_i].s0, s0 = _b === void 0 ? 5 : _b; - } - var _b; -}); -(function () { - var s1; - for (var _i = 0, _a = [{}]; _i < _a.length; _i++) { - _b = _a[_i].s1, s1 = _b === void 0 ? 5 : _b; - } - var _b; -}); -(function () { - var s1; - for (var _i = 0, _a = [{}]; _i < _a.length; _i++) { - _b = _a[_i].s1, s1 = _b === void 0 ? 5 : _b; - } - var _b; -}); -(function () { - var s2; - for (var _i = 0, _a = [{ s2: "" }]; _i < _a.length; _i++) { - _b = _a[_i].s2, s2 = _b === void 0 ? 5 : _b; - } - var _b; -}); -(function () { - var s2; - for (var _i = 0, _a = [{ s2: "" }]; _i < _a.length; _i++) { - _b = _a[_i].s2, s2 = _b === void 0 ? 5 : _b; - } - var _b; -}); -(function () { - var s3; - for (var _i = 0, _a = [{ s3: "" }]; _i < _a.length; _i++) { - _b = _a[_i].s3, s3 = _b === void 0 ? 5 : _b; - } - var _b; -}); -(function () { - var s3; - for (var _i = 0, _a = [{ s3: "" }]; _i < _a.length; _i++) { - _b = _a[_i].s3, s3 = _b === void 0 ? 5 : _b; - } - var _b; -}); -(function () { - var y; - (_a = { y: 1 }, _b = _a.y, y = _b === void 0 ? 5 : _b, _a); - var _a, _b; -}); -(function () { - var y; - (_a = { y: 1 }, _b = _a.y, y = _b === void 0 ? 5 : _b, _a); - var _a, _b; -}); -(function () { - var y0; - (_a = { y0: 1 }, _b = _a.y0, y0 = _b === void 0 ? 5 : _b, _a); - var _a, _b; -}); -(function () { - var y0; - (_a = { y0: 1 }, _b = _a.y0, y0 = _b === void 0 ? 5 : _b, _a); - var _a, _b; -}); -(function () { - var y1; - (_a = {}, _b = _a.y1, y1 = _b === void 0 ? 5 : _b, _a); - var _a, _b; -}); -(function () { - var y1; - (_a = {}, _b = _a.y1, y1 = _b === void 0 ? 5 : _b, _a); - var _a, _b; -}); -(function () { - var y2, y3; - (_a = {}, _b = _a.y2, y2 = _b === void 0 ? 5 : _b, _c = _a.y3, y3 = _c === void 0 ? { x: 1 } : _c, _a); - var _a, _b, _c; -}); -(function () { - var y2, y3; - (_a = {}, _b = _a.y2, y2 = _b === void 0 ? 5 : _b, _c = _a.y3, y3 = _c === void 0 ? { x: 1 } : _c, _a); - var _a, _b, _c; -}); -(function () { - var y4, y5; - (_a = {}, _b = _a.y4, y4 = _b === void 0 ? 5 : _b, _c = _a.y5, y5 = _c === void 0 ? { x: 1 } : _c, _a); - var _a, _b, _c; -}); -(function () { - var y4, y5; - (_a = {}, _b = _a.y4, y4 = _b === void 0 ? 5 : _b, _c = _a.y5, y5 = _c === void 0 ? { x: 1 } : _c, _a); - var _a, _b, _c; -}); -(function () { - var z; - (_a = { z: { x: 1 } }, _b = _a.z, z = _b === void 0 ? { x: 5 } : _b, _a); - var _a, _b; -}); -(function () { - var z; - (_a = { z: { x: 1 } }, _b = _a.z, z = _b === void 0 ? { x: 5 } : _b, _a); - var _a, _b; -}); -(function () { - var a = { s: s }; -}); -function foo(_a) { - var _b = _a.a, a = _b === void 0 ? 4 : _b, _c = _a.b, b = _c === void 0 ? { x: 5 } : _c; -} diff --git a/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring_ES6.errors.txt b/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring_ES6.errors.txt deleted file mode 100644 index 7e6b6b35d7fa0..0000000000000 --- a/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring_ES6.errors.txt +++ /dev/null @@ -1,164 +0,0 @@ -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(16,9): error TS2459: Type '{}' has no property 's1' and no string index signature. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(22,9): error TS2459: Type '{}' has no property 's1' and no string index signature. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(40,9): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(46,12): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(72,5): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(77,8): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(82,5): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(82,13): error TS2322: Type '{ x: number; }' is not assignable to type '{ x: string; }'. - Types of property 'x' are incompatible. - Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(87,8): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(87,19): error TS2322: Type '{ x: number; }' is not assignable to type '{ x: string; }'. - Types of property 'x' are incompatible. - Type 'number' is not assignable to type 'string'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(113,12): error TS2304: Cannot find name 's'. -tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(113,14): error TS1312: '=' can only be used in an object literal property inside a destructuring assignment. - - -==== tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts (12 errors) ==== - - - (function() { - var s0; - for ({ s0 = 5 } of [{ s0: 1 }]) { - } - }); - (function() { - var s0; - for ({ s0:s0 = 5 } of [{ s0: 1 }]) { - } - }); - - (function() { - var s1; - for ({ s1 = 5 } of [{}]) { - ~~ -!!! error TS2459: Type '{}' has no property 's1' and no string index signature. - } - }); - - (function() { - var s1; - for ({ s1:s1 = 5 } of [{}]) { - ~~ -!!! error TS2459: Type '{}' has no property 's1' and no string index signature. - } - }); - - (function() { - var s2; - for ({ s2 = 5 } of [{ s2: "" }]) { - } - }); - - (function() { - var s2; - for ({ s2:s2 = 5 } of [{ s2: "" }]) { - } - }); - - (function() { - var s3: string; - for ({ s3 = 5 } of [{ s3: "" }]) { - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - } - }); - - (function() { - var s3: string; - for ({ s3:s3 = 5 } of [{ s3: "" }]) { - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - } - }); - - (function() { - let y; - ({ y = 5 } = { y: 1 }) - }); - - (function() { - let y; - ({ y:y = 5 } = { y: 1 }) - }); - - (function() { - let y0: number; - ({ y0 = 5 } = { y0: 1 }) - }); - - (function() { - let y0: number; - ({ y0:y0 = 5 } = { y0: 1 }) - }); - - (function() { - let y1: string; - ({ y1 = 5 } = {}) - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - }); - - (function() { - let y1: string; - ({ y1:y1 = 5 } = {}) - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - }); - - (function() { - let y2: string, y3: { x: string }; - ({ y2 = 5, y3 = { x: 1 } } = {}) - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - ~~ -!!! error TS2322: Type '{ x: number; }' is not assignable to type '{ x: string; }'. -!!! error TS2322: Types of property 'x' are incompatible. -!!! error TS2322: Type 'number' is not assignable to type 'string'. - }); - - (function() { - let y2: string, y3: { x: string }; - ({ y2:y2 = 5, y3:y3 = { x: 1 } } = {}) - ~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. - ~~ -!!! error TS2322: Type '{ x: number; }' is not assignable to type '{ x: string; }'. -!!! error TS2322: Types of property 'x' are incompatible. -!!! error TS2322: Type 'number' is not assignable to type 'string'. - }); - - (function() { - let y4: number, y5: { x: number }; - ({ y4 = 5, y5 = { x: 1 } } = {}) - }); - - (function() { - let y4: number, y5: { x: number }; - ({ y4:y4 = 5, y5:y5 = { x: 1 } } = {}) - }); - - - (function() { - let z; - ({ z = { x: 5 } } = { z: { x: 1 } }); - }); - - - (function() { - let z; - ({ z:z = { x: 5 } } = { z: { x: 1 } }); - }); - - (function() { - let a = { s = 5 }; - ~ -!!! error TS2304: Cannot find name 's'. - ~ -!!! error TS1312: '=' can only be used in an object literal property inside a destructuring assignment. - }); - - function foo({a = 4, b = { x: 5 }}) { - } \ No newline at end of file diff --git a/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring_ES6.js b/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring_ES6.js deleted file mode 100644 index 11068708110a4..0000000000000 --- a/tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring_ES6.js +++ /dev/null @@ -1,213 +0,0 @@ -//// [shorthandPropertyAssignmentsInDestructuring_ES6.ts] - - -(function() { - var s0; - for ({ s0 = 5 } of [{ s0: 1 }]) { - } -}); -(function() { - var s0; - for ({ s0:s0 = 5 } of [{ s0: 1 }]) { - } -}); - -(function() { - var s1; - for ({ s1 = 5 } of [{}]) { - } -}); - -(function() { - var s1; - for ({ s1:s1 = 5 } of [{}]) { - } -}); - -(function() { - var s2; - for ({ s2 = 5 } of [{ s2: "" }]) { - } -}); - -(function() { - var s2; - for ({ s2:s2 = 5 } of [{ s2: "" }]) { - } -}); - -(function() { - var s3: string; - for ({ s3 = 5 } of [{ s3: "" }]) { - } -}); - -(function() { - var s3: string; - for ({ s3:s3 = 5 } of [{ s3: "" }]) { - } -}); - -(function() { - let y; - ({ y = 5 } = { y: 1 }) -}); - -(function() { - let y; - ({ y:y = 5 } = { y: 1 }) -}); - -(function() { - let y0: number; - ({ y0 = 5 } = { y0: 1 }) -}); - -(function() { - let y0: number; - ({ y0:y0 = 5 } = { y0: 1 }) -}); - -(function() { - let y1: string; - ({ y1 = 5 } = {}) -}); - -(function() { - let y1: string; - ({ y1:y1 = 5 } = {}) -}); - -(function() { - let y2: string, y3: { x: string }; - ({ y2 = 5, y3 = { x: 1 } } = {}) -}); - -(function() { - let y2: string, y3: { x: string }; - ({ y2:y2 = 5, y3:y3 = { x: 1 } } = {}) -}); - -(function() { - let y4: number, y5: { x: number }; - ({ y4 = 5, y5 = { x: 1 } } = {}) -}); - -(function() { - let y4: number, y5: { x: number }; - ({ y4:y4 = 5, y5:y5 = { x: 1 } } = {}) -}); - - -(function() { - let z; - ({ z = { x: 5 } } = { z: { x: 1 } }); -}); - - -(function() { - let z; - ({ z:z = { x: 5 } } = { z: { x: 1 } }); -}); - -(function() { - let a = { s = 5 }; -}); - -function foo({a = 4, b = { x: 5 }}) { -} - -//// [shorthandPropertyAssignmentsInDestructuring_ES6.js] -(function () { - var s0; - for ({ s0 = 5 } of [{ s0: 1 }]) { - } -}); -(function () { - var s0; - for ({ s0: s0 = 5 } of [{ s0: 1 }]) { - } -}); -(function () { - var s1; - for ({ s1 = 5 } of [{}]) { - } -}); -(function () { - var s1; - for ({ s1: s1 = 5 } of [{}]) { - } -}); -(function () { - var s2; - for ({ s2 = 5 } of [{ s2: "" }]) { - } -}); -(function () { - var s2; - for ({ s2: s2 = 5 } of [{ s2: "" }]) { - } -}); -(function () { - var s3; - for ({ s3 = 5 } of [{ s3: "" }]) { - } -}); -(function () { - var s3; - for ({ s3: s3 = 5 } of [{ s3: "" }]) { - } -}); -(function () { - let y; - ({ y = 5 } = { y: 1 }); -}); -(function () { - let y; - ({ y: y = 5 } = { y: 1 }); -}); -(function () { - let y0; - ({ y0 = 5 } = { y0: 1 }); -}); -(function () { - let y0; - ({ y0: y0 = 5 } = { y0: 1 }); -}); -(function () { - let y1; - ({ y1 = 5 } = {}); -}); -(function () { - let y1; - ({ y1: y1 = 5 } = {}); -}); -(function () { - let y2, y3; - ({ y2 = 5, y3 = { x: 1 } } = {}); -}); -(function () { - let y2, y3; - ({ y2: y2 = 5, y3: y3 = { x: 1 } } = {}); -}); -(function () { - let y4, y5; - ({ y4 = 5, y5 = { x: 1 } } = {}); -}); -(function () { - let y4, y5; - ({ y4: y4 = 5, y5: y5 = { x: 1 } } = {}); -}); -(function () { - let z; - ({ z = { x: 5 } } = { z: { x: 1 } }); -}); -(function () { - let z; - ({ z: z = { x: 5 } } = { z: { x: 1 } }); -}); -(function () { - let a = { s = 5 }; -}); -function foo({ a = 4, b = { x: 5 } }) { -} diff --git a/tests/baselines/reference/tsxErrorRecovery2.errors.txt b/tests/baselines/reference/tsxErrorRecovery2.errors.txt deleted file mode 100644 index 9a0b5790b2ce2..0000000000000 --- a/tests/baselines/reference/tsxErrorRecovery2.errors.txt +++ /dev/null @@ -1,18 +0,0 @@ -tests/cases/conformance/jsx/file1.tsx(6,1): error TS2657: JSX expressions must have one parent element -tests/cases/conformance/jsx/file2.tsx(2,1): error TS2657: JSX expressions must have one parent element - - -==== tests/cases/conformance/jsx/file1.tsx (1 errors) ==== - - declare namespace JSX { interface Element { } } - -
-
- - -!!! error TS2657: JSX expressions must have one parent element -==== tests/cases/conformance/jsx/file2.tsx (1 errors) ==== - var x =
- - -!!! error TS2657: JSX expressions must have one parent element \ No newline at end of file diff --git a/tests/baselines/reference/tsxErrorRecovery2.js b/tests/baselines/reference/tsxErrorRecovery2.js deleted file mode 100644 index be5b6c7342431..0000000000000 --- a/tests/baselines/reference/tsxErrorRecovery2.js +++ /dev/null @@ -1,19 +0,0 @@ -//// [tests/cases/conformance/jsx/tsxErrorRecovery2.tsx] //// - -//// [file1.tsx] - -declare namespace JSX { interface Element { } } - -
-
- -//// [file2.tsx] -var x =
- - -//// [file1.jsx] -
- , -
; -//// [file2.jsx] -var x =
,
; diff --git a/tests/baselines/reference/tsxErrorRecovery3.errors.txt b/tests/baselines/reference/tsxErrorRecovery3.errors.txt deleted file mode 100644 index f7e4bc170a9f0..0000000000000 --- a/tests/baselines/reference/tsxErrorRecovery3.errors.txt +++ /dev/null @@ -1,30 +0,0 @@ -tests/cases/conformance/jsx/file1.tsx(4,2): error TS2304: Cannot find name 'React'. -tests/cases/conformance/jsx/file1.tsx(5,2): error TS2304: Cannot find name 'React'. -tests/cases/conformance/jsx/file1.tsx(6,1): error TS2657: JSX expressions must have one parent element -tests/cases/conformance/jsx/file2.tsx(1,10): error TS2304: Cannot find name 'React'. -tests/cases/conformance/jsx/file2.tsx(1,21): error TS2304: Cannot find name 'React'. -tests/cases/conformance/jsx/file2.tsx(2,1): error TS2657: JSX expressions must have one parent element - - -==== tests/cases/conformance/jsx/file1.tsx (3 errors) ==== - - declare namespace JSX { interface Element { } } - -
- ~~~ -!!! error TS2304: Cannot find name 'React'. -
- ~~~ -!!! error TS2304: Cannot find name 'React'. - - -!!! error TS2657: JSX expressions must have one parent element -==== tests/cases/conformance/jsx/file2.tsx (3 errors) ==== - var x =
- ~~~ -!!! error TS2304: Cannot find name 'React'. - ~~~ -!!! error TS2304: Cannot find name 'React'. - - -!!! error TS2657: JSX expressions must have one parent element \ No newline at end of file diff --git a/tests/baselines/reference/tsxErrorRecovery3.js b/tests/baselines/reference/tsxErrorRecovery3.js deleted file mode 100644 index 93b34a14c1947..0000000000000 --- a/tests/baselines/reference/tsxErrorRecovery3.js +++ /dev/null @@ -1,19 +0,0 @@ -//// [tests/cases/conformance/jsx/tsxErrorRecovery3.tsx] //// - -//// [file1.tsx] - -declare namespace JSX { interface Element { } } - -
-
- -//// [file2.tsx] -var x =
- - -//// [file1.js] -React.createElement("div", null) - , - React.createElement("div", null); -//// [file2.js] -var x = React.createElement("div", null), React.createElement("div", null); diff --git a/tests/baselines/reference/underscoreTest1.types b/tests/baselines/reference/underscoreTest1.types index 7fe233c4490d2..9c9671b1a16f1 100644 --- a/tests/baselines/reference/underscoreTest1.types +++ b/tests/baselines/reference/underscoreTest1.types @@ -863,8 +863,8 @@ $('#underscore_button').bind('click', buttonView.onClick); >onClick : () => void var fibonacci = _.memoize(function (n) { ->fibonacci : (n: any) => any ->_.memoize(function (n) { return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);}) : (n: any) => any +>fibonacci : {} +>_.memoize(function (n) { return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);}) : {} >_.memoize : (func: T, hashFunction?: Function) => T >_ : Underscore.Static >memoize : (func: T, hashFunction?: Function) => T @@ -879,12 +879,12 @@ var fibonacci = _.memoize(function (n) { >n : any >fibonacci(n - 1) + fibonacci(n - 2) : any >fibonacci(n - 1) : any ->fibonacci : (n: any) => any +>fibonacci : {} >n - 1 : number >n : any >1 : number >fibonacci(n - 2) : any ->fibonacci : (n: any) => any +>fibonacci : {} >n - 2 : number >n : any >2 : number diff --git a/tests/cases/compiler/APISample_linter.ts b/tests/cases/compiler/APISample_linter.ts deleted file mode 100644 index 8cb6934cee37d..0000000000000 --- a/tests/cases/compiler/APISample_linter.ts +++ /dev/null @@ -1,67 +0,0 @@ -// @module: commonjs -// @includebuiltfile: typescript_standalone.d.ts -// @stripInternal:true - -/* - * Note: This test is a public API sample. The sample sources can be found - at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter - * Please log a "breaking change" issue for any API breaking change affecting this issue - */ - -declare var process: any; -declare var console: any; -declare var readFileSync: any; - -import * as ts from "typescript"; - -export function delint(sourceFile: ts.SourceFile) { - delintNode(sourceFile); - - function delintNode(node: ts.Node) { - switch (node.kind) { - case ts.SyntaxKind.ForStatement: - case ts.SyntaxKind.ForInStatement: - case ts.SyntaxKind.WhileStatement: - case ts.SyntaxKind.DoStatement: - if ((node).statement.kind !== ts.SyntaxKind.Block) { - report(node, "A looping statement's contents should be wrapped in a block body."); - } - break; - - case ts.SyntaxKind.IfStatement: - let ifStatement = (node); - if (ifStatement.thenStatement.kind !== ts.SyntaxKind.Block) { - report(ifStatement.thenStatement, "An if statement's contents should be wrapped in a block body."); - } - if (ifStatement.elseStatement && - ifStatement.elseStatement.kind !== ts.SyntaxKind.Block && - ifStatement.elseStatement.kind !== ts.SyntaxKind.IfStatement) { - report(ifStatement.elseStatement, "An else statement's contents should be wrapped in a block body."); - } - break; - - case ts.SyntaxKind.BinaryExpression: - let op = (node).operatorToken.kind; - if (op === ts.SyntaxKind.EqualsEqualsToken || op == ts.SyntaxKind.ExclamationEqualsToken) { - report(node, "Use '===' and '!=='.") - } - break; - } - - ts.forEachChild(node, delintNode); - } - - function report(node: ts.Node, message: string) { - let { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart()); - console.log(`${sourceFile.fileName} (${line + 1},${character + 1}): ${message}`); - } -} - -const fileNames = process.argv.slice(2); -fileNames.forEach(fileName => { - // Parse a file - let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true); - - // delint it - delint(sourceFile); -}); \ No newline at end of file diff --git a/tests/cases/compiler/arrayBufferIsViewNarrowsType.ts b/tests/cases/compiler/arrayBufferIsViewNarrowsType.ts deleted file mode 100644 index ba653408c20df..0000000000000 --- a/tests/cases/compiler/arrayBufferIsViewNarrowsType.ts +++ /dev/null @@ -1,5 +0,0 @@ -var obj: Object; -if (ArrayBuffer.isView(obj)) { - // isView should be a guard that narrows type to ArrayBufferView. - var ab: ArrayBufferView = obj; -} \ No newline at end of file diff --git a/tests/cases/compiler/assignmentCompat1.ts b/tests/cases/compiler/assignmentCompat1.ts index e93da76d95e06..b37a11b20d9bd 100644 --- a/tests/cases/compiler/assignmentCompat1.ts +++ b/tests/cases/compiler/assignmentCompat1.ts @@ -5,7 +5,3 @@ x = y; // Error y = x; // Ok because index signature type is any x = z; // Error z = x; // Ok because index signature type is any -y = "foo"; // Error -z = "foo"; // OK, string has numeric indexer -z = false; // Error - diff --git a/tests/cases/compiler/assignmentToParenthesizedExpression1.ts b/tests/cases/compiler/assignmentToParenthesizedExpression1.ts deleted file mode 100644 index 2afc08a753210..0000000000000 --- a/tests/cases/compiler/assignmentToParenthesizedExpression1.ts +++ /dev/null @@ -1,2 +0,0 @@ -var x; -(1, x)=0; \ No newline at end of file diff --git a/tests/cases/compiler/nestedRedeclarationInES6AMD.ts b/tests/cases/compiler/nestedRedeclarationInES6AMD.ts deleted file mode 100644 index e518e354cd086..0000000000000 --- a/tests/cases/compiler/nestedRedeclarationInES6AMD.ts +++ /dev/null @@ -1,8 +0,0 @@ -// @target: ES6 -// @module: AMD -function a() { - { - let status = 1; - status = 2; - } -} \ No newline at end of file diff --git a/tests/cases/compiler/objectLiteralExcessProperties.ts b/tests/cases/compiler/objectLiteralExcessProperties.ts deleted file mode 100644 index 0aa81703c33c3..0000000000000 --- a/tests/cases/compiler/objectLiteralExcessProperties.ts +++ /dev/null @@ -1,21 +0,0 @@ -interface Book { - foreword: string; -} - -interface Cover { - color?: string; -} - -var b1: Book = { forword: "oops" }; - -var b2: Book | string = { foreward: "nope" }; - -var b3: Book | (Book[]) = [{ foreword: "hello" }, { forwards: "back" }]; - -var b4: Book & Cover = { foreword: "hi", colour: "blue" }; - -var b5: Book & Cover = { foreward: "hi", color: "blue" }; - -var b6: Book & Cover = { foreword: "hi", color: "blue", price: 10.99 }; - -var b7: Book & number = { foreword: "hi", price: 10.99 }; diff --git a/tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts b/tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts deleted file mode 100644 index 9750ea5d16119..0000000000000 --- a/tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts +++ /dev/null @@ -1,118 +0,0 @@ -// @target: ES5 - - -(function() { - var s0; - for ({ s0 = 5 } of [{ s0: 1 }]) { - } -}); -(function() { - var s0; - for ({ s0:s0 = 5 } of [{ s0: 1 }]) { - } -}); - -(function() { - var s1; - for ({ s1 = 5 } of [{}]) { - } -}); - -(function() { - var s1; - for ({ s1:s1 = 5 } of [{}]) { - } -}); - -(function() { - var s2; - for ({ s2 = 5 } of [{ s2: "" }]) { - } -}); - -(function() { - var s2; - for ({ s2:s2 = 5 } of [{ s2: "" }]) { - } -}); - -(function() { - var s3: string; - for ({ s3 = 5 } of [{ s3: "" }]) { - } -}); - -(function() { - var s3: string; - for ({ s3:s3 = 5 } of [{ s3: "" }]) { - } -}); - -(function() { - let y; - ({ y = 5 } = { y: 1 }) -}); - -(function() { - let y; - ({ y:y = 5 } = { y: 1 }) -}); - -(function() { - let y0: number; - ({ y0 = 5 } = { y0: 1 }) -}); - -(function() { - let y0: number; - ({ y0:y0 = 5 } = { y0: 1 }) -}); - -(function() { - let y1: string; - ({ y1 = 5 } = {}) -}); - -(function() { - let y1: string; - ({ y1:y1 = 5 } = {}) -}); - -(function() { - let y2: string, y3: { x: string }; - ({ y2 = 5, y3 = { x: 1 } } = {}) -}); - -(function() { - let y2: string, y3: { x: string }; - ({ y2:y2 = 5, y3:y3 = { x: 1 } } = {}) -}); - -(function() { - let y4: number, y5: { x: number }; - ({ y4 = 5, y5 = { x: 1 } } = {}) -}); - -(function() { - let y4: number, y5: { x: number }; - ({ y4:y4 = 5, y5:y5 = { x: 1 } } = {}) -}); - - -(function() { - let z; - ({ z = { x: 5 } } = { z: { x: 1 } }); -}); - - -(function() { - let z; - ({ z:z = { x: 5 } } = { z: { x: 1 } }); -}); - -(function() { - let a = { s = 5 }; -}); - -function foo({a = 4, b = { x: 5 }}) { -} \ No newline at end of file diff --git a/tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts b/tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts deleted file mode 100644 index d479511b11dfb..0000000000000 --- a/tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts +++ /dev/null @@ -1,118 +0,0 @@ -// @target: ES6 - - -(function() { - var s0; - for ({ s0 = 5 } of [{ s0: 1 }]) { - } -}); -(function() { - var s0; - for ({ s0:s0 = 5 } of [{ s0: 1 }]) { - } -}); - -(function() { - var s1; - for ({ s1 = 5 } of [{}]) { - } -}); - -(function() { - var s1; - for ({ s1:s1 = 5 } of [{}]) { - } -}); - -(function() { - var s2; - for ({ s2 = 5 } of [{ s2: "" }]) { - } -}); - -(function() { - var s2; - for ({ s2:s2 = 5 } of [{ s2: "" }]) { - } -}); - -(function() { - var s3: string; - for ({ s3 = 5 } of [{ s3: "" }]) { - } -}); - -(function() { - var s3: string; - for ({ s3:s3 = 5 } of [{ s3: "" }]) { - } -}); - -(function() { - let y; - ({ y = 5 } = { y: 1 }) -}); - -(function() { - let y; - ({ y:y = 5 } = { y: 1 }) -}); - -(function() { - let y0: number; - ({ y0 = 5 } = { y0: 1 }) -}); - -(function() { - let y0: number; - ({ y0:y0 = 5 } = { y0: 1 }) -}); - -(function() { - let y1: string; - ({ y1 = 5 } = {}) -}); - -(function() { - let y1: string; - ({ y1:y1 = 5 } = {}) -}); - -(function() { - let y2: string, y3: { x: string }; - ({ y2 = 5, y3 = { x: 1 } } = {}) -}); - -(function() { - let y2: string, y3: { x: string }; - ({ y2:y2 = 5, y3:y3 = { x: 1 } } = {}) -}); - -(function() { - let y4: number, y5: { x: number }; - ({ y4 = 5, y5 = { x: 1 } } = {}) -}); - -(function() { - let y4: number, y5: { x: number }; - ({ y4:y4 = 5, y5:y5 = { x: 1 } } = {}) -}); - - -(function() { - let z; - ({ z = { x: 5 } } = { z: { x: 1 } }); -}); - - -(function() { - let z; - ({ z:z = { x: 5 } } = { z: { x: 1 } }); -}); - -(function() { - let a = { s = 5 }; -}); - -function foo({a = 4, b = { x: 5 }}) { -} \ No newline at end of file diff --git a/tests/cases/conformance/es6/for-ofStatements/for-of47.ts b/tests/cases/conformance/es6/for-ofStatements/for-of47.ts deleted file mode 100644 index bd21a0db382be..0000000000000 --- a/tests/cases/conformance/es6/for-ofStatements/for-of47.ts +++ /dev/null @@ -1,8 +0,0 @@ -//@target: ES6 -var x: string, y: number; -var array = [{ x: "", y: true }] -enum E { x } -for ({x, y: y = E.x} of array) { - x; - y; -} \ No newline at end of file diff --git a/tests/cases/conformance/es6/for-ofStatements/for-of48.ts b/tests/cases/conformance/es6/for-ofStatements/for-of48.ts deleted file mode 100644 index c2e228d0fb923..0000000000000 --- a/tests/cases/conformance/es6/for-ofStatements/for-of48.ts +++ /dev/null @@ -1,8 +0,0 @@ -//@target: ES6 -var x: string, y: number; -var array = [{ x: "", y: true }] -enum E { x } -for ({x, y = E.x} of array) { - x; - y; -} \ No newline at end of file diff --git a/tests/cases/conformance/jsx/tsxErrorRecovery2.tsx b/tests/cases/conformance/jsx/tsxErrorRecovery2.tsx deleted file mode 100644 index e189e0e7791bd..0000000000000 --- a/tests/cases/conformance/jsx/tsxErrorRecovery2.tsx +++ /dev/null @@ -1,10 +0,0 @@ -//@jsx: preserve - -//@filename: file1.tsx -declare namespace JSX { interface Element { } } - -
-
- -//@filename: file2.tsx -var x =
diff --git a/tests/cases/conformance/jsx/tsxErrorRecovery3.tsx b/tests/cases/conformance/jsx/tsxErrorRecovery3.tsx deleted file mode 100644 index 9d444b07cb20c..0000000000000 --- a/tests/cases/conformance/jsx/tsxErrorRecovery3.tsx +++ /dev/null @@ -1,10 +0,0 @@ -//@jsx: react - -//@filename: file1.tsx -declare namespace JSX { interface Element { } } - -
-
- -//@filename: file2.tsx -var x =
diff --git a/tests/cases/fourslash/formatAfterMultilineComment.ts b/tests/cases/fourslash/formatAfterMultilineComment.ts deleted file mode 100644 index b795ab5c8c9ce..0000000000000 --- a/tests/cases/fourslash/formatAfterMultilineComment.ts +++ /dev/null @@ -1,7 +0,0 @@ -/// -/////*foo -////*/"123123"; - -format.document(); -verify.currentFileContentIs(`/*foo -*/"123123";`) diff --git a/tests/webTestServer.ts b/tests/webTestServer.ts index 18a1f05d47ba4..6d3144eeaec52 100644 --- a/tests/webTestServer.ts +++ b/tests/webTestServer.ts @@ -5,7 +5,6 @@ import fs = require("fs"); import path = require("path"); import url = require("url"); import child_process = require("child_process"); -import os = require('os'); /// Command line processing /// @@ -264,20 +263,7 @@ http.createServer(function (req: http.ServerRequest, res: http.ServerResponse) { var browserPath: string; if ((browser && browser === 'chrome')) { - let defaultChromePath = ""; - switch (os.platform()) { - case "win32": - case "win64": - defaultChromePath = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"; - break; - case "linux": - defaultChromePath = "/opt/google/chrome/chrome" - break; - default: - console.log(`default Chrome location is unknown for platform '${os.platform()}'`); - break; - } - + var defaultChromePath = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"; if (fs.existsSync(defaultChromePath)) { browserPath = defaultChromePath; } else { From d34b4e28db39a6c1fecf954b4ce1af8b21ef3235 Mon Sep 17 00:00:00 2001 From: Yanxing Wang Date: Fri, 23 Oct 2015 01:36:16 -0700 Subject: [PATCH 02/43] lint --- src/compiler/.idea/typescript-compiler.xml | 6 + src/compiler/.idea/workspace.xml | 117 +++++- src/compiler/checker.ts | 348 +++++++++--------- src/compiler/emitter.ts | 33 +- src/compiler/parser.ts | 2 +- src/compiler/types.ts | 2 +- .../reference/castExpressionParentheses.js | 94 ----- .../castExpressionParentheses.symbols | 79 ---- .../reference/castExpressionParentheses.types | 246 ------------- .../baselines/reference/defaultIndexProps2.js | 30 -- .../reference/defaultIndexProps2.symbols | 24 -- .../reference/defaultIndexProps2.types | 35 -- tests/baselines/reference/enumBasics.symbols | 210 ----------- tests/baselines/reference/enumBasics.types | 232 ------------ ...ralsWithTrailingDecimalPoints01.errors.txt | 33 -- ...ericLiteralsWithTrailingDecimalPoints01.js | 38 -- .../parserComputedPropertyName25.errors.txt | 17 - .../reference/parserComputedPropertyName25.js | 14 - .../parserComputedPropertyName26.errors.txt | 14 - .../reference/parserComputedPropertyName26.js | 13 - .../parserComputedPropertyName27.errors.txt | 17 - .../reference/parserComputedPropertyName27.js | 14 - .../parserComputedPropertyName33.errors.txt | 20 - .../reference/parserComputedPropertyName33.js | 15 - .../stringHasStringValuedNumericIndexer.js | 5 - ...tringHasStringValuedNumericIndexer.symbols | 4 - .../stringHasStringValuedNumericIndexer.types | 7 - .../compiler/castExpressionParentheses.ts | 49 --- tests/cases/compiler/defaultIndexProps2.ts | 14 - tests/cases/compiler/enumBasics1.ts | 37 -- ...ericLiteralsWithTrailingDecimalPoints01.ts | 17 - .../stringHasStringValuedNumericIndexer.ts | 1 - tests/cases/conformance/enums/enumBasics.ts | 79 ---- .../parserComputedPropertyName25.ts | 6 - .../parserComputedPropertyName26.ts | 6 - .../parserComputedPropertyName27.ts | 6 - .../parserComputedPropertyName33.ts | 6 - 37 files changed, 318 insertions(+), 1572 deletions(-) create mode 100644 src/compiler/.idea/typescript-compiler.xml delete mode 100644 tests/baselines/reference/castExpressionParentheses.js delete mode 100644 tests/baselines/reference/castExpressionParentheses.symbols delete mode 100644 tests/baselines/reference/castExpressionParentheses.types delete mode 100644 tests/baselines/reference/defaultIndexProps2.js delete mode 100644 tests/baselines/reference/defaultIndexProps2.symbols delete mode 100644 tests/baselines/reference/defaultIndexProps2.types delete mode 100644 tests/baselines/reference/enumBasics.symbols delete mode 100644 tests/baselines/reference/enumBasics.types delete mode 100644 tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.errors.txt delete mode 100644 tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js delete mode 100644 tests/baselines/reference/parserComputedPropertyName25.errors.txt delete mode 100644 tests/baselines/reference/parserComputedPropertyName25.js delete mode 100644 tests/baselines/reference/parserComputedPropertyName26.errors.txt delete mode 100644 tests/baselines/reference/parserComputedPropertyName26.js delete mode 100644 tests/baselines/reference/parserComputedPropertyName27.errors.txt delete mode 100644 tests/baselines/reference/parserComputedPropertyName27.js delete mode 100644 tests/baselines/reference/parserComputedPropertyName33.errors.txt delete mode 100644 tests/baselines/reference/parserComputedPropertyName33.js delete mode 100644 tests/baselines/reference/stringHasStringValuedNumericIndexer.js delete mode 100644 tests/baselines/reference/stringHasStringValuedNumericIndexer.symbols delete mode 100644 tests/baselines/reference/stringHasStringValuedNumericIndexer.types delete mode 100644 tests/cases/compiler/castExpressionParentheses.ts delete mode 100644 tests/cases/compiler/defaultIndexProps2.ts delete mode 100644 tests/cases/compiler/enumBasics1.ts delete mode 100644 tests/cases/compiler/numericLiteralsWithTrailingDecimalPoints01.ts delete mode 100644 tests/cases/compiler/stringHasStringValuedNumericIndexer.ts delete mode 100644 tests/cases/conformance/enums/enumBasics.ts delete mode 100644 tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName25.ts delete mode 100644 tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName26.ts delete mode 100644 tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName27.ts delete mode 100644 tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName33.ts diff --git a/src/compiler/.idea/typescript-compiler.xml b/src/compiler/.idea/typescript-compiler.xml new file mode 100644 index 0000000000000..25cc839394c7f --- /dev/null +++ b/src/compiler/.idea/typescript-compiler.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/src/compiler/.idea/workspace.xml b/src/compiler/.idea/workspace.xml index 632469ad277f7..46b662229c913 100644 --- a/src/compiler/.idea/workspace.xml +++ b/src/compiler/.idea/workspace.xml @@ -19,6 +19,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true @@ -81,8 +139,12 @@ + + + + @@ -94,13 +156,16 @@ - + + - + + + @@ -109,12 +174,18 @@ - - + + + + @@ -122,4 +193,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6901caeb80a57..3067ea9301a1e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -111,13 +111,13 @@ namespace ts { let unknownType = createIntrinsicType(TypeFlags.Any, "unknown"); let circularType = createIntrinsicType(TypeFlags.Any, "__circular__"); - var i8Type = createIntrinsicType(TypeFlags.I8, "i8"); - var u8Type = createIntrinsicType(TypeFlags.U8, "u8"); - var i16Type = createIntrinsicType(TypeFlags.I16, "i16"); - var u16Type = createIntrinsicType(TypeFlags.U16, "u16"); - var i32Type = createIntrinsicType(TypeFlags.I32, "i32"); - var u32Type = createIntrinsicType(TypeFlags.U32, "u32"); - var floatType = createIntrinsicType(TypeFlags.Float, "float"); + let i8Type = createIntrinsicType(TypeFlags.I8, "i8"); + let u8Type = createIntrinsicType(TypeFlags.U8, "u8"); + let i16Type = createIntrinsicType(TypeFlags.I16, "i16"); + let u16Type = createIntrinsicType(TypeFlags.U16, "u16"); + let i32Type = createIntrinsicType(TypeFlags.I32, "i32"); + let u32Type = createIntrinsicType(TypeFlags.U32, "u32"); + let floatType = createIntrinsicType(TypeFlags.Float, "float"); let emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); let emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); @@ -126,7 +126,7 @@ namespace ts { let anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); // The anyFunctionType contains the anyFunctionType by definition. The flag is further propagated // in getPropagatingFlagsOfTypes, and it is checked in inferFromTypes. - //anyFunctionType.flags |= TypeFlags.ContainsAnyFunctionType; + // anyFunctionType.flags |= TypeFlags.ContainsAnyFunctionType; let noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); @@ -155,17 +155,6 @@ namespace ts { let globalIteratorType: GenericType; let globalIterableIteratorType: GenericType; - var globalIntType : ObjectType; - var globalUintType : ObjectType; - var globalI8Type : ObjectType; - var globalU8Type : ObjectType; - var globalI16Type : ObjectType; - var globalU16Type : ObjectType; - var globalI32Type : ObjectType; - var globalU32Type : ObjectType; - var globalFloatType : ObjectType; - var globalDoubleType : ObjectType; - let anyArrayType: Type; let getGlobalClassDecoratorType: () => ObjectType; let getGlobalParameterDecoratorType: () => ObjectType; @@ -6068,9 +6057,9 @@ namespace ts { // it as an inference candidate. Hopefully, a better candidate will come along that does // not contain anyFunctionType when we come back to this argument for its second round // of inference. - //if (source.flags & TypeFlags.ContainsAnyFunctionType) { + // if (source.flags & TypeFlags.ContainsAnyFunctionType) { // return; - //} + // } let typeParameters = context.typeParameters; for (let i = 0; i < typeParameters.length; i++) { @@ -7174,7 +7163,7 @@ namespace ts { return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined; } - function getContextualTypeForJsxExpression(expr: JsxExpression|JsxSpreadAttribute): Type { + function getContextualTypeForJsxExpression(expr: JsxExpression | JsxSpreadAttribute): Type { // Contextual type only applies to JSX expressions that are in attribute assignments (not in 'Children' positions) if (expr.parent.kind === SyntaxKind.JsxAttribute) { let attrib = expr.parent; @@ -7685,7 +7674,7 @@ namespace ts { /** * Returns true iff React would emit this tag name as a string rather than an identifier or qualified name */ - function isJsxIntrinsicIdentifier(tagName: Identifier|QualifiedName) { + function isJsxIntrinsicIdentifier(tagName: Identifier | QualifiedName) { if (tagName.kind === SyntaxKind.QualifiedName) { return false; } @@ -7771,7 +7760,7 @@ namespace ts { /// If this is a class-based tag (otherwise returns undefined), returns the symbol of the class /// type or factory function. /// Otherwise, returns unknownSymbol. - function getJsxElementTagSymbol(node: JsxOpeningLikeElement|JsxClosingElement): Symbol { + function getJsxElementTagSymbol(node: JsxOpeningLikeElement | JsxClosingElement): Symbol { let flags: JsxFlags = JsxFlags.UnknownElement; let links = getNodeLinks(node); if (!links.resolvedSymbol) { @@ -7784,7 +7773,7 @@ namespace ts { } return links.resolvedSymbol; - function lookupIntrinsicTag(node: JsxOpeningLikeElement|JsxClosingElement): Symbol { + function lookupIntrinsicTag(node: JsxOpeningLikeElement | JsxClosingElement): Symbol { let intrinsicElementsType = getJsxIntrinsicElementsType(); if (intrinsicElementsType !== unknownType) { // Property case @@ -7812,7 +7801,7 @@ namespace ts { } } - function lookupClassTag(node: JsxOpeningLikeElement|JsxClosingElement): Symbol { + function lookupClassTag(node: JsxOpeningLikeElement | JsxClosingElement): Symbol { let valueSymbol: Symbol = resolveJsxTagName(node); // Look up the value in the current scope @@ -7826,7 +7815,7 @@ namespace ts { return valueSymbol || unknownSymbol; } - function resolveJsxTagName(node: JsxOpeningLikeElement|JsxClosingElement): Symbol { + function resolveJsxTagName(node: JsxOpeningLikeElement | JsxClosingElement): Symbol { if (node.tagName.kind === SyntaxKind.Identifier) { let tag = node.tagName; let sym = getResolvedSymbol(tag); @@ -8243,7 +8232,7 @@ namespace ts { } } - var symbol = getResolvedSymbol((node.expression)); + let symbol = getResolvedSymbol((node.expression)); if (symbol && (symbol.flags & SymbolFlags.Struct)) { node.isStructArray = true; } @@ -8703,7 +8692,8 @@ namespace ts { if (argType.flags & TypeFlags.PrimitiveType) { args[i] = convertToLower(argType, argType, arg, true); - } else if (paramType.flags & TypeFlags.PrimitiveType) { + } + else if (paramType.flags & TypeFlags.PrimitiveType) { args[i] = convertToLower(paramType, argType, arg); } } @@ -10044,8 +10034,8 @@ namespace ts { function checkPrefixUnaryExpression(node: PrefixUnaryExpression): Type { let operandType = checkExpression(node.operand); - var primType : boolean = false; - if(operandType.flags & TypeFlags.PrimitiveType) primType = true; + let primType = false; + if (operandType.flags & TypeFlags.PrimitiveType) primType = true; switch (node.operator) { case SyntaxKind.PlusToken: @@ -10066,38 +10056,39 @@ namespace ts { Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer, Diagnostics.The_operand_of_an_increment_or_decrement_operator_cannot_be_a_constant); } - if(primType) { - var newOp : SyntaxKind = node.operator === SyntaxKind.PlusPlusToken ? SyntaxKind.PlusToken : SyntaxKind.MinusToken; - var newNode = createBinaryNode(node.operand, SyntaxKind.EqualsToken, createLitNode(node.operand, newOp, "1")); - var retType = checkBinaryExpression(newNode); + if (primType) { + let newOp: SyntaxKind = node.operator === SyntaxKind.PlusPlusToken ? SyntaxKind.PlusToken : SyntaxKind.MinusToken; + let newNode = createBinaryNode(node.operand, SyntaxKind.EqualsToken, createLitNode(node.operand, newOp, "1")); + let retType = checkBinaryExpression(newNode); node.operator = SyntaxKind.Unknown; node.operand = createParenNode(newNode); return retType; - } else { + } + else { return numberType; } } return unknownType; } - function checkDuplicationOfName(name : string, block : Node) : boolean { - var symbol = resolveName(block, name, SymbolFlags.Variable, undefined, ""); + function checkDuplicationOfName(name: string, block: Node): boolean { + let symbol = resolveName(block, name, SymbolFlags.Variable, undefined, ""); return symbol !== undefined; } - function getNextHelperVarName(block : Block | SourceFile) : string { - if(block.helperVarCount === undefined) block.helperVarCount = 0; - var count : Number = block.helperVarCount++; + function getNextHelperVarName(block: Block | SourceFile): string { + if (block.helperVarCount === undefined) block.helperVarCount = 0; + let count: Number = block.helperVarCount++; - var name = "_$" + count.toString(); - while(checkDuplicationOfName(name, block)) { + let name = "_$" + count.toString(); + while (checkDuplicationOfName(name, block)) { count = block.helperVarCount++; name = "_$" + count.toString(); } return name; } - function createVariableDeclarationStatement(varName : string, varType? : Type) : VariableStatement{ + function createVariableDeclarationStatement(varName: string, varType?: Type): VariableStatement { let ret = createNode(SyntaxKind.VariableStatement); let varDecl = createNode(SyntaxKind.VariableDeclaration); @@ -10106,7 +10097,6 @@ namespace ts { let varNameIdentifier = createNode(SyntaxKind.Identifier); varNameIdentifier.text = varName; - //varNameIdentifier.parent = varDecl; varDecl.name = varNameIdentifier; varDecl.parent = declList; @@ -10115,18 +10105,18 @@ namespace ts { return ret; } - function createPostIncrementExpression(tempVarIdentifier : Identifier, incExp : Node) { - //return: (t = incExp, incExp = incExp + 1, t) + function createPostIncrementExpression(tempVarIdentifier: Identifier, incExp: Node) { + // return: (t = incExp, incExp = incExp + 1, t) - var nodeLeft = createBinaryNode(tempVarIdentifier, SyntaxKind.EqualsToken, incExp); + let nodeLeft = createBinaryNode(tempVarIdentifier, SyntaxKind.EqualsToken, incExp); - var nodeMid1 = createLitNode(incExp, SyntaxKind.PlusToken, "1"); - var nodeMid0 = createBinaryNode(incExp, SyntaxKind.EqualsToken, nodeMid1); + let nodeMid1 = createLitNode(incExp, SyntaxKind.PlusToken, "1"); + let nodeMid0 = createBinaryNode(incExp, SyntaxKind.EqualsToken, nodeMid1); checkExpression(nodeMid0); - var nodeRight = tempVarIdentifier; + let nodeRight = tempVarIdentifier; - var ret = createBinaryNode(nodeLeft, SyntaxKind.CommaToken, nodeMid0); + let ret = createBinaryNode(nodeLeft, SyntaxKind.CommaToken, nodeMid0); ret = createParenNode(createBinaryNode(ret, SyntaxKind.CommaToken, nodeRight)); return ret; } @@ -10134,29 +10124,30 @@ namespace ts { function checkPostfixUnaryExpression(node: PostfixUnaryExpression): Type { let operandType = checkExpression(node.operand); - var primType : boolean = false; - if(operandType.flags & TypeFlags.PrimitiveType) primType = true; + let primType = false; + if (operandType.flags & TypeFlags.PrimitiveType) primType = true; - if(primType && node.operator != SyntaxKind.Unknown) { - var block = node.parent; - while(block != undefined) { - var blockInstance : Block | SourceFile; + if (primType && node.operator != SyntaxKind.Unknown) { + let block = node.parent; + while (block != undefined) { + let blockInstance: Block | SourceFile; if (block.kind === SyntaxKind.Block) { blockInstance = (block); - } else if(block.kind === SyntaxKind.SourceFile) { + } + else if (block.kind === SyntaxKind.SourceFile) { blockInstance = (block); } - if(blockInstance != undefined && blockInstance.statements != undefined) { - var helperVarName = getNextHelperVarName(blockInstance); - var varDecl = createVariableDeclarationStatement(helperVarName); + if (blockInstance != undefined && blockInstance.statements != undefined) { + let helperVarName = getNextHelperVarName(blockInstance); + let varDecl = createVariableDeclarationStatement(helperVarName); varDecl.parent = blockInstance; - if(blockInstance.helperStatements === undefined) { + if (blockInstance.helperStatements === undefined) { blockInstance.helperStatements = >[]; } blockInstance.helperStatements.push(varDecl); - var nameId = (varDecl.declarationList.declarations[0].name); + let nameId = (varDecl.declarationList.declarations[0].name); node.operand = createPostIncrementExpression(nameId, node.operand); node.operator = SyntaxKind.Unknown; break; @@ -10359,29 +10350,34 @@ namespace ts { return sourceType; } - function getSizeOfPrimType(type : Type) : number { - if(type.flags & (TypeFlags.U8 | type.flags & TypeFlags.I8)) { + function getSizeOfPrimType(type: Type): number { + if (type.flags & (TypeFlags.U8 | type.flags & TypeFlags.I8)) { return 1; - } else if(type.flags & (TypeFlags.U16 | type.flags & TypeFlags.I16)) { + } + else if (type.flags & (TypeFlags.U16 | type.flags & TypeFlags.I16)) { return 2; - } else if(type.flags & (TypeFlags.U32 | type.flags & TypeFlags.I32 | type.flags & TypeFlags.Float)) { + } + else if (type.flags & (TypeFlags.U32 | type.flags & TypeFlags.I32 | type.flags & TypeFlags.Float)) { return 4; - } else if(type.flags & (TypeFlags.Number | TypeFlags.Enum)) { + } + else if (type.flags & (TypeFlags.Number | TypeFlags.Enum)) { return 8; - } else { + } + else { return 8; } } - function isSigned(type : Type) { - if(type.flags & (TypeFlags.U8 | type.flags & TypeFlags.U16 | type.flags & TypeFlags.U32)) { + function isSigned(type: Type) { + if (type.flags & (TypeFlags.U8 | type.flags & TypeFlags.U16 | type.flags & TypeFlags.U32)) { return false; - } else { + } + else { return true; } } - function createLitNode(node : Node, op : SyntaxKind, lit : string) { + function createLitNode(node: Node, op: SyntaxKind, lit: string) { let newer = createNode(SyntaxKind.BinaryExpression); let literal = createNode(SyntaxKind.NumericLiteral); @@ -10394,14 +10390,14 @@ namespace ts { return newer; } - function createParenNode(node : Node) { + function createParenNode(node: Node) { let newer = createNode(SyntaxKind.ParenthesizedExpression); newer.expression = (node); return newer; } - function createBinaryNode(node : Node, op : SyntaxKind, nodeRight : Node) : Expression{ - var newer = (new (objectAllocator.getNodeConstructor(SyntaxKind.BinaryExpression))()); + function createBinaryNode(node: Node, op: SyntaxKind, nodeRight: Node): Expression { + let newer = (new (objectAllocator.getNodeConstructor(SyntaxKind.BinaryExpression))()); newer.pos = node.pos; newer.end = node.end; @@ -10412,8 +10408,8 @@ namespace ts { return newer; } - function isAssignmentOp(op : SyntaxKind) { - switch(op){ + function isAssignmentOp(op: SyntaxKind) { + switch (op) { case SyntaxKind.EqualsToken: case SyntaxKind.PlusEqualsToken: case SyntaxKind.MinusEqualsToken: @@ -10432,8 +10428,8 @@ namespace ts { } } - function splitAssignmentOp(op : SyntaxKind) { - switch(op){ + function splitAssignmentOp(op: SyntaxKind) { + switch (op) { case SyntaxKind.PlusEqualsToken: return SyntaxKind.PlusToken; case SyntaxKind.MinusEqualsToken: @@ -10461,13 +10457,14 @@ namespace ts { } } - function getNumericLiteralValue(originNode : Node) : number { - var val : number; - if(originNode.kind == SyntaxKind.NumericLiteral) { - var text = ((originNode)).text; + function getNumericLiteralValue(originNode: Node): number { + let val: number; + if (originNode.kind == SyntaxKind.NumericLiteral) { + let text = ((originNode)).text; val = parseFloat(text); - } else { - var unaryNode = originNode; + } + else { + let unaryNode = originNode; /* if(unaryNode.operand.kind == SyntaxKind.NumericLiteral){ var text = ((unaryNode.operand)).text; @@ -10489,26 +10486,28 @@ namespace ts { return val; } - function canIgnoreConvertNumericLit(targetType : Type, originType : Type, originNode : Expression) { + function canIgnoreConvertNumericLit(targetType: Type, originType: Type, originNode: Expression) { if (originNode.kind == SyntaxKind.NumericLiteral || originNode.kind == SyntaxKind.PostfixUnaryExpression) { - var val : number = getNumericLiteralValue(originNode); - var val2 : number; + let val: number = getNumericLiteralValue(originNode); + let val2: number; - var sizeLeft = getSizeOfPrimType(targetType); - var signedLeft = isSigned(targetType); - var widthLeft = sizeLeft << 3; - var sizeRight = getSizeOfPrimType(originType); - var signedRight = isSigned(originType); - var widthRight = sizeRight << 3; + let sizeLeft = getSizeOfPrimType(targetType); + let signedLeft = isSigned(targetType); + let widthLeft = sizeLeft << 3; + let sizeRight = getSizeOfPrimType(originType); + let signedRight = isSigned(originType); + let widthRight = sizeRight << 3; if (widthLeft !== 32 && widthLeft < widthRight) { val2 = val & ((1 << widthLeft) - 1); if (signedLeft) val2 = (val2 << (32 - widthLeft)) >> (32 - widthLeft); - } else if (widthLeft !== widthRight || signedLeft != signedRight) { + } + else if (widthLeft !== widthRight || signedLeft != signedRight) { if (signedLeft) val2 = val | 0; else val2 = val >>> 0; - } else { + } + else { val2 = val; } @@ -10519,15 +10518,14 @@ namespace ts { return false; } - function convertDoubleToFloat(originNode : Expression) : Expression { - + function convertDoubleToFloat(originNode: Expression): Expression { emitMathFround = true; - var funcName = (new (objectAllocator.getNodeConstructor(SyntaxKind.Identifier))()); + let funcName = (new (objectAllocator.getNodeConstructor(SyntaxKind.Identifier))()); funcName.text = "Math.fround"; funcName.pos = funcName.end = -1; - var newer = (new (objectAllocator.getNodeConstructor(SyntaxKind.CallExpression))()); + let newer = (new (objectAllocator.getNodeConstructor(SyntaxKind.CallExpression))()); newer.expression = funcName; newer.arguments = >[]; newer.arguments.push(originNode); @@ -10537,35 +10535,35 @@ namespace ts { return newer; } - function convertToLower(targetType : Type, originType : Type, originNode : Expression, forceConv : boolean = false) : Expression { - if(originType.flags & TypeFlags.RealNumber && + function convertToLower(targetType: Type, originType: Type, originNode: Expression, forceConv = false): Expression { + if (originType.flags & TypeFlags.RealNumber && targetType.flags & TypeFlags.SinglePrecisionFloat) { return convertDoubleToFloat(originNode); } - if(!(targetType.flags & TypeFlags.PrimitiveType)) { + if (!(targetType.flags & TypeFlags.PrimitiveType)) { return originNode; } - if(targetType.flags == originType.flags && !forceConv){ + if (targetType.flags == originType.flags && !forceConv) { return originNode; } - if(canIgnoreConvertNumericLit(targetType, originType, originNode)) { + if (canIgnoreConvertNumericLit(targetType, originType, originNode)) { return originNode; } - if(!forceConv && !isTypeAssignableTo(originType, targetType)) { + if (!forceConv && !isTypeAssignableTo(originType, targetType)) { return originNode; } - var sizeLeft = getSizeOfPrimType(targetType); - var signedLeft = isSigned(targetType); - var widthLeft = sizeLeft << 3; - var mask = (1 << widthLeft) - 1; - var shift = 32 - widthLeft; + let sizeLeft = getSizeOfPrimType(targetType); + let signedLeft = isSigned(targetType); + let widthLeft = sizeLeft << 3; + let mask = (1 << widthLeft) - 1; + let shift = 32 - widthLeft; - var widthRight = getSizeOfPrimType(originType) << 3; + let widthRight = getSizeOfPrimType(originType) << 3; - var retNode : Expression = undefined; + let retNode: Expression = undefined; if (widthLeft !== 32 && widthLeft < widthRight) { retNode = createLitNode(originNode, SyntaxKind.AmpersandToken, new Number(mask).toString()); @@ -10575,7 +10573,8 @@ namespace ts { retNode = createLitNode(retNode, SyntaxKind.LessThanLessThanToken, new Number(shift).toString()); retNode = createLitNode(retNode, SyntaxKind.GreaterThanGreaterThanToken, new Number(shift).toString()); } - } else { + } + else { retNode = createLitNode(originNode, signedLeft ? SyntaxKind.BarToken : SyntaxKind.GreaterThanGreaterThanGreaterThanToken, "0"); } @@ -10583,7 +10582,7 @@ namespace ts { } function checkAndMarkExpression(node: Expression, contextualMapper?: TypeMapper): Type { - var result = checkExpression(node, contextualMapper); + let result = checkExpression(node, contextualMapper); getNodeLinks(node).flags |= NodeCheckFlags.TypeChecked; return result; } @@ -10593,17 +10592,17 @@ namespace ts { } function checkBinaryLikeExpression(left: Expression, operatorToken: Node, right: Expression, contextualMapper?: TypeMapper, errorNode?: Node) { - function getBestSizeFromNumberLikes(leftType : Type, rightType : Type) : Type { - if((leftType.flags & TypeFlags.Enum) || (rightType.flags & TypeFlags.Enum)) { + function getBestSizeFromNumberLikes(leftType: Type, rightType: Type): Type { + if ((leftType.flags & TypeFlags.Enum) || (rightType.flags & TypeFlags.Enum)) { return numberType; } - if(leftType.flags & TypeFlags.Number || rightType.flags & TypeFlags.Number) { + if (leftType.flags & TypeFlags.Number || rightType.flags & TypeFlags.Number) { return numberType; } - if(leftType.flags & TypeFlags.Float || rightType.flags & TypeFlags.Float) { + if (leftType.flags & TypeFlags.Float || rightType.flags & TypeFlags.Float) { return floatType; } - if(leftType.flags & TypeFlags.U32 || rightType.flags & TypeFlags.U32) { + if (leftType.flags & TypeFlags.U32 || rightType.flags & TypeFlags.U32) { return u32Type; } return i32Type; @@ -10616,34 +10615,36 @@ namespace ts { let leftType = checkExpression(left, contextualMapper); let rightType = checkExpression(right, contextualMapper); - var node = (errorNode); - var splitedOp = splitAssignmentOp(operator); + let node = (errorNode); + let splitedOp = splitAssignmentOp(operator); - if(leftType.flags & TypeFlags.PrimitiveType && splitedOp != undefined) { + if (leftType.flags & TypeFlags.PrimitiveType && splitedOp != undefined) { operator = node.operatorToken.kind = SyntaxKind.EqualsToken; - var lowerNode = convertToLower(leftType, rightType, node.right); + let lowerNode = convertToLower(leftType, rightType, node.right); node.right = createBinaryNode(node.left, splitedOp, createParenNode(lowerNode)); } if (isAssignmentOp(operator)) { if (leftType.flags & TypeFlags.Reference) { - var tRef = leftType; + let tRef = leftType; if (tRef.target === globalArrayType && node.right.kind === SyntaxKind.ArrayLiteralExpression && tRef.typeArguments[0].flags & TypeFlags.PrimitiveType) { - for (var i = 0; i < (node.right).elements.length; ++i) { - var origType = checkAndMarkExpression((node.right).elements[i]); + for (let i = 0; i < (node.right).elements.length; ++i) { + let origType = checkAndMarkExpression((node.right).elements[i]); (node.right).elements[i] = convertToLower(tRef.typeArguments[0], origType, (node.right).elements[i]); } } - } else { - var lowerNode = convertToLower(leftType, rightType, node.right); + } + else { + let lowerNode = convertToLower(leftType, rightType, node.right); - if(lowerNode != node.right) { + if (lowerNode != node.right) { node.right = lowerNode; rightType = leftType; - } else { - if(leftType.flags & TypeFlags.PrimitiveType && + } + else { + if (leftType.flags & TypeFlags.PrimitiveType && canIgnoreConvertNumericLit(leftType, rightType, node.right)) { rightType = leftType; } @@ -10701,14 +10702,16 @@ namespace ts { } if (leftType.flags & TypeFlags.NumberLike && rightType.flags & TypeFlags.NumberLike) { - var leftSize = getSizeOfPrimType(leftType); - var rightSize = getSizeOfPrimType(rightType); + let leftSize = getSizeOfPrimType(leftType); + let rightSize = getSizeOfPrimType(rightType); if (leftSize != rightSize) { if (canIgnoreConvertNumericLit(leftType, rightType, node.right)) { return leftType; - } else if (canIgnoreConvertNumericLit(rightType, leftType, node.left)) { + } + else if (canIgnoreConvertNumericLit(rightType, leftType, node.left)) { return rightType; - } else { + } + else { return leftSize < rightSize ? rightType : leftType; } } @@ -12450,21 +12453,21 @@ namespace ts { checkFunctionAndClassExpressionBodies(node); } - if(node.helperStatements !== undefined) { - while(node.helperStatements.length > 0){ + if (node.helperStatements !== undefined) { + while (node.helperStatements.length > 0) { node.statements.unshift(node.helperStatements.pop()); } } - if(isFunctionBlock(node)) { - var func = getContainingFunction(node); + if (isFunctionBlock(node)) { + let func = getContainingFunction(node); for (var i = 0, n = func.parameters.length; i < n; i++) { - var param = func.parameters[i]; + let param = func.parameters[i]; - if(param.type && param.name) { - var type = getTypeFromTypeNode(param.type); - //if(type && type.flags & TypeFlags.PrimitiveType) { + if (param.type && param.name) { + let type = getTypeFromTypeNode(param.type); + // if(type && type.flags & TypeFlags.PrimitiveType) { // var nameNode : Identifier; // if (typeof param.name === 'Identifier') { // nameNode = (param.name); @@ -12475,7 +12478,7 @@ namespace ts { // ret.expression.parent = ret; // ret.parent = node; // node.statements.unshift(ret); - //} + // } } } } @@ -12643,8 +12646,7 @@ namespace ts { // names of block-scoped and function scoped variables can collide only // if block scoped variable is defined in the function\module\source file scope (because of variable hoisting) let namesShareScope = - container && - (container.kind === SyntaxKind.Block && isFunctionLike(container.parent) || + container && (container.kind === SyntaxKind.Block && isFunctionLike(container.parent) || container.kind === SyntaxKind.ModuleBlock || container.kind === SyntaxKind.ModuleDeclaration || container.kind === SyntaxKind.SourceFile); @@ -12736,11 +12738,11 @@ namespace ts { checkTypeAssignableTo(checkExpressionCached(node.initializer), type, node, /*headMessage*/ undefined); checkParameterInitializer(node); - if(type.flags & TypeFlags.PrimitiveType) { + if (type.flags & TypeFlags.PrimitiveType) { - var initializerType = checkAndMarkExpression(node.initializer); - if(type.flags & TypeFlags.PrimitiveType) { - if(canIgnoreConvertNumericLit(type, initializerType, node.initializer)) { + let initializerType = checkAndMarkExpression(node.initializer); + if (type.flags & TypeFlags.PrimitiveType) { + if (canIgnoreConvertNumericLit(type, initializerType, node.initializer)) { initializerType = type; } } @@ -13247,7 +13249,7 @@ namespace ts { } if (returnType.flags & TypeFlags.PrimitiveType) { - var returnExpType = checkExpression(node.expression); + let returnExpType = checkExpression(node.expression); node.expression = convertToLower(returnType, returnExpType, node.expression, true); } } @@ -14475,7 +14477,7 @@ namespace ts { } function hasExportedMembers(moduleSymbol: Symbol) { - for (var id in moduleSymbol.exports) { + for (let id in moduleSymbol.exports) { if (id !== "export=") { return true; } @@ -14779,8 +14781,8 @@ namespace ts { potentialThisCollisions.length = 0; } - if(node.helperStatements !== undefined) { - while(node.helperStatements.length > 0){ + if (node.helperStatements !== undefined) { + while (node.helperStatements.length > 0) { node.statements.unshift(node.helperStatements.pop()); } } @@ -15541,25 +15543,35 @@ namespace ts { if (type.flags & TypeFlags.I32) { writer.write("TypedObject.int32"); - } else if (type.flags & TypeFlags.U32) { + } + else if (type.flags & TypeFlags.U32) { writer.write("TypedObject.uint32"); - } else if (type.flags & TypeFlags.U16) { + } + else if (type.flags & TypeFlags.U16) { writer.write("TypedObject.uint16"); - } else if (type.flags & TypeFlags.I16) { + } + else if (type.flags & TypeFlags.I16) { writer.write("TypedObject.int16"); - } else if (type.flags & TypeFlags.U8) { + } + else if (type.flags & TypeFlags.U8) { writer.write("TypedObject.uint8"); - } else if (type.flags & TypeFlags.I8) { + } + else if (type.flags & TypeFlags.I8) { writer.write("TypedObject.int8"); - } else if (type.flags & TypeFlags.Float) { + } + else if (type.flags & TypeFlags.Float) { writer.write("TypedObject.float32"); - } else if (type.flags & TypeFlags.Number) { + } + else if (type.flags & TypeFlags.Number) { writer.write("TypedObject.float64"); - } else if (type.flags & TypeFlags.String) { + } + else if (type.flags & TypeFlags.String) { writer.write("TypedObject.string"); - } else if (type.flags & TypeFlags.Any) { + } + else if (type.flags & TypeFlags.Any) { writer.write("TypedObject.Any"); - } else { + } + else { writer.write("TypedObject.Object"); } return true; @@ -16360,7 +16372,7 @@ namespace ts { } } - function checkGrammarJsxElement(node: JsxOpeningElement|JsxSelfClosingElement) { + function checkGrammarJsxElement(node: JsxOpeningElement | JsxSelfClosingElement) { const seen: Map = {}; for (let attr of node.attributes) { if (attr.kind === SyntaxKind.JsxSpreadAttribute) { diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 7a0ea3eb9ecdf..ad842b0e5cd98 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1417,10 +1417,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emit(span.literal); } - function jsxEmitReact(node: JsxElement|JsxSelfClosingElement) { + function jsxEmitReact(node: JsxElement | JsxSelfClosingElement) { /// Emit a tag name, which is either '"div"' for lower-cased names, or /// 'Div' for upper-cased or dotted names - function emitTagName(name: Identifier|QualifiedName) { + function emitTagName(name: Identifier | QualifiedName) { if (name.kind === SyntaxKind.Identifier && isIntrinsicJsxName((name).text)) { write("\""); emit(name); @@ -1570,7 +1570,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } } - function jsxEmitPreserve(node: JsxElement|JsxSelfClosingElement) { + function jsxEmitPreserve(node: JsxElement | JsxSelfClosingElement) { function emitJsxAttribute(node: JsxAttribute) { emit(node.name); if (node.initializer) { @@ -1585,7 +1585,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi write("}"); } - function emitAttributes(attribs: NodeArray) { + function emitAttributes(attribs: NodeArray) { for (let i = 0, n = attribs.length; i < n; i++) { if (i > 0) { write(" "); @@ -1601,7 +1601,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } } - function emitJsxOpeningOrSelfClosingElement(node: JsxOpeningElement|JsxSelfClosingElement) { + function emitJsxOpeningOrSelfClosingElement(node: JsxOpeningElement | JsxSelfClosingElement) { write("<"); emit(node.tagName); if (node.attributes.length > 0 || (node.kind === SyntaxKind.JsxSelfClosingElement)) { @@ -2463,13 +2463,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi return; } - if(node.isStructArray) { + if (node.isStructArray) { write("("); emit(node.expression); - write("._TO.array(") + write("._TO.array("); emit(node.argumentExpression); write("))()"); - } else { + } + else { emit(node.expression); write("["); emit(node.argumentExpression); @@ -4584,7 +4585,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi if (isStruct !== undefined) { emitStructMemberPrefix(node, member); - } else { + } + else { emitClassMemberPrefix(node, member); } @@ -5102,7 +5104,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi function emitStructProperties(node: StructLikeDeclaration) { for (var i = 0; i < node.members.length; i++) { - if(node.members[i].kind == SyntaxKind.PropertyDeclaration && !(node.members[i].flags & NodeFlags.Static)) { + if (node.members[i].kind == SyntaxKind.PropertyDeclaration && !(node.members[i].flags & NodeFlags.Static)) { increaseIndent(); writeStructProperty(node.members[i]); @@ -5148,7 +5150,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitTrailingComments(node); function emitConstructorOfStruct() { - var ctor = getFirstConstructorWithBody(node); + let ctor = getFirstConstructorWithBody(node); if (ctor) { emitLeadingComments(ctor); } @@ -5170,9 +5172,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitParameterPropertyAssignments(ctor); } - //emitMemberAssignments(node, /*nonstatic*/0); if (ctor) { - var statements: Node[] = (ctor.body).statements; + let statements: Node[] = (ctor.body).statements; emitLines(statements); } writeLine(); @@ -5204,7 +5205,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi write("_ctor.call(obj"); if (ctor) { - if(ctor.parameters.length > 0) write(" ,"); + if (ctor.parameters.length > 0) write(" ,"); } write(");"); @@ -5657,7 +5658,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi const parameters = valueDeclaration.parameters; const parameterCount = parameters.length; if (parameterCount > 0) { - for (var i = 0; i < parameterCount; i++) { + for (let i = 0; i < parameterCount; i++) { if (i > 0) { write(", "); } @@ -7424,7 +7425,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi return emitTemplateSpan(node); case SyntaxKind.JsxElement: case SyntaxKind.JsxSelfClosingElement: - return emitJsxElement(node); + return emitJsxElement(node); case SyntaxKind.JsxText: return emitJsxText(node); case SyntaxKind.JsxExpression: diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 8d15c78ad389b..38ff6b69ec502 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3570,7 +3570,7 @@ namespace ts { return result; } - function parseJsxOpeningOrSelfClosingElement(inExpressionContext: boolean): JsxOpeningElement|JsxSelfClosingElement { + function parseJsxOpeningOrSelfClosingElement(inExpressionContext: boolean): JsxOpeningElement | JsxSelfClosingElement { let fullStart = scanner.getStartPos(); parseExpected(SyntaxKind.LessThanToken); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index c32444a80f307..cd86a0c8af8f3 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1846,7 +1846,7 @@ namespace ts { /* @internal */ ContainsObjectLiteral = 0x00400000, // Type is or contains object literal type /* @internal */ - //ContainsAnyFunctionType = 0x00800000, // Type is or contains object literal type + // ContainsAnyFunctionType = 0x00800000, // Type is or contains object literal type ESSymbol = 0x00800000, // Type of symbol primitive introduced in ES6 ThisType = 0x01000000, // This type diff --git a/tests/baselines/reference/castExpressionParentheses.js b/tests/baselines/reference/castExpressionParentheses.js deleted file mode 100644 index b4754d0ad41e1..0000000000000 --- a/tests/baselines/reference/castExpressionParentheses.js +++ /dev/null @@ -1,94 +0,0 @@ -//// [castExpressionParentheses.ts] -declare var a; - -// parentheses should be omitted -// literals -({a:0}); -([1,3,]); -("string"); -(23.0); -(1); -(1.); -(1.0); -(12e+34); -(0xff); -(/regexp/g); -(false); -(true); -(null); -// names and dotted names -(this); -(this.x); -((a).x); -(a); -(a[0]); -(a.b["0"]); -(a()).x; - -declare var A; - -// should keep the parentheses in emit -(1).foo; -(1.).foo; -(1.0).foo; -(12e+34).foo; -(0xff).foo; -((1.0)); -(new A).foo; -(typeof A).x; -(-A).x; -new (A()); -(()=> {})(); -(function foo() { })(); -(-A).x; - -// nested cast, should keep one pair of parenthese -((-A)).x; - -// nested parenthesized expression, should keep one pair of parenthese -((A)) - - - -//// [castExpressionParentheses.js] -// parentheses should be omitted -// literals -{ a: 0 }; -[1, 3,]; -"string"; -23.0; -1; -1.; -1.0; -12e+34; -0xff; -/regexp/g; -false; -true; -null; -// names and dotted names -this; -this.x; -a.x; -a; -a[0]; -a.b["0"]; -a().x; -// should keep the parentheses in emit -(1).foo; -(1.).foo; -(1.0).foo; -(12e+34).foo; -(0xff).foo; -(1.0); -(new A).foo; -(typeof A).x; -(-A).x; -new (A()); -(function () { })(); -(function foo() { })(); -(-A).x; -// nested cast, should keep one pair of parenthese -(-A).x; -// nested parenthesized expression, should keep one pair of parenthese -(A); diff --git a/tests/baselines/reference/castExpressionParentheses.symbols b/tests/baselines/reference/castExpressionParentheses.symbols deleted file mode 100644 index ef31ee0f226c2..0000000000000 --- a/tests/baselines/reference/castExpressionParentheses.symbols +++ /dev/null @@ -1,79 +0,0 @@ -=== tests/cases/compiler/castExpressionParentheses.ts === -declare var a; ->a : Symbol(a, Decl(castExpressionParentheses.ts, 0, 11)) - -// parentheses should be omitted -// literals -({a:0}); ->a : Symbol(a, Decl(castExpressionParentheses.ts, 4, 7)) - -([1,3,]); -("string"); -(23.0); -(1); -(1.); -(1.0); -(12e+34); -(0xff); -(/regexp/g); -(false); -(true); -(null); -// names and dotted names -(this); -(this.x); -((a).x); ->a : Symbol(a, Decl(castExpressionParentheses.ts, 0, 11)) - -(a); ->a : Symbol(a, Decl(castExpressionParentheses.ts, 0, 11)) - -(a[0]); ->a : Symbol(a, Decl(castExpressionParentheses.ts, 0, 11)) - -(a.b["0"]); ->a : Symbol(a, Decl(castExpressionParentheses.ts, 0, 11)) - -(a()).x; ->a : Symbol(a, Decl(castExpressionParentheses.ts, 0, 11)) - -declare var A; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) - -// should keep the parentheses in emit -(1).foo; -(1.).foo; -(1.0).foo; -(12e+34).foo; -(0xff).foo; -((1.0)); -(new A).foo; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) - -(typeof A).x; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) - -(-A).x; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) - -new (A()); ->A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) - -(()=> {})(); ->Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 39, 2)) - -(function foo() { })(); ->foo : Symbol(foo, Decl(castExpressionParentheses.ts, 40, 6)) - -(-A).x; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) - -// nested cast, should keep one pair of parenthese -((-A)).x; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) - -// nested parenthesized expression, should keep one pair of parenthese -((A)) ->A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) - - diff --git a/tests/baselines/reference/castExpressionParentheses.types b/tests/baselines/reference/castExpressionParentheses.types deleted file mode 100644 index cdfbf4f7db236..0000000000000 --- a/tests/baselines/reference/castExpressionParentheses.types +++ /dev/null @@ -1,246 +0,0 @@ -=== tests/cases/compiler/castExpressionParentheses.ts === -declare var a; ->a : any - -// parentheses should be omitted -// literals -({a:0}); ->({a:0}) : any ->{a:0} : any ->{a:0} : { a: number; } ->a : number ->0 : number - -([1,3,]); ->([1,3,]) : any ->[1,3,] : any ->[1,3,] : number[] ->1 : number ->3 : number - -("string"); ->("string") : any ->"string" : any ->"string" : string - -(23.0); ->(23.0) : any ->23.0 : any ->23.0 : number - -(1); ->(1) : any ->1 : any ->1 : number - -(1.); ->(1.) : any ->1. : any ->1. : number - -(1.0); ->(1.0) : any ->1.0 : any ->1.0 : number - -(12e+34); ->(12e+34) : any ->12e+34 : any ->12e+34 : number - -(0xff); ->(0xff) : any ->0xff : any ->0xff : number - -(/regexp/g); ->(/regexp/g) : any ->/regexp/g : any ->/regexp/g : RegExp - -(false); ->(false) : any ->false : any ->false : boolean - -(true); ->(true) : any ->true : any ->true : boolean - -(null); ->(null) : any ->null : any ->null : null - -// names and dotted names -(this); ->(this) : any ->this : any ->this : any - -(this.x); ->(this.x) : any ->this.x : any ->this.x : any ->this : any ->x : any - -((a).x); ->((a).x) : any ->(a).x : any ->(a).x : any ->(a) : any ->a : any ->a : any ->x : any - -(a); ->(a) : any ->a : any ->a : any ->a : any - -(a[0]); ->(a[0]) : any ->a[0] : any ->a[0] : any ->a : any ->0 : number - -(a.b["0"]); ->(a.b["0"]) : any ->a.b["0"] : any ->a.b["0"] : any ->a.b : any ->a : any ->b : any ->"0" : string - -(a()).x; ->(a()).x : any ->(a()) : any ->a() : any ->a() : any ->a : any ->x : any - -declare var A; ->A : any - -// should keep the parentheses in emit -(1).foo; ->(1).foo : any ->(1) : any ->1 : any ->1 : number ->foo : any - -(1.).foo; ->(1.).foo : any ->(1.) : any ->1. : any ->1. : number ->foo : any - -(1.0).foo; ->(1.0).foo : any ->(1.0) : any ->1.0 : any ->1.0 : number ->foo : any - -(12e+34).foo; ->(12e+34).foo : any ->(12e+34) : any ->12e+34 : any ->12e+34 : number ->foo : any - -(0xff).foo; ->(0xff).foo : any ->(0xff) : any ->0xff : any ->0xff : number ->foo : any - -((1.0)); ->((1.0)) : any ->(1.0) : any ->(1.0) : number ->1.0 : number - -(new A).foo; ->(new A).foo : any ->(new A) : any ->new A : any ->new A : any ->A : any ->foo : any - -(typeof A).x; ->(typeof A).x : any ->(typeof A) : any ->typeof A : any ->typeof A : string ->A : any ->x : any - -(-A).x; ->(-A).x : any ->(-A) : any ->-A : any ->-A : number ->A : any ->x : any - -new (A()); ->new (A()) : any ->(A()) : any ->A() : any ->A() : any ->A : any - -(()=> {})(); ->(()=> {})() : void ->(()=> {}) : () => void ->()=> {} : () => void ->Tany : Tany - -(function foo() { })(); ->(function foo() { })() : any ->(function foo() { }) : any ->function foo() { } : any ->function foo() { } : () => void ->foo : () => void - -(-A).x; ->(-A).x : any ->(-A) : any ->-A : any ->-A : number ->-A : any ->-A : number ->A : any ->x : any - -// nested cast, should keep one pair of parenthese -((-A)).x; ->((-A)).x : any ->((-A)) : any ->(-A) : any ->(-A) : number ->(-A) : any ->-A : any ->-A : number ->A : any ->x : any - -// nested parenthesized expression, should keep one pair of parenthese -((A)) ->((A)) : any ->(A) : any ->(A) : any ->A : any - - diff --git a/tests/baselines/reference/defaultIndexProps2.js b/tests/baselines/reference/defaultIndexProps2.js deleted file mode 100644 index 7652e591cc978..0000000000000 --- a/tests/baselines/reference/defaultIndexProps2.js +++ /dev/null @@ -1,30 +0,0 @@ -//// [defaultIndexProps2.ts] -class Foo { - public v = "Yo"; -} - -var f = new Foo(); - -// WScript.Echo(f[0]); - -var o = {v:"Yo2"}; - -// WScript.Echo(o[0]); - -1[0]; -var q = "s"[0]; - - -//// [defaultIndexProps2.js] -var Foo = (function () { - function Foo() { - this.v = "Yo"; - } - return Foo; -})(); -var f = new Foo(); -// WScript.Echo(f[0]); -var o = { v: "Yo2" }; -// WScript.Echo(o[0]); -1[0]; -var q = "s"[0]; diff --git a/tests/baselines/reference/defaultIndexProps2.symbols b/tests/baselines/reference/defaultIndexProps2.symbols deleted file mode 100644 index 53d433351c331..0000000000000 --- a/tests/baselines/reference/defaultIndexProps2.symbols +++ /dev/null @@ -1,24 +0,0 @@ -=== tests/cases/compiler/defaultIndexProps2.ts === -class Foo { ->Foo : Symbol(Foo, Decl(defaultIndexProps2.ts, 0, 0)) - - public v = "Yo"; ->v : Symbol(v, Decl(defaultIndexProps2.ts, 0, 11)) -} - -var f = new Foo(); ->f : Symbol(f, Decl(defaultIndexProps2.ts, 4, 3)) ->Foo : Symbol(Foo, Decl(defaultIndexProps2.ts, 0, 0)) - -// WScript.Echo(f[0]); - -var o = {v:"Yo2"}; ->o : Symbol(o, Decl(defaultIndexProps2.ts, 8, 3)) ->v : Symbol(v, Decl(defaultIndexProps2.ts, 8, 9)) - -// WScript.Echo(o[0]); - -1[0]; -var q = "s"[0]; ->q : Symbol(q, Decl(defaultIndexProps2.ts, 13, 3)) - diff --git a/tests/baselines/reference/defaultIndexProps2.types b/tests/baselines/reference/defaultIndexProps2.types deleted file mode 100644 index 3b0d8278966e1..0000000000000 --- a/tests/baselines/reference/defaultIndexProps2.types +++ /dev/null @@ -1,35 +0,0 @@ -=== tests/cases/compiler/defaultIndexProps2.ts === -class Foo { ->Foo : Foo - - public v = "Yo"; ->v : string ->"Yo" : string -} - -var f = new Foo(); ->f : Foo ->new Foo() : Foo ->Foo : typeof Foo - -// WScript.Echo(f[0]); - -var o = {v:"Yo2"}; ->o : { v: string; } ->{v:"Yo2"} : { v: string; } ->v : string ->"Yo2" : string - -// WScript.Echo(o[0]); - -1[0]; ->1[0] : any ->1 : number ->0 : number - -var q = "s"[0]; ->q : string ->"s"[0] : string ->"s" : string ->0 : number - diff --git a/tests/baselines/reference/enumBasics.symbols b/tests/baselines/reference/enumBasics.symbols deleted file mode 100644 index 23f2a400741fe..0000000000000 --- a/tests/baselines/reference/enumBasics.symbols +++ /dev/null @@ -1,210 +0,0 @@ -=== tests/cases/conformance/enums/enumBasics.ts === -// Enum without initializers have first member = 0 and successive members = N + 1 -enum E1 { ->E1 : Symbol(E1, Decl(enumBasics.ts, 0, 0)) - - A, ->A : Symbol(E1.A, Decl(enumBasics.ts, 1, 9)) - - B, ->B : Symbol(E1.B, Decl(enumBasics.ts, 2, 6)) - - C ->C : Symbol(E1.C, Decl(enumBasics.ts, 3, 6)) -} - -// Enum type is a subtype of Number -var x: number = E1.A; ->x : Symbol(x, Decl(enumBasics.ts, 8, 3)) ->E1.A : Symbol(E1.A, Decl(enumBasics.ts, 1, 9)) ->E1 : Symbol(E1, Decl(enumBasics.ts, 0, 0)) ->A : Symbol(E1.A, Decl(enumBasics.ts, 1, 9)) - -// Enum object type is anonymous with properties of the enum type and numeric indexer -var e = E1; ->e : Symbol(e, Decl(enumBasics.ts, 11, 3), Decl(enumBasics.ts, 12, 3), Decl(enumBasics.ts, 18, 3)) ->E1 : Symbol(E1, Decl(enumBasics.ts, 0, 0)) - -var e: { ->e : Symbol(e, Decl(enumBasics.ts, 11, 3), Decl(enumBasics.ts, 12, 3), Decl(enumBasics.ts, 18, 3)) - - A: E1; ->A : Symbol(A, Decl(enumBasics.ts, 12, 8)) ->E1 : Symbol(E1, Decl(enumBasics.ts, 0, 0)) - - B: E1; ->B : Symbol(B, Decl(enumBasics.ts, 13, 10)) ->E1 : Symbol(E1, Decl(enumBasics.ts, 0, 0)) - - C: E1; ->C : Symbol(C, Decl(enumBasics.ts, 14, 10)) ->E1 : Symbol(E1, Decl(enumBasics.ts, 0, 0)) - - [n: number]: string; ->n : Symbol(n, Decl(enumBasics.ts, 16, 5)) - -}; -var e: typeof E1; ->e : Symbol(e, Decl(enumBasics.ts, 11, 3), Decl(enumBasics.ts, 12, 3), Decl(enumBasics.ts, 18, 3)) ->E1 : Symbol(E1, Decl(enumBasics.ts, 0, 0)) - -// Reverse mapping of enum returns string name of property -var s = E1[e.A]; ->s : Symbol(s, Decl(enumBasics.ts, 21, 3), Decl(enumBasics.ts, 22, 3)) ->E1 : Symbol(E1, Decl(enumBasics.ts, 0, 0)) ->e.A : Symbol(E1.A, Decl(enumBasics.ts, 1, 9)) ->e : Symbol(e, Decl(enumBasics.ts, 11, 3), Decl(enumBasics.ts, 12, 3), Decl(enumBasics.ts, 18, 3)) ->A : Symbol(E1.A, Decl(enumBasics.ts, 1, 9)) - -var s: string; ->s : Symbol(s, Decl(enumBasics.ts, 21, 3), Decl(enumBasics.ts, 22, 3)) - - -// Enum with only constant members -enum E2 { ->E2 : Symbol(E2, Decl(enumBasics.ts, 22, 14)) - - A = 1, B = 2, C = 3 ->A : Symbol(E2.A, Decl(enumBasics.ts, 26, 9)) ->B : Symbol(E2.B, Decl(enumBasics.ts, 27, 10)) ->C : Symbol(E2.C, Decl(enumBasics.ts, 27, 17)) -} - -// Enum with only computed members -enum E3 { ->E3 : Symbol(E3, Decl(enumBasics.ts, 28, 1)) - - X = 'foo'.length, Y = 4 + 3, Z = +'foo' ->X : Symbol(E3.X, Decl(enumBasics.ts, 31, 9)) ->'foo'.length : Symbol(String.length, Decl(lib.d.ts, --, --)) ->length : Symbol(String.length, Decl(lib.d.ts, --, --)) ->Y : Symbol(E3.Y, Decl(enumBasics.ts, 32, 21)) ->Z : Symbol(E3.Z, Decl(enumBasics.ts, 32, 32)) -} - -// Enum with constant members followed by computed members -enum E4 { ->E4 : Symbol(E4, Decl(enumBasics.ts, 33, 1)) - - X = 0, Y, Z = 'foo'.length ->X : Symbol(E4.X, Decl(enumBasics.ts, 36, 9)) ->Y : Symbol(E4.Y, Decl(enumBasics.ts, 37, 10)) ->Z : Symbol(E4.Z, Decl(enumBasics.ts, 37, 13)) ->'foo'.length : Symbol(String.length, Decl(lib.d.ts, --, --)) ->length : Symbol(String.length, Decl(lib.d.ts, --, --)) -} - -// Enum with > 2 constant members with no initializer for first member, non zero initializer for second element -enum E5 { ->E5 : Symbol(E5, Decl(enumBasics.ts, 38, 1)) - - A, ->A : Symbol(E5.A, Decl(enumBasics.ts, 41, 9)) - - B = 3, ->B : Symbol(E5.B, Decl(enumBasics.ts, 42, 6)) - - C // 4 ->C : Symbol(E5.C, Decl(enumBasics.ts, 43, 10)) -} - -enum E6 { ->E6 : Symbol(E6, Decl(enumBasics.ts, 45, 1)) - - A, ->A : Symbol(E6.A, Decl(enumBasics.ts, 47, 9)) - - B = 0, ->B : Symbol(E6.B, Decl(enumBasics.ts, 48, 6)) - - C // 1 ->C : Symbol(E6.C, Decl(enumBasics.ts, 49, 10)) -} - -// Enum with computed member initializer of type 'any' -enum E7 { ->E7 : Symbol(E7, Decl(enumBasics.ts, 51, 1)) - - A = 'foo'['foo'] ->A : Symbol(E7.A, Decl(enumBasics.ts, 54, 9)) -} - -// Enum with computed member initializer of type number -enum E8 { ->E8 : Symbol(E8, Decl(enumBasics.ts, 56, 1)) - - B = 'foo'['foo'] ->B : Symbol(E8.B, Decl(enumBasics.ts, 59, 9)) -} - -//Enum with computed member intializer of same enum type -enum E9 { ->E9 : Symbol(E9, Decl(enumBasics.ts, 61, 1)) - - A, ->A : Symbol(E9.A, Decl(enumBasics.ts, 64, 9)) - - B = A ->B : Symbol(E9.B, Decl(enumBasics.ts, 65, 6)) ->A : Symbol(E9.A, Decl(enumBasics.ts, 64, 9)) -} - -// (refer to .js to validate) -// Enum constant members are propagated -var doNotPropagate = [ ->doNotPropagate : Symbol(doNotPropagate, Decl(enumBasics.ts, 71, 3)) - - E8.B, E7.A, E4.Z, E3.X, E3.Y, E3.Z ->E8.B : Symbol(E8.B, Decl(enumBasics.ts, 59, 9)) ->E8 : Symbol(E8, Decl(enumBasics.ts, 56, 1)) ->B : Symbol(E8.B, Decl(enumBasics.ts, 59, 9)) ->E7.A : Symbol(E7.A, Decl(enumBasics.ts, 54, 9)) ->E7 : Symbol(E7, Decl(enumBasics.ts, 51, 1)) ->A : Symbol(E7.A, Decl(enumBasics.ts, 54, 9)) ->E4.Z : Symbol(E4.Z, Decl(enumBasics.ts, 37, 13)) ->E4 : Symbol(E4, Decl(enumBasics.ts, 33, 1)) ->Z : Symbol(E4.Z, Decl(enumBasics.ts, 37, 13)) ->E3.X : Symbol(E3.X, Decl(enumBasics.ts, 31, 9)) ->E3 : Symbol(E3, Decl(enumBasics.ts, 28, 1)) ->X : Symbol(E3.X, Decl(enumBasics.ts, 31, 9)) ->E3.Y : Symbol(E3.Y, Decl(enumBasics.ts, 32, 21)) ->E3 : Symbol(E3, Decl(enumBasics.ts, 28, 1)) ->Y : Symbol(E3.Y, Decl(enumBasics.ts, 32, 21)) ->E3.Z : Symbol(E3.Z, Decl(enumBasics.ts, 32, 32)) ->E3 : Symbol(E3, Decl(enumBasics.ts, 28, 1)) ->Z : Symbol(E3.Z, Decl(enumBasics.ts, 32, 32)) - -]; -// Enum computed members are not propagated -var doPropagate = [ ->doPropagate : Symbol(doPropagate, Decl(enumBasics.ts, 75, 3)) - - E9.A, E9.B, E6.B, E6.C, E6.A, E5.A, E5.B, E5.C ->E9.A : Symbol(E9.A, Decl(enumBasics.ts, 64, 9)) ->E9 : Symbol(E9, Decl(enumBasics.ts, 61, 1)) ->A : Symbol(E9.A, Decl(enumBasics.ts, 64, 9)) ->E9.B : Symbol(E9.B, Decl(enumBasics.ts, 65, 6)) ->E9 : Symbol(E9, Decl(enumBasics.ts, 61, 1)) ->B : Symbol(E9.B, Decl(enumBasics.ts, 65, 6)) ->E6.B : Symbol(E6.B, Decl(enumBasics.ts, 48, 6)) ->E6 : Symbol(E6, Decl(enumBasics.ts, 45, 1)) ->B : Symbol(E6.B, Decl(enumBasics.ts, 48, 6)) ->E6.C : Symbol(E6.C, Decl(enumBasics.ts, 49, 10)) ->E6 : Symbol(E6, Decl(enumBasics.ts, 45, 1)) ->C : Symbol(E6.C, Decl(enumBasics.ts, 49, 10)) ->E6.A : Symbol(E6.A, Decl(enumBasics.ts, 47, 9)) ->E6 : Symbol(E6, Decl(enumBasics.ts, 45, 1)) ->A : Symbol(E6.A, Decl(enumBasics.ts, 47, 9)) ->E5.A : Symbol(E5.A, Decl(enumBasics.ts, 41, 9)) ->E5 : Symbol(E5, Decl(enumBasics.ts, 38, 1)) ->A : Symbol(E5.A, Decl(enumBasics.ts, 41, 9)) ->E5.B : Symbol(E5.B, Decl(enumBasics.ts, 42, 6)) ->E5 : Symbol(E5, Decl(enumBasics.ts, 38, 1)) ->B : Symbol(E5.B, Decl(enumBasics.ts, 42, 6)) ->E5.C : Symbol(E5.C, Decl(enumBasics.ts, 43, 10)) ->E5 : Symbol(E5, Decl(enumBasics.ts, 38, 1)) ->C : Symbol(E5.C, Decl(enumBasics.ts, 43, 10)) - -]; - - diff --git a/tests/baselines/reference/enumBasics.types b/tests/baselines/reference/enumBasics.types deleted file mode 100644 index 87f6f12b6b2ef..0000000000000 --- a/tests/baselines/reference/enumBasics.types +++ /dev/null @@ -1,232 +0,0 @@ -=== tests/cases/conformance/enums/enumBasics.ts === -// Enum without initializers have first member = 0 and successive members = N + 1 -enum E1 { ->E1 : E1 - - A, ->A : E1 - - B, ->B : E1 - - C ->C : E1 -} - -// Enum type is a subtype of Number -var x: number = E1.A; ->x : number ->E1.A : E1 ->E1 : typeof E1 ->A : E1 - -// Enum object type is anonymous with properties of the enum type and numeric indexer -var e = E1; ->e : typeof E1 ->E1 : typeof E1 - -var e: { ->e : typeof E1 - - A: E1; ->A : E1 ->E1 : E1 - - B: E1; ->B : E1 ->E1 : E1 - - C: E1; ->C : E1 ->E1 : E1 - - [n: number]: string; ->n : number - -}; -var e: typeof E1; ->e : typeof E1 ->E1 : typeof E1 - -// Reverse mapping of enum returns string name of property -var s = E1[e.A]; ->s : string ->E1[e.A] : string ->E1 : typeof E1 ->e.A : E1 ->e : typeof E1 ->A : E1 - -var s: string; ->s : string - - -// Enum with only constant members -enum E2 { ->E2 : E2 - - A = 1, B = 2, C = 3 ->A : E2 ->1 : number ->B : E2 ->2 : number ->C : E2 ->3 : number -} - -// Enum with only computed members -enum E3 { ->E3 : E3 - - X = 'foo'.length, Y = 4 + 3, Z = +'foo' ->X : E3 ->'foo'.length : number ->'foo' : string ->length : number ->Y : E3 ->4 + 3 : number ->4 : number ->3 : number ->Z : E3 ->+'foo' : number ->'foo' : string -} - -// Enum with constant members followed by computed members -enum E4 { ->E4 : E4 - - X = 0, Y, Z = 'foo'.length ->X : E4 ->0 : number ->Y : E4 ->Z : E4 ->'foo'.length : number ->'foo' : string ->length : number -} - -// Enum with > 2 constant members with no initializer for first member, non zero initializer for second element -enum E5 { ->E5 : E5 - - A, ->A : E5 - - B = 3, ->B : E5 ->3 : number - - C // 4 ->C : E5 -} - -enum E6 { ->E6 : E6 - - A, ->A : E6 - - B = 0, ->B : E6 ->0 : number - - C // 1 ->C : E6 -} - -// Enum with computed member initializer of type 'any' -enum E7 { ->E7 : E7 - - A = 'foo'['foo'] ->A : E7 ->'foo'['foo'] : any ->'foo' : string ->'foo' : string -} - -// Enum with computed member initializer of type number -enum E8 { ->E8 : E8 - - B = 'foo'['foo'] ->B : E8 ->'foo'['foo'] : any ->'foo' : string ->'foo' : string -} - -//Enum with computed member intializer of same enum type -enum E9 { ->E9 : E9 - - A, ->A : E9 - - B = A ->B : E9 ->A : E9 -} - -// (refer to .js to validate) -// Enum constant members are propagated -var doNotPropagate = [ ->doNotPropagate : (E8 | E7 | E4 | E3)[] ->[ E8.B, E7.A, E4.Z, E3.X, E3.Y, E3.Z] : (E8 | E7 | E4 | E3)[] - - E8.B, E7.A, E4.Z, E3.X, E3.Y, E3.Z ->E8.B : E8 ->E8 : typeof E8 ->B : E8 ->E7.A : E7 ->E7 : typeof E7 ->A : E7 ->E4.Z : E4 ->E4 : typeof E4 ->Z : E4 ->E3.X : E3 ->E3 : typeof E3 ->X : E3 ->E3.Y : E3 ->E3 : typeof E3 ->Y : E3 ->E3.Z : E3 ->E3 : typeof E3 ->Z : E3 - -]; -// Enum computed members are not propagated -var doPropagate = [ ->doPropagate : (E9 | E6 | E5)[] ->[ E9.A, E9.B, E6.B, E6.C, E6.A, E5.A, E5.B, E5.C] : (E9 | E6 | E5)[] - - E9.A, E9.B, E6.B, E6.C, E6.A, E5.A, E5.B, E5.C ->E9.A : E9 ->E9 : typeof E9 ->A : E9 ->E9.B : E9 ->E9 : typeof E9 ->B : E9 ->E6.B : E6 ->E6 : typeof E6 ->B : E6 ->E6.C : E6 ->E6 : typeof E6 ->C : E6 ->E6.A : E6 ->E6 : typeof E6 ->A : E6 ->E5.A : E5 ->E5 : typeof E5 ->A : E5 ->E5.B : E5 ->E5 : typeof E5 ->B : E5 ->E5.C : E5 ->E5 : typeof E5 ->C : E5 - -]; - - diff --git a/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.errors.txt b/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.errors.txt deleted file mode 100644 index f08750ff0a65b..0000000000000 --- a/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.errors.txt +++ /dev/null @@ -1,33 +0,0 @@ -tests/cases/compiler/numericLiteralsWithTrailingDecimalPoints01.ts(3,3): error TS1005: ';' expected. -tests/cases/compiler/numericLiteralsWithTrailingDecimalPoints01.ts(9,15): error TS1005: ',' expected. -tests/cases/compiler/numericLiteralsWithTrailingDecimalPoints01.ts(9,23): error TS1005: '=' expected. -tests/cases/compiler/numericLiteralsWithTrailingDecimalPoints01.ts(9,24): error TS1109: Expression expected. - - -==== tests/cases/compiler/numericLiteralsWithTrailingDecimalPoints01.ts (4 errors) ==== - 1..toString(); - 1.0.toString(); - 1.toString(); - ~~~~~~~~ -!!! error TS1005: ';' expected. - 1.+2.0 + 3. ; - - // Preserve whitespace where important for JS compatibility - var i: number = 1; - var test1 = i.toString(); - var test2 = 2.toString(); - ~~~~~~~~ -!!! error TS1005: ',' expected. - ~ -!!! error TS1005: '=' expected. - ~ -!!! error TS1109: Expression expected. - var test3 = 3 .toString(); - var test4 = 3 .toString(); - var test5 = 3 .toString(); - var test6 = 3.['toString'](); - var test7 = 3 - .toString(); - var test8 = new Number(4).toString(); - var test9 = 3. + 3. - \ No newline at end of file diff --git a/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js b/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js deleted file mode 100644 index 0b1ce1f07365d..0000000000000 --- a/tests/baselines/reference/numericLiteralsWithTrailingDecimalPoints01.js +++ /dev/null @@ -1,38 +0,0 @@ -//// [numericLiteralsWithTrailingDecimalPoints01.ts] -1..toString(); -1.0.toString(); -1.toString(); -1.+2.0 + 3. ; - -// Preserve whitespace where important for JS compatibility -var i: number = 1; -var test1 = i.toString(); -var test2 = 2.toString(); -var test3 = 3 .toString(); -var test4 = 3 .toString(); -var test5 = 3 .toString(); -var test6 = 3.['toString'](); -var test7 = 3 -.toString(); -var test8 = new Number(4).toString(); -var test9 = 3. + 3. - - -//// [numericLiteralsWithTrailingDecimalPoints01.js] -1..toString(); -1.0.toString(); -1.; -toString(); -1. + 2.0 + 3.; -// Preserve whitespace where important for JS compatibility -var i = 1; -var test1 = i.toString(); -var test2 = 2., toString = (); -var test3 = 3 .toString(); -var test4 = 3 .toString(); -var test5 = 3 .toString(); -var test6 = 3.['toString'](); -var test7 = 3 - .toString(); -var test8 = new Number(4).toString(); -var test9 = 3. + 3.; diff --git a/tests/baselines/reference/parserComputedPropertyName25.errors.txt b/tests/baselines/reference/parserComputedPropertyName25.errors.txt deleted file mode 100644 index 0d0a2a8d8332b..0000000000000 --- a/tests/baselines/reference/parserComputedPropertyName25.errors.txt +++ /dev/null @@ -1,17 +0,0 @@ -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName25.ts(3,5): error TS1166: A computed property name in a class property declaration must directly refer to a built-in symbol. -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName25.ts(3,6): error TS2304: Cannot find name 'e'. -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName25.ts(4,6): error TS2304: Cannot find name 'e2'. - - -==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName25.ts (3 errors) ==== - class C { - // No ASI - [e] = 0 - ~~~ -!!! error TS1166: A computed property name in a class property declaration must directly refer to a built-in symbol. - ~ -!!! error TS2304: Cannot find name 'e'. - [e2] = 1 - ~~ -!!! error TS2304: Cannot find name 'e2'. - } \ No newline at end of file diff --git a/tests/baselines/reference/parserComputedPropertyName25.js b/tests/baselines/reference/parserComputedPropertyName25.js deleted file mode 100644 index 47670fe14b8dc..0000000000000 --- a/tests/baselines/reference/parserComputedPropertyName25.js +++ /dev/null @@ -1,14 +0,0 @@ -//// [parserComputedPropertyName25.ts] -class C { - // No ASI - [e] = 0 - [e2] = 1 -} - -//// [parserComputedPropertyName25.js] -class C { - constructor() { - // No ASI - this[e] = 0[e2] = 1; - } -} diff --git a/tests/baselines/reference/parserComputedPropertyName26.errors.txt b/tests/baselines/reference/parserComputedPropertyName26.errors.txt deleted file mode 100644 index 9b1cf1d53f481..0000000000000 --- a/tests/baselines/reference/parserComputedPropertyName26.errors.txt +++ /dev/null @@ -1,14 +0,0 @@ -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName26.ts(3,5): error TS1164: Computed property names are not allowed in enums. -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName26.ts(4,6): error TS2304: Cannot find name 'e2'. - - -==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName26.ts (2 errors) ==== - enum E { - // No ASI - [e] = 0 - ~~~ -!!! error TS1164: Computed property names are not allowed in enums. - [e2] = 1 - ~~ -!!! error TS2304: Cannot find name 'e2'. - } \ No newline at end of file diff --git a/tests/baselines/reference/parserComputedPropertyName26.js b/tests/baselines/reference/parserComputedPropertyName26.js deleted file mode 100644 index fe47249a22eef..0000000000000 --- a/tests/baselines/reference/parserComputedPropertyName26.js +++ /dev/null @@ -1,13 +0,0 @@ -//// [parserComputedPropertyName26.ts] -enum E { - // No ASI - [e] = 0 - [e2] = 1 -} - -//// [parserComputedPropertyName26.js] -var E; -(function (E) { - // No ASI - E[E[e] = 0[e2] = 1] = e; -})(E || (E = {})); diff --git a/tests/baselines/reference/parserComputedPropertyName27.errors.txt b/tests/baselines/reference/parserComputedPropertyName27.errors.txt deleted file mode 100644 index 3792cf0eccd0b..0000000000000 --- a/tests/baselines/reference/parserComputedPropertyName27.errors.txt +++ /dev/null @@ -1,17 +0,0 @@ -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName27.ts(3,6): error TS2304: Cannot find name 'e'. -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName27.ts(4,6): error TS2304: Cannot find name 'e2'. -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName27.ts(4,9): error TS1005: ';' expected. - - -==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName27.ts (3 errors) ==== - class C { - // No ASI - [e]: number = 0 - ~ -!!! error TS2304: Cannot find name 'e'. - [e2]: number - ~~ -!!! error TS2304: Cannot find name 'e2'. - ~ -!!! error TS1005: ';' expected. - } \ No newline at end of file diff --git a/tests/baselines/reference/parserComputedPropertyName27.js b/tests/baselines/reference/parserComputedPropertyName27.js deleted file mode 100644 index f872e95e7e659..0000000000000 --- a/tests/baselines/reference/parserComputedPropertyName27.js +++ /dev/null @@ -1,14 +0,0 @@ -//// [parserComputedPropertyName27.ts] -class C { - // No ASI - [e]: number = 0 - [e2]: number -} - -//// [parserComputedPropertyName27.js] -class C { - constructor() { - // No ASI - this[e] = 0[e2]; - } -} diff --git a/tests/baselines/reference/parserComputedPropertyName33.errors.txt b/tests/baselines/reference/parserComputedPropertyName33.errors.txt deleted file mode 100644 index 5ce420ceb60a6..0000000000000 --- a/tests/baselines/reference/parserComputedPropertyName33.errors.txt +++ /dev/null @@ -1,20 +0,0 @@ -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName33.ts(3,6): error TS2304: Cannot find name 'e'. -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName33.ts(4,6): error TS2304: Cannot find name 'e2'. -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName33.ts(4,12): error TS1005: ';' expected. -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName33.ts(5,1): error TS1128: Declaration or statement expected. - - -==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName33.ts (4 errors) ==== - class C { - // No ASI - [e] = 0 - ~ -!!! error TS2304: Cannot find name 'e'. - [e2]() { } - ~~ -!!! error TS2304: Cannot find name 'e2'. - ~ -!!! error TS1005: ';' expected. - } - ~ -!!! error TS1128: Declaration or statement expected. \ No newline at end of file diff --git a/tests/baselines/reference/parserComputedPropertyName33.js b/tests/baselines/reference/parserComputedPropertyName33.js deleted file mode 100644 index ab80967c21a02..0000000000000 --- a/tests/baselines/reference/parserComputedPropertyName33.js +++ /dev/null @@ -1,15 +0,0 @@ -//// [parserComputedPropertyName33.ts] -class C { - // No ASI - [e] = 0 - [e2]() { } -} - -//// [parserComputedPropertyName33.js] -class C { - constructor() { - // No ASI - this[e] = 0[e2](); - } -} -{ } diff --git a/tests/baselines/reference/stringHasStringValuedNumericIndexer.js b/tests/baselines/reference/stringHasStringValuedNumericIndexer.js deleted file mode 100644 index 0ebc77a87419b..0000000000000 --- a/tests/baselines/reference/stringHasStringValuedNumericIndexer.js +++ /dev/null @@ -1,5 +0,0 @@ -//// [stringHasStringValuedNumericIndexer.ts] -var str: string = ""[0]; - -//// [stringHasStringValuedNumericIndexer.js] -var str = ""[0]; diff --git a/tests/baselines/reference/stringHasStringValuedNumericIndexer.symbols b/tests/baselines/reference/stringHasStringValuedNumericIndexer.symbols deleted file mode 100644 index 282aa5e7658e0..0000000000000 --- a/tests/baselines/reference/stringHasStringValuedNumericIndexer.symbols +++ /dev/null @@ -1,4 +0,0 @@ -=== tests/cases/compiler/stringHasStringValuedNumericIndexer.ts === -var str: string = ""[0]; ->str : Symbol(str, Decl(stringHasStringValuedNumericIndexer.ts, 0, 3)) - diff --git a/tests/baselines/reference/stringHasStringValuedNumericIndexer.types b/tests/baselines/reference/stringHasStringValuedNumericIndexer.types deleted file mode 100644 index ce9504358eb0d..0000000000000 --- a/tests/baselines/reference/stringHasStringValuedNumericIndexer.types +++ /dev/null @@ -1,7 +0,0 @@ -=== tests/cases/compiler/stringHasStringValuedNumericIndexer.ts === -var str: string = ""[0]; ->str : string ->""[0] : string ->"" : string ->0 : number - diff --git a/tests/cases/compiler/castExpressionParentheses.ts b/tests/cases/compiler/castExpressionParentheses.ts deleted file mode 100644 index 06908f39936c8..0000000000000 --- a/tests/cases/compiler/castExpressionParentheses.ts +++ /dev/null @@ -1,49 +0,0 @@ -declare var a; - -// parentheses should be omitted -// literals -({a:0}); -([1,3,]); -("string"); -(23.0); -(1); -(1.); -(1.0); -(12e+34); -(0xff); -(/regexp/g); -(false); -(true); -(null); -// names and dotted names -(this); -(this.x); -((a).x); -(a); -(a[0]); -(a.b["0"]); -(a()).x; - -declare var A; - -// should keep the parentheses in emit -(1).foo; -(1.).foo; -(1.0).foo; -(12e+34).foo; -(0xff).foo; -((1.0)); -(new A).foo; -(typeof A).x; -(-A).x; -new (A()); -(()=> {})(); -(function foo() { })(); -(-A).x; - -// nested cast, should keep one pair of parenthese -((-A)).x; - -// nested parenthesized expression, should keep one pair of parenthese -((A)) - diff --git a/tests/cases/compiler/defaultIndexProps2.ts b/tests/cases/compiler/defaultIndexProps2.ts deleted file mode 100644 index 35abeca3f876f..0000000000000 --- a/tests/cases/compiler/defaultIndexProps2.ts +++ /dev/null @@ -1,14 +0,0 @@ -class Foo { - public v = "Yo"; -} - -var f = new Foo(); - -// WScript.Echo(f[0]); - -var o = {v:"Yo2"}; - -// WScript.Echo(o[0]); - -1[0]; -var q = "s"[0]; diff --git a/tests/cases/compiler/enumBasics1.ts b/tests/cases/compiler/enumBasics1.ts deleted file mode 100644 index b3614d5bcc2b9..0000000000000 --- a/tests/cases/compiler/enumBasics1.ts +++ /dev/null @@ -1,37 +0,0 @@ -enum E { - A = 1, - B, - C -} - -/* -var a: E; -var b = E["B"]; // shouldn't error - - -function foo(e: E) {} - -foo(a); // shouldn't error - - -class C { - public e: E; - - public m(): E { return this.e; } // shouldn't error -} - - -var e = E; // shouldn't error -*/ -E.A.A; // should error - - -enum E2 { - A, - B, -} - -enum E2 { // should error for continued autonumbering - C, - D, -} \ No newline at end of file diff --git a/tests/cases/compiler/numericLiteralsWithTrailingDecimalPoints01.ts b/tests/cases/compiler/numericLiteralsWithTrailingDecimalPoints01.ts deleted file mode 100644 index 23cd2ba45f8da..0000000000000 --- a/tests/cases/compiler/numericLiteralsWithTrailingDecimalPoints01.ts +++ /dev/null @@ -1,17 +0,0 @@ -1..toString(); -1.0.toString(); -1.toString(); -1.+2.0 + 3. ; - -// Preserve whitespace where important for JS compatibility -var i: number = 1; -var test1 = i.toString(); -var test2 = 2.toString(); -var test3 = 3 .toString(); -var test4 = 3 .toString(); -var test5 = 3 .toString(); -var test6 = 3.['toString'](); -var test7 = 3 -.toString(); -var test8 = new Number(4).toString(); -var test9 = 3. + 3. diff --git a/tests/cases/compiler/stringHasStringValuedNumericIndexer.ts b/tests/cases/compiler/stringHasStringValuedNumericIndexer.ts deleted file mode 100644 index 1b3d70c99587c..0000000000000 --- a/tests/cases/compiler/stringHasStringValuedNumericIndexer.ts +++ /dev/null @@ -1 +0,0 @@ -var str: string = ""[0]; \ No newline at end of file diff --git a/tests/cases/conformance/enums/enumBasics.ts b/tests/cases/conformance/enums/enumBasics.ts deleted file mode 100644 index 756e08e97afb2..0000000000000 --- a/tests/cases/conformance/enums/enumBasics.ts +++ /dev/null @@ -1,79 +0,0 @@ -// Enum without initializers have first member = 0 and successive members = N + 1 -enum E1 { - A, - B, - C -} - -// Enum type is a subtype of Number -var x: number = E1.A; - -// Enum object type is anonymous with properties of the enum type and numeric indexer -var e = E1; -var e: { - A: E1; - B: E1; - C: E1; - [n: number]: string; -}; -var e: typeof E1; - -// Reverse mapping of enum returns string name of property -var s = E1[e.A]; -var s: string; - - -// Enum with only constant members -enum E2 { - A = 1, B = 2, C = 3 -} - -// Enum with only computed members -enum E3 { - X = 'foo'.length, Y = 4 + 3, Z = +'foo' -} - -// Enum with constant members followed by computed members -enum E4 { - X = 0, Y, Z = 'foo'.length -} - -// Enum with > 2 constant members with no initializer for first member, non zero initializer for second element -enum E5 { - A, - B = 3, - C // 4 -} - -enum E6 { - A, - B = 0, - C // 1 -} - -// Enum with computed member initializer of type 'any' -enum E7 { - A = 'foo'['foo'] -} - -// Enum with computed member initializer of type number -enum E8 { - B = 'foo'['foo'] -} - -//Enum with computed member intializer of same enum type -enum E9 { - A, - B = A -} - -// (refer to .js to validate) -// Enum constant members are propagated -var doNotPropagate = [ - E8.B, E7.A, E4.Z, E3.X, E3.Y, E3.Z -]; -// Enum computed members are not propagated -var doPropagate = [ - E9.A, E9.B, E6.B, E6.C, E6.A, E5.A, E5.B, E5.C -]; - diff --git a/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName25.ts b/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName25.ts deleted file mode 100644 index 3bf29e66baeb0..0000000000000 --- a/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName25.ts +++ /dev/null @@ -1,6 +0,0 @@ -//@target: ES6 -class C { - // No ASI - [e] = 0 - [e2] = 1 -} \ No newline at end of file diff --git a/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName26.ts b/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName26.ts deleted file mode 100644 index e6eaa682b5922..0000000000000 --- a/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName26.ts +++ /dev/null @@ -1,6 +0,0 @@ -//@target: ES6 -enum E { - // No ASI - [e] = 0 - [e2] = 1 -} \ No newline at end of file diff --git a/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName27.ts b/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName27.ts deleted file mode 100644 index d52b7d43cfc94..0000000000000 --- a/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName27.ts +++ /dev/null @@ -1,6 +0,0 @@ -//@target: ES6 -class C { - // No ASI - [e]: number = 0 - [e2]: number -} \ No newline at end of file diff --git a/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName33.ts b/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName33.ts deleted file mode 100644 index 2ca7ac84af70d..0000000000000 --- a/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName33.ts +++ /dev/null @@ -1,6 +0,0 @@ -//@target: ES6 -class C { - // No ASI - [e] = 0 - [e2]() { } -} \ No newline at end of file From 8c900c6904f94e47862f0c059392cc4bbd9eeee1 Mon Sep 17 00:00:00 2001 From: Yanxing Wang Date: Thu, 29 Oct 2015 16:15:44 -0700 Subject: [PATCH 03/43] bug fixed --- src/compiler/.idea/workspace.xml | 133 ++++++++++++++++++++++++++----- src/compiler/checker.ts | 27 ++++--- src/compiler/emitter.ts | 2 +- src/compiler/types.ts | 2 +- 4 files changed, 127 insertions(+), 37 deletions(-) diff --git a/src/compiler/.idea/workspace.xml b/src/compiler/.idea/workspace.xml index 46b662229c913..0dda51e04f8c0 100644 --- a/src/compiler/.idea/workspace.xml +++ b/src/compiler/.idea/workspace.xml @@ -24,8 +24,18 @@ - - + + + + + + + + + + + + @@ -43,21 +53,21 @@ - + - - + + - + - - + + @@ -70,10 +80,10 @@ @@ -112,8 +122,8 @@ - + @@ -140,6 +150,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -158,24 +203,24 @@ - + + - - + + - - - + + + - - + @@ -196,7 +241,7 @@ - + @@ -222,13 +267,57 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3067ea9301a1e..2e590b53c9343 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15535,46 +15535,47 @@ namespace ts { } } - function writeStructProperty(node: ClassElement, writer: EmitTextWriter): boolean { + function writeStructProperty(node: ClassElement): string { let symbol = getSymbolOfNode(node); let type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature)) ? getTypeOfSymbol(symbol) : unknownType; + let ret = ""; if (type.flags & TypeFlags.I32) { - writer.write("TypedObject.int32"); + ret = "TypedObject.int32"; } else if (type.flags & TypeFlags.U32) { - writer.write("TypedObject.uint32"); + ret = "TypedObject.uint32"; } else if (type.flags & TypeFlags.U16) { - writer.write("TypedObject.uint16"); + ret = "TypedObject.uint16"; } else if (type.flags & TypeFlags.I16) { - writer.write("TypedObject.int16"); + ret = "TypedObject.int16"; } else if (type.flags & TypeFlags.U8) { - writer.write("TypedObject.uint8"); + ret = "TypedObject.uint8"; } else if (type.flags & TypeFlags.I8) { - writer.write("TypedObject.int8"); + ret = "TypedObject.int8"; } else if (type.flags & TypeFlags.Float) { - writer.write("TypedObject.float32"); + ret = "TypedObject.float32"; } else if (type.flags & TypeFlags.Number) { - writer.write("TypedObject.float64"); + ret = "TypedObject.float64"; } else if (type.flags & TypeFlags.String) { - writer.write("TypedObject.string"); + ret = "TypedObject.string"; } else if (type.flags & TypeFlags.Any) { - writer.write("TypedObject.Any"); + ret = "TypedObject.Any"; } else { - writer.write("TypedObject.Object"); + ret = "TypedObject.Object"; } - return true; + return ret; } function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter) { diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index ad842b0e5cd98..2f93263aa6aff 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -5099,7 +5099,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitDeclarationName(node); write(": "); - resolver.writeStructProperty(node, writer); + write(resolver.writeStructProperty(node)); } function emitStructProperties(node: StructLikeDeclaration) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index cd86a0c8af8f3..041c5ed87ab66 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1636,7 +1636,7 @@ namespace ts { isDeclarationVisible(node: Declaration): boolean; collectLinkedAliases(node: Identifier): Node[]; isImplementationOfOverload(node: FunctionLikeDeclaration): boolean; - writeStructProperty(node: ClassElement, writer: EmitTextWriter): boolean; + writeStructProperty(node: ClassElement): string; writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; writeReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; From 57c540fd16020a7a027a96520dd93a9e6197bcf0 Mon Sep 17 00:00:00 2001 From: jied Date: Sun, 1 Nov 2015 20:58:43 -0800 Subject: [PATCH 04/43] add struct to types.ts --- src/compiler/types.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index cd86a0c8af8f3..af18bbea1af23 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -553,7 +553,7 @@ namespace ts { } // SyntaxKind.Property - export interface PropertyDeclaration extends Declaration, ClassElement { + export interface PropertyDeclaration extends Declaration, ClassElement, StructElement { name: DeclarationName; // Declared property name questionToken?: Node; // Present on optional property type?: TypeNode; // Optional type annotation @@ -632,11 +632,11 @@ namespace ts { // Because of this, it may be necessary to determine what sort of MethodDeclaration you have // at later stages of the compiler pipeline. In that case, you can either check the parent kind // of the method, or use helpers like isObjectLiteralMethodDeclaration - export interface MethodDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { + export interface MethodDeclaration extends FunctionLikeDeclaration, ClassElement, StructElement, ObjectLiteralElement { body?: Block; } - export interface ConstructorDeclaration extends FunctionLikeDeclaration, ClassElement { + export interface ConstructorDeclaration extends FunctionLikeDeclaration, ClassElement, StructElement { body?: Block; } @@ -1053,7 +1053,7 @@ namespace ts { name?: Identifier; typeParameters?: NodeArray; heritageClauses?: NodeArray; - members: NodeArray; + members: NodeArray; } export interface StructDeclaration extends StructLikeDeclaration, Statement { @@ -1062,8 +1062,7 @@ namespace ts { export interface StructExpression extends StructLikeDeclaration, PrimaryExpression { } - export interface StructElement extends Declaration { - _structElementBrand: any; + export interface StructElement extends ClassElement { } export interface InterfaceDeclaration extends Declaration, Statement { @@ -1636,7 +1635,7 @@ namespace ts { isDeclarationVisible(node: Declaration): boolean; collectLinkedAliases(node: Identifier): Node[]; isImplementationOfOverload(node: FunctionLikeDeclaration): boolean; - writeStructProperty(node: ClassElement, writer: EmitTextWriter): boolean; + writeStructProperty(node: StructElement, writer: EmitTextWriter): boolean; writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; writeReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; @@ -1656,7 +1655,7 @@ namespace ts { Property = 0x00000004, // Property or enum member EnumMember = 0x00000008, // Enum member Function = 0x00000010, // Function - Struct = 0x40000000, // Struct + Struct = 0x80000000, // Struct Class = 0x00000020, // Class Interface = 0x00000040, // Interface ConstEnum = 0x00000080, // Const enum @@ -1742,7 +1741,7 @@ namespace ts { declarations?: Declaration[]; // Declarations associated with this symbol valueDeclaration?: Declaration; // First value declaration of the symbol - members?: SymbolTable; // Class, interface or literal instance members + members?: SymbolTable; // Class, struct, interface or literal instance members exports?: SymbolTable; // Module exports /* @internal */ id?: number; // Unique id (used to look up SymbolLinks) /* @internal */ mergeId?: number; // Merge id (used to look up merged symbol) @@ -1755,7 +1754,7 @@ namespace ts { export interface SymbolLinks { target?: Symbol; // Resolved (non-alias) target of an alias type?: Type; // Type of value symbol - declaredType?: Type; // Type of class, interface, enum, type alias, or type parameter + declaredType?: Type; // Type of class, struct, interface, enum, type alias, or type parameter typeParameters?: TypeParameter[]; // Type parameters of type alias (undefined if non-generic) instantiations?: Map; // Instantiations of generic type alias (undefined if non-generic) mapper?: TypeMapper; // Type mapper for instantiation alias @@ -1795,6 +1794,7 @@ namespace ts { EnumValuesComputed = 0x00002000, BlockScopedBindingInLoop = 0x00004000, LexicalModuleMergesWithClass= 0x00008000, // Instantiated lexical module declaration is merged with a previous class declaration. + LexicalModuleMergesWithStruct= 0x00010000, // Instantiated lexical module declaration is merged with a previous struct declaration. } /* @internal */ @@ -1828,7 +1828,7 @@ namespace ts { StringLiteral = 0x00000100, // String literal type TypeParameter = 0x00000200, // Type parameter Class = 0x00000400, // Class - Struct = 0x80000000, // Struct + Struct = 0x100000000, // Struct Interface = 0x00000800, // Interface Reference = 0x00001000, // Generic type reference Tuple = 0x00002000, // Tuple @@ -1864,7 +1864,7 @@ namespace ts { Primitive = String | Number | I8 | U8 | I16 | U16 | I32 | U32 | Float | Boolean | ESSymbol | Void | Undefined | Null | StringLiteral | Enum, StringLike = String | StringLiteral, NumberLike = Number | Enum | I8 | U8 | I16 | U16 | I32 | U32 | Float, - ObjectType = Class | Interface | Reference | Tuple | Anonymous, + ObjectType = Struct | Class | Interface | Reference | Tuple | Anonymous, PrimitiveType = I8 | U8 | I16 | U16 | I32 | U32 | Float, DoublePrecisionFloat = Number, From 7f1df2cd48cb92544002c748bd06f1d21af45732 Mon Sep 17 00:00:00 2001 From: jied Date: Sun, 1 Nov 2015 21:02:46 -0800 Subject: [PATCH 05/43] add struct to parser.ts --- src/compiler/parser.ts | 96 +++++++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 20 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 38ff6b69ec502..307d43e4e593c 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1131,12 +1131,12 @@ namespace ts { if (token === SyntaxKind.ExportKeyword) { nextToken(); if (token === SyntaxKind.DefaultKeyword) { - return lookAhead(nextTokenIsClassOrFunction); + return lookAhead(nextTokenIsClassOrStructOrFunction); } return token !== SyntaxKind.AsteriskToken && token !== SyntaxKind.OpenBraceToken && canFollowModifier(); } if (token === SyntaxKind.DefaultKeyword) { - return nextTokenIsClassOrFunction(); + return nextTokenIsClassOrStructOrFunction(); } if (token === SyntaxKind.StaticKeyword) { nextToken(); @@ -1161,7 +1161,7 @@ namespace ts { || isLiteralPropertyName(); } - function nextTokenIsClassOrFunction(): boolean { + function nextTokenIsClassOrStructOrFunction(): boolean { nextToken(); return token === SyntaxKind.ClassKeyword || token === SyntaxKind.StructKeyword || token === SyntaxKind.FunctionKeyword; } @@ -4897,6 +4897,18 @@ namespace ts { return false; } + function isStructMemberModifier(idToken: SyntaxKind) { + switch (idToken) { + case SyntaxKind.PublicKeyword: + case SyntaxKind.PrivateKeyword: + // case SyntaxKind.ProtectedKeyword: + case SyntaxKind.StaticKeyword: + return true; + default: + return false; + } + } + function isStructMemberStart(): boolean { let idToken: SyntaxKind; @@ -4907,13 +4919,13 @@ namespace ts { // Eat up all modifiers, but hold on to the last one in case it is actually an identifier. while (isModifier(token)) { idToken = token; - // If the idToken is a class modifier (protected, private, public, and static), it is - // certain that we are starting to parse class member. This allows better error recovery + // If the idToken is a struct modifier (private, public, and static), it is + // certain that we are starting to parse struct member. This allows better error recovery // Example: // public foo() ... // true // public @dec blah ... // true; we will then report an error later // export public ... // true; we will then report an error later - if (isClassMemberModifier(idToken)) { + if (isStructMemberModifier(idToken)) { return true; } @@ -4925,26 +4937,27 @@ namespace ts { } // Try to get the first property-like token following all modifiers. - // This can either be an identifier or the 'get' or 'set' keywords. + // This has to be an identifier, and the 'get' or 'set' keywords are not allowed in struct. if (isLiteralPropertyName()) { idToken = token; nextToken(); } - // Index signatures and computed properties are class members; we can parse. + // Index like signatures are actually struct array, they are members of struct; we can parse. if (token === SyntaxKind.OpenBracketToken) { return true; } // If we were able to get any potential identifier... if (idToken !== undefined) { - // If we have a non-keyword identifier, or if we have an accessor, then it's safe to parse. - if (!isKeyword(idToken) || idToken === SyntaxKind.SetKeyword || idToken === SyntaxKind.GetKeyword) { + // If we have a non-keyword identifier, it's safe to parse. + // accessors are not allowed in struct. + if (!isKeyword(idToken)) { return true; } // If it *is* a keyword, but not an accessor, check a little farther along - // to see if it should actually be parsed as a class member. + // to see if it should actually be parsed as a struct member. switch (token) { case SyntaxKind.OpenParenToken: // Method declaration case SyntaxKind.LessThanToken: // Generic Method declaration @@ -4955,7 +4968,7 @@ namespace ts { default: // Covers // - Semicolons (declaration termination) - // - Closing braces (end-of-class, must be declaration) + // - Closing braces (end-of-struct, must be declaration) // - End-of-files (not valid, but permitted so that it gets caught later on) // - Line-breaks (enabling *automatic semicolon insertion*) return canParseSemicolon(); @@ -5077,6 +5090,45 @@ namespace ts { Debug.fail("Should not have attempted to parse class member declaration."); } + function parseStructElement(): StructElement { + let fullStart = getNodePos(); + let decorators = parseDecorators(); + let modifiers = parseModifiers(); + + if (token === SyntaxKind.ConstructorKeyword) { + return parseConstructorDeclaration(fullStart, decorators, modifiers); + } + + // index signature not allowed in struct + if (token === SyntaxKind.OpenBracketToken) { + + } + + if (isIndexSignature()) { + return parseIndexSignatureDeclaration(fullStart, decorators, modifiers); + } + + // It is very important that we check this *after* checking indexers because + // the [ token can start an index signature or a computed property name + if (tokenIsIdentifierOrKeyword(token) || + token === SyntaxKind.StringLiteral || + token === SyntaxKind.NumericLiteral || + token === SyntaxKind.AsteriskToken || + token === SyntaxKind.OpenBracketToken) { + + return parsePropertyOrMethodDeclaration(fullStart, decorators, modifiers); + } + + if (decorators || modifiers) { + // treat this as a property declaration with a missing name. + let name = createMissingNode(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ true, Diagnostics.Declaration_expected); + return parsePropertyDeclaration(fullStart, decorators, modifiers, name, /*questionToken*/ undefined); + } + + // 'isStructMemberStart' should have hinted not to attempt parsing. + Debug.fail("Should not have attempted to parse struct member declaration."); + } + function parseClassExpression(): ClassExpression { return parseClassDeclarationOrExpression( /*fullStart*/ scanner.getStartPos(), @@ -5090,7 +5142,7 @@ namespace ts { /*fullStart*/ scanner.getStartPos(), /*decorators*/ undefined, /*modifiers*/ undefined, - SyntaxKind.ClassExpression); + SyntaxKind.StructExpression); } function parseClassDeclaration(fullStart: number, decorators: NodeArray, modifiers: ModifiersArray): ClassDeclaration { @@ -5124,22 +5176,22 @@ namespace ts { } function parseStructDeclarationOrExpression(fullStart: number, decorators: NodeArray, modifiers: ModifiersArray, kind: SyntaxKind): StructLikeDeclaration { - let node = createNode(kind, fullStart); + let node = createNode(kind, fullStart); node.decorators = decorators; setModifiers(node, modifiers); parseExpected(SyntaxKind.StructKeyword); node.name = parseNameOfStructDeclarationOrExpression(); node.typeParameters = parseTypeParameters(); - node.heritageClauses = parseHeritageClauses(/*isClassHeritageClause*/ true); + node.heritageClauses = parseHeritageClauses(/*isStructHeritageClause*/ true); if (parseExpected(SyntaxKind.OpenBraceToken)) { - // ClassTail[Yield,Await] : (Modified) See 14.5 - // ClassHeritage[?Yield,?Await]opt { ClassBody[?Yield,?Await]opt } - node.members = parseClassMembers(); + // StructTail[Yield,Await] : (Modified) See 14.5 + // StructHeritage[?Yield,?Await]opt { StructBody[?Yield,?Await]opt } + node.members = parseStructMembers(); parseExpected(SyntaxKind.CloseBraceToken); } else { - node.members = createMissingList(); + node.members = createMissingList(); } return finishNode(node); @@ -5216,6 +5268,10 @@ namespace ts { return parseList(ParsingContext.ClassMembers, parseClassElement); } + function parseStructMembers() { + return parseList(ParsingContext.StructMembers, parseStructElement); + } + function parseInterfaceDeclaration(fullStart: number, decorators: NodeArray, modifiers: ModifiersArray): InterfaceDeclaration { let node = createNode(SyntaxKind.InterfaceDeclaration, fullStart); node.decorators = decorators; @@ -5631,7 +5687,7 @@ namespace ts { TypeParameters, // Type parameters in type parameter list TypeArguments, // Type arguments in type argument list TupleElementTypes, // Element types in tuple element type list - HeritageClauses, // Heritage clauses for a class or interface declaration. + HeritageClauses, // Heritage clauses for a class, struct or interface declaration. ImportOrExportSpecifiers, // Named import clause's import specifier list JSDocFunctionParameters, JSDocTypeArguments, From 4404b30ddcd2f41d9e11f575074c1a9a944d206f Mon Sep 17 00:00:00 2001 From: jied Date: Sun, 1 Nov 2015 21:04:12 -0800 Subject: [PATCH 06/43] add struct to binder.ts --- src/compiler/binder.ts | 45 +++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 961720d08f9e9..45fba1e035d24 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -440,11 +440,9 @@ namespace ts { case SyntaxKind.ClassExpression: case SyntaxKind.ClassDeclaration: - return declareClassMember(node, symbolFlags, symbolExcludes); - case SyntaxKind.StructExpression: case SyntaxKind.StructDeclaration: - return declareStructMember(node, symbolFlags, symbolExcludes); + return declareClassOrStructMember(node, symbolFlags, symbolExcludes); case SyntaxKind.EnumDeclaration: return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); @@ -483,13 +481,7 @@ namespace ts { } } - function declareClassMember(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags) { - return node.flags & NodeFlags.Static - ? declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes) - : declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); - } - - function declareStructMember(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags) { + function declareClassOrStructMember(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags) { return node.flags & NodeFlags.Static ? declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes) : declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); @@ -679,6 +671,10 @@ namespace ts { return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode; } + if (getContainingStruct(node)) { + return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Struct_definitions_are_automatically_in_strict_mode; + } + if (file.externalModuleIndicator) { return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode; } @@ -737,6 +733,10 @@ namespace ts { return Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode; } + if (getContainingStruct(node)) { + return Diagnostics.Invalid_use_of_0_Struct_definitions_are_automatically_in_strict_mode; + } + if (file.externalModuleIndicator) { return Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode; } @@ -834,6 +834,8 @@ namespace ts { return; case SyntaxKind.ClassDeclaration: case SyntaxKind.ClassExpression: + case SyntaxKind.StructDeclaration: + case SyntaxKind.StructExpression: // All classes are automatically in strict mode in ES6. inStrictMode = true; return; @@ -1008,7 +1010,7 @@ namespace ts { else { let bindingName = node.name ? node.name.text : "__struct"; bindAnonymousDeclaration(node, SymbolFlags.Struct, bindingName); - // Add name of class expression into the map for semantic classifier + // Add name of struct expression into the map for semantic classifier if (node.name) { classifiableNames[node.name.text] = node.name.text; } @@ -1017,12 +1019,12 @@ namespace ts { let symbol = node.symbol; // TypeScript 1.0 spec (April 2014): 8.4 - // Every class automatically contains a static property member named 'prototype', the - // type of which is an instantiation of the class type with type Any supplied as a type + // Every struct automatically contains a static property member named 'prototype', the + // type of which is an instantiation of the struct type with type Any supplied as a type // argument for each type parameter. It is an error to explicitly declare a static // property member with the name 'prototype'. // - // Note: we check for this here because this class may be merging into a module. The + // Note: we check for this here because this struct may be merging into a module. The // module might have an exported variable called 'prototype'. We can't allow that as // that would clash with the built-in 'prototype' for the class. let prototypeSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Prototype, "prototype"); @@ -1122,12 +1124,15 @@ namespace ts { // If this is a property-parameter, then also declare the property symbol into the // containing class. - if (node.flags & NodeFlags.AccessibilityModifier && - node.parent.kind === SyntaxKind.Constructor && - isClassLike(node.parent.parent)) { - - let classDeclaration = node.parent.parent; - declareSymbol(classDeclaration.symbol.members, classDeclaration.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes); + if (node.flags & NodeFlags.AccessibilityModifier && node.parent.kind === SyntaxKind.Constructor) { + if (isClassLike(node.parent.parent)) { + let classDeclaration = node.parent.parent; + declareSymbol(classDeclaration.symbol.members, classDeclaration.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes); + } + else if (isStructLike(node.parent.parent)) { + let structDeclaration = node.parent.parent; + declareSymbol(structDeclaration.symbol.members, structDeclaration.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes); + } } } From 8de38fa0033c1913dcc0ce2e35eab0828b5986c5 Mon Sep 17 00:00:00 2001 From: jied Date: Sun, 1 Nov 2015 21:04:50 -0800 Subject: [PATCH 07/43] add struct to emiter.ts --- src/compiler/emitter.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index ad842b0e5cd98..c3ceee022a7d0 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4950,7 +4950,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitDeclarationName(node); } - let baseTypeNode = getClassExtendsHeritageClauseElement(node); + let baseTypeNode = getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { write(" extends "); emit(baseTypeNode.expression); @@ -5035,7 +5035,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } write("(function ("); - let baseTypeNode = getClassExtendsHeritageClauseElement(node); + let baseTypeNode = getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { write("_super"); } From 6cbeb181c7b6ae0a02375dd1ad1f67f5fd32fe5d Mon Sep 17 00:00:00 2001 From: jied Date: Sun, 1 Nov 2015 21:08:15 -0800 Subject: [PATCH 08/43] lint --- src/compiler/binder.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 45fba1e035d24..c3ac2f807601f 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -671,9 +671,9 @@ namespace ts { return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode; } - if (getContainingStruct(node)) { - return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Struct_definitions_are_automatically_in_strict_mode; - } +if (getContainingStruct(node)) { + return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Struct_definitions_are_automatically_in_strict_mode; +} if (file.externalModuleIndicator) { return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode; @@ -733,9 +733,9 @@ namespace ts { return Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode; } - if (getContainingStruct(node)) { - return Diagnostics.Invalid_use_of_0_Struct_definitions_are_automatically_in_strict_mode; - } +if (getContainingStruct(node)) { + return Diagnostics.Invalid_use_of_0_Struct_definitions_are_automatically_in_strict_mode; +} if (file.externalModuleIndicator) { return Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode; @@ -834,8 +834,8 @@ namespace ts { return; case SyntaxKind.ClassDeclaration: case SyntaxKind.ClassExpression: - case SyntaxKind.StructDeclaration: - case SyntaxKind.StructExpression: + case SyntaxKind.StructDeclaration: + case SyntaxKind.StructExpression: // All classes are automatically in strict mode in ES6. inStrictMode = true; return; From 24946b7e822d96edd9470ce5cd7dfe24044d6b78 Mon Sep 17 00:00:00 2001 From: jied Date: Sun, 1 Nov 2015 21:09:51 -0800 Subject: [PATCH 09/43] add struct to utilities.ts and lint --- src/compiler/utilities.ts | 42 +++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 36510d674d497..144463ef1bba2 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -457,7 +457,7 @@ namespace ts { // Specialized signatures can have string literals as their parameters' type names return node.parent.kind === SyntaxKind.Parameter; case SyntaxKind.ExpressionWithTypeArguments: - return !isExpressionWithTypeArgumentsInClassExtendsClause(node); + return !isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node); // Identifiers and qualified names may be type nodes, depending on their context. Climb // above them to find the lowest container @@ -490,7 +490,7 @@ namespace ts { } switch (parent.kind) { case SyntaxKind.ExpressionWithTypeArguments: - return !isExpressionWithTypeArgumentsInClassExtendsClause(parent); + return !isExpressionWithTypeArgumentsInClassOrStructExtendsClause(parent); case SyntaxKind.TypeParameter: return node === (parent).constraint; case SyntaxKind.PropertyDeclaration: @@ -621,6 +621,10 @@ namespace ts { return node && (node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression); } +export function isStructLike(node: Node): node is StructLikeDeclaration { +return node && (node.kind === SyntaxKind.StructDeclaration || node.kind === SyntaxKind.StructExpression); +} + export function isFunctionLike(node: Node): node is FunctionLikeDeclaration { if (node) { switch (node.kind) { @@ -674,6 +678,15 @@ namespace ts { } } + export function getContainingStruct(node: Node): StructLikeDeclaration { + while (true) { + node = node.parent; + if (!node || isStructLike(node)) { + return node; + } + } + } + export function getContainingClass(node: Node): ClassLikeDeclaration { while (true) { node = node.parent; @@ -996,7 +1009,7 @@ namespace ts { case SyntaxKind.JsxSpreadAttribute: return true; case SyntaxKind.ExpressionWithTypeArguments: - return (parent).expression === node && isExpressionWithTypeArgumentsInClassExtendsClause(parent); + return (parent).expression === node && isExpressionWithTypeArgumentsInClassOrStructExtendsClause(parent); default: if (isExpression(parent)) { return true; @@ -1224,6 +1237,18 @@ namespace ts { } } +export function isStructElement(n: Node): boolean { + switch (n.kind) { + case SyntaxKind.Constructor: + case SyntaxKind.PropertyDeclaration: + case SyntaxKind.MethodDeclaration: + case SyntaxKind.MethodSignature: + return true; + default: + return false; + } +} + export function isClassElement(n: Node): boolean { switch (n.kind) { case SyntaxKind.Constructor: @@ -1311,17 +1336,12 @@ namespace ts { node.kind === SyntaxKind.ExportAssignment && (node).expression.kind === SyntaxKind.Identifier; } - export function getStructExtendsHeritageClauseElement(node: StructLikeDeclaration) { - let heritageClause = getHeritageClause(node.heritageClauses, SyntaxKind.ExtendsKeyword); - return heritageClause && heritageClause.types.length > 0 ? heritageClause.types[0] : undefined; - } - export function getStructImplementsHeritageClauseElements(node: StructLikeDeclaration) { let heritageClause = getHeritageClause(node.heritageClauses, SyntaxKind.ImplementsKeyword); return heritageClause ? heritageClause.types : undefined; } - export function getClassExtendsHeritageClauseElement(node: ClassLikeDeclaration) { + export function getClassOrStructExtendsHeritageClauseElement(node: ClassLikeDeclaration | StructLikeDeclaration) { let heritageClause = getHeritageClause(node.heritageClauses, SyntaxKind.ExtendsKeyword); return heritageClause && heritageClause.types.length > 0 ? heritageClause.types[0] : undefined; } @@ -2049,10 +2069,10 @@ namespace ts { return token >= SyntaxKind.FirstAssignment && token <= SyntaxKind.LastAssignment; } - export function isExpressionWithTypeArgumentsInClassExtendsClause(node: Node): boolean { + export function isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node: Node): boolean { return node.kind === SyntaxKind.ExpressionWithTypeArguments && (node.parent).token === SyntaxKind.ExtendsKeyword && - isClassLike(node.parent.parent); + (isClassLike(node.parent.parent) || isStructLike(node.parent.parent)); } // Returns false if this heritage clause element's expression contains something unsupported From 8564dcd0b3b4f9239c98b156bac57b02b8921650 Mon Sep 17 00:00:00 2001 From: jied Date: Sun, 1 Nov 2015 21:22:53 -0800 Subject: [PATCH 10/43] add struct to checker and partial lint --- src/compiler/checker.ts | 692 +++++++++++++++++++++++++--------------- 1 file changed, 442 insertions(+), 250 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3067ea9301a1e..6b661976f1061 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -157,6 +157,7 @@ namespace ts { let anyArrayType: Type; let getGlobalClassDecoratorType: () => ObjectType; + let getGlobalStructDecoratorType: () => ObjectType; let getGlobalParameterDecoratorType: () => ObjectType; let getGlobalPropertyDecoratorType: () => ObjectType; let getGlobalMethodDecoratorType: () => ObjectType; @@ -560,12 +561,18 @@ namespace ts { case SyntaxKind.PropertySignature: // TypeScript 1.0 spec (April 2014): 8.4.1 // Initializer expressions for instance member variables are evaluated in the scope - // of the class constructor body but are not permitted to reference parameters or + // of the class/struct constructor body but are not permitted to reference parameters or // local variables of the constructor. This effectively means that entities from outer scopes // by the same name as a constructor parameter or local variable are inaccessible // in initializer expressions for instance member variables. - if (isClassLike(location.parent) && !(location.flags & NodeFlags.Static)) { - let ctor = findConstructorDeclaration(location.parent); + if (!(location.flags & NodeFlags.Static)) { + let ctor: ConstructorDeclaration; + if (isClassLike(location.parent)) { + ctor = findClassConstructorDeclaration(location.parent); + } + else if (isStructLike(location.parent)) { + ctor = findStructConstructorDeclaration(location.parent); + } if (ctor && ctor.locals) { if (getSymbol(ctor.locals, name, meaning & SymbolFlags.Value)) { // Remember the property node, it will be used later to report appropriate error @@ -601,21 +608,20 @@ namespace ts { if (lastLocation && lastLocation.flags & NodeFlags.Static) { // TypeScript 1.0 spec (April 2014): 3.4.1 // The scope of a type parameter extends over the entire declaration with which the type - // parameter list is associated, with the exception of static member declarations in classes. - error(errorLocation, Diagnostics.Static_members_cannot_reference_class_type_parameters); + // parameter list is associated, with the exception of static member declarations in structs. + error(errorLocation, Diagnostics.Static_members_cannot_reference_struct_type_parameters); return undefined; } break loop; } if (location.kind === SyntaxKind.StructExpression && meaning & SymbolFlags.Struct) { - let className = (location).name; - if (className && name === className.text) { + let structName = (location).name; + if (structName && name === structName.text) { result = location.symbol; break loop; } } break; - // It is not legal to reference a class's own type parameters from a computed property name that // belongs to the class. For example: // @@ -678,7 +684,7 @@ namespace ts { // @y method(x, y) {} // <-- decorator y should be resolved at the class declaration, not the method. // } // - if (location.parent && isClassElement(location.parent)) { + if (location.parent && isClassElement(location.parent) && isStructElement(location.parent)) { location = location.parent; } break; @@ -1181,7 +1187,16 @@ namespace ts { return false; } - function findConstructorDeclaration(node: ClassLikeDeclaration): ConstructorDeclaration { + function findClassConstructorDeclaration(node: ClassLikeDeclaration): ConstructorDeclaration { + let members = node.members; + for (let member of members) { + if (member.kind === SyntaxKind.Constructor && nodeIsPresent((member).body)) { + return member; + } + } + } + + function findStructConstructorDeclaration(node: StructLikeDeclaration): ConstructorDeclaration { let members = node.members; for (let member of members) { if (member.kind === SyntaxKind.Constructor && nodeIsPresent((member).body)) { @@ -1578,8 +1593,9 @@ namespace ts { } switch (declaration.kind) { case SyntaxKind.ClassExpression: - case SyntaxKind.StructExpression: return "(Anonymous class)"; + case SyntaxKind.StructExpression: + return "(Anonymous struct)"; case SyntaxKind.FunctionExpression: case SyntaxKind.ArrowFunction: return "(Anonymous function)"; @@ -1697,7 +1713,7 @@ namespace ts { else if (type.flags & TypeFlags.Reference) { writeTypeReference(type, flags); } - else if (type.flags & (TypeFlags.Class | TypeFlags.Interface | TypeFlags.Enum | TypeFlags.TypeParameter)) { + else if (type.flags & (TypeFlags.Class | TypeFlags.Struct | TypeFlags.Interface | TypeFlags.Enum | TypeFlags.TypeParameter)) { // The specified symbol flags need to be reinterpreted as type flags buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, flags); } @@ -1739,7 +1755,7 @@ namespace ts { function writeSymbolTypeReference(symbol: Symbol, typeArguments: Type[], pos: number, end: number, flags: TypeFormatFlags) { // Unnamed function expressions and arrow functions have reserved names that we don't want to display - if (symbol.flags & SymbolFlags.Class || !isReservedMemberName(symbol.name)) { + if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Struct) || !isReservedMemberName(symbol.name)) { buildSymbolDisplay(symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, flags); } if (pos < end) { @@ -1809,7 +1825,7 @@ namespace ts { let symbol = type.symbol; if (symbol) { // Always use 'typeof T' for type of class, enum, and module objects - if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) { + if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Struct | SymbolFlags.Enum | SymbolFlags.ValueModule)) { writeTypeofSymbol(type, flags); } else if (shouldWriteTypeOfFunctionSymbol()) { @@ -1988,8 +2004,8 @@ namespace ts { function buildTypeParameterDisplayFromSymbol(symbol: Symbol, writer: SymbolWriter, enclosingDeclaraiton?: Node, flags?: TypeFormatFlags) { let targetSymbol = getTargetSymbol(symbol); - if (targetSymbol.flags & SymbolFlags.Class || targetSymbol.flags & SymbolFlags.Interface || targetSymbol.flags & SymbolFlags.TypeAlias) { - buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol), writer, enclosingDeclaraiton, flags); + if (targetSymbol.flags & SymbolFlags.Class || targetSymbol.flags & SymbolFlags.Struct || targetSymbol.flags & SymbolFlags.Interface || targetSymbol.flags & SymbolFlags.TypeAlias) { + buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrStructOrInterfaceOrTypeAlias(symbol), writer, enclosingDeclaraiton, flags); } } @@ -2344,7 +2360,7 @@ namespace ts { return getSymbolLinks(target).declaredType; } if (propertyName === TypeSystemPropertyName.ResolvedBaseConstructorType) { - Debug.assert(!!((target).flags & TypeFlags.Class)); + Debug.assert(!!((target).flags & (TypeFlags.Class | TypeFlags.Struct))); return (target).resolvedBaseConstructorType; } if (propertyName === TypeSystemPropertyName.ResolvedReturnType) { @@ -2711,7 +2727,7 @@ namespace ts { return links.type; } - function getTypeOfFuncClassEnumModule(symbol: Symbol): Type { + function getTypeOfFuncClassStructEnumModule(symbol: Symbol): Type { let links = getSymbolLinks(symbol); if (!links.type) { links.type = createObjectType(TypeFlags.Anonymous, symbol); @@ -2759,8 +2775,8 @@ namespace ts { if (symbol.flags & (SymbolFlags.Variable | SymbolFlags.Property)) { return getTypeOfVariableOrParameterOrProperty(symbol); } - if (symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) { - return getTypeOfFuncClassEnumModule(symbol); + if (symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.Struct | SymbolFlags.Enum | SymbolFlags.ValueModule)) { + return getTypeOfFuncClassStructEnumModule(symbol); } if (symbol.flags & SymbolFlags.EnumMember) { return getTypeOfEnumMember(symbol); @@ -2824,14 +2840,14 @@ namespace ts { } // The outer type parameters are those defined by enclosing generic classes, methods, or functions. - function getOuterTypeParametersOfClassOrInterface(symbol: Symbol): TypeParameter[] { - let declaration = symbol.flags & SymbolFlags.Class ? symbol.valueDeclaration : getDeclarationOfKind(symbol, SyntaxKind.InterfaceDeclaration); + function getOuterTypeParametersOfClassOrStructOrInterface(symbol: Symbol): TypeParameter[] { + let declaration = symbol.flags & (SymbolFlags.Class | SymbolFlags.Struct) ? symbol.valueDeclaration : getDeclarationOfKind(symbol, SyntaxKind.InterfaceDeclaration); return appendOuterTypeParameters(undefined, declaration); } // The local type parameters are the combined set of type parameters from all declarations of the class, // interface, or type alias. - function getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol: Symbol): TypeParameter[] { + function getLocalTypeParametersOfClassOrStructOrInterfaceOrTypeAlias(symbol: Symbol): TypeParameter[] { let result: TypeParameter[]; for (let node of symbol.declarations) { if (node.kind === SyntaxKind.InterfaceDeclaration || node.kind === SyntaxKind.ClassDeclaration || @@ -2849,7 +2865,7 @@ namespace ts { // The full set of type parameters for a generic class or interface type consists of its outer type parameters plus // its locally declared type parameters. function getTypeParametersOfClassOrInterface(symbol: Symbol): TypeParameter[] { - return concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol)); + return concatenate(getOuterTypeParametersOfClassOrStructOrInterface(symbol), getLocalTypeParametersOfClassOrStructOrInterfaceOrTypeAlias(symbol)); } function isConstructorType(type: Type): boolean { @@ -2857,11 +2873,11 @@ namespace ts { } function getBaseTypeNodeOfClass(type: InterfaceType): ExpressionWithTypeArguments { - return getClassExtendsHeritageClauseElement(type.symbol.valueDeclaration); + return getClassOrStructExtendsHeritageClauseElement(type.symbol.valueDeclaration); } function getBaseTypeNodeOfStruct(type: InterfaceType): ExpressionWithTypeArguments { - return getStructExtendsHeritageClauseElement(type.symbol.valueDeclaration); + return getClassOrStructExtendsHeritageClauseElement(type.symbol.valueDeclaration); } function getConstructorsForTypeArguments(type: ObjectType, typeArgumentNodes: TypeNode[]): Signature[] { @@ -2923,7 +2939,7 @@ namespace ts { } let baseConstructorType = checkExpression(baseTypeNode.expression); if (baseConstructorType.flags & TypeFlags.ObjectType) { - // Resolving the members of a class requires us to resolve the base class of that class. + // Resolving the members of a struct requires us to resolve the base struct of that struct. // We force resolution here such that we catch circularities now. resolveStructuredTypeMembers(baseConstructorType); } @@ -2942,32 +2958,36 @@ namespace ts { function getBaseTypes(type: InterfaceType): ObjectType[] { if (!type.resolvedBaseTypes) { - if (type.symbol.flags & SymbolFlags.Class) { - resolveBaseTypesOfClass(type); + if (type.symbol.flags & (SymbolFlags.Class | SymbolFlags.Struct)) { + resolveBaseTypesOfClassOrStruct(type); } else if (type.symbol.flags & SymbolFlags.Interface) { resolveBaseTypesOfInterface(type); } else { - Debug.fail("type must be class or interface"); + Debug.fail("type must be class, struct or interface"); } } return type.resolvedBaseTypes; } - function resolveBaseTypesOfClass(type: InterfaceType): void { + function resolveBaseTypesOfClassOrStruct(type: InterfaceType): void { + let isInClassDecl = true; + if (type.symbol.flags & SymbolFlags.Struct) { + isInClassDecl = false; + } type.resolvedBaseTypes = emptyArray; - let baseContructorType = getBaseConstructorTypeOfClass(type); + let baseContructorType = isInClassDecl ? getBaseConstructorTypeOfClass(type) : getBaseConstructorTypeOfStruct(type); if (!(baseContructorType.flags & TypeFlags.ObjectType)) { return; } - let baseTypeNode = getBaseTypeNodeOfClass(type); + let baseTypeNode = isInClassDecl ? getBaseTypeNodeOfClass(type) : getBaseTypeNodeOfStruct(type); let baseType: Type; - if (baseContructorType.symbol && baseContructorType.symbol.flags & SymbolFlags.Class) { + if (baseContructorType.symbol && baseContructorType.symbol.flags & (SymbolFlags.Class | SymbolFlags.Struct)) { // When base constructor type is a class we know that the constructors all have the same type parameters as the // class and all return the instance type of the class. There is no need for further checks and we can apply the // type arguments in the same manner as a type reference to get the same error reporting experience. - baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseContructorType.symbol); + baseType = getTypeFromClassOrStructOrInterfaceReference(baseTypeNode, baseContructorType.symbol); } else { // The class derives from a "class-like" constructor function, check that we have at least one construct signature @@ -2983,7 +3003,7 @@ namespace ts { if (baseType === unknownType) { return; } - if (!(getTargetType(baseType).flags & (TypeFlags.Class | TypeFlags.Interface))) { + if (!(getTargetType(baseType).flags & (TypeFlags.Class | TypeFlags.Struct | TypeFlags.Interface))) { error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); return; } @@ -3033,7 +3053,7 @@ namespace ts { for (let node of baseTypeNodes) { if (isSupportedExpressionWithTypeArguments(node)) { let baseSymbol = resolveEntityName(node.expression, SymbolFlags.Type, /*ignoreErrors*/ true); - if (!baseSymbol || !(baseSymbol.flags & SymbolFlags.Interface) || getDeclaredTypeOfClassOrInterface(baseSymbol).thisType) { + if (!baseSymbol || !(baseSymbol.flags & SymbolFlags.Interface) || getDeclaredTypeOfClassOrStructOrInterface(baseSymbol).thisType) { return false; } } @@ -3044,19 +3064,22 @@ namespace ts { return true; } - function getDeclaredTypeOfClassOrInterface(symbol: Symbol): InterfaceType { + function getDeclaredTypeOfClassOrStructOrInterface(symbol: Symbol): InterfaceType { let links = getSymbolLinks(symbol); if (!links.declaredType) { - let kind = symbol.flags & SymbolFlags.Class ? TypeFlags.Class : TypeFlags.Interface; + let kind: TypeFlags; + if (symbol.flags & SymbolFlags.Class) kind = TypeFlags.Class; + else if (symbol.flags & SymbolFlags.Struct) kind = TypeFlags.Struct; + else kind = TypeFlags.Interface; let type = links.declaredType = createObjectType(kind, symbol); - let outerTypeParameters = getOuterTypeParametersOfClassOrInterface(symbol); - let localTypeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); - // A class or interface is generic if it has type parameters or a "this" type. We always give classes a "this" type + let outerTypeParameters = getOuterTypeParametersOfClassOrStructOrInterface(symbol); + let localTypeParameters = getLocalTypeParametersOfClassOrStructOrInterfaceOrTypeAlias(symbol); + // A class, struct or interface is generic if it has type parameters or a "this" type. We always give classes a "this" type // because it is not feasible to analyze all members to determine if the "this" type escapes the class (in particular, // property types inferred from initializers and method return types inferred from return statements are very hard // to exhaustively analyze). We give interfaces a "this" type if we can't definitely determine that they are free of // "this" references. - if (outerTypeParameters || localTypeParameters || kind === TypeFlags.Class || !isIndependentInterface(symbol)) { + if (outerTypeParameters || localTypeParameters || kind === TypeFlags.Class || kind === TypeFlags.Struct || !isIndependentInterface(symbol)) { type.flags |= TypeFlags.Reference; type.typeParameters = concatenate(outerTypeParameters, localTypeParameters); type.outerTypeParameters = outerTypeParameters; @@ -3084,7 +3107,7 @@ namespace ts { let declaration = getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration); let type = getTypeFromTypeNode(declaration.type); if (popTypeResolution()) { - links.typeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); + links.typeParameters = getLocalTypeParametersOfClassOrStructOrInterfaceOrTypeAlias(symbol); if (links.typeParameters) { // Initialize the instantiation cache for generic type aliases. The declared type corresponds to // an instantiation of the type alias with the type parameters supplied as type arguments. @@ -3134,8 +3157,8 @@ namespace ts { function getDeclaredTypeOfSymbol(symbol: Symbol): Type { Debug.assert((symbol.flags & SymbolFlags.Instantiated) === 0); - if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface)) { - return getDeclaredTypeOfClassOrInterface(symbol); + if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Struct | SymbolFlags.Interface)) { + return getDeclaredTypeOfClassOrStructOrInterface(symbol); } if (symbol.flags & SymbolFlags.TypeAlias) { return getDeclaredTypeOfTypeAlias(symbol); @@ -3212,7 +3235,7 @@ namespace ts { return true; } - // Returns true if the class or interface member given by the symbol is free of "this" references. The + // Returns true if the class, struct or interface member given by the symbol is free of "this" references. The // function may return false for symbols that are actually free of "this" references because it is not // feasible to perform a complete analysis in all cases. In particular, property members with types // inferred from their initializers and function members with inferred return types are convervatively @@ -3322,7 +3345,7 @@ namespace ts { setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexType, numberIndexType); } - function resolveClassOrInterfaceMembers(type: InterfaceType): void { + function resolveClassOrStructOrInterfaceMembers(type: InterfaceType): void { resolveObjectTypeMembers(type, resolveDeclaredMembers(type), emptyArray, emptyArray); } @@ -3353,13 +3376,21 @@ namespace ts { sig.minArgumentCount, sig.hasRestParameter, sig.hasStringLiterals); } - function getDefaultConstructSignatures(classType: InterfaceType): Signature[] { - if (!getBaseTypes(classType).length) { - return [createSignature(undefined, classType.localTypeParameters, emptyArray, classType, undefined, 0, false, false)]; + function getDefaultConstructSignatures(classOrStructType: InterfaceType): Signature[] { + if (!getBaseTypes(classOrStructType).length) { + return [createSignature(undefined, classOrStructType.localTypeParameters, emptyArray, classOrStructType, undefined, 0, false, false)]; + } + let baseConstructorType: ObjectType; + let baseTypeNode: ExpressionWithTypeArguments; + if (classOrStructType.symbol.flags & SymbolFlags.Class) { + baseConstructorType = getBaseConstructorTypeOfClass(classOrStructType); + baseTypeNode = getBaseTypeNodeOfClass(classOrStructType); + } + else if (classOrStructType.symbol.flags & SymbolFlags.Struct) { + baseConstructorType = getBaseConstructorTypeOfStruct(classOrStructType); + baseTypeNode = getBaseTypeNodeOfStruct(classOrStructType); } - let baseConstructorType = getBaseConstructorTypeOfClass(classType); let baseSignatures = getSignaturesOfType(baseConstructorType, SignatureKind.Construct); - let baseTypeNode = getBaseTypeNodeOfClass(classType); let typeArguments = map(baseTypeNode.typeArguments, getTypeFromTypeNode); let typeArgCount = typeArguments ? typeArguments.length : 0; let result: Signature[] = []; @@ -3367,8 +3398,8 @@ namespace ts { let typeParamCount = baseSig.typeParameters ? baseSig.typeParameters.length : 0; if (typeParamCount === typeArgCount) { let sig = typeParamCount ? getSignatureInstantiation(baseSig, typeArguments) : cloneSignature(baseSig); - sig.typeParameters = classType.localTypeParameters; - sig.resolvedReturnType = classType; + sig.typeParameters = classOrStructType.localTypeParameters; + sig.resolvedReturnType = classOrStructType; result.push(sig); } } @@ -3524,7 +3555,7 @@ namespace ts { numberIndexType = getIndexTypeOfSymbol(symbol, IndexKind.Number); } else { - // Combinations of function, class, enum and module + // Combinations of function, class, struct, enum and module members = emptySymbols; callSignatures = emptyArray; constructSignatures = emptyArray; @@ -3534,13 +3565,13 @@ namespace ts { if (symbol.flags & (SymbolFlags.Function | SymbolFlags.Method)) { callSignatures = getSignaturesOfSymbol(symbol); } - if (symbol.flags & SymbolFlags.Class) { - let classType = getDeclaredTypeOfClassOrInterface(symbol); + if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Struct)) { + let classOrStructType = getDeclaredTypeOfClassOrStructOrInterface(symbol); constructSignatures = getSignaturesOfSymbol(symbol.members["__constructor"]); if (!constructSignatures.length) { - constructSignatures = getDefaultConstructSignatures(classType); + constructSignatures = getDefaultConstructSignatures(classOrStructType); } - let baseConstructorType = getBaseConstructorTypeOfClass(classType); + let baseConstructorType = getBaseConstructorTypeOfClass(classOrStructType); if (baseConstructorType.flags & TypeFlags.ObjectType) { members = createSymbolTable(getNamedMembers(members)); addInheritedMembers(members, getPropertiesOfObjectType(baseConstructorType)); @@ -3557,8 +3588,8 @@ namespace ts { if (type.flags & TypeFlags.Reference) { resolveTypeReferenceMembers(type); } - else if (type.flags & (TypeFlags.Class | TypeFlags.Interface)) { - resolveClassOrInterfaceMembers(type); + else if (type.flags & (TypeFlags.Class | TypeFlags.Struct | TypeFlags.Interface)) { + resolveClassOrStructOrInterfaceMembers(type); } else if (type.flags & TypeFlags.Anonymous) { resolveAnonymousTypeMembers(type); @@ -3816,10 +3847,22 @@ namespace ts { function getSignatureFromDeclaration(declaration: SignatureDeclaration): Signature { let links = getNodeLinks(declaration); if (!links.resolvedSignature) { - let classType = declaration.kind === SyntaxKind.Constructor ? - getDeclaredTypeOfClassOrInterface(getMergedSymbol((declaration.parent).symbol)) - : undefined; - let typeParameters = classType ? classType.localTypeParameters : + let classOrStructType: InterfaceType; + if (declaration.kind === SyntaxKind.Constructor) { + let parentSymbol: Symbol; +if (declaration.parent.kind === SyntaxKind.ClassDeclaration) { +parentSymbol = (declaration.parent).symbol; +} +else if (declaration.parent.kind === SyntaxKind.StructDeclaration) { +parentSymbol = (declaration.parent).symbol; +} + classOrStructType = getDeclaredTypeOfClassOrStructOrInterface(getMergedSymbol(parentSymbol)); + } + else { + classOrStructType = undefined; + } + + let typeParameters = classOrStructType ? classOrStructType.localTypeParameters : declaration.typeParameters ? getTypeParametersFromDeclaration(declaration.typeParameters) : undefined; let parameters: Symbol[] = []; let hasStringLiterals = false; @@ -3848,8 +3891,8 @@ namespace ts { let returnType: Type; let typePredicate: TypePredicate; - if (classType) { - returnType = classType; + if (classOrStructType) { + returnType = classOrStructType; } else if (declaration.type) { returnType = getTypeFromTypeNode(declaration.type); @@ -4132,8 +4175,8 @@ namespace ts { } } - // Get type from reference to class or interface - function getTypeFromClassOrInterfaceReference(node: TypeReferenceNode | ExpressionWithTypeArguments, symbol: Symbol): Type { + // Get type from reference to class, struct or interface + function getTypeFromClassOrStructOrInterfaceReference(node: TypeReferenceNode | ExpressionWithTypeArguments, symbol: Symbol): Type { let type = getDeclaredTypeOfSymbol(symbol); let typeParameters = type.localTypeParameters; if (typeParameters) { @@ -4141,9 +4184,9 @@ namespace ts { error(node, Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, /*enclosingDeclaration*/ undefined, TypeFormatFlags.WriteArrayAsGenericType), typeParameters.length); return unknownType; } - // In a type reference, the outer type parameters of the referenced class or interface are automatically + // In a type reference, the outer type parameters of the referenced class, struct or interface are automatically // supplied as type arguments and the type reference only specifies arguments for the local type parameters - // of the class or interface. + // of the class, struct or interface. return createTypeReference(type, concatenate(type.outerTypeParameters, map(node.typeArguments, getTypeFromTypeNode))); } if (node.typeArguments) { @@ -4201,7 +4244,7 @@ namespace ts { undefined; let symbol = typeNameOrExpression && resolveEntityName(typeNameOrExpression, SymbolFlags.Type) || unknownSymbol; let type = symbol === unknownSymbol ? unknownType : - symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface) ? getTypeFromClassOrInterfaceReference(node, symbol) : + symbol.flags & (SymbolFlags.Class | SymbolFlags.Struct | SymbolFlags.Interface) ? getTypeFromClassOrStructOrInterfaceReference(node, symbol) : symbol.flags & SymbolFlags.TypeAlias ? getTypeFromTypeAliasReference(node, symbol) : getTypeFromNonGenericTypeReference(node, symbol); // Cache both the resolved symbol and the resolved type. The resolved symbol is needed in when we check the @@ -4244,7 +4287,7 @@ namespace ts { } let type = getDeclaredTypeOfSymbol(symbol); if (!(type.flags & TypeFlags.ObjectType)) { - error(getTypeDeclaration(symbol), Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); + error(getTypeDeclaration(symbol), Diagnostics.Global_type_0_must_be_a_class_or_struct_or_interface_type, symbol.name); return arity ? emptyGenericType : emptyObjectType; } if (((type).typeParameters ? (type).typeParameters.length : 0) !== arity) { @@ -4506,9 +4549,9 @@ namespace ts { function getThisType(node: TypeNode): Type { let container = getThisContainer(node, /*includeArrowFunctions*/ false); let parent = container && container.parent; - if (parent && (isClassLike(parent) || parent.kind === SyntaxKind.InterfaceDeclaration)) { + if (parent && (isClassLike(parent) || isStructLike(parent) || parent.kind === SyntaxKind.InterfaceDeclaration)) { if (!(container.flags & NodeFlags.Static)) { - return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(parent)).thisType; + return getDeclaredTypeOfClassOrStructOrInterface(getSymbolOfNode(parent)).thisType; } } error(node, Diagnostics.A_this_type_is_available_only_in_a_non_static_member_of_a_class_or_interface); @@ -4753,7 +4796,7 @@ namespace ts { return mapper(type); } if (type.flags & TypeFlags.Anonymous) { - return type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) ? + return type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.Struct | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) ? instantiateAnonymousType(type, mapper) : type; } if (type.flags & TypeFlags.Reference) { @@ -6735,8 +6778,8 @@ namespace ts { function captureLexicalThis(node: Node, container: Node): void { getNodeLinks(node).flags |= NodeCheckFlags.LexicalThis; if (container.kind === SyntaxKind.PropertyDeclaration || container.kind === SyntaxKind.Constructor) { - let classNode = container.parent; - getNodeLinks(classNode).flags |= NodeCheckFlags.CaptureThis; + let classOrStructNode = container.parent; + getNodeLinks(classOrStructNode).flags |= NodeCheckFlags.CaptureThis; } else { getNodeLinks(container).flags |= NodeCheckFlags.CaptureThis; @@ -6788,7 +6831,7 @@ namespace ts { captureLexicalThis(node, container); } - if (isClassLike(container.parent)) { + if (isClassLike(container.parent) || isStructLike(container.parent)) { let symbol = getSymbolOfNode(container.parent); return container.flags & NodeFlags.Static ? getTypeOfSymbol(symbol) : (getDeclaredTypeOfSymbol(symbol)).thisType; } @@ -6806,9 +6849,14 @@ namespace ts { function checkSuperExpression(node: Node): Type { let isCallExpression = node.parent.kind === SyntaxKind.CallExpression && (node.parent).expression === node; - let classDeclaration = getContainingClass(node); - let classType = classDeclaration && getDeclaredTypeOfSymbol(getSymbolOfNode(classDeclaration)); - let baseClassType = classType && getBaseTypes(classType)[0]; + let isInClassDecl = true; + let classOrStructDeclaration = getContainingClass(node); + if (!classOrStructDeclaration) { + isInClassDecl = false; + classOrStructDeclaration = getContainingStruct(node); + } + let classOrStructType = classOrStructDeclaration && getDeclaredTypeOfSymbol(getSymbolOfNode(classOrStructDeclaration)); + let baseClassOrStructType = classOrStructType && getBaseTypes(classOrStructType)[0]; let container = getSuperContainer(node, /*includeFunctions*/ true); let needToCaptureLexicalThis = false; @@ -6843,9 +6891,12 @@ namespace ts { } } - if (!baseClassType) { - if (!classDeclaration || !getClassExtendsHeritageClauseElement(classDeclaration)) { - error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_class); + if (!baseClassOrStructType && (!classOrStructDeclaration || !getClassOrStructExtendsHeritageClauseElement(classOrStructDeclaration))) { + if (isInClassDecl) { + error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_class); + } + else if (!isInClassDecl) { + error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_struct); } return unknownType; } @@ -6871,8 +6922,8 @@ namespace ts { } return nodeCheckFlag === NodeCheckFlags.SuperStatic - ? getBaseConstructorTypeOfClass(classType) - : baseClassType; + ? (isInClassDecl ? getBaseConstructorTypeOfClass(classOrStructType) : getBaseConstructorTypeOfStruct(classOrStructType)) + : baseClassOrStructType; function isLegalUsageOfSuperExpression(container: Node): boolean { if (!container) { @@ -6881,16 +6932,16 @@ namespace ts { if (isCallExpression) { // TS 1.0 SPEC (April 2014): 4.8.1 - // Super calls are only permitted in constructors of derived classes + // Super calls are only permitted in constructors of derived classes/structs return container.kind === SyntaxKind.Constructor; } else { // TS 1.0 SPEC (April 2014) // 'super' property access is allowed - // - In a constructor, instance member function, instance member accessor, or instance member variable initializer where this references a derived class instance + // - In a constructor, instance member function, instance member accessor, or instance member variable initializer where this references a derived class/struct instance // - In a static member function or static member accessor - // topmost container must be something that is directly nested in the class declaration + // topmost container must be something that is directly nested in the class/struct declaration if (container && isClassLike(container.parent)) { if (container.flags & NodeFlags.Static) { return container.kind === SyntaxKind.MethodDeclaration || @@ -6908,6 +6959,19 @@ namespace ts { container.kind === SyntaxKind.Constructor; } } + else if (container && isStructLike(container.parent)) { + if (container.flags & NodeFlags.Static) { + return container.kind === SyntaxKind.MethodDeclaration || + container.kind === SyntaxKind.MethodSignature; + } + else { + return container.kind === SyntaxKind.MethodDeclaration || + container.kind === SyntaxKind.MethodSignature || + container.kind === SyntaxKind.PropertyDeclaration || + container.kind === SyntaxKind.PropertySignature || + container.kind === SyntaxKind.Constructor; + } + } } return false; @@ -8082,9 +8146,11 @@ namespace ts { * @param type The type of left. * @param prop The symbol for the right hand side of the property access. */ - function checkClassPropertyAccess(node: PropertyAccessExpression | QualifiedName, left: Expression | QualifiedName, type: Type, prop: Symbol): boolean { + function checkClassOrStructPropertyAccess(node: PropertyAccessExpression | QualifiedName, left: Expression | QualifiedName, type: Type, prop: Symbol): boolean { let flags = getDeclarationFlagsFromSymbol(prop); - let declaringClass = getDeclaredTypeOfSymbol(prop.parent); + let isInClassDecl = true; + if (prop.parent.flags & SymbolFlags.Struct) isInClassDecl = false; + let declaringClassOrStruct = getDeclaredTypeOfSymbol(prop.parent); if (left.kind === SyntaxKind.SuperKeyword) { let errorNode = node.kind === SyntaxKind.PropertyAccessExpression ? @@ -8099,10 +8165,14 @@ namespace ts { // where this references the constructor function object of a derived class, // a super property access is permitted and must specify a public static member function of the base class. if (getDeclarationKindFromSymbol(prop) !== SyntaxKind.MethodDeclaration) { - // `prop` refers to a *property* declared in the super class + // `prop` refers to a *property* declared in the super class/struct // rather than a *method*, so it does not satisfy the above criteria. - - error(errorNode, Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); + if (isInClassDecl) { + error(errorNode, Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); + } + else { + error(errorNode, Diagnostics.Only_public_methods_of_the_base_struct_are_accessible_via_the_super_keyword); + } return false; } @@ -8111,8 +8181,7 @@ namespace ts { // This error could mask a private property access error. But, a member // cannot simultaneously be private and abstract, so this will trigger an // additional error elsewhere. - - error(errorNode, Diagnostics.Abstract_method_0_in_class_1_cannot_be_accessed_via_super_expression, symbolToString(prop), typeToString(declaringClass)); + error(errorNode, Diagnostics.Abstract_method_0_in_class_1_cannot_be_accessed_via_super_expression, symbolToString(prop), typeToString(declaringClassOrStruct)); return false; } } @@ -8124,14 +8193,19 @@ namespace ts { // Property is known to be private or protected at this point // Get the declaring and enclosing class instance types - let enclosingClassDeclaration = getContainingClass(node); + let enclosingClassOrStructDeclaration = isInClassDecl ? getContainingClass(node) : getContainingStruct(node); - let enclosingClass = enclosingClassDeclaration ? getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingClassDeclaration)) : undefined; + let enclosingClassOrStruct = enclosingClassOrStructDeclaration ? getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingClassOrStructDeclaration)) : undefined; // Private property is accessible if declaring and enclosing class are the same if (flags & NodeFlags.Private) { - if (declaringClass !== enclosingClass) { - error(node, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClass)); + if (declaringClassOrStruct !== enclosingClassOrStruct) { + if (isInClassDecl) { + error(node, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClassOrStruct)); + } + else { + error(node, Diagnostics.Property_0_is_private_and_only_accessible_within_struct_1, symbolToString(prop), typeToString(declaringClassOrStruct)); + } return false; } return true; @@ -8144,8 +8218,13 @@ namespace ts { return true; } // A protected property is accessible in the declaring class and classes derived from it - if (!enclosingClass || !hasBaseType(enclosingClass, declaringClass)) { - error(node, Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(declaringClass)); + if (!enclosingClassOrStruct || !hasBaseType(enclosingClassOrStruct, declaringClassOrStruct)) { + if (isInClassDecl) { + error(node, Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(declaringClassOrStruct)); + } + else { + error(node, Diagnostics.Property_0_is_protected_and_only_accessible_within_struct_1_and_its_substructs, symbolToString(prop), typeToString(declaringClassOrStruct)); + } return false; } // No further restrictions for static properties @@ -8159,8 +8238,8 @@ namespace ts { } // TODO: why is the first part of this check here? - if (!(getTargetType(type).flags & (TypeFlags.Class | TypeFlags.Interface) && hasBaseType(type, enclosingClass))) { - error(node, Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClass)); + if (!(getTargetType(type).flags & (TypeFlags.Class | TypeFlags.Struct | TypeFlags.Interface) && hasBaseType(type, enclosingClassOrStruct))) { + error(node, Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClassOrStruct)); return false; } return true; @@ -8195,8 +8274,8 @@ namespace ts { getNodeLinks(node).resolvedSymbol = prop; - if (prop.parent && prop.parent.flags & SymbolFlags.Class) { - checkClassPropertyAccess(node, left, apparentType, prop); + if (prop.parent && prop.parent.flags & (SymbolFlags.Class | SymbolFlags.Struct)) { + checkClassOrStructPropertyAccess(node, left, apparentType, prop); } return getTypeOfSymbol(prop); } @@ -8209,8 +8288,8 @@ namespace ts { let type = checkExpression(left); if (type !== unknownType && !isTypeAny(type)) { let prop = getPropertyOfType(getWidenedType(type), propertyName); - if (prop && prop.parent && prop.parent.flags & SymbolFlags.Class) { - return checkClassPropertyAccess(node, left, type, prop); + if (prop && prop.parent && prop.parent.flags & (SymbolFlags.Class | SymbolFlags.Struct)) { + return checkClassOrStructPropertyAccess(node, left, type, prop); } } return true; @@ -8808,8 +8887,8 @@ namespace ts { if (node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.StructDeclaration) { // For a class decorator, the `target` is the type of the class (e.g. the // "static" or "constructor" side of the class) - let classSymbol = getSymbolOfNode(node); - return getTypeOfSymbol(classSymbol); + let classOrStructSymbol = getSymbolOfNode(node); + return getTypeOfSymbol(classOrStructSymbol); } if (node.kind === SyntaxKind.Parameter) { @@ -8817,22 +8896,28 @@ namespace ts { // parameter's containing method. node = node.parent; if (node.kind === SyntaxKind.Constructor) { - let classSymbol = getSymbolOfNode(node); - return getTypeOfSymbol(classSymbol); + let classOrStructSymbol = getSymbolOfNode(node); + return getTypeOfSymbol(classOrStructSymbol); } } - if (node.kind === SyntaxKind.PropertyDeclaration || - node.kind === SyntaxKind.MethodDeclaration || - node.kind === SyntaxKind.GetAccessor || - node.kind === SyntaxKind.SetAccessor) { - // For a property or method decorator, the `target` is the - // "static"-side type of the parent of the member if the member is - // declared "static"; otherwise, it is the "instance"-side type of the - // parent of the member. - return getParentTypeOfClassElement(node); + if (node.parent.kind === SyntaxKind.ClassDeclaration) { + if (node.kind === SyntaxKind.PropertyDeclaration || + node.kind === SyntaxKind.MethodDeclaration || + node.kind === SyntaxKind.GetAccessor || + node.kind === SyntaxKind.SetAccessor) { + // For a property or method decorator, the `target` is the + // "static"-side type of the parent of the member if the member is + // declared "static"; otherwise, it is the "instance"-side type of the + // parent of the member. + return getParentTypeOfClassOrStructElement(node); + } + } + else if (node.parent.kind === SyntaxKind.StructDeclaration) { + if (node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.MethodDeclaration) { + return getParentTypeOfClassOrStructElement(node); + } } - Debug.fail("Unsupported decorator target."); return unknownType; } @@ -8883,7 +8968,7 @@ namespace ts { // string literal type if the member name is an identifier, number, or string; // otherwise, if the member name is a computed property name it will // be either string or symbol. - let element = node; + let element = (node.parent.kind === SyntaxKind.ClassDeclaration) ? node : node; switch (element.name.kind) { case SyntaxKind.Identifier: case SyntaxKind.NumericLiteral: @@ -9260,7 +9345,13 @@ namespace ts { if (superType !== unknownType) { // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated // with the type arguments specified in the extends clause. - let baseTypeNode = getClassExtendsHeritageClauseElement(getContainingClass(node)); + let baseTypeNode: ExpressionWithTypeArguments; + if (getContainingClass(node)) { + baseTypeNode = getClassOrStructExtendsHeritageClauseElement(getContainingClass(node)); + } + else { + baseTypeNode = getClassOrStructExtendsHeritageClauseElement(getContainingStruct(node)); + } let baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments); return resolveCall(node, baseConstructors, candidatesOutArray); } @@ -9413,7 +9504,7 @@ namespace ts { case SyntaxKind.StructDeclaration: case SyntaxKind.StructExpression: - return Diagnostics.Unable_to_resolve_signature_of_class_decorator_when_called_as_an_expression; + return Diagnostics.Unable_to_resolve_signature_of_struct_decorator_when_called_as_an_expression; case SyntaxKind.Parameter: return Diagnostics.Unable_to_resolve_signature_of_parameter_decorator_when_called_as_an_expression; @@ -9910,7 +10001,7 @@ namespace ts { } } - checkFunctionAndClassExpressionBodies(node.body); + checkFunctionClassStructExpressionBodies(node.body); } } } @@ -11407,6 +11498,9 @@ namespace ts { else if (isClassLike(n)) { return forEach((n).members, containsSuperCallAsComputedPropertyName); } + else if (isStructLike(n)) { + return forEach((n).members, containsSuperCallAsComputedPropertyName); + } return forEachChild(n, containsSuperCall); } @@ -11428,53 +11522,104 @@ namespace ts { // TS 1.0 spec (April 2014): 8.3.2 // Constructors of classes with no extends clause may not contain super calls, whereas // constructors of derived classes must contain at least one super call somewhere in their function body. - let containingClassDecl = node.parent; - if (getClassExtendsHeritageClauseElement(containingClassDecl)) { - let containingClassSymbol = getSymbolOfNode(containingClassDecl); - let containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); - let baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); - - if (containsSuperCall(node.body)) { - if (baseConstructorType === nullType) { - error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); - } - - // The first statement in the body of a constructor (excluding prologue directives) must be a super call - // if both of the following are true: - // - The containing class is a derived class. - // - The constructor declares parameter properties - // or the containing class declares instance member variables with initializers. - let superCallShouldBeFirst = - forEach((node.parent).members, isInstancePropertyWithInitializer) || - forEach(node.parameters, p => p.flags & (NodeFlags.Public | NodeFlags.Private | NodeFlags.Protected)); - - // Skip past any prologue directives to find the first statement - // to ensure that it was a super call. - if (superCallShouldBeFirst) { - let statements = (node.body).statements; - let superCallStatement: ExpressionStatement; - for (let statement of statements) { - if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { - superCallStatement = statement; - break; - } - if (!isPrologueDirective(statement)) { - break; - } - } - if (!superCallStatement) { - error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); - } - else { - // In such a required super call, it is a compile-time error for argument expressions to reference this. - markThisReferencesAsErrors(superCallStatement.expression); - } - } - } - else if (baseConstructorType !== nullType) { - error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); - } - } + if (node.parent.kind === SyntaxKind.ClassDeclaration) { + let containingClassDecl = node.parent; + if (getClassOrStructExtendsHeritageClauseElement(containingClassDecl)) { + let containingClassSymbol = getSymbolOfNode(containingClassDecl); + let containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); + let baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); + + if (containsSuperCall(node.body)) { + if (baseConstructorType === nullType) { + error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); + } + + // The first statement in the body of a constructor (excluding prologue directives) must be a super call + // if both of the following are true: + // - The containing class is a derived class. + // - The constructor declares parameter properties + // or the containing class declares instance member variables with initializers. + let superCallShouldBeFirst = + forEach((node.parent).members, isInstancePropertyWithInitializer) || + forEach(node.parameters, p => p.flags & (NodeFlags.Public | NodeFlags.Private | NodeFlags.Protected)); + + // Skip past any prologue directives to find the first statement + // to ensure that it was a super call. + if (superCallShouldBeFirst) { + let statements = (node.body).statements; + let superCallStatement: ExpressionStatement; + for (let statement of statements) { + if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { + superCallStatement = statement; + break; + } + if (!isPrologueDirective(statement)) { + break; + } + } + if (!superCallStatement) { + error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); + } + else { + // In such a required super call, it is a compile-time error for argument expressions to reference this. + markThisReferencesAsErrors(superCallStatement.expression); + } + } + } + else if (baseConstructorType !== nullType) { + error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); + } + } + } + else if (node.parent.kind === SyntaxKind.StructDeclaration) { + let containingStructDecl = node.parent; + if (getClassOrStructExtendsHeritageClauseElement(containingStructDecl)) { + let containingStructSymbol = getSymbolOfNode(containingStructDecl); + let containingStructInstanceType = getDeclaredTypeOfSymbol(containingStructSymbol); + let baseConstructorType = getBaseConstructorTypeOfStruct(containingStructInstanceType); + + if (containsSuperCall(node.body)) { + if (baseConstructorType === nullType) { + error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_struct_extends_null); + } + + // The first statement in the body of a constructor (excluding prologue directives) must be a super call + // if both of the following are true: + // - The containing struct is a derived struct. + // - The constructor declares parameter properties + // or the containing struct declares instance member variables with initializers. + let superCallShouldBeFirst = + forEach((node.parent).members, isInstancePropertyWithInitializer) || + forEach(node.parameters, p => p.flags & (NodeFlags.Public | NodeFlags.Private)); + + // Skip past any prologue directives to find the first statement + // to ensure that it was a super call. + if (superCallShouldBeFirst) { + let statements = (node.body).statements; + let superCallStatement: ExpressionStatement; + for (let statement of statements) { + if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { + superCallStatement = statement; + break; + } + if (!isPrologueDirective(statement)) { + break; + } + } + if (!superCallStatement) { + error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_struct_contains_initialized_properties_or_has_parameter_properties); + } + else { + // In such a required super call, it is a compile-time error for argument expressions to reference this. + markThisReferencesAsErrors(superCallStatement.expression); + } + } + } + else if (baseConstructorType !== nullType) { + error(node, Diagnostics.Constructors_for_derived_structs_must_contain_a_super_call); + } + } + } } function checkAccessorDeclaration(node: AccessorDeclaration) { @@ -12222,9 +12367,9 @@ namespace ts { switch (node.parent.kind) { case SyntaxKind.ClassDeclaration: case SyntaxKind.StructDeclaration: - let classSymbol = getSymbolOfNode(node.parent); - let classConstructorType = getTypeOfSymbol(classSymbol); - expectedReturnType = getUnionType([classConstructorType, voidType]); + let classOrStructSymbol = getSymbolOfNode(node.parent); + let classOrStructConstructorType = getTypeOfSymbol(classOrStructSymbol); + expectedReturnType = getUnionType([classOrStructConstructorType, voidType]); break; case SyntaxKind.Parameter: @@ -12450,7 +12595,7 @@ namespace ts { forEach(node.statements, checkSourceElement); if (isFunctionBlock(node) || node.kind === SyntaxKind.ModuleBlock) { - checkFunctionAndClassExpressionBodies(node); + checkFunctionClassStructExpressionBodies(node); } if (node.helperStatements !== undefined) { @@ -12557,20 +12702,36 @@ namespace ts { // bubble up and find containing type let enclosingClass = getContainingClass(node); + let enclosingStruct = getContainingStruct(node); + // if containing type was not found or it is ambient - exit (no codegen) - if (!enclosingClass || isInAmbientContext(enclosingClass)) { + if (!enclosingClass && !enclosingStruct || isInAmbientContext(enclosingClass) || isInAmbientContext(enclosingStruct)) { return; } - if (getClassExtendsHeritageClauseElement(enclosingClass)) { - let isDeclaration = node.kind !== SyntaxKind.Identifier; - if (isDeclaration) { - error(node, Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); - } - else { - error(node, Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference); - } - } + if (enclosingClass) { + if (getClassOrStructExtendsHeritageClauseElement(enclosingClass)) { + let isDeclaration = node.kind !== SyntaxKind.Identifier; + if (isDeclaration) { + error(node, Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); + } + else { + error(node, Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference); + } + } + } + else if (enclosingStruct) { + if (getClassOrStructExtendsHeritageClauseElement(enclosingStruct)) { + let isDeclaration = node.kind !== SyntaxKind.Identifier; + if (isDeclaration) { + error(node, Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_struct_reference); + } + else { + error(node, Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_struct_reference); + } + } + } + } function checkCollisionWithRequireExportsInGeneratedCode(node: Node, name: Identifier) { @@ -13229,7 +13390,10 @@ namespace ts { error(node.expression, Diagnostics.Setters_cannot_return_a_value); } else if (func.kind === SyntaxKind.Constructor) { - if (!isTypeAssignableTo(exprType, returnType)) { + if (returnType.flags & TypeFlags.Struct) { // struct constructor cannot have return expression + error(node.expression, Diagnostics.Struct_constructor_cannot_have_return_expression); + } + else if (!isTypeAssignableTo(exprType, returnType)) { error(node.expression, Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); } } @@ -13520,7 +13684,7 @@ namespace ts { function checkStructDeclaration(node: StructDeclaration) { if (!node.name && !(node.flags & NodeFlags.Default)) { - grammarErrorOnFirstToken(node, Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); + grammarErrorOnFirstToken(node, Diagnostics.A_struct_declaration_without_the_default_modifier_must_have_a_name); } checkStructLikeDeclaration(node); @@ -13540,7 +13704,7 @@ namespace ts { checkGrammarStructDeclarationHeritageClauses(node); checkDecorators(node); if (node.name) { - checkTypeNameIsReserved(node.name, Diagnostics.Class_name_cannot_be_0); + checkTypeNameIsReserved(node.name, Diagnostics.Struct_name_cannot_be_0); checkCollisionWithCapturedThisVariable(node, node.name); checkCollisionWithRequireExportsInGeneratedCode(node, node.name); } @@ -13551,7 +13715,7 @@ namespace ts { let typeWithThis = getTypeWithThisArgument(type); let staticType = getTypeOfSymbol(symbol); - let baseTypeNode = getStructExtendsHeritageClauseElement(node); + let baseTypeNode = getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitExtends = emitExtends || !isInAmbientContext(node); let baseTypes = getBaseTypes(type); @@ -13567,15 +13731,15 @@ namespace ts { } } } - checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType, type.thisType), node.name || node, Diagnostics.Class_0_incorrectly_extends_base_class_1); + checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType, type.thisType), node.name || node, Diagnostics.Struct_0_incorrectly_extends_base_struct_1); checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node, - Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); + Diagnostics.Struct_static_side_0_incorrectly_extends_base_struct_static_side_1); if (!(staticBaseType.symbol && staticBaseType.symbol.flags & SymbolFlags.Struct)) { - // When the static base type is a "class-like" constructor function (but not actually a class), we verify + // When the static base type is a "struct-like" constructor function (but not actually a struct), we verify // that all instantiated base constructor signatures return the same type. We can simply compare the type // references (as opposed to checking the structure of the types) because elsewhere we have already checked - // that the base type is a class or interface type (and not, for example, an anonymous object type). + // that the base type is a struct (and not, for example, an anonymous object type). let constructors = getInstantiatedConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments); if (forEach(constructors, sig => getReturnTypeOfSignature(sig) !== baseType)) { error(baseTypeNode.expression, Diagnostics.Base_constructors_must_all_have_the_same_return_type); @@ -13585,30 +13749,8 @@ namespace ts { } } - let implementedTypeNodes = getStructImplementsHeritageClauseElements(node); - if (implementedTypeNodes) { - for (let typeRefNode of implementedTypeNodes) { - if (!isSupportedExpressionWithTypeArguments(typeRefNode)) { - error(typeRefNode.expression, Diagnostics.A_class_can_only_implement_an_identifier_Slashqualified_name_with_optional_type_arguments); - } - checkTypeReferenceNode(typeRefNode); - if (produceDiagnostics) { - let t = getTypeFromTypeNode(typeRefNode); - if (t !== unknownType) { - let declaredType = (t.flags & TypeFlags.Reference) ? (t).target : t; - if (declaredType.flags & (TypeFlags.Struct | TypeFlags.Interface)) { - checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(t, type.thisType), node.name || node, Diagnostics.Class_0_incorrectly_implements_interface_1); - } - else { - error(typeRefNode, Diagnostics.A_class_may_only_implement_another_class_or_interface); - } - } - } - } - } - if (produceDiagnostics) { - checkIndexConstraints(type); + // checkIndexConstraints(type); checkTypeForDuplicateIndexSignatures(node); } } @@ -13628,7 +13770,7 @@ namespace ts { let typeWithThis = getTypeWithThisArgument(type); let staticType = getTypeOfSymbol(symbol); - let baseTypeNode = getClassExtendsHeritageClauseElement(node); + let baseTypeNode = getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitExtends = emitExtends || !isInAmbientContext(node); let baseTypes = getBaseTypes(type); @@ -14175,10 +14317,10 @@ namespace ts { } } - function getFirstNonAmbientClassOrFunctionDeclaration(symbol: Symbol): Declaration { + function getFirstNonAmbientClassOrStructOrFunctionDeclaration(symbol: Symbol): Declaration { let declarations = symbol.declarations; for (let declaration of declarations) { - if ((declaration.kind === SyntaxKind.ClassDeclaration || + if ((declaration.kind === SyntaxKind.ClassDeclaration || declaration.kind === SyntaxKind.StructDeclaration || (declaration.kind === SyntaxKind.FunctionDeclaration && nodeIsPresent((declaration).body))) && !isInAmbientContext(declaration)) { return declaration; @@ -14229,13 +14371,13 @@ namespace ts { && symbol.declarations.length > 1 && !isInAmbientContext(node) && isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules)) { - let firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); - if (firstNonAmbientClassOrFunc) { - if (getSourceFileOfNode(node) !== getSourceFileOfNode(firstNonAmbientClassOrFunc)) { - error(node.name, Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged); + let firstNonAmbientClassOrStructOrFunc = getFirstNonAmbientClassOrStructOrFunctionDeclaration(symbol); + if (firstNonAmbientClassOrStructOrFunc) { + if (getSourceFileOfNode(node) !== getSourceFileOfNode(firstNonAmbientClassOrStructOrFunc)) { + error(node.name, Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_struct_or_function_with_which_it_is_merged); } - else if (node.pos < firstNonAmbientClassOrFunc.pos) { - error(node.name, Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged); + else if (node.pos < firstNonAmbientClassOrStructOrFunc.pos) { + error(node.name, Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_struct_or_function_with_which_it_is_merged); } } @@ -14246,6 +14388,14 @@ namespace ts { inSameLexicalScope(node, mergedClass)) { getNodeLinks(node).flags |= NodeCheckFlags.LexicalModuleMergesWithClass; } + if (!mergedClass) { + let mergedStruct = getDeclarationOfKind(symbol, SyntaxKind.StructDeclaration); + if (mergedStruct && + inSameLexicalScope(node, mergedStruct)) { + getNodeLinks(node).flags |= NodeCheckFlags.LexicalModuleMergesWithStruct; + } + + } } // Checks for ambient external modules. @@ -14515,6 +14665,7 @@ namespace ts { // hitting the cancellation token on every node we check. switch (kind) { case SyntaxKind.ModuleDeclaration: + case SyntaxKind.StructDeclaration: case SyntaxKind.ClassDeclaration: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.FunctionDeclaration: @@ -14602,6 +14753,8 @@ namespace ts { return checkVariableDeclaration(node); case SyntaxKind.BindingElement: return checkBindingElement(node); + case SyntaxKind.StructDeclaration: + return checkStructDeclaration(node); case SyntaxKind.ClassDeclaration: return checkClassDeclaration(node); case SyntaxKind.InterfaceDeclaration: @@ -14640,25 +14793,25 @@ namespace ts { // Here, performing a full type check of the body of the function expression whilst in the process of // determining the type of foo would cause foo to be given type any because of the recursive reference. // Delaying the type check of the body ensures foo has been assigned a type. - function checkFunctionAndClassExpressionBodies(node: Node): void { + function checkFunctionClassStructExpressionBodies(node: Node): void { switch (node.kind) { case SyntaxKind.FunctionExpression: case SyntaxKind.ArrowFunction: - forEach((node).parameters, checkFunctionAndClassExpressionBodies); + forEach((node).parameters, checkFunctionClassStructExpressionBodies); checkFunctionExpressionOrObjectLiteralMethodBody(node); break; case SyntaxKind.ClassExpression: forEach((node).members, checkSourceElement); - forEachChild(node, checkFunctionAndClassExpressionBodies); + forEachChild(node, checkFunctionClassStructExpressionBodies); break; case SyntaxKind.StructExpression: forEach((node).members, checkSourceElement); - forEachChild(node, checkFunctionAndClassExpressionBodies); + forEachChild(node, checkFunctionClassStructExpressionBodies); break; case SyntaxKind.MethodDeclaration: case SyntaxKind.MethodSignature: - forEach(node.decorators, checkFunctionAndClassExpressionBodies); - forEach((node).parameters, checkFunctionAndClassExpressionBodies); + forEach(node.decorators, checkFunctionClassStructExpressionBodies); + forEach((node).parameters, checkFunctionClassStructExpressionBodies); if (isObjectLiteralMethod(node)) { checkFunctionExpressionOrObjectLiteralMethodBody(node); } @@ -14667,10 +14820,10 @@ namespace ts { case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: case SyntaxKind.FunctionDeclaration: - forEach((node).parameters, checkFunctionAndClassExpressionBodies); + forEach((node).parameters, checkFunctionClassStructExpressionBodies); break; case SyntaxKind.WithStatement: - checkFunctionAndClassExpressionBodies((node).expression); + checkFunctionClassStructExpressionBodies((node).expression); break; case SyntaxKind.Decorator: case SyntaxKind.Parameter: @@ -14738,7 +14891,7 @@ namespace ts { case SyntaxKind.JsxAttribute: case SyntaxKind.JsxSpreadAttribute: case SyntaxKind.JsxOpeningElement: - forEachChild(node, checkFunctionAndClassExpressionBodies); + forEachChild(node, checkFunctionClassStructExpressionBodies); break; } } @@ -14770,7 +14923,7 @@ namespace ts { potentialThisCollisions.length = 0; forEach(node.statements, checkSourceElement); - checkFunctionAndClassExpressionBodies(node); + checkFunctionClassStructExpressionBodies(node); if (isExternalModule(node)) { checkExternalModuleExports(node); @@ -14895,11 +15048,15 @@ namespace ts { copySymbols(getSymbolOfNode(location).exports, meaning & SymbolFlags.EnumMember); break; case SyntaxKind.ClassExpression: - case SyntaxKind.StructExpression: let className = (location).name; if (className) { copySymbol(location.symbol, meaning); } + case SyntaxKind.StructExpression: + let structName = (location).name; + if (structName) { + copySymbol(location.symbol, meaning); + } // fall through; this fall-through is necessary because we would like to handle // type parameter inside class expression similar to how we handle it in classDeclaration and interface Declaration case SyntaxKind.ClassDeclaration: @@ -15047,7 +15204,7 @@ namespace ts { meaning = SymbolFlags.Type; // In a class 'extends' clause we are also looking for a value. - if (isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { + if (isExpressionWithTypeArgumentsInClassOrStructExtendsClause(entityName.parent)) { meaning |= SymbolFlags.Value; } } @@ -15153,7 +15310,13 @@ namespace ts { // constructor keyword for an overload, should take us to the definition if it exist let constructorDeclaration = node.parent; if (constructorDeclaration && constructorDeclaration.kind === SyntaxKind.Constructor) { - return (constructorDeclaration.parent).symbol; + if (constructorDeclaration.parent.kind === SyntaxKind.ClassDeclaration) { + return (constructorDeclaration.parent).symbol; + } + else if (constructorDeclaration.parent.kind === SyntaxKind.StructDeclaration) { + return (constructorDeclaration.parent).symbol; + } + } return undefined; @@ -15205,7 +15368,7 @@ namespace ts { return getTypeOfExpression(node); } - if (isExpressionWithTypeArgumentsInClassExtendsClause(node)) { + if (isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node)) { // A SyntaxKind.ExpressionWithTypeArguments is considered a type node, except when it occurs in the // extends clause of a class. We handle that case here. return getBaseTypes(getDeclaredTypeOfSymbol(getSymbolOfNode(node.parent.parent)))[0]; @@ -15258,11 +15421,11 @@ namespace ts { * Gets either the static or instance type of a class element, based on * whether the element is declared as "static". */ - function getParentTypeOfClassElement(node: ClassElement) { - let classSymbol = getSymbolOfNode(node.parent); + function getParentTypeOfClassOrStructElement(node: ClassElement | StructElement) { + let classOrStructSymbol = getSymbolOfNode(node.parent); return node.flags & NodeFlags.Static - ? getTypeOfSymbol(classSymbol) - : getDeclaredTypeOfSymbol(classSymbol); + ? getTypeOfSymbol(classOrStructSymbol) + : getDeclaredTypeOfSymbol(classOrStructSymbol); } // Return the list of properties of the given type, augmented with properties from Function @@ -15535,7 +15698,7 @@ namespace ts { } } - function writeStructProperty(node: ClassElement, writer: EmitTextWriter): boolean { + function writeStructProperty(node: StructElement, writer: EmitTextWriter): boolean { let symbol = getSymbolOfNode(node); let type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature)) ? getTypeOfSymbol(symbol) @@ -15684,6 +15847,7 @@ namespace ts { jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element); getGlobalClassDecoratorType = memoize(() => getGlobalType("ClassDecorator")); + getGlobalClassDecoratorType = memoize(() => getGlobalType("StructDecorator")); getGlobalPropertyDecoratorType = memoize(() => getGlobalType("PropertyDecorator")); getGlobalMethodDecoratorType = memoize(() => getGlobalType("MethodDecorator")); getGlobalParameterDecoratorType = memoize(() => getGlobalType("ParameterDecorator")); @@ -15886,6 +16050,9 @@ namespace ts { else if (node.parent.kind === SyntaxKind.ClassDeclaration) { return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_class_element, "export"); } + else if (node.parent.kind === SyntaxKind.StructDeclaration) { + return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_struct_element, "export"); + } else if (node.kind === SyntaxKind.Parameter) { return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_parameter, "export"); } @@ -15902,6 +16069,9 @@ namespace ts { else if (node.parent.kind === SyntaxKind.ClassDeclaration) { return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_class_element, "declare"); } + else if (node.parent.kind === SyntaxKind.StructDeclaration) { + return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_struct_element, "declare"); + } else if (node.kind === SyntaxKind.Parameter) { return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_parameter, "declare"); } @@ -16180,7 +16350,7 @@ namespace ts { } if (heritageClause.types.length > 1) { - return grammarErrorOnFirstToken(heritageClause.types[1], Diagnostics.Classes_can_only_extend_a_single_class); + return grammarErrorOnFirstToken(heritageClause.types[1], Diagnostics.Structs_can_only_extend_a_single_struct); } seenExtendsClause = true; @@ -16194,7 +16364,7 @@ namespace ts { seenImplementsClause = true; } - // Grammar checking heritageClause inside class declaration + // Grammar checking heritageClause inside struct declaration checkGrammarHeritageClause(heritageClause); } } @@ -16508,6 +16678,22 @@ namespace ts { return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_method_overload_must_directly_refer_to_a_built_in_symbol); } } + else if (isStructLike(node.parent)) { + if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.A_struct_member_cannot_be_declared_optional)) { + return true; + } + // Technically, computed properties in ambient contexts is disallowed + // for property declarations and accessors too, not just methods. + // However, property declarations disallow computed names in general, + // and accessors are not allowed in ambient contexts in general, + // so this error only really matters for methods. + if (isInAmbientContext(node)) { + return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol); + } + else if (!node.body) { + return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_method_overload_must_directly_refer_to_a_built_in_symbol); + } + } else if (node.parent.kind === SyntaxKind.InterfaceDeclaration) { return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol); } @@ -16761,6 +16947,12 @@ namespace ts { return true; } } + else if (isStructLike(node.parent)) { + if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.A_struct_member_cannot_be_declared_optional) || + checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_struct_property_declaration_must_directly_refer_to_a_built_in_symbol)) { + return true; + } + } else if (node.parent.kind === SyntaxKind.InterfaceDeclaration) { if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol)) { return true; From 993c2383545021cc769500878a5e5e6847b1f2c7 Mon Sep 17 00:00:00 2001 From: jied Date: Thu, 5 Nov 2015 14:02:34 -0800 Subject: [PATCH 11/43] struct expression enables and struct body with statemetns not allowed --- .../structBodyWithStatements.errors.txt | 28 ++++++++ .../reference/structBodyWithStatements.js | 60 +++++++++++++++++ tests/baselines/reference/structExpression.js | 64 +++++++++++++++++++ .../reference/structExpression.symbols | 25 ++++++++ .../reference/structExpression.types | 29 +++++++++ 5 files changed, 206 insertions(+) create mode 100644 tests/baselines/reference/structBodyWithStatements.errors.txt create mode 100644 tests/baselines/reference/structBodyWithStatements.js create mode 100644 tests/baselines/reference/structExpression.js create mode 100644 tests/baselines/reference/structExpression.symbols create mode 100644 tests/baselines/reference/structExpression.types diff --git a/tests/baselines/reference/structBodyWithStatements.errors.txt b/tests/baselines/reference/structBodyWithStatements.errors.txt new file mode 100644 index 0000000000000..b49d56b1b6068 --- /dev/null +++ b/tests/baselines/reference/structBodyWithStatements.errors.txt @@ -0,0 +1,28 @@ +tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts(2,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected. +tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts(3,1): error TS1128: Declaration or statement expected. +tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts(6,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected. +tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts(7,1): error TS1128: Declaration or statement expected. + + +==== tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts (4 errors) ==== + struct C { + var x = 1; // error, variable statement + ~~~ +!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected. + } + ~ +!!! error TS1128: Declaration or statement expected. + + struct C2 { + function foo() {} // error, function declaration + ~~~~~~~~ +!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected. + } + ~ +!!! error TS1128: Declaration or statement expected. + + var x = 1; + var y = 2; + struct C3 { + x: number = y + 1; // ok, not a statement (need var in the statement production). + } \ No newline at end of file diff --git a/tests/baselines/reference/structBodyWithStatements.js b/tests/baselines/reference/structBodyWithStatements.js new file mode 100644 index 0000000000000..db29448d8a090 --- /dev/null +++ b/tests/baselines/reference/structBodyWithStatements.js @@ -0,0 +1,60 @@ +//// [structBodyWithStatements.ts] +struct C { + var x = 1; // error, variable statement +} + +struct C2 { + function foo() {} // error, function declaration +} + +var x = 1; +var y = 2; +struct C3 { + x: number = y + 1; // ok, not a statement (need var in the statement production). +} + +//// [structBodyWithStatements.js] +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var x = 1; // error, variable statement +var C2 = (function () { + var _C2 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C2() { + var obj = new _C2(); + _ctor.call(obj); + return obj; + } + C2._TO = _C2; + return C2; +})(); +function foo() { } // error, function declaration +var x = 1; +var y = 2; +var C3 = (function () { + var _C3 = new TypedObject.StructType({ + x: TypedObject.float64 + }); + function _ctor() { + } + function C3() { + var obj = new _C3(); + _ctor.call(obj); + return obj; + } + C3._TO = _C3; + return C3; +})(); diff --git a/tests/baselines/reference/structExpression.js b/tests/baselines/reference/structExpression.js new file mode 100644 index 0000000000000..5155df6afe441 --- /dev/null +++ b/tests/baselines/reference/structExpression.js @@ -0,0 +1,64 @@ +//// [structExpression.ts] +// struct expression are supported + +var x = struct C { +} + +var y = { + foo: struct C2 { + } +} + +module M { + var z = struct C4 { + } +} + +//// [structExpression.js] +// struct expression are supported +var x = var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +; +var y = { + foo: var C2 = (function () { + var _C2 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C2() { + var obj = new _C2(); + _ctor.call(obj); + return obj; + } + C2._TO = _C2; + return C2; + })(); +}; +var M; +(function (M) { + var z = var C4 = (function () { + var _C4 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C4() { + var obj = new _C4(); + _ctor.call(obj); + return obj; + } + C4._TO = _C4; + return C4; + })(); + ; +})(M || (M = {})); diff --git a/tests/baselines/reference/structExpression.symbols b/tests/baselines/reference/structExpression.symbols new file mode 100644 index 0000000000000..33584eb1e553a --- /dev/null +++ b/tests/baselines/reference/structExpression.symbols @@ -0,0 +1,25 @@ +=== tests/cases/conformance/structs/structExpression.ts === +// struct expression are supported + +var x = struct C { +>x : Symbol(x, Decl(structExpression.ts, 2, 3)) +>C : Symbol(C, Decl(structExpression.ts, 2, 7)) +} + +var y = { +>y : Symbol(y, Decl(structExpression.ts, 5, 3)) + + foo: struct C2 { +>foo : Symbol(foo, Decl(structExpression.ts, 5, 9)) +>C2 : Symbol(C2, Decl(structExpression.ts, 6, 8)) + } +} + +module M { +>M : Symbol(M, Decl(structExpression.ts, 8, 1)) + + var z = struct C4 { +>z : Symbol(z, Decl(structExpression.ts, 11, 7)) +>C4 : Symbol(C4, Decl(structExpression.ts, 11, 11)) + } +} diff --git a/tests/baselines/reference/structExpression.types b/tests/baselines/reference/structExpression.types new file mode 100644 index 0000000000000..3b627cfc87fc2 --- /dev/null +++ b/tests/baselines/reference/structExpression.types @@ -0,0 +1,29 @@ +=== tests/cases/conformance/structs/structExpression.ts === +// struct expression are supported + +var x = struct C { +>x : typeof C +>struct C {} : typeof C +>C : typeof C +} + +var y = { +>y : { foo: typeof C2; } +>{ foo: struct C2 { }} : { foo: typeof C2; } + + foo: struct C2 { +>foo : typeof C2 +>struct C2 { } : typeof C2 +>C2 : typeof C2 + } +} + +module M { +>M : typeof M + + var z = struct C4 { +>z : typeof C4 +>struct C4 { } : typeof C4 +>C4 : typeof C4 + } +} From e5147a3c3dfab49f709756449de00df000c76cc2 Mon Sep 17 00:00:00 2001 From: jied Date: Thu, 5 Nov 2015 14:03:27 -0800 Subject: [PATCH 12/43] struct expression test --- .../cases/conformance/structs/structExpression.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/cases/conformance/structs/structExpression.ts diff --git a/tests/cases/conformance/structs/structExpression.ts b/tests/cases/conformance/structs/structExpression.ts new file mode 100644 index 0000000000000..85ae8888c3c1f --- /dev/null +++ b/tests/cases/conformance/structs/structExpression.ts @@ -0,0 +1,14 @@ +// struct expression are supported + +var x = struct C { +} + +var y = { + foo: struct C2 { + } +} + +module M { + var z = struct C4 { + } +} \ No newline at end of file From 07118def63cb369dff03ed6685d19549036f1cf1 Mon Sep 17 00:00:00 2001 From: jied Date: Thu, 5 Nov 2015 14:07:12 -0800 Subject: [PATCH 13/43] test - struct with predefined types as names --- ...tructWithPredefinedTypesAsNames.errors.txt | 21 +++++++ .../structWithPredefinedTypesAsNames.js | 62 +++++++++++++++++++ .../structWithPredefinedTypesAsNames.ts | 6 ++ 3 files changed, 89 insertions(+) create mode 100644 tests/baselines/reference/structWithPredefinedTypesAsNames.errors.txt create mode 100644 tests/baselines/reference/structWithPredefinedTypesAsNames.js create mode 100644 tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames.ts diff --git a/tests/baselines/reference/structWithPredefinedTypesAsNames.errors.txt b/tests/baselines/reference/structWithPredefinedTypesAsNames.errors.txt new file mode 100644 index 0000000000000..5fccee31781ab --- /dev/null +++ b/tests/baselines/reference/structWithPredefinedTypesAsNames.errors.txt @@ -0,0 +1,21 @@ +tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames.ts(3,8): error TS20002: Struct name cannot be 'any' +tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames.ts(4,8): error TS20002: Struct name cannot be 'number' +tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames.ts(5,8): error TS20002: Struct name cannot be 'boolean' +tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames.ts(6,8): error TS20002: Struct name cannot be 'string' + + +==== tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames.ts (4 errors) ==== + // structs cannot use predefined types as names + + struct any { } + ~~~ +!!! error TS20002: Struct name cannot be 'any' + struct number { } + ~~~~~~ +!!! error TS20002: Struct name cannot be 'number' + struct boolean { } + ~~~~~~~ +!!! error TS20002: Struct name cannot be 'boolean' + struct string { } + ~~~~~~ +!!! error TS20002: Struct name cannot be 'string' \ No newline at end of file diff --git a/tests/baselines/reference/structWithPredefinedTypesAsNames.js b/tests/baselines/reference/structWithPredefinedTypesAsNames.js new file mode 100644 index 0000000000000..3ab9370eb6413 --- /dev/null +++ b/tests/baselines/reference/structWithPredefinedTypesAsNames.js @@ -0,0 +1,62 @@ +//// [structWithPredefinedTypesAsNames.ts] +// structs cannot use predefined types as names + +struct any { } +struct number { } +struct boolean { } +struct string { } + +//// [structWithPredefinedTypesAsNames.js] +// structs cannot use predefined types as names +var any = (function () { + var _any = new TypedObject.StructType({ + }); + function _ctor() { + } + function any() { + var obj = new _any(); + _ctor.call(obj); + return obj; + } + any._TO = _any; + return any; +})(); +var number = (function () { + var _number = new TypedObject.StructType({ + }); + function _ctor() { + } + function number() { + var obj = new _number(); + _ctor.call(obj); + return obj; + } + number._TO = _number; + return number; +})(); +var boolean = (function () { + var _boolean = new TypedObject.StructType({ + }); + function _ctor() { + } + function boolean() { + var obj = new _boolean(); + _ctor.call(obj); + return obj; + } + boolean._TO = _boolean; + return boolean; +})(); +var string = (function () { + var _string = new TypedObject.StructType({ + }); + function _ctor() { + } + function string() { + var obj = new _string(); + _ctor.call(obj); + return obj; + } + string._TO = _string; + return string; +})(); diff --git a/tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames.ts b/tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames.ts new file mode 100644 index 0000000000000..fb6c8ef08b6f0 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames.ts @@ -0,0 +1,6 @@ +// structs cannot use predefined types as names + +struct any { } +struct number { } +struct boolean { } +struct string { } \ No newline at end of file From 1daf34e0f7a87461722e417fdb2bb504ff5ec750 Mon Sep 17 00:00:00 2001 From: jied Date: Thu, 5 Nov 2015 15:02:26 -0800 Subject: [PATCH 14/43] tests - struct has the same name as class, interface and variables --- .../structAndClassWithSameName.errors.txt | 27 +++++++++ .../reference/structAndClassWithSameName.js | 56 +++++++++++++++++++ .../structAndInterfaceWithSameName.errors.txt | 27 +++++++++ .../structAndInterfaceWithSameName.js | 46 +++++++++++++++ .../structAndVariableWithSameName.errors.txt | 25 +++++++++ .../structAndVariableWithSameName.js | 48 ++++++++++++++++ .../structAndClassWithSameName.ts | 12 ++++ .../structAndInterfaceWithSameName.ts | 12 ++++ .../structAndVariableWithSameName.ts | 10 ++++ 9 files changed, 263 insertions(+) create mode 100644 tests/baselines/reference/structAndClassWithSameName.errors.txt create mode 100644 tests/baselines/reference/structAndClassWithSameName.js create mode 100644 tests/baselines/reference/structAndInterfaceWithSameName.errors.txt create mode 100644 tests/baselines/reference/structAndInterfaceWithSameName.js create mode 100644 tests/baselines/reference/structAndVariableWithSameName.errors.txt create mode 100644 tests/baselines/reference/structAndVariableWithSameName.js create mode 100644 tests/cases/conformance/structs/structDeclarations/structAndClassWithSameName.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structAndInterfaceWithSameName.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structAndVariableWithSameName.ts diff --git a/tests/baselines/reference/structAndClassWithSameName.errors.txt b/tests/baselines/reference/structAndClassWithSameName.errors.txt new file mode 100644 index 0000000000000..d6417703ea4ad --- /dev/null +++ b/tests/baselines/reference/structAndClassWithSameName.errors.txt @@ -0,0 +1,27 @@ +tests/cases/conformance/structs/structDeclarations/structAndClassWithSameName.ts(1,8): error TS2300: Duplicate identifier 'C'. +tests/cases/conformance/structs/structDeclarations/structAndClassWithSameName.ts(2,7): error TS2300: Duplicate identifier 'C'. +tests/cases/conformance/structs/structDeclarations/structAndClassWithSameName.ts(5,12): error TS2300: Duplicate identifier 'D'. +tests/cases/conformance/structs/structDeclarations/structAndClassWithSameName.ts(9,11): error TS2300: Duplicate identifier 'D'. + + +==== tests/cases/conformance/structs/structDeclarations/structAndClassWithSameName.ts (4 errors) ==== + struct C { foo: string; } + ~ +!!! error TS2300: Duplicate identifier 'C'. + class C { foo: string; } // error + ~ +!!! error TS2300: Duplicate identifier 'C'. + + module M { + struct D { + ~ +!!! error TS2300: Duplicate identifier 'D'. + bar: string; + } + + class D { // error + ~ +!!! error TS2300: Duplicate identifier 'D'. + bar: string; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/structAndClassWithSameName.js b/tests/baselines/reference/structAndClassWithSameName.js new file mode 100644 index 0000000000000..82a3f6f6b776d --- /dev/null +++ b/tests/baselines/reference/structAndClassWithSameName.js @@ -0,0 +1,56 @@ +//// [structAndClassWithSameName.ts] +struct C { foo: string; } +class C { foo: string; } // error + +module M { + struct D { + bar: string; + } + + class D { // error + bar: string; + } +} + +//// [structAndClassWithSameName.js] +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var C = (function () { + function C() { + } + return C; +})(); // error +var M; +(function (M) { + var D = (function () { + var _D = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; + })(); + var D = (function () { + function D() { + } + return D; + })(); +})(M || (M = {})); diff --git a/tests/baselines/reference/structAndInterfaceWithSameName.errors.txt b/tests/baselines/reference/structAndInterfaceWithSameName.errors.txt new file mode 100644 index 0000000000000..ecd3c46dd15ec --- /dev/null +++ b/tests/baselines/reference/structAndInterfaceWithSameName.errors.txt @@ -0,0 +1,27 @@ +tests/cases/conformance/structs/structDeclarations/structAndInterfaceWithSameName.ts(1,12): error TS2300: Duplicate identifier 'foo'. +tests/cases/conformance/structs/structDeclarations/structAndInterfaceWithSameName.ts(2,15): error TS2300: Duplicate identifier 'foo'. +tests/cases/conformance/structs/structDeclarations/structAndInterfaceWithSameName.ts(6,9): error TS2300: Duplicate identifier 'bar'. +tests/cases/conformance/structs/structDeclarations/structAndInterfaceWithSameName.ts(10,9): error TS2300: Duplicate identifier 'bar'. + + +==== tests/cases/conformance/structs/structDeclarations/structAndInterfaceWithSameName.ts (4 errors) ==== + struct C { foo: string; } + ~~~ +!!! error TS2300: Duplicate identifier 'foo'. + interface C { foo: string; } // error + ~~~ +!!! error TS2300: Duplicate identifier 'foo'. + + module M { + struct D { + bar: string; + ~~~ +!!! error TS2300: Duplicate identifier 'bar'. + } + + interface D { // error + bar: string; + ~~~ +!!! error TS2300: Duplicate identifier 'bar'. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/structAndInterfaceWithSameName.js b/tests/baselines/reference/structAndInterfaceWithSameName.js new file mode 100644 index 0000000000000..97da3c792969a --- /dev/null +++ b/tests/baselines/reference/structAndInterfaceWithSameName.js @@ -0,0 +1,46 @@ +//// [structAndInterfaceWithSameName.ts] +struct C { foo: string; } +interface C { foo: string; } // error + +module M { + struct D { + bar: string; + } + + interface D { // error + bar: string; + } +} + +//// [structAndInterfaceWithSameName.js] +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var M; +(function (M) { + var D = (function () { + var _D = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; + })(); +})(M || (M = {})); diff --git a/tests/baselines/reference/structAndVariableWithSameName.errors.txt b/tests/baselines/reference/structAndVariableWithSameName.errors.txt new file mode 100644 index 0000000000000..f454afcc3d7e9 --- /dev/null +++ b/tests/baselines/reference/structAndVariableWithSameName.errors.txt @@ -0,0 +1,25 @@ +tests/cases/conformance/structs/structDeclarations/structAndVariableWithSameName.ts(1,8): error TS2300: Duplicate identifier 'C'. +tests/cases/conformance/structs/structDeclarations/structAndVariableWithSameName.ts(2,5): error TS2300: Duplicate identifier 'C'. +tests/cases/conformance/structs/structDeclarations/structAndVariableWithSameName.ts(5,12): error TS2300: Duplicate identifier 'D'. +tests/cases/conformance/structs/structDeclarations/structAndVariableWithSameName.ts(9,9): error TS2300: Duplicate identifier 'D'. + + +==== tests/cases/conformance/structs/structDeclarations/structAndVariableWithSameName.ts (4 errors) ==== + struct C { foo: string; } // error + ~ +!!! error TS2300: Duplicate identifier 'C'. + var C = ''; // error + ~ +!!! error TS2300: Duplicate identifier 'C'. + + module M { + struct D { // error + ~ +!!! error TS2300: Duplicate identifier 'D'. + bar: string; + } + + var D = 1; // error + ~ +!!! error TS2300: Duplicate identifier 'D'. + } \ No newline at end of file diff --git a/tests/baselines/reference/structAndVariableWithSameName.js b/tests/baselines/reference/structAndVariableWithSameName.js new file mode 100644 index 0000000000000..463c61b1e11b2 --- /dev/null +++ b/tests/baselines/reference/structAndVariableWithSameName.js @@ -0,0 +1,48 @@ +//// [structAndVariableWithSameName.ts] +struct C { foo: string; } // error +var C = ''; // error + +module M { + struct D { // error + bar: string; + } + + var D = 1; // error +} + +//// [structAndVariableWithSameName.js] +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); + // error + // error +var C = ''; // error +var M; +(function (M) { + var D = (function () { + var _D = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; + })(); + var D = 1; // error +})(M || (M = {})); diff --git a/tests/cases/conformance/structs/structDeclarations/structAndClassWithSameName.ts b/tests/cases/conformance/structs/structDeclarations/structAndClassWithSameName.ts new file mode 100644 index 0000000000000..fe28891fce08c --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structAndClassWithSameName.ts @@ -0,0 +1,12 @@ +struct C { foo: string; } +class C { foo: string; } // error + +module M { + struct D { + bar: string; + } + + class D { // error + bar: string; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structDeclarations/structAndInterfaceWithSameName.ts b/tests/cases/conformance/structs/structDeclarations/structAndInterfaceWithSameName.ts new file mode 100644 index 0000000000000..43dc306e2a626 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structAndInterfaceWithSameName.ts @@ -0,0 +1,12 @@ +struct C { foo: string; } +interface C { foo: string; } // error + +module M { + struct D { + bar: string; + } + + interface D { // error + bar: string; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structDeclarations/structAndVariableWithSameName.ts b/tests/cases/conformance/structs/structDeclarations/structAndVariableWithSameName.ts new file mode 100644 index 0000000000000..21bdaa82266a0 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structAndVariableWithSameName.ts @@ -0,0 +1,10 @@ +struct C { foo: string; } // error +var C = ''; // error + +module M { + struct D { // error + bar: string; + } + + var D = 1; // error +} \ No newline at end of file From 8584cf05f55126a637045789862e5dcecfc2a871 Mon Sep 17 00:00:00 2001 From: jied Date: Thu, 5 Nov 2015 15:04:55 -0800 Subject: [PATCH 15/43] test - struct has members of JS objects --- ...uctAppearsToHaveMembersOfObject.errors.txt | 17 +++++++++++ .../structAppearsToHaveMembersOfObject.js | 30 +++++++++++++++++++ .../structAppearsToHaveMembersOfObject.ts | 7 +++++ 3 files changed, 54 insertions(+) create mode 100644 tests/baselines/reference/structAppearsToHaveMembersOfObject.errors.txt create mode 100644 tests/baselines/reference/structAppearsToHaveMembersOfObject.js create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structAppearsToHaveMembersOfObject.ts diff --git a/tests/baselines/reference/structAppearsToHaveMembersOfObject.errors.txt b/tests/baselines/reference/structAppearsToHaveMembersOfObject.errors.txt new file mode 100644 index 0000000000000..cd122858720eb --- /dev/null +++ b/tests/baselines/reference/structAppearsToHaveMembersOfObject.errors.txt @@ -0,0 +1,17 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structAppearsToHaveMembersOfObject.ts(6,5): error TS2322: Type 'C' is not assignable to type 'Object'. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structAppearsToHaveMembersOfObject.ts(7,5): error TS2322: Type 'C' is not assignable to type '{}'. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structAppearsToHaveMembersOfObject.ts (2 errors) ==== + struct C { foo: string; } + + var c: C; + var r = c.toString(); // call members of JS Object + var r2 = c.hasOwnProperty(''); + var o: Object = c; // error, not assignable + ~ +!!! error TS2322: Type 'C' is not assignable to type 'Object'. + var o2: {} = c; // error, not assignable + ~~ +!!! error TS2322: Type 'C' is not assignable to type '{}'. + \ No newline at end of file diff --git a/tests/baselines/reference/structAppearsToHaveMembersOfObject.js b/tests/baselines/reference/structAppearsToHaveMembersOfObject.js new file mode 100644 index 0000000000000..1499fbc65bf01 --- /dev/null +++ b/tests/baselines/reference/structAppearsToHaveMembersOfObject.js @@ -0,0 +1,30 @@ +//// [structAppearsToHaveMembersOfObject.ts] +struct C { foo: string; } + +var c: C; +var r = c.toString(); // call members of JS Object +var r2 = c.hasOwnProperty(''); +var o: Object = c; // error, not assignable +var o2: {} = c; // error, not assignable + + +//// [structAppearsToHaveMembersOfObject.js] +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var c; +var r = c.toString(); // call members of JS Object +var r2 = c.hasOwnProperty(''); +var o = c; // error, not assignable +var o2 = c; // error, not assignable diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structAppearsToHaveMembersOfObject.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structAppearsToHaveMembersOfObject.ts new file mode 100644 index 0000000000000..dce0c7561e78a --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structAppearsToHaveMembersOfObject.ts @@ -0,0 +1,7 @@ +struct C { foo: string; } + +var c: C; +var r = c.toString(); // call members of JS Object +var r2 = c.hasOwnProperty(''); +var o: Object = c; // error, not assignable +var o2: {} = c; // error, not assignable From 28975a5d767aa7fcba147eeb8ff0e53acbf115ce Mon Sep 17 00:00:00 2001 From: jied Date: Thu, 5 Nov 2015 15:08:34 -0800 Subject: [PATCH 16/43] tests - struct cannot extend primitives --- .../structExtendingPrimitive.errors.txt | 44 +++++ .../reference/structExtendingPrimitive.js | 158 ++++++++++++++++++ .../structExtendingPrimitive2.errors.txt | 10 ++ .../reference/structExtendingPrimitive2.js | 40 +++++ .../structExtendingPrimitive.ts | 14 ++ .../structExtendingPrimitive2.ts | 4 + 6 files changed, 270 insertions(+) create mode 100644 tests/baselines/reference/structExtendingPrimitive.errors.txt create mode 100644 tests/baselines/reference/structExtendingPrimitive.js create mode 100644 tests/baselines/reference/structExtendingPrimitive2.errors.txt create mode 100644 tests/baselines/reference/structExtendingPrimitive2.js create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive2.ts diff --git a/tests/baselines/reference/structExtendingPrimitive.errors.txt b/tests/baselines/reference/structExtendingPrimitive.errors.txt new file mode 100644 index 0000000000000..f0246f99e781a --- /dev/null +++ b/tests/baselines/reference/structExtendingPrimitive.errors.txt @@ -0,0 +1,44 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts(3,18): error TS2304: Cannot find name 'number'. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts(4,19): error TS2304: Cannot find name 'string'. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts(5,19): error TS2304: Cannot find name 'boolean'. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts(6,19): error TS2304: Cannot find name 'Void'. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts(7,20): error TS1109: Expression expected. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts(8,19): error TS2304: Cannot find name 'Null'. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts(10,19): error TS2507: Type 'undefined' is not a constructor function type. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts(11,19): error TS2304: Cannot find name 'Undefined'. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts(14,19): error TS2507: Type 'typeof E' is not a constructor function type. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts (9 errors) ==== + // structes cannot extend primitives + + struct C extends number { } + ~~~~~~ +!!! error TS2304: Cannot find name 'number'. + struct C2 extends string { } + ~~~~~~ +!!! error TS2304: Cannot find name 'string'. + struct C3 extends boolean { } + ~~~~~~~ +!!! error TS2304: Cannot find name 'boolean'. + struct C4 extends Void { } + ~~~~ +!!! error TS2304: Cannot find name 'Void'. + struct C4a extends void {} + ~~~~ +!!! error TS1109: Expression expected. + struct C5 extends Null { } + ~~~~ +!!! error TS2304: Cannot find name 'Null'. + struct C5a extends null { } + struct C6 extends undefined { } + ~~~~~~~~~ +!!! error TS2507: Type 'undefined' is not a constructor function type. + struct C7 extends Undefined { } + ~~~~~~~~~ +!!! error TS2304: Cannot find name 'Undefined'. + + enum E { A } + struct C8 extends E { } + ~ +!!! error TS2507: Type 'typeof E' is not a constructor function type. \ No newline at end of file diff --git a/tests/baselines/reference/structExtendingPrimitive.js b/tests/baselines/reference/structExtendingPrimitive.js new file mode 100644 index 0000000000000..ea6df1ae8edc1 --- /dev/null +++ b/tests/baselines/reference/structExtendingPrimitive.js @@ -0,0 +1,158 @@ +//// [structExtendingPrimitive.ts] +// structes cannot extend primitives + +struct C extends number { } +struct C2 extends string { } +struct C3 extends boolean { } +struct C4 extends Void { } +struct C4a extends void {} +struct C5 extends Null { } +struct C5a extends null { } +struct C6 extends undefined { } +struct C7 extends Undefined { } + +enum E { A } +struct C8 extends E { } + +//// [structExtendingPrimitive.js] +// structes cannot extend primitives +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var C2 = (function () { + var _C2 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C2() { + var obj = new _C2(); + _ctor.call(obj); + return obj; + } + C2._TO = _C2; + return C2; +})(); +var C3 = (function () { + var _C3 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C3() { + var obj = new _C3(); + _ctor.call(obj); + return obj; + } + C3._TO = _C3; + return C3; +})(); +var C4 = (function () { + var _C4 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C4() { + var obj = new _C4(); + _ctor.call(obj); + return obj; + } + C4._TO = _C4; + return C4; +})(); +var C4a = (function () { + var _C4a = new TypedObject.StructType({ + }); + function _ctor() { + } + function C4a() { + var obj = new _C4a(); + _ctor.call(obj); + return obj; + } + C4a._TO = _C4a; + return C4a; +})(); +void {}; +var C5 = (function () { + var _C5 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C5() { + var obj = new _C5(); + _ctor.call(obj); + return obj; + } + C5._TO = _C5; + return C5; +})(); +var C5a = (function () { + var _C5a = new TypedObject.StructType({ + }); + function _ctor() { + } + function C5a() { + var obj = new _C5a(); + _ctor.call(obj); + return obj; + } + C5a._TO = _C5a; + return C5a; +})(); +var C6 = (function () { + var _C6 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C6() { + var obj = new _C6(); + _ctor.call(obj); + return obj; + } + C6._TO = _C6; + return C6; +})(); +var C7 = (function () { + var _C7 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C7() { + var obj = new _C7(); + _ctor.call(obj); + return obj; + } + C7._TO = _C7; + return C7; +})(); +var E; +(function (E) { + E[E["A"] = 0] = "A"; +})(E || (E = {})); +var C8 = (function () { + var _C8 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C8() { + var obj = new _C8(); + _ctor.call(obj); + return obj; + } + C8._TO = _C8; + return C8; +})(); diff --git a/tests/baselines/reference/structExtendingPrimitive2.errors.txt b/tests/baselines/reference/structExtendingPrimitive2.errors.txt new file mode 100644 index 0000000000000..02c2e52ad872f --- /dev/null +++ b/tests/baselines/reference/structExtendingPrimitive2.errors.txt @@ -0,0 +1,10 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive2.ts(3,20): error TS1109: Expression expected. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive2.ts (1 errors) ==== + // structes cannot extend primitives + + struct C4a extends void {} + ~~~~ +!!! error TS1109: Expression expected. + struct C5a extends null { } \ No newline at end of file diff --git a/tests/baselines/reference/structExtendingPrimitive2.js b/tests/baselines/reference/structExtendingPrimitive2.js new file mode 100644 index 0000000000000..a7d506b426a3c --- /dev/null +++ b/tests/baselines/reference/structExtendingPrimitive2.js @@ -0,0 +1,40 @@ +//// [structExtendingPrimitive2.ts] +// structes cannot extend primitives + +struct C4a extends void {} +struct C5a extends null { } + +//// [structExtendingPrimitive2.js] +// structes cannot extend primitives +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C4a = (function () { + var _C4a = new TypedObject.StructType({ + }); + function _ctor() { + } + function C4a() { + var obj = new _C4a(); + _ctor.call(obj); + return obj; + } + C4a._TO = _C4a; + return C4a; +})(); +void {}; +var C5a = (function () { + var _C5a = new TypedObject.StructType({ + }); + function _ctor() { + } + function C5a() { + var obj = new _C5a(); + _ctor.call(obj); + return obj; + } + C5a._TO = _C5a; + return C5a; +})(); diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts new file mode 100644 index 0000000000000..2d1da9621a27c --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive.ts @@ -0,0 +1,14 @@ +// structes cannot extend primitives + +struct C extends number { } +struct C2 extends string { } +struct C3 extends boolean { } +struct C4 extends Void { } +struct C4a extends void {} +struct C5 extends Null { } +struct C5a extends null { } +struct C6 extends undefined { } +struct C7 extends Undefined { } + +enum E { A } +struct C8 extends E { } \ No newline at end of file diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive2.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive2.ts new file mode 100644 index 0000000000000..75155332c6a35 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingPrimitive2.ts @@ -0,0 +1,4 @@ +// structes cannot extend primitives + +struct C4a extends void {} +struct C5a extends null { } \ No newline at end of file From f63d7238058247c6a2e415f29f0844c97c72f7f6 Mon Sep 17 00:00:00 2001 From: jied Date: Thu, 5 Nov 2015 16:09:19 -0800 Subject: [PATCH 17/43] struct empty body - assgin to different types --- .../reference/structWithEmptyBody.errors.txt | 66 ++++++++++++++ .../reference/structWithEmptyBody.js | 88 +++++++++++++++++++ .../structBody/structWithEmptyBody.ts | 29 ++++++ 3 files changed, 183 insertions(+) create mode 100644 tests/baselines/reference/structWithEmptyBody.errors.txt create mode 100644 tests/baselines/reference/structWithEmptyBody.js create mode 100644 tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts diff --git a/tests/baselines/reference/structWithEmptyBody.errors.txt b/tests/baselines/reference/structWithEmptyBody.errors.txt new file mode 100644 index 0000000000000..8b71dd31e25ff --- /dev/null +++ b/tests/baselines/reference/structWithEmptyBody.errors.txt @@ -0,0 +1,66 @@ +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(5,5): error TS2322: Type 'C' is not assignable to type '{}'. +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(6,1): error TS2322: Type 'number' is not assignable to type 'C'. +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(7,1): error TS2322: Type '{ foo: string; }' is not assignable to type 'C'. +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(8,1): error TS2322: Type '() => void' is not assignable to type 'C'. +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(13,16): error TS20020: Cannot have return expression in struct constructor. +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(18,5): error TS2322: Type 'D' is not assignable to type '{}'. +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(19,1): error TS2322: Type 'number' is not assignable to type 'D'. +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(20,1): error TS2322: Type '{ foo: string; }' is not assignable to type 'D'. +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(21,1): error TS2322: Type '() => void' is not assignable to type 'D'. +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(22,1): error TS2322: Type 'C' is not assignable to type 'D'. +tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts(29,1): error TS2322: Type 'number' is not assignable to type 'E'. + + +==== tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts (11 errors) ==== + struct C { + } + + var c: C; + var o: {} = c; // error + ~ +!!! error TS2322: Type 'C' is not assignable to type '{}'. + c = 1; // error + ~ +!!! error TS2322: Type 'number' is not assignable to type 'C'. + c = { foo: '' }; // error + ~ +!!! error TS2322: Type '{ foo: string; }' is not assignable to type 'C'. + c = () => { }; // error + ~ +!!! error TS2322: Type '() => void' is not assignable to type 'C'. + c = new C(); // ok + + struct D { + constructor() { + return 1; // error + ~ +!!! error TS20020: Cannot have return expression in struct constructor. + } + } + + var d: D; + var o: {} = d; // error + ~ +!!! error TS2322: Type 'D' is not assignable to type '{}'. + d = 1; // error + ~ +!!! error TS2322: Type 'number' is not assignable to type 'D'. + d = { foo: '' }; // error + ~ +!!! error TS2322: Type '{ foo: string; }' is not assignable to type 'D'. + d = () => { }; // error + ~ +!!! error TS2322: Type '() => void' is not assignable to type 'D'. + d = c; // error, no inheritance + ~ +!!! error TS2322: Type 'C' is not assignable to type 'D'. + + struct E { + foo: number; + } + + var e: E; + e = 1; // error, Type 'number' is not assignable to type 'E' + ~ +!!! error TS2322: Type 'number' is not assignable to type 'E'. + \ No newline at end of file diff --git a/tests/baselines/reference/structWithEmptyBody.js b/tests/baselines/reference/structWithEmptyBody.js new file mode 100644 index 0000000000000..8d09d987be2a1 --- /dev/null +++ b/tests/baselines/reference/structWithEmptyBody.js @@ -0,0 +1,88 @@ +//// [structWithEmptyBody.ts] +struct C { +} + +var c: C; +var o: {} = c; // error +c = 1; // error +c = { foo: '' }; // error +c = () => { }; // error +c = new C(); // ok + +struct D { + constructor() { + return 1; // error + } +} + +var d: D; +var o: {} = d; // error +d = 1; // error +d = { foo: '' }; // error +d = () => { }; // error +d = c; // error, no inheritance + +struct E { + foo: number; +} + +var e: E; +e = 1; // error, Type 'number' is not assignable to type 'E' + + +//// [structWithEmptyBody.js] +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var c; +var o = c; // error +c = 1; // error +c = { foo: '' }; // error +c = function () { }; // error +c = new C(); // ok +var D = (function () { + var _D = new TypedObject.StructType({ + }); + function _ctor() { + return 1; // error + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); +var d; +var o = d; // error +d = 1; // error +d = { foo: '' }; // error +d = function () { }; // error +d = c; // error, no inheritance +var E = (function () { + var _E = new TypedObject.StructType({ + foo: TypedObject.float64 + }); + function _ctor() { + } + function E() { + var obj = new _E(); + _ctor.call(obj); + return obj; + } + E._TO = _E; + return E; +})(); +var e; +e = 1; // error, Type 'number' is not assignable to type 'E' diff --git a/tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts b/tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts new file mode 100644 index 0000000000000..33fc2e5298c34 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structBody/structWithEmptyBody.ts @@ -0,0 +1,29 @@ +struct C { +} + +var c: C; +var o: {} = c; // error +c = 1; // error +c = { foo: '' }; // error +c = () => { }; // error +c = new C(); // ok + +struct D { + constructor() { + return 1; // error + } +} + +var d: D; +var o: {} = d; // error +d = 1; // error +d = { foo: '' }; // error +d = () => { }; // error +d = c; // error, no inheritance + +struct E { + foo: number; +} + +var e: E; +e = 1; // error, Type 'number' is not assignable to type 'E' From d53902256f7cf38059642e2cba55d3ea6e79a6fc Mon Sep 17 00:00:00 2001 From: jied Date: Thu, 5 Nov 2015 16:11:48 -0800 Subject: [PATCH 18/43] struct extends structs --- .../reference/structExtendingStruct.js | 89 +++++++++++++++++++ .../reference/structExtendingStruct.symbols | 65 ++++++++++++++ .../reference/structExtendingStruct.types | 67 ++++++++++++++ .../structBody/structBodyWithStatements.ts | 13 +++ .../structExtendingStruct.ts | 31 +++++++ 5 files changed, 265 insertions(+) create mode 100644 tests/baselines/reference/structExtendingStruct.js create mode 100644 tests/baselines/reference/structExtendingStruct.symbols create mode 100644 tests/baselines/reference/structExtendingStruct.types create mode 100644 tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingStruct.ts diff --git a/tests/baselines/reference/structExtendingStruct.js b/tests/baselines/reference/structExtendingStruct.js new file mode 100644 index 0000000000000..5d927d9bc2bbb --- /dev/null +++ b/tests/baselines/reference/structExtendingStruct.js @@ -0,0 +1,89 @@ +//// [structExtendingStruct.ts] +struct C { + foo: string; + thing() { } + static other() { } +} + +struct D extends C { + bar: string; +} + +var d: D; +var r = d.foo; +var r2 = d.bar; +var r3 = d.thing(); +var r4 = D.other(); + +/* struct C2 { + foo: T; + thing(x: T) { } + static other(x: T) { } +} + +struct D2 extends C2 { + bar: string; +} + +var d2: D2; +var r5 = d2.foo; +var r6 = d2.bar; +var r7 = d2.thing(''); +var r8 = D2.other(1); */ + +//// [structExtendingStruct.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.thing = function () { }; + _C.other = function () { }; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); +var d; +var r = d.foo; +var r2 = d.bar; +var r3 = d.thing(); +var r4 = D.other(); +/* struct C2 { + foo: T; + thing(x: T) { } + static other(x: T) { } +} + +struct D2 extends C2 { + bar: string; +} + +var d2: D2; +var r5 = d2.foo; +var r6 = d2.bar; +var r7 = d2.thing(''); +var r8 = D2.other(1); */ diff --git a/tests/baselines/reference/structExtendingStruct.symbols b/tests/baselines/reference/structExtendingStruct.symbols new file mode 100644 index 0000000000000..e0d13baac2464 --- /dev/null +++ b/tests/baselines/reference/structExtendingStruct.symbols @@ -0,0 +1,65 @@ +=== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingStruct.ts === +struct C { +>C : Symbol(C, Decl(structExtendingStruct.ts, 0, 0)) + + foo: string; +>foo : Symbol(foo, Decl(structExtendingStruct.ts, 0, 10)) + + thing() { } +>thing : Symbol(thing, Decl(structExtendingStruct.ts, 1, 16)) + + static other() { } +>other : Symbol(C.other, Decl(structExtendingStruct.ts, 2, 15)) +} + +struct D extends C { +>D : Symbol(D, Decl(structExtendingStruct.ts, 4, 1)) +>C : Symbol(C, Decl(structExtendingStruct.ts, 0, 0)) + + bar: string; +>bar : Symbol(bar, Decl(structExtendingStruct.ts, 6, 20)) +} + +var d: D; +>d : Symbol(d, Decl(structExtendingStruct.ts, 10, 3)) +>D : Symbol(D, Decl(structExtendingStruct.ts, 4, 1)) + +var r = d.foo; +>r : Symbol(r, Decl(structExtendingStruct.ts, 11, 3)) +>d.foo : Symbol(C.foo, Decl(structExtendingStruct.ts, 0, 10)) +>d : Symbol(d, Decl(structExtendingStruct.ts, 10, 3)) +>foo : Symbol(C.foo, Decl(structExtendingStruct.ts, 0, 10)) + +var r2 = d.bar; +>r2 : Symbol(r2, Decl(structExtendingStruct.ts, 12, 3)) +>d.bar : Symbol(D.bar, Decl(structExtendingStruct.ts, 6, 20)) +>d : Symbol(d, Decl(structExtendingStruct.ts, 10, 3)) +>bar : Symbol(D.bar, Decl(structExtendingStruct.ts, 6, 20)) + +var r3 = d.thing(); +>r3 : Symbol(r3, Decl(structExtendingStruct.ts, 13, 3)) +>d.thing : Symbol(C.thing, Decl(structExtendingStruct.ts, 1, 16)) +>d : Symbol(d, Decl(structExtendingStruct.ts, 10, 3)) +>thing : Symbol(C.thing, Decl(structExtendingStruct.ts, 1, 16)) + +var r4 = D.other(); +>r4 : Symbol(r4, Decl(structExtendingStruct.ts, 14, 3)) +>D.other : Symbol(C.other, Decl(structExtendingStruct.ts, 2, 15)) +>D : Symbol(D, Decl(structExtendingStruct.ts, 4, 1)) +>other : Symbol(C.other, Decl(structExtendingStruct.ts, 2, 15)) + +/* struct C2 { + foo: T; + thing(x: T) { } + static other(x: T) { } +} + +struct D2 extends C2 { + bar: string; +} + +var d2: D2; +var r5 = d2.foo; +var r6 = d2.bar; +var r7 = d2.thing(''); +var r8 = D2.other(1); */ diff --git a/tests/baselines/reference/structExtendingStruct.types b/tests/baselines/reference/structExtendingStruct.types new file mode 100644 index 0000000000000..36b5ef5bc812c --- /dev/null +++ b/tests/baselines/reference/structExtendingStruct.types @@ -0,0 +1,67 @@ +=== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingStruct.ts === +struct C { +>C : C + + foo: string; +>foo : string + + thing() { } +>thing : () => void + + static other() { } +>other : () => void +} + +struct D extends C { +>D : D +>C : C + + bar: string; +>bar : string +} + +var d: D; +>d : D +>D : D + +var r = d.foo; +>r : string +>d.foo : string +>d : D +>foo : string + +var r2 = d.bar; +>r2 : string +>d.bar : string +>d : D +>bar : string + +var r3 = d.thing(); +>r3 : void +>d.thing() : void +>d.thing : () => void +>d : D +>thing : () => void + +var r4 = D.other(); +>r4 : void +>D.other() : void +>D.other : () => void +>D : typeof D +>other : () => void + +/* struct C2 { + foo: T; + thing(x: T) { } + static other(x: T) { } +} + +struct D2 extends C2 { + bar: string; +} + +var d2: D2; +var r5 = d2.foo; +var r6 = d2.bar; +var r7 = d2.thing(''); +var r8 = D2.other(1); */ diff --git a/tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts b/tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts new file mode 100644 index 0000000000000..83a95b37bdd4c --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts @@ -0,0 +1,13 @@ +struct C { + var x = 1; // error, variable statement +} + +struct C2 { + function foo() {} // error, function declaration +} + +var x = 1; +var y = 2; +struct C3 { + x: number = y + 1; // ok, not a statement (need var in the statement production). +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingStruct.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingStruct.ts new file mode 100644 index 0000000000000..8522cc1e0bdf7 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendingStruct.ts @@ -0,0 +1,31 @@ +struct C { + foo: string; + thing() { } + static other() { } +} + +struct D extends C { + bar: string; +} + +var d: D; +var r = d.foo; +var r2 = d.bar; +var r3 = d.thing(); +var r4 = D.other(); + +/* struct C2 { + foo: T; + thing(x: T) { } + static other(x: T) { } +} + +struct D2 extends C2 { + bar: string; +} + +var d2: D2; +var r5 = d2.foo; +var r6 = d2.bar; +var r7 = d2.thing(''); +var r8 = D2.other(1); */ \ No newline at end of file From 61073fc7531b9942ab2050b9cda659d75774e6de Mon Sep 17 00:00:00 2001 From: jied Date: Thu, 5 Nov 2015 20:00:39 -0800 Subject: [PATCH 19/43] tests - struct entends1 --- .../InterfaceNotExtendingStruct.errors.txt | 13 ++ .../reference/InterfaceNotExtendingStruct.js | 24 ++++ .../classNotExtendingStruct.errors.txt | 14 +++ .../reference/classNotExtendingStruct.js | 37 ++++++ .../structExtendsEveryObjectType.errors.txt | 43 +++++++ .../reference/structExtendsEveryObjectType.js | 119 ++++++++++++++++++ .../InterfaceNotExtendingStruct.ts | 6 + .../classNotExtendingStruct.ts | 7 ++ .../structExtendsEveryObjectType.ts | 16 +++ 9 files changed, 279 insertions(+) create mode 100644 tests/baselines/reference/InterfaceNotExtendingStruct.errors.txt create mode 100644 tests/baselines/reference/InterfaceNotExtendingStruct.js create mode 100644 tests/baselines/reference/classNotExtendingStruct.errors.txt create mode 100644 tests/baselines/reference/classNotExtendingStruct.js create mode 100644 tests/baselines/reference/structExtendsEveryObjectType.errors.txt create mode 100644 tests/baselines/reference/structExtendsEveryObjectType.js create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/InterfaceNotExtendingStruct.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/classNotExtendingStruct.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts diff --git a/tests/baselines/reference/InterfaceNotExtendingStruct.errors.txt b/tests/baselines/reference/InterfaceNotExtendingStruct.errors.txt new file mode 100644 index 0000000000000..152e536185ed0 --- /dev/null +++ b/tests/baselines/reference/InterfaceNotExtendingStruct.errors.txt @@ -0,0 +1,13 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/InterfaceNotExtendingStruct.ts(5,21): error TS2312: An interface may only extend a class or another interface. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/InterfaceNotExtendingStruct.ts (1 errors) ==== + struct S { + bar: string; + } + + interface I extends S { + ~ +!!! error TS2312: An interface may only extend a class or another interface. + } + \ No newline at end of file diff --git a/tests/baselines/reference/InterfaceNotExtendingStruct.js b/tests/baselines/reference/InterfaceNotExtendingStruct.js new file mode 100644 index 0000000000000..7f2a7d3f02bbf --- /dev/null +++ b/tests/baselines/reference/InterfaceNotExtendingStruct.js @@ -0,0 +1,24 @@ +//// [InterfaceNotExtendingStruct.ts] +struct S { + bar: string; +} + +interface I extends S { +} + + +//// [InterfaceNotExtendingStruct.js] +var S = (function () { + var _S = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function S() { + var obj = new _S(); + _ctor.call(obj); + return obj; + } + S._TO = _S; + return S; +})(); diff --git a/tests/baselines/reference/classNotExtendingStruct.errors.txt b/tests/baselines/reference/classNotExtendingStruct.errors.txt new file mode 100644 index 0000000000000..9963baea150f6 --- /dev/null +++ b/tests/baselines/reference/classNotExtendingStruct.errors.txt @@ -0,0 +1,14 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/classNotExtendingStruct.ts(5,17): error TS2509: Base constructor return type 'S' is not a class or interface type. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/classNotExtendingStruct.ts (1 errors) ==== + struct S { + bar: string; + } + + class C extends S { // error, class may only extend class + ~ +!!! error TS2509: Base constructor return type 'S' is not a class or interface type. + foo: string; + } + \ No newline at end of file diff --git a/tests/baselines/reference/classNotExtendingStruct.js b/tests/baselines/reference/classNotExtendingStruct.js new file mode 100644 index 0000000000000..45a397762bc8f --- /dev/null +++ b/tests/baselines/reference/classNotExtendingStruct.js @@ -0,0 +1,37 @@ +//// [classNotExtendingStruct.ts] +struct S { + bar: string; +} + +class C extends S { // error, class may only extend class + foo: string; +} + + +//// [classNotExtendingStruct.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var S = (function () { + var _S = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function S() { + var obj = new _S(); + _ctor.call(obj); + return obj; + } + S._TO = _S; + return S; +})(); +var C = (function (_super) { + __extends(C, _super); + function C() { + _super.apply(this, arguments); + } + return C; +})(S); diff --git a/tests/baselines/reference/structExtendsEveryObjectType.errors.txt b/tests/baselines/reference/structExtendsEveryObjectType.errors.txt new file mode 100644 index 0000000000000..6a43192ab9d46 --- /dev/null +++ b/tests/baselines/reference/structExtendsEveryObjectType.errors.txt @@ -0,0 +1,43 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts(4,18): error TS2304: Cannot find name 'I'. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts(6,19): error TS2507: Type '{ foo: any; }' is not a constructor function type. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts(6,26): error TS2304: Cannot find name 'string'. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts(6,32): error TS1005: ',' expected. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts(8,19): error TS2507: Type '{ foo: string; }' is not a constructor function type. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts(11,19): error TS2507: Type 'typeof M' is not a constructor function type. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts(14,19): error TS2507: Type '() => void' is not a constructor function type. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts(16,19): error TS2507: Type 'undefined[]' is not a constructor function type. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts (8 errors) ==== + interface I { + foo: string; + } + struct C extends I { } // error, cannot find I + ~ +!!! error TS2304: Cannot find name 'I'. + + struct C2 extends { foo: string; } { } // error + ~~~~~~~~~~~~~~~~ +!!! error TS2507: Type '{ foo: any; }' is not a constructor function type. + ~~~~~~ +!!! error TS2304: Cannot find name 'string'. + ~ +!!! error TS1005: ',' expected. + var x: { foo: string; } + struct C3 extends x { } // error + ~ +!!! error TS2507: Type '{ foo: string; }' is not a constructor function type. + + module M { export var x = 1; } + struct C4 extends M { } // error + ~ +!!! error TS2507: Type 'typeof M' is not a constructor function type. + + function foo() { } + struct C5 extends foo { } // error + ~~~ +!!! error TS2507: Type '() => void' is not a constructor function type. + + struct C6 extends []{ } // error + ~~ +!!! error TS2507: Type 'undefined[]' is not a constructor function type. \ No newline at end of file diff --git a/tests/baselines/reference/structExtendsEveryObjectType.js b/tests/baselines/reference/structExtendsEveryObjectType.js new file mode 100644 index 0000000000000..2e36b49311c42 --- /dev/null +++ b/tests/baselines/reference/structExtendsEveryObjectType.js @@ -0,0 +1,119 @@ +//// [structExtendsEveryObjectType.ts] +interface I { + foo: string; +} +struct C extends I { } // error, cannot find I + +struct C2 extends { foo: string; } { } // error +var x: { foo: string; } +struct C3 extends x { } // error + +module M { export var x = 1; } +struct C4 extends M { } // error + +function foo() { } +struct C5 extends foo { } // error + +struct C6 extends []{ } // error + +//// [structExtendsEveryObjectType.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); + // error, cannot find I + // error, cannot find I +var C2 = (function () { + var _C2 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C2() { + var obj = new _C2(); + _ctor.call(obj); + return obj; + } + C2._TO = _C2; + return C2; +})(); + // error + // error +var x; +var C3 = (function () { + var _C3 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C3() { + var obj = new _C3(); + _ctor.call(obj); + return obj; + } + C3._TO = _C3; + return C3; +})(); + // error + // error +var M; +(function (M) { + M.x = 1; +})(M || (M = {})); +var C4 = (function () { + var _C4 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C4() { + var obj = new _C4(); + _ctor.call(obj); + return obj; + } + C4._TO = _C4; + return C4; +})(); + // error + // error +function foo() { } +var C5 = (function () { + var _C5 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C5() { + var obj = new _C5(); + _ctor.call(obj); + return obj; + } + C5._TO = _C5; + return C5; +})(); + // error + // error +var C6 = (function () { + var _C6 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C6() { + var obj = new _C6(); + _ctor.call(obj); + return obj; + } + C6._TO = _C6; + return C6; +})(); + // error // error diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/InterfaceNotExtendingStruct.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/InterfaceNotExtendingStruct.ts new file mode 100644 index 0000000000000..379c567f92665 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/InterfaceNotExtendingStruct.ts @@ -0,0 +1,6 @@ +struct S { + bar: string; +} + +interface I extends S { +} diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/classNotExtendingStruct.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/classNotExtendingStruct.ts new file mode 100644 index 0000000000000..6b37a7fa3d1fa --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/classNotExtendingStruct.ts @@ -0,0 +1,7 @@ +struct S { + bar: string; +} + +class C extends S { // error, class may only extend class + foo: string; +} diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts new file mode 100644 index 0000000000000..94fcbaa8a26f7 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsEveryObjectType.ts @@ -0,0 +1,16 @@ +interface I { + foo: string; +} +struct C extends I { } // error, cannot find I + +struct C2 extends { foo: string; } { } // error +var x: { foo: string; } +struct C3 extends x { } // error + +module M { export var x = 1; } +struct C4 extends M { } // error + +function foo() { } +struct C5 extends foo { } // error + +struct C6 extends []{ } // error \ No newline at end of file From 423fd4478811aed5aa5f6acc5a23d90cec6471b1 Mon Sep 17 00:00:00 2001 From: jied Date: Thu, 5 Nov 2015 20:38:07 -0800 Subject: [PATCH 20/43] struct extends or implements --- .../structNotExtendingClass.errors.txt | 16 ++++++++ .../reference/structNotExtendingClass.js | 39 +++++++++++++++++++ .../structNotImplementingInterface.errors.txt | 14 +++++++ .../structNotImplementingInterface.js | 25 ++++++++++++ .../structNotExtendingClass.ts | 9 +++++ .../structNotImplementingInterface.ts | 7 ++++ 6 files changed, 110 insertions(+) create mode 100644 tests/baselines/reference/structNotExtendingClass.errors.txt create mode 100644 tests/baselines/reference/structNotExtendingClass.js create mode 100644 tests/baselines/reference/structNotImplementingInterface.errors.txt create mode 100644 tests/baselines/reference/structNotImplementingInterface.js create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotExtendingClass.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotImplementingInterface.ts diff --git a/tests/baselines/reference/structNotExtendingClass.errors.txt b/tests/baselines/reference/structNotExtendingClass.errors.txt new file mode 100644 index 0000000000000..dab6b92569e77 --- /dev/null +++ b/tests/baselines/reference/structNotExtendingClass.errors.txt @@ -0,0 +1,16 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotExtendingClass.ts(7,18): error TS20023: Base constructor return type 'C' is not a struct type. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotExtendingClass.ts (1 errors) ==== + class C { + foo: string; + thing() { } + static other() { } + } + + struct D extends C { // error, struct can only extend struct + ~ +!!! error TS20023: Base constructor return type 'C' is not a struct type. + bar: string; + } + \ No newline at end of file diff --git a/tests/baselines/reference/structNotExtendingClass.js b/tests/baselines/reference/structNotExtendingClass.js new file mode 100644 index 0000000000000..94ac5dc8e8a02 --- /dev/null +++ b/tests/baselines/reference/structNotExtendingClass.js @@ -0,0 +1,39 @@ +//// [structNotExtendingClass.ts] +class C { + foo: string; + thing() { } + static other() { } +} + +struct D extends C { // error, struct can only extend struct + bar: string; +} + + +//// [structNotExtendingClass.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + function C() { + } + C.prototype.thing = function () { }; + C.other = function () { }; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); diff --git a/tests/baselines/reference/structNotImplementingInterface.errors.txt b/tests/baselines/reference/structNotImplementingInterface.errors.txt new file mode 100644 index 0000000000000..d86f16b63592b --- /dev/null +++ b/tests/baselines/reference/structNotImplementingInterface.errors.txt @@ -0,0 +1,14 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotImplementingInterface.ts(5,9): error TS20024: A struct can not implement another class or interface. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotImplementingInterface.ts (1 errors) ==== + interface C { + foo: string; + } + + struct D implements C { // error. struct cannot implement interface + ~~~~~~~~~~~~~ +!!! error TS20024: A struct can not implement another class or interface. + foo: string; + } + \ No newline at end of file diff --git a/tests/baselines/reference/structNotImplementingInterface.js b/tests/baselines/reference/structNotImplementingInterface.js new file mode 100644 index 0000000000000..3a031726f4670 --- /dev/null +++ b/tests/baselines/reference/structNotImplementingInterface.js @@ -0,0 +1,25 @@ +//// [structNotImplementingInterface.ts] +interface C { + foo: string; +} + +struct D implements C { // error. struct cannot implement interface + foo: string; +} + + +//// [structNotImplementingInterface.js] +var D = (function () { + var _D = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotExtendingClass.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotExtendingClass.ts new file mode 100644 index 0000000000000..2e2e89b747a35 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotExtendingClass.ts @@ -0,0 +1,9 @@ +class C { + foo: string; + thing() { } + static other() { } +} + +struct D extends C { // error, struct can only extend struct + bar: string; +} diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotImplementingInterface.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotImplementingInterface.ts new file mode 100644 index 0000000000000..2929931349b74 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structNotImplementingInterface.ts @@ -0,0 +1,7 @@ +interface C { + foo: string; +} + +struct D implements C { // error. struct cannot implement interface + foo: string; +} From 5b4e768f40cf183df3f3fa599adba12221664e31 Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 12:44:06 -0800 Subject: [PATCH 21/43] struct extends shadowed constructor --- ...endsShadowedConstructorFunction.errors.txt | 14 ++++++ ...tructExtendsShadowedConstructorFunction.js | 48 +++++++++++++++++++ ...tructExtendsShadowedConstructorFunction.ts | 8 ++++ 3 files changed, 70 insertions(+) create mode 100644 tests/baselines/reference/structExtendsShadowedConstructorFunction.errors.txt create mode 100644 tests/baselines/reference/structExtendsShadowedConstructorFunction.js create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsShadowedConstructorFunction.ts diff --git a/tests/baselines/reference/structExtendsShadowedConstructorFunction.errors.txt b/tests/baselines/reference/structExtendsShadowedConstructorFunction.errors.txt new file mode 100644 index 0000000000000..f91bc5068d5a0 --- /dev/null +++ b/tests/baselines/reference/structExtendsShadowedConstructorFunction.errors.txt @@ -0,0 +1,14 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsShadowedConstructorFunction.ts(5,22): error TS2507: Type 'number' is not a constructor function type. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsShadowedConstructorFunction.ts (1 errors) ==== + struct C { foo: string; } + + module M { + var C = 1; + struct D extends C { // error, C must evaluate to constructor function + ~ +!!! error TS2507: Type 'number' is not a constructor function type. + bar: string; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/structExtendsShadowedConstructorFunction.js b/tests/baselines/reference/structExtendsShadowedConstructorFunction.js new file mode 100644 index 0000000000000..9dc8b85427a15 --- /dev/null +++ b/tests/baselines/reference/structExtendsShadowedConstructorFunction.js @@ -0,0 +1,48 @@ +//// [structExtendsShadowedConstructorFunction.ts] +struct C { foo: string; } + +module M { + var C = 1; + struct D extends C { // error, C must evaluate to constructor function + bar: string; + } +} + +//// [structExtendsShadowedConstructorFunction.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var M; +(function (M) { + var C = 1; + var D = (function () { + var _D = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; + })(); +})(M || (M = {})); diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsShadowedConstructorFunction.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsShadowedConstructorFunction.ts new file mode 100644 index 0000000000000..f0e9f3756778f --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsShadowedConstructorFunction.ts @@ -0,0 +1,8 @@ +struct C { foo: string; } + +module M { + var C = 1; + struct D extends C { // error, C must evaluate to constructor function + bar: string; + } +} \ No newline at end of file From 0682db05ccde4a281e588faa8df132361d653fe8 Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 12:44:39 -0800 Subject: [PATCH 22/43] add struct type --- src/compiler/utilities.ts | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 144463ef1bba2..3c08782f668f8 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -335,6 +335,8 @@ namespace ts { case SyntaxKind.BindingElement: case SyntaxKind.ClassDeclaration: case SyntaxKind.ClassExpression: + case SyntaxKind.StructDeclaration: + case SyntaxKind.StructExpression: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.ModuleDeclaration: case SyntaxKind.EnumDeclaration: @@ -573,6 +575,8 @@ namespace ts { case SyntaxKind.TypeAliasDeclaration: case SyntaxKind.ClassDeclaration: case SyntaxKind.ClassExpression: + case SyntaxKind.StructDeclaration: +case SyntaxKind.StructExpression: // These are not allowed inside a generator now, but eventually they may be allowed // as local types. Regardless, any yield statements contained within them should be // skipped in this traversal. @@ -708,7 +712,7 @@ return node && (node.kind === SyntaxKind.StructDeclaration || node.kind === Synt // then the computed property is not a 'this' container. // A computed property name in a class needs to be a this container // so that we can error on it. - if (isClassLike(node.parent.parent)) { + if (isClassLike(node.parent.parent) || isStructLike(node.parent.parent)) { return node; } // If this is a computed property, then the parent should not @@ -720,12 +724,12 @@ return node && (node.kind === SyntaxKind.StructDeclaration || node.kind === Synt break; case SyntaxKind.Decorator: // Decorators are always applied outside of the body of a class or method. - if (node.parent.kind === SyntaxKind.Parameter && isClassElement(node.parent.parent)) { + if (node.parent.kind === SyntaxKind.Parameter && (isClassElement(node.parent.parent) || isStructElement(node.parent.parent))) { // If the decorator's parent is a Parameter, we resolve the this container from // the grandparent class declaration. node = node.parent.parent; } - else if (isClassElement(node.parent)) { + else if (isClassElement(node.parent) || isStructElement(node.parent)) { // If the decorator's parent is a class element, we resolve the 'this' container // from the parent class declaration. node = node.parent; @@ -766,7 +770,7 @@ return node && (node.kind === SyntaxKind.StructDeclaration || node.kind === Synt // then the computed property is not a 'super' container. // A computed property name in a class needs to be a super container // so that we can error on it. - if (isClassLike(node.parent.parent)) { + if (isClassLike(node.parent.parent) || isStructLike(node.parent.parent)) { return node; } // If this is a computed property, then the parent should not @@ -778,12 +782,12 @@ return node && (node.kind === SyntaxKind.StructDeclaration || node.kind === Synt break; case SyntaxKind.Decorator: // Decorators are always applied outside of the body of a class or method. - if (node.parent.kind === SyntaxKind.Parameter && isClassElement(node.parent.parent)) { + if (node.parent.kind === SyntaxKind.Parameter && (isClassElement(node.parent.parent) || isStructElement(node.parent.parent))) { // If the decorator's parent is a Parameter, we resolve the this container from // the grandparent class declaration. node = node.parent.parent; } - else if (isClassElement(node.parent)) { + else if (isClassElement(node.parent) || isStructElement(node.parent.parent)) { // If the decorator's parent is a class element, we resolve the 'this' container // from the parent class declaration. node = node.parent; @@ -835,22 +839,23 @@ return node && (node.kind === SyntaxKind.StructDeclaration || node.kind === Synt export function nodeCanBeDecorated(node: Node): boolean { switch (node.kind) { case SyntaxKind.ClassDeclaration: +case SyntaxKind.StructDeclaration: // 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 node.parent.kind === SyntaxKind.ClassDeclaration || node.parent.kind === SyntaxKind.StructDeclaration; case SyntaxKind.Parameter: // if the parameter's parent has a body and its grandparent is a class declaration, this is a valid target; - return (node.parent).body && node.parent.parent.kind === SyntaxKind.ClassDeclaration; + return (node.parent).body && (node.parent.parent.kind === SyntaxKind.ClassDeclaration || node.parent.parent.kind === SyntaxKind.StructDeclaration); 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 (node).body && node.parent.kind === SyntaxKind.ClassDeclaration; + return (node).body && (node.parent.kind === SyntaxKind.ClassDeclaration || node.parent.kind === SyntaxKind.StructDeclaration); } return false; @@ -859,6 +864,7 @@ return node && (node.kind === SyntaxKind.StructDeclaration || node.kind === Synt export function nodeIsDecorated(node: Node): boolean { switch (node.kind) { case SyntaxKind.ClassDeclaration: +case SyntaxKind.StructDeclaration: if (node.decorators) { return true; } @@ -896,7 +902,8 @@ return node && (node.kind === SyntaxKind.StructDeclaration || node.kind === Synt switch (node.kind) { case SyntaxKind.ClassDeclaration: return forEach((node).members, nodeOrChildIsDecorated); - +case SyntaxKind.StructDeclaration: +return forEach((node).members, nodeOrChildIsDecorated); case SyntaxKind.MethodDeclaration: case SyntaxKind.SetAccessor: return forEach((node).parameters, nodeIsDecorated); @@ -936,6 +943,7 @@ return node && (node.kind === SyntaxKind.StructDeclaration || node.kind === Synt case SyntaxKind.ParenthesizedExpression: case SyntaxKind.FunctionExpression: case SyntaxKind.ClassExpression: +case SyntaxKind.StructExpression: case SyntaxKind.ArrowFunction: case SyntaxKind.VoidExpression: case SyntaxKind.DeleteExpression: @@ -1181,6 +1189,8 @@ return node && (node.kind === SyntaxKind.StructDeclaration || node.kind === Synt case SyntaxKind.BindingElement: case SyntaxKind.ClassDeclaration: case SyntaxKind.ClassExpression: +case SyntaxKind.StructDeclaration: +case SyntaxKind.StructExpression: case SyntaxKind.Constructor: case SyntaxKind.EnumDeclaration: case SyntaxKind.EnumMember: @@ -1815,7 +1825,7 @@ export function isStructElement(n: Node): boolean { return getLineAndCharacterOfPosition(currentSourceFile, pos).line; } - export function getFirstConstructorWithBody(node: ClassLikeDeclaration): ConstructorDeclaration { + export function getFirstConstructorWithBody(node: ClassLikeDeclaration | StructLikeDeclaration): ConstructorDeclaration { return forEach(node.members, member => { if (member.kind === SyntaxKind.Constructor && nodeIsPresent((member).body)) { return member; @@ -2046,6 +2056,7 @@ export function isStructElement(n: Node): boolean { case SyntaxKind.ParenthesizedExpression: case SyntaxKind.ObjectLiteralExpression: case SyntaxKind.ClassExpression: +case SyntaxKind.StructExpression: case SyntaxKind.FunctionExpression: case SyntaxKind.Identifier: case SyntaxKind.RegularExpressionLiteral: @@ -2438,7 +2449,7 @@ namespace ts { export function getTypeParameterOwner(d: Declaration): Declaration { if (d && d.kind === SyntaxKind.TypeParameter) { for (let current: Node = d; current; current = current.parent) { - if (isFunctionLike(current) || isClassLike(current) || current.kind === SyntaxKind.InterfaceDeclaration) { + if (isFunctionLike(current) || isClassLike(current) || isStructLike(current) || current.kind === SyntaxKind.InterfaceDeclaration) { return current; } } From 74696d50a3c558fe86c8e66587f99511a4abbdea Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 12:45:11 -0800 Subject: [PATCH 23/43] add struct --- src/compiler/binder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index c3ac2f807601f..86484fbc8a5ec 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1026,7 +1026,7 @@ if (getContainingStruct(node)) { // // Note: we check for this here because this struct may be merging into a module. The // module might have an exported variable called 'prototype'. We can't allow that as - // that would clash with the built-in 'prototype' for the class. + // that would clash with the built-in 'prototype' for the struct. let prototypeSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Prototype, "prototype"); if (hasProperty(symbol.exports, prototypeSymbol.name)) { if (node.name) { From a65e4f7f364120347c9ddf9ebcfb6e2b3708a6e3 Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 12:45:41 -0800 Subject: [PATCH 24/43] struct cannot implement --- src/compiler/parser.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 307d43e4e593c..a0bdfce4a4ce8 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5183,6 +5183,13 @@ namespace ts { node.name = parseNameOfStructDeclarationOrExpression(); node.typeParameters = parseTypeParameters(); node.heritageClauses = parseHeritageClauses(/*isStructHeritageClause*/ true); + if (node.heritageClauses) { + for (let hc of node.heritageClauses) { + if (hc.token === SyntaxKind.ImplementsKeyword) { +parseErrorAtPosition(hc.pos, hc.end - hc.pos, Diagnostics.A_struct_can_not_implement_a_class_or_interface); + } + } + } if (parseExpected(SyntaxKind.OpenBraceToken)) { // StructTail[Yield,Await] : (Modified) See 14.5 From 864496341d32931e19ff0e71060d79a7bafe42cc Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 12:46:42 -0800 Subject: [PATCH 25/43] partial lint --- src/compiler/checker.ts | 363 +++++++++++++++++++++++----------------- 1 file changed, 207 insertions(+), 156 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6b661976f1061..caa0da9d8f0e1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2405,6 +2405,14 @@ namespace ts { return type && (type.flags & TypeFlags.Any) !== 0; } + function isTypeStruct(type: Type) { + return type && (type.flags & TypeFlags.Struct) !== 0; + } + + function isTypeReferenceStruct(type: Type) { + return type && (type.flags & TypeFlags.Reference) && (type.symbol.flags & SymbolFlags.Struct) !== 0; + } + // Return the type of a binding element parent. We check SymbolLinks first to see if a type has been // assigned by contextual typing. function getTypeForBindingElementParent(node: VariableLikeDeclaration) { @@ -3003,9 +3011,13 @@ namespace ts { if (baseType === unknownType) { return; } - if (!(getTargetType(baseType).flags & (TypeFlags.Class | TypeFlags.Struct | TypeFlags.Interface))) { + if (!(getTargetType(baseType).flags & (TypeFlags.Class | TypeFlags.Interface)) || (isInClassDecl && (getTargetType(baseType).flags & TypeFlags.Struct))) { error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); return; + } + else if (!isInClassDecl && !(getTargetType(baseType).flags & TypeFlags.Struct)) { + error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_struct_type, typeToString(baseType)); + return; } if (type === baseType || hasBaseType(baseType, type)) { error(type.symbol.valueDeclaration, Diagnostics.Type_0_recursively_references_itself_as_a_base_type, @@ -3067,10 +3079,10 @@ namespace ts { function getDeclaredTypeOfClassOrStructOrInterface(symbol: Symbol): InterfaceType { let links = getSymbolLinks(symbol); if (!links.declaredType) { - let kind: TypeFlags; - if (symbol.flags & SymbolFlags.Class) kind = TypeFlags.Class; - else if (symbol.flags & SymbolFlags.Struct) kind = TypeFlags.Struct; - else kind = TypeFlags.Interface; + let kind: TypeFlags; + if (symbol.flags & SymbolFlags.Class) kind = TypeFlags.Class; + else if (symbol.flags & SymbolFlags.Struct) kind = TypeFlags.Struct; + else kind = TypeFlags.Interface; let type = links.declaredType = createObjectType(kind, symbol); let outerTypeParameters = getOuterTypeParametersOfClassOrStructOrInterface(symbol); let localTypeParameters = getLocalTypeParametersOfClassOrStructOrInterfaceOrTypeAlias(symbol); @@ -4896,7 +4908,21 @@ parentSymbol = (declaration.parent).symbol; return checkTypeRelatedTo(sourceType, targetType, assignableRelation, /*errorNode*/ undefined); } - /** + function isBothStructType(source: Type, target: Type): boolean { + return isTypeStruct(source) && isTypeStruct(target); + } + + function isOneTypeStructType(source: Type, target: Type): boolean { + // return (isTypeStruct(source) && !isTypeStruct(target)) || (isTypeStruct(target) && !isTypeStruct(source)); + return (isTypeStruct(source) && !isTypeStruct(target)) || + (isTypeStruct(target) && !isTypeStruct(source)); + } + + function write(info: string): void { + console.log(info); + } + + /** * Checks if 'source' is related to 'target' (e.g.: is a assignable to). * @param source The left-hand-side of the relation. * @param target The right-hand-side of the relation. @@ -4973,13 +4999,25 @@ parentSymbol = (declaration.parent).symbol; return isIdenticalTo(source, target); } - if (isTypeAny(target)) return Ternary.True; + if (isTypeAny(target) && !isTypeReferenceStruct(source)) { + return Ternary.True; + } if (source === undefinedType) return Ternary.True; if (source === nullType && target !== undefinedType) return Ternary.True; if (source.flags & TypeFlags.Enum && target === numberType) return Ternary.True; if (source.flags & TypeFlags.StringLiteral && target === stringType) return Ternary.True; + + // assignability doesn't hold between struct and non-struct types + if (isOneTypeStructType(source, target)) { + reportRelationError(headMessage, source, target); + return Ternary.False; + } + if (relation === assignableRelation) { - if (isTypeAny(source)) return Ternary.True; + // if (isTypeAny(source) && !(target.flags & TypeFlags.Struct)) return Ternary.True; + if (isTypeAny(source) && !isTypeReferenceStruct(target)) { + return Ternary.True; + } if (source === numberType && target.flags & TypeFlags.Enum) return Ternary.True; } @@ -5082,10 +5120,25 @@ parentSymbol = (declaration.parent).symbol; if (apparentType.flags & (TypeFlags.ObjectType | TypeFlags.Intersection) && target.flags & TypeFlags.ObjectType) { // Report structural errors only if we haven't reported any errors yet let reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo; - if (result = objectTypeRelatedTo(apparentType, target, reportStructuralErrors)) { - errorInfo = saveErrorInfo; - return result; - } + if (isBothStructType(source, target)) { + // for struct assignability, check its inheritance chain + let baseTypes = (apparentType).resolvedBaseTypes; + while (baseTypes && baseTypes.length > 0) { + if (baseTypes.indexOf(target) > -1) { // target is on source's inheritance chain + errorInfo = saveErrorInfo; + return Ternary.True; + } + else { +baseTypes = (baseTypes[0]).resolvedBaseTypes; // expand the inheritance chain + } + } + } + else { // structural comparison + if (result = objectTypeRelatedTo(apparentType, target, reportStructuralErrors)) { + errorInfo = saveErrorInfo; + return result; + } + } } } @@ -6851,10 +6904,10 @@ parentSymbol = (declaration.parent).symbol; let isCallExpression = node.parent.kind === SyntaxKind.CallExpression && (node.parent).expression === node; let isInClassDecl = true; let classOrStructDeclaration = getContainingClass(node); - if (!classOrStructDeclaration) { - isInClassDecl = false; - classOrStructDeclaration = getContainingStruct(node); - } + if (!classOrStructDeclaration) { + isInClassDecl = false; + classOrStructDeclaration = getContainingStruct(node); + } let classOrStructType = classOrStructDeclaration && getDeclaredTypeOfSymbol(getSymbolOfNode(classOrStructDeclaration)); let baseClassOrStructType = classOrStructType && getBaseTypes(classOrStructType)[0]; @@ -11499,7 +11552,7 @@ parentSymbol = (declaration.parent).symbol; return forEach((n).members, containsSuperCallAsComputedPropertyName); } else if (isStructLike(n)) { - return forEach((n).members, containsSuperCallAsComputedPropertyName); +return forEach((n).members, containsSuperCallAsComputedPropertyName); } return forEachChild(n, containsSuperCall); } @@ -11522,105 +11575,105 @@ parentSymbol = (declaration.parent).symbol; // TS 1.0 spec (April 2014): 8.3.2 // Constructors of classes with no extends clause may not contain super calls, whereas // constructors of derived classes must contain at least one super call somewhere in their function body. - if (node.parent.kind === SyntaxKind.ClassDeclaration) { - let containingClassDecl = node.parent; - if (getClassOrStructExtendsHeritageClauseElement(containingClassDecl)) { - let containingClassSymbol = getSymbolOfNode(containingClassDecl); - let containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); - let baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); - - if (containsSuperCall(node.body)) { - if (baseConstructorType === nullType) { - error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); - } + if (node.parent.kind === SyntaxKind.ClassDeclaration) { + let containingClassDecl = node.parent; + if (getClassOrStructExtendsHeritageClauseElement(containingClassDecl)) { + let containingClassSymbol = getSymbolOfNode(containingClassDecl); + let containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); + let baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); + + if (containsSuperCall(node.body)) { + if (baseConstructorType === nullType) { + error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); + } - // The first statement in the body of a constructor (excluding prologue directives) must be a super call - // if both of the following are true: - // - The containing class is a derived class. - // - The constructor declares parameter properties - // or the containing class declares instance member variables with initializers. - let superCallShouldBeFirst = - forEach((node.parent).members, isInstancePropertyWithInitializer) || - forEach(node.parameters, p => p.flags & (NodeFlags.Public | NodeFlags.Private | NodeFlags.Protected)); - - // Skip past any prologue directives to find the first statement - // to ensure that it was a super call. - if (superCallShouldBeFirst) { - let statements = (node.body).statements; - let superCallStatement: ExpressionStatement; - for (let statement of statements) { - if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { - superCallStatement = statement; - break; - } - if (!isPrologueDirective(statement)) { - break; - } - } - if (!superCallStatement) { - error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); + // The first statement in the body of a constructor (excluding prologue directives) must be a super call + // if both of the following are true: + // - The containing class is a derived class. + // - The constructor declares parameter properties + // or the containing class declares instance member variables with initializers. + let superCallShouldBeFirst = + forEach((node.parent).members, isInstancePropertyWithInitializer) || + forEach(node.parameters, p => p.flags & (NodeFlags.Public | NodeFlags.Private | NodeFlags.Protected)); + + // Skip past any prologue directives to find the first statement + // to ensure that it was a super call. + if (superCallShouldBeFirst) { + let statements = (node.body).statements; + let superCallStatement: ExpressionStatement; + for (let statement of statements) { + if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { + superCallStatement = statement; + break; } - else { - // In such a required super call, it is a compile-time error for argument expressions to reference this. - markThisReferencesAsErrors(superCallStatement.expression); + if (!isPrologueDirective(statement)) { + break; } } - } - else if (baseConstructorType !== nullType) { - error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); - } - } - } - else if (node.parent.kind === SyntaxKind.StructDeclaration) { - let containingStructDecl = node.parent; - if (getClassOrStructExtendsHeritageClauseElement(containingStructDecl)) { - let containingStructSymbol = getSymbolOfNode(containingStructDecl); - let containingStructInstanceType = getDeclaredTypeOfSymbol(containingStructSymbol); - let baseConstructorType = getBaseConstructorTypeOfStruct(containingStructInstanceType); - - if (containsSuperCall(node.body)) { - if (baseConstructorType === nullType) { - error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_struct_extends_null); + if (!superCallStatement) { + error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); } - - // The first statement in the body of a constructor (excluding prologue directives) must be a super call - // if both of the following are true: - // - The containing struct is a derived struct. - // - The constructor declares parameter properties - // or the containing struct declares instance member variables with initializers. - let superCallShouldBeFirst = - forEach((node.parent).members, isInstancePropertyWithInitializer) || - forEach(node.parameters, p => p.flags & (NodeFlags.Public | NodeFlags.Private)); - - // Skip past any prologue directives to find the first statement - // to ensure that it was a super call. - if (superCallShouldBeFirst) { - let statements = (node.body).statements; - let superCallStatement: ExpressionStatement; - for (let statement of statements) { - if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { - superCallStatement = statement; - break; - } - if (!isPrologueDirective(statement)) { - break; - } - } - if (!superCallStatement) { - error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_struct_contains_initialized_properties_or_has_parameter_properties); - } - else { - // In such a required super call, it is a compile-time error for argument expressions to reference this. - markThisReferencesAsErrors(superCallStatement.expression); - } + else { + // In such a required super call, it is a compile-time error for argument expressions to reference this. + markThisReferencesAsErrors(superCallStatement.expression); } } - else if (baseConstructorType !== nullType) { - error(node, Diagnostics.Constructors_for_derived_structs_must_contain_a_super_call); - } + } + else if (baseConstructorType !== nullType) { + error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); } } } + else if (node.parent.kind === SyntaxKind.StructDeclaration) { + let containingStructDecl = node.parent; + if (getClassOrStructExtendsHeritageClauseElement(containingStructDecl)) { + let containingStructSymbol = getSymbolOfNode(containingStructDecl); + let containingStructInstanceType = getDeclaredTypeOfSymbol(containingStructSymbol); + let baseConstructorType = getBaseConstructorTypeOfStruct(containingStructInstanceType); + + if (containsSuperCall(node.body)) { + if (baseConstructorType === nullType) { +error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_struct_extends_null); + } + + // The first statement in the body of a constructor (excluding prologue directives) must be a super call + // if both of the following are true: + // - The containing struct is a derived struct. + // - The constructor declares parameter properties + // or the containing struct declares instance member variables with initializers. + let superCallShouldBeFirst = + forEach((node.parent).members, isInstancePropertyWithInitializer) || + forEach(node.parameters, p => p.flags & (NodeFlags.Public | NodeFlags.Private)); + + // Skip past any prologue directives to find the first statement + // to ensure that it was a super call. + if (superCallShouldBeFirst) { + let statements = (node.body).statements; + let superCallStatement: ExpressionStatement; + for (let statement of statements) { + if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { +superCallStatement = statement; +break; + } + if (!isPrologueDirective(statement)) { +break; + } + } + if (!superCallStatement) { +error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_struct_contains_initialized_properties_or_has_parameter_properties); + } + else { + // In such a required super call, it is a compile-time error for argument expressions to reference this. +markThisReferencesAsErrors(superCallStatement.expression); + } + } + } + else if (baseConstructorType !== nullType) { +error(node, Diagnostics.Constructors_for_derived_structs_must_contain_a_super_call); + } + } + } + } function checkAccessorDeclaration(node: AccessorDeclaration) { if (produceDiagnostics) { @@ -12702,35 +12755,35 @@ parentSymbol = (declaration.parent).symbol; // bubble up and find containing type let enclosingClass = getContainingClass(node); - let enclosingStruct = getContainingStruct(node); + let enclosingStruct = getContainingStruct(node); // if containing type was not found or it is ambient - exit (no codegen) if (!enclosingClass && !enclosingStruct || isInAmbientContext(enclosingClass) || isInAmbientContext(enclosingStruct)) { return; } - if (enclosingClass) { - if (getClassOrStructExtendsHeritageClauseElement(enclosingClass)) { - let isDeclaration = node.kind !== SyntaxKind.Identifier; - if (isDeclaration) { - error(node, Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); - } - else { - error(node, Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference); - } - } - } - else if (enclosingStruct) { - if (getClassOrStructExtendsHeritageClauseElement(enclosingStruct)) { - let isDeclaration = node.kind !== SyntaxKind.Identifier; - if (isDeclaration) { - error(node, Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_struct_reference); - } - else { - error(node, Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_struct_reference); - } - } - } + if (enclosingClass) { + if (getClassOrStructExtendsHeritageClauseElement(enclosingClass)) { + let isDeclaration = node.kind !== SyntaxKind.Identifier; + if (isDeclaration) { +error(node, Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); + } + else { +error(node, Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference); + } + } + } + else if (enclosingStruct) { + if (getClassOrStructExtendsHeritageClauseElement(enclosingStruct)) { + let isDeclaration = node.kind !== SyntaxKind.Identifier; + if (isDeclaration) { +error(node, Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_struct_reference); + } + else { +error(node, Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_struct_reference); + } + } + } } @@ -13391,7 +13444,7 @@ parentSymbol = (declaration.parent).symbol; } else if (func.kind === SyntaxKind.Constructor) { if (returnType.flags & TypeFlags.Struct) { // struct constructor cannot have return expression - error(node.expression, Diagnostics.Struct_constructor_cannot_have_return_expression); +error(node.expression, Diagnostics.Struct_constructor_cannot_have_return_expression); } else if (!isTypeAssignableTo(exprType, returnType)) { error(node.expression, Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); @@ -14388,14 +14441,12 @@ parentSymbol = (declaration.parent).symbol; inSameLexicalScope(node, mergedClass)) { getNodeLinks(node).flags |= NodeCheckFlags.LexicalModuleMergesWithClass; } - if (!mergedClass) { - let mergedStruct = getDeclarationOfKind(symbol, SyntaxKind.StructDeclaration); - if (mergedStruct && - inSameLexicalScope(node, mergedStruct)) { - getNodeLinks(node).flags |= NodeCheckFlags.LexicalModuleMergesWithStruct; - } - - } + if (!mergedClass) { + let mergedStruct = getDeclarationOfKind(symbol, SyntaxKind.StructDeclaration); + if (mergedStruct && inSameLexicalScope(node, mergedStruct)) { +getNodeLinks(node).flags |= NodeCheckFlags.LexicalModuleMergesWithStruct; + } + } } // Checks for ambient external modules. @@ -15847,7 +15898,7 @@ parentSymbol = (declaration.parent).symbol; jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element); getGlobalClassDecoratorType = memoize(() => getGlobalType("ClassDecorator")); - getGlobalClassDecoratorType = memoize(() => getGlobalType("StructDecorator")); +getGlobalClassDecoratorType = memoize(() => getGlobalType("StructDecorator")); getGlobalPropertyDecoratorType = memoize(() => getGlobalType("PropertyDecorator")); getGlobalMethodDecoratorType = memoize(() => getGlobalType("MethodDecorator")); getGlobalParameterDecoratorType = memoize(() => getGlobalType("ParameterDecorator")); @@ -16051,7 +16102,7 @@ parentSymbol = (declaration.parent).symbol; return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_class_element, "export"); } else if (node.parent.kind === SyntaxKind.StructDeclaration) { - return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_struct_element, "export"); +return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_struct_element, "export"); } else if (node.kind === SyntaxKind.Parameter) { return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_parameter, "export"); @@ -16070,7 +16121,7 @@ parentSymbol = (declaration.parent).symbol; return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_class_element, "declare"); } else if (node.parent.kind === SyntaxKind.StructDeclaration) { - return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_struct_element, "declare"); +return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_struct_element, "declare"); } else if (node.kind === SyntaxKind.Parameter) { return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_parameter, "declare"); @@ -16679,20 +16730,20 @@ parentSymbol = (declaration.parent).symbol; } } else if (isStructLike(node.parent)) { - if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.A_struct_member_cannot_be_declared_optional)) { - return true; - } + if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.A_struct_member_cannot_be_declared_optional)) { +return true; + } // Technically, computed properties in ambient contexts is disallowed // for property declarations and accessors too, not just methods. // However, property declarations disallow computed names in general, // and accessors are not allowed in ambient contexts in general, // so this error only really matters for methods. - if (isInAmbientContext(node)) { - return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol); - } - else if (!node.body) { - return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_method_overload_must_directly_refer_to_a_built_in_symbol); - } + if (isInAmbientContext(node)) { +return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol); + } + else if (!node.body) { +return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_method_overload_must_directly_refer_to_a_built_in_symbol); + } } else if (node.parent.kind === SyntaxKind.InterfaceDeclaration) { return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol); @@ -16947,12 +16998,12 @@ parentSymbol = (declaration.parent).symbol; return true; } } - else if (isStructLike(node.parent)) { - if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.A_struct_member_cannot_be_declared_optional) || - checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_struct_property_declaration_must_directly_refer_to_a_built_in_symbol)) { - return true; - } - } + else if (isStructLike(node.parent)) { + if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.A_struct_member_cannot_be_declared_optional) || + checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_struct_property_declaration_must_directly_refer_to_a_built_in_symbol)) { + return true; + } + } else if (node.parent.kind === SyntaxKind.InterfaceDeclaration) { if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol)) { return true; From a365b11b5fb7fc10d25bb1e172b8ef5a6c22851f Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 12:47:27 -0800 Subject: [PATCH 26/43] TypeFlags.Struct - needs to assign a unique value --- src/compiler/types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index af18bbea1af23..a89a77f159eba 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1828,7 +1828,7 @@ namespace ts { StringLiteral = 0x00000100, // String literal type TypeParameter = 0x00000200, // Type parameter Class = 0x00000400, // Class - Struct = 0x100000000, // Struct + Struct = 0x80000000, // Struct Interface = 0x00000800, // Interface Reference = 0x00001000, // Generic type reference Tuple = 0x00002000, // Tuple @@ -1856,7 +1856,7 @@ namespace ts { U16 = 0x10000000, I32 = 0x20000000, U32 = 0x40000000, - Float = 0x80000000, + Float = 0x40000000, /* @internal */ Intrinsic = Any | String | Number | Boolean | I8 | U8 | I16 | U16 | I32 | U32 | Float | ESSymbol | Void | Undefined | Null, From 6ee8c46d7df6619416dfeee62e717a20093f65ad Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 15:15:53 -0800 Subject: [PATCH 27/43] struct extends structs --- .../reference/structExtendsItself.errors.txt | 11 +++ .../reference/structExtendsItself.js | 30 ++++++ .../structExtendsItselfIndirectly.errors.txt | 26 ++++++ .../structExtendsItselfIndirectly.js | 68 ++++++++++++++ .../structExtendsItselfIndirectly2.errors.txt | 37 ++++++++ .../structExtendsItselfIndirectly2.js | 91 +++++++++++++++++++ ...ExtendsValidConstructorFunction.errors.txt | 11 +++ .../structExtendsValidConstructorFunction.js | 29 ++++++ ...tructInstanceTypeIsAssignableToBaseType.js | 69 ++++++++++++++ ...InstanceTypeIsAssignableToBaseType.symbols | 42 +++++++++ ...ctInstanceTypeIsAssignableToBaseType.types | 45 +++++++++ .../structExtendsItself.ts | 5 + .../structExtendsItselfIndirectly.ts | 11 +++ .../structExtendsItselfIndirectly2.ts | 22 +++++ .../structExtendsValidConstructorFunction.ts | 5 + ...tructInstanceTypeIsAssignableToBaseType.ts | 21 +++++ 16 files changed, 523 insertions(+) create mode 100644 tests/baselines/reference/structExtendsItself.errors.txt create mode 100644 tests/baselines/reference/structExtendsItself.js create mode 100644 tests/baselines/reference/structExtendsItselfIndirectly.errors.txt create mode 100644 tests/baselines/reference/structExtendsItselfIndirectly.js create mode 100644 tests/baselines/reference/structExtendsItselfIndirectly2.errors.txt create mode 100644 tests/baselines/reference/structExtendsItselfIndirectly2.js create mode 100644 tests/baselines/reference/structExtendsValidConstructorFunction.errors.txt create mode 100644 tests/baselines/reference/structExtendsValidConstructorFunction.js create mode 100644 tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.js create mode 100644 tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.symbols create mode 100644 tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.types create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItself.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly2.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsValidConstructorFunction.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structInstanceTypeIsAssignableToBaseType.ts diff --git a/tests/baselines/reference/structExtendsItself.errors.txt b/tests/baselines/reference/structExtendsItself.errors.txt new file mode 100644 index 0000000000000..35b116ef6e019 --- /dev/null +++ b/tests/baselines/reference/structExtendsItself.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItself.ts(1,8): error TS2506: 'C' is referenced directly or indirectly in its own base expression. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItself.ts (1 errors) ==== + struct C extends C { } // error + ~ +!!! error TS2506: 'C' is referenced directly or indirectly in its own base expression. + + // struct D extends D { } // error + + // struct E extends E { } // error \ No newline at end of file diff --git a/tests/baselines/reference/structExtendsItself.js b/tests/baselines/reference/structExtendsItself.js new file mode 100644 index 0000000000000..01e28aeb4723e --- /dev/null +++ b/tests/baselines/reference/structExtendsItself.js @@ -0,0 +1,30 @@ +//// [structExtendsItself.ts] +struct C extends C { } // error + +// struct D extends D { } // error + +// struct E extends E { } // error + +//// [structExtendsItself.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); + // error + // error +// struct D extends D { } // error +// struct E extends E { } // error diff --git a/tests/baselines/reference/structExtendsItselfIndirectly.errors.txt b/tests/baselines/reference/structExtendsItselfIndirectly.errors.txt new file mode 100644 index 0000000000000..df67f275cdea1 --- /dev/null +++ b/tests/baselines/reference/structExtendsItselfIndirectly.errors.txt @@ -0,0 +1,26 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly.ts(1,8): error TS2506: 'C' is referenced directly or indirectly in its own base expression. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly.ts(3,8): error TS2506: 'D' is referenced directly or indirectly in its own base expression. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly.ts(5,8): error TS2506: 'E' is referenced directly or indirectly in its own base expression. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly.ts(11,39): error TS1010: '*/' expected. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly.ts (4 errors) ==== + struct C extends E { foo: string; } // error + ~ +!!! error TS2506: 'C' is referenced directly or indirectly in its own base expression. + + struct D extends C { bar: string; } + ~ +!!! error TS2506: 'D' is referenced directly or indirectly in its own base expression. + + struct E extends D { baz: number; } + ~ +!!! error TS2506: 'E' is referenced directly or indirectly in its own base expression. + + /* struct C2 extends E2 { foo: T; } // error + + struct D2 extends C2 { bar: T; } + + struct E2 extends D2 { baz: T; } + +!!! error TS1010: '*/' expected. \ No newline at end of file diff --git a/tests/baselines/reference/structExtendsItselfIndirectly.js b/tests/baselines/reference/structExtendsItselfIndirectly.js new file mode 100644 index 0000000000000..b08f775e5d672 --- /dev/null +++ b/tests/baselines/reference/structExtendsItselfIndirectly.js @@ -0,0 +1,68 @@ +//// [structExtendsItselfIndirectly.ts] +struct C extends E { foo: string; } // error + +struct D extends C { bar: string; } + +struct E extends D { baz: number; } + +/* struct C2 extends E2 { foo: T; } // error + +struct D2 extends C2 { bar: T; } + +struct E2 extends D2 { baz: T; } + +//// [structExtendsItselfIndirectly.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); + // error + // error +var D = (function () { + var _D = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); +var E = (function () { + var _E = new TypedObject.StructType({ + baz: TypedObject.float64 + }); + function _ctor() { + } + function E() { + var obj = new _E(); + _ctor.call(obj); + return obj; + } + E._TO = _E; + return E; +})(); +/* struct C2 extends E2 { foo: T; } // error + +struct D2 extends C2 { bar: T; } + +struct E2 extends D2 { baz: T; } diff --git a/tests/baselines/reference/structExtendsItselfIndirectly2.errors.txt b/tests/baselines/reference/structExtendsItselfIndirectly2.errors.txt new file mode 100644 index 0000000000000..0ac9a0bfaf58b --- /dev/null +++ b/tests/baselines/reference/structExtendsItselfIndirectly2.errors.txt @@ -0,0 +1,37 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly2.ts(1,8): error TS2506: 'C' is referenced directly or indirectly in its own base expression. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly2.ts(4,19): error TS2506: 'D' is referenced directly or indirectly in its own base expression. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly2.ts(9,19): error TS2506: 'E' is referenced directly or indirectly in its own base expression. +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly2.ts(22,2): error TS1010: '*/' expected. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly2.ts (4 errors) ==== + struct C extends N.E { foo: string; } // error + ~ +!!! error TS2506: 'C' is referenced directly or indirectly in its own base expression. + + module M { + export struct D extends C { bar: string; } + ~ +!!! error TS2506: 'D' is referenced directly or indirectly in its own base expression. + + } + + module N { + export struct E extends M.D { baz: number; } + ~ +!!! error TS2506: 'E' is referenced directly or indirectly in its own base expression. + } + + /* module O { + struct C2 extends Q.E2 { foo: T; } // error + + module P { + export struct D2 extends C2 { bar: T; } + } + + module Q { + export struct E2 extends P.D2 { baz: T; } + } + } + +!!! error TS1010: '*/' expected. \ No newline at end of file diff --git a/tests/baselines/reference/structExtendsItselfIndirectly2.js b/tests/baselines/reference/structExtendsItselfIndirectly2.js new file mode 100644 index 0000000000000..4c7f6abed96ac --- /dev/null +++ b/tests/baselines/reference/structExtendsItselfIndirectly2.js @@ -0,0 +1,91 @@ +//// [structExtendsItselfIndirectly2.ts] +struct C extends N.E { foo: string; } // error + +module M { + export struct D extends C { bar: string; } + +} + +module N { + export struct E extends M.D { baz: number; } +} + +/* module O { + struct C2 extends Q.E2 { foo: T; } // error + + module P { + export struct D2 extends C2 { bar: T; } + } + + module Q { + export struct E2 extends P.D2 { baz: T; } + } +} + +//// [structExtendsItselfIndirectly2.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); + // error + // error +var M; +(function (M) { + var D = (function () { + var _D = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; + })(); +})(M || (M = {})); +var N; +(function (N) { + var E = (function () { + var _E = new TypedObject.StructType({ + baz: TypedObject.float64 + }); + function _ctor() { + } + function E() { + var obj = new _E(); + _ctor.call(obj); + return obj; + } + E._TO = _E; + return E; + })(); +})(N || (N = {})); +/* module O { + struct C2 extends Q.E2 { foo: T; } // error + + module P { + export struct D2 extends C2 { bar: T; } + } + + module Q { + export struct E2 extends P.D2 { baz: T; } + } +} diff --git a/tests/baselines/reference/structExtendsValidConstructorFunction.errors.txt b/tests/baselines/reference/structExtendsValidConstructorFunction.errors.txt new file mode 100644 index 0000000000000..f2935e6e63914 --- /dev/null +++ b/tests/baselines/reference/structExtendsValidConstructorFunction.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsValidConstructorFunction.ts(5,18): error TS2507: Type '() => void' is not a constructor function type. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsValidConstructorFunction.ts (1 errors) ==== + function foo() { } + + var x = new foo(); // can be used as a constructor function + + struct C extends foo { } // error, cannot extend it though + ~~~ +!!! error TS2507: Type '() => void' is not a constructor function type. \ No newline at end of file diff --git a/tests/baselines/reference/structExtendsValidConstructorFunction.js b/tests/baselines/reference/structExtendsValidConstructorFunction.js new file mode 100644 index 0000000000000..c85dceabd3f70 --- /dev/null +++ b/tests/baselines/reference/structExtendsValidConstructorFunction.js @@ -0,0 +1,29 @@ +//// [structExtendsValidConstructorFunction.ts] +function foo() { } + +var x = new foo(); // can be used as a constructor function + +struct C extends foo { } // error, cannot extend it though + +//// [structExtendsValidConstructorFunction.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +function foo() { } +var x = new foo(); // can be used as a constructor function +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); + // error, cannot extend it though // error, cannot extend it though diff --git a/tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.js b/tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.js new file mode 100644 index 0000000000000..364dc5666b0e7 --- /dev/null +++ b/tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.js @@ -0,0 +1,69 @@ +//// [structInstanceTypeIsAssignableToBaseType.ts] +// doc 1.1 +// Because struct uses nominal typing, the instance type of the declared struct doesn’t need to be +// assignable to the base type reference. However, member variables override is not allowed. +// ok + +struct C { + foo: string; + thing(): string { + return "1"; + } +} + +struct D extends C { + thing(): number { + return 1; // ok + } +} + +var c: C; +var d: D; +c = d; + +//// [structInstanceTypeIsAssignableToBaseType.js] +// doc 1.1 +// Because struct uses nominal typing, the instance type of the declared struct doesn’t need to be +// assignable to the base type reference. However, member variables override is not allowed. +// ok +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.thing = function () { + return "1"; + }; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + _D.prototype.thing = function () { + return 1; // ok + }; + return D; +})(); +var c; +var d; +c = d; diff --git a/tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.symbols b/tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.symbols new file mode 100644 index 0000000000000..cd4b5d3d55024 --- /dev/null +++ b/tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.symbols @@ -0,0 +1,42 @@ +=== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structInstanceTypeIsAssignableToBaseType.ts === +// doc 1.1 +// Because struct uses nominal typing, the instance type of the declared struct doesn’t need to be +// assignable to the base type reference. However, member variables override is not allowed. +// ok + +struct C { +>C : Symbol(C, Decl(structInstanceTypeIsAssignableToBaseType.ts, 0, 0)) + + foo: string; +>foo : Symbol(foo, Decl(structInstanceTypeIsAssignableToBaseType.ts, 5, 10)) + + thing(): string { +>thing : Symbol(thing, Decl(structInstanceTypeIsAssignableToBaseType.ts, 6, 13)) + + return "1"; + } +} + +struct D extends C { +>D : Symbol(D, Decl(structInstanceTypeIsAssignableToBaseType.ts, 10, 1)) +>C : Symbol(C, Decl(structInstanceTypeIsAssignableToBaseType.ts, 0, 0)) + + thing(): number { +>thing : Symbol(thing, Decl(structInstanceTypeIsAssignableToBaseType.ts, 12, 20)) + + return 1; // ok + } +} + +var c: C; +>c : Symbol(c, Decl(structInstanceTypeIsAssignableToBaseType.ts, 18, 3)) +>C : Symbol(C, Decl(structInstanceTypeIsAssignableToBaseType.ts, 0, 0)) + +var d: D; +>d : Symbol(d, Decl(structInstanceTypeIsAssignableToBaseType.ts, 19, 3)) +>D : Symbol(D, Decl(structInstanceTypeIsAssignableToBaseType.ts, 10, 1)) + +c = d; +>c : Symbol(c, Decl(structInstanceTypeIsAssignableToBaseType.ts, 18, 3)) +>d : Symbol(d, Decl(structInstanceTypeIsAssignableToBaseType.ts, 19, 3)) + diff --git a/tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.types b/tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.types new file mode 100644 index 0000000000000..d1e115034b047 --- /dev/null +++ b/tests/baselines/reference/structInstanceTypeIsAssignableToBaseType.types @@ -0,0 +1,45 @@ +=== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structInstanceTypeIsAssignableToBaseType.ts === +// doc 1.1 +// Because struct uses nominal typing, the instance type of the declared struct doesn’t need to be +// assignable to the base type reference. However, member variables override is not allowed. +// ok + +struct C { +>C : C + + foo: string; +>foo : string + + thing(): string { +>thing : () => string + + return "1"; +>"1" : string + } +} + +struct D extends C { +>D : D +>C : C + + thing(): number { +>thing : () => number + + return 1; // ok +>1 : number + } +} + +var c: C; +>c : C +>C : C + +var d: D; +>d : D +>D : D + +c = d; +>c = d : D +>c : C +>d : D + diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItself.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItself.ts new file mode 100644 index 0000000000000..f7e2fd89dd0a0 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItself.ts @@ -0,0 +1,5 @@ +struct C extends C { } // error + +// struct D extends D { } // error + +// struct E extends E { } // error \ No newline at end of file diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly.ts new file mode 100644 index 0000000000000..82e074370799a --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly.ts @@ -0,0 +1,11 @@ +struct C extends E { foo: string; } // error + +struct D extends C { bar: string; } + +struct E extends D { baz: number; } + +/* struct C2 extends E2 { foo: T; } // error + +struct D2 extends C2 { bar: T; } + +struct E2 extends D2 { baz: T; } \ No newline at end of file diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly2.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly2.ts new file mode 100644 index 0000000000000..542cffef67ea2 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsItselfIndirectly2.ts @@ -0,0 +1,22 @@ +struct C extends N.E { foo: string; } // error + +module M { + export struct D extends C { bar: string; } + +} + +module N { + export struct E extends M.D { baz: number; } +} + +/* module O { + struct C2 extends Q.E2 { foo: T; } // error + + module P { + export struct D2 extends C2 { bar: T; } + } + + module Q { + export struct E2 extends P.D2 { baz: T; } + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsValidConstructorFunction.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsValidConstructorFunction.ts new file mode 100644 index 0000000000000..3ba5d8eebd7aa --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structExtendsValidConstructorFunction.ts @@ -0,0 +1,5 @@ +function foo() { } + +var x = new foo(); // can be used as a constructor function + +struct C extends foo { } // error, cannot extend it though \ No newline at end of file diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structInstanceTypeIsAssignableToBaseType.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structInstanceTypeIsAssignableToBaseType.ts new file mode 100644 index 0000000000000..c1fb00b238dc4 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structInstanceTypeIsAssignableToBaseType.ts @@ -0,0 +1,21 @@ +// doc 1.1 +// Because struct uses nominal typing, the instance type of the declared struct doesn’t need to be +// assignable to the base type reference. However, member variables override is not allowed. +// ok + +struct C { + foo: string; + thing(): string { + return "1"; + } +} + +struct D extends C { + thing(): number { + return 1; // ok + } +} + +var c: C; +var d: D; +c = d; \ No newline at end of file From fcace4fd2f1383e1b24107c28527998882a2800b Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 15:17:02 -0800 Subject: [PATCH 28/43] struct inheritance checking - ignore declaration & revert lint for easy reading --- src/compiler/checker.ts | 65 +++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index caa0da9d8f0e1..ad59a0ca11fd4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3011,13 +3011,15 @@ namespace ts { if (baseType === unknownType) { return; } - if (!(getTargetType(baseType).flags & (TypeFlags.Class | TypeFlags.Interface)) || (isInClassDecl && (getTargetType(baseType).flags & TypeFlags.Struct))) { - error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); - return; - } - else if (!isInClassDecl && !(getTargetType(baseType).flags & TypeFlags.Struct)) { - error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_struct_type, typeToString(baseType)); - return; + if (!(getTargetType(baseType).flags & (TypeFlags.Class | TypeFlags.Struct | TypeFlags.Interface))) { + if (isInClassDecl) { + error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); + return; + } + else { + error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_struct_type, typeToString(baseType)); + return; + } } if (type === baseType || hasBaseType(baseType, type)) { error(type.symbol.valueDeclaration, Diagnostics.Type_0_recursively_references_itself_as_a_base_type, @@ -4909,13 +4911,13 @@ parentSymbol = (declaration.parent).symbol; } function isBothStructType(source: Type, target: Type): boolean { - return isTypeStruct(source) && isTypeStruct(target); + return isTypeReferenceStruct(source) && isTypeReferenceStruct(target); } function isOneTypeStructType(source: Type, target: Type): boolean { // return (isTypeStruct(source) && !isTypeStruct(target)) || (isTypeStruct(target) && !isTypeStruct(source)); - return (isTypeStruct(source) && !isTypeStruct(target)) || - (isTypeStruct(target) && !isTypeStruct(source)); + return (isTypeReferenceStruct(source) && !isTypeReferenceStruct(target)) || + (isTypeReferenceStruct(target) && !isTypeReferenceStruct(source)); } function write(info: string): void { @@ -5120,25 +5122,30 @@ parentSymbol = (declaration.parent).symbol; if (apparentType.flags & (TypeFlags.ObjectType | TypeFlags.Intersection) && target.flags & TypeFlags.ObjectType) { // Report structural errors only if we haven't reported any errors yet let reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo; - if (isBothStructType(source, target)) { - // for struct assignability, check its inheritance chain - let baseTypes = (apparentType).resolvedBaseTypes; - while (baseTypes && baseTypes.length > 0) { - if (baseTypes.indexOf(target) > -1) { // target is on source's inheritance chain - errorInfo = saveErrorInfo; - return Ternary.True; - } - else { -baseTypes = (baseTypes[0]).resolvedBaseTypes; // expand the inheritance chain - } - } - } - else { // structural comparison - if (result = objectTypeRelatedTo(apparentType, target, reportStructuralErrors)) { - errorInfo = saveErrorInfo; - return result; - } - } + if (isBothStructType(source, target)) { + // for struct assignability, check its inheritance chain + let baseTypes = (apparentType).resolvedBaseTypes; + if (baseTypes) { + while (baseTypes && baseTypes.length > 0) { + if (baseTypes.indexOf(target) > -1) { // target is on source's inheritance chain + errorInfo = saveErrorInfo; + return Ternary.True; + } + else { + baseTypes = (baseTypes[0]).resolvedBaseTypes; // expand the inheritance chain + } + } + } + else { // struct declaration + return Ternary.True; + } + } + else { // structural comparison + if (result = objectTypeRelatedTo(apparentType, target, reportStructuralErrors)) { + errorInfo = saveErrorInfo; + return result; + } + } } } From 5699303161739943f6b1afbd68792d7592c9214a Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 15:33:41 -0800 Subject: [PATCH 29/43] struct extends --- ...uctorFunctionTypeIsAssignableToBaseType.js | 66 +++++++++++++++ ...FunctionTypeIsAssignableToBaseType.symbols | 38 +++++++++ ...orFunctionTypeIsAssignableToBaseType.types | 38 +++++++++ ...tionTypeIsAssignableToBaseType2.errors.txt | 38 +++++++++ ...ctorFunctionTypeIsAssignableToBaseType2.js | 84 +++++++++++++++++++ ...vedTypeDoesRequireExtendsClause.errors.txt | 24 ++++++ ...ructDerivedTypeDoesRequireExtendsClause.js | 74 ++++++++++++++++ ...uctorFunctionTypeIsAssignableToBaseType.ts | 19 +++++ ...ctorFunctionTypeIsAssignableToBaseType2.ts | 32 +++++++ ...ructDerivedTypeDoesRequireExtendsClause.ts | 18 ++++ 10 files changed, 431 insertions(+) create mode 100644 tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.js create mode 100644 tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.symbols create mode 100644 tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.types create mode 100644 tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType2.errors.txt create mode 100644 tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType2.js create mode 100644 tests/baselines/reference/structDerivedTypeDoesRequireExtendsClause.errors.txt create mode 100644 tests/baselines/reference/structDerivedTypeDoesRequireExtendsClause.js create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType2.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structDerivedTypeDoesRequireExtendsClause.ts diff --git a/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.js b/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.js new file mode 100644 index 0000000000000..aad49e9ac502c --- /dev/null +++ b/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.js @@ -0,0 +1,66 @@ +//// [structConstructorFunctionTypeIsAssignableToBaseType.ts] +struct Base { + static foo: { + bar: Object; + } +} + +struct Derived extends Base { + // ok + static foo: { + bar: number; + } +} + +struct Derived2 extends Base { + // ok + static foo: { + bar: any; + } +} + +//// [structConstructorFunctionTypeIsAssignableToBaseType.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var Derived2 = (function () { + var _Derived2 = new TypedObject.StructType({ + }); + function _ctor() { + } + function Derived2() { + var obj = new _Derived2(); + _ctor.call(obj); + return obj; + } + Derived2._TO = _Derived2; + return Derived2; +})(); diff --git a/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.symbols b/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.symbols new file mode 100644 index 0000000000000..ce083f68c4471 --- /dev/null +++ b/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.symbols @@ -0,0 +1,38 @@ +=== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType.ts === +struct Base { +>Base : Symbol(Base, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 0, 0)) + + static foo: { +>foo : Symbol(Base.foo, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 0, 13)) + + bar: Object; +>bar : Symbol(bar, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 1, 17)) +>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) + } +} + +struct Derived extends Base { +>Derived : Symbol(Derived, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 4, 1)) +>Base : Symbol(Base, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 0, 0)) + + // ok + static foo: { +>foo : Symbol(Derived.foo, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 6, 29)) + + bar: number; +>bar : Symbol(bar, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 8, 17)) + } +} + +struct Derived2 extends Base { +>Derived2 : Symbol(Derived2, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 11, 1)) +>Base : Symbol(Base, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 0, 0)) + + // ok + static foo: { +>foo : Symbol(Derived2.foo, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 13, 30)) + + bar: any; +>bar : Symbol(bar, Decl(structConstructorFunctionTypeIsAssignableToBaseType.ts, 15, 17)) + } +} diff --git a/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.types b/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.types new file mode 100644 index 0000000000000..95843ef05e8c1 --- /dev/null +++ b/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType.types @@ -0,0 +1,38 @@ +=== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType.ts === +struct Base { +>Base : Base + + static foo: { +>foo : { bar: Object; } + + bar: Object; +>bar : Object +>Object : Object + } +} + +struct Derived extends Base { +>Derived : Derived +>Base : Base + + // ok + static foo: { +>foo : { bar: number; } + + bar: number; +>bar : number + } +} + +struct Derived2 extends Base { +>Derived2 : Derived2 +>Base : Base + + // ok + static foo: { +>foo : { bar: any; } + + bar: any; +>bar : any + } +} diff --git a/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType2.errors.txt b/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType2.errors.txt new file mode 100644 index 0000000000000..8abfe0c209fc4 --- /dev/null +++ b/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType2.errors.txt @@ -0,0 +1,38 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType2.ts(30,16): error TS20020: Cannot have return expression in struct constructor. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType2.ts (1 errors) ==== + // the constructor function itself does not need to be a subtype of the base type constructor function + + struct Base { + static foo: { + bar: Object; + } + constructor(x: Object) { + } + } + + struct Derived extends Base { + // ok + static foo: { + bar: number; + } + + constructor(x: number) { // ok + super(x); + } + } + + struct Derived2 extends Base { + static foo: { + bar: number; + } + + // ok, not enforcing assignability relation on this + constructor(x: any) { + super(x); + return 1; // return expression not allowed + ~ +!!! error TS20020: Cannot have return expression in struct constructor. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType2.js b/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType2.js new file mode 100644 index 0000000000000..832d0908ca014 --- /dev/null +++ b/tests/baselines/reference/structConstructorFunctionTypeIsAssignableToBaseType2.js @@ -0,0 +1,84 @@ +//// [structConstructorFunctionTypeIsAssignableToBaseType2.ts] +// the constructor function itself does not need to be a subtype of the base type constructor function + +struct Base { + static foo: { + bar: Object; + } + constructor(x: Object) { + } +} + +struct Derived extends Base { + // ok + static foo: { + bar: number; + } + + constructor(x: number) { // ok + super(x); + } +} + +struct Derived2 extends Base { + static foo: { + bar: number; + } + + // ok, not enforcing assignability relation on this + constructor(x: any) { + super(x); + return 1; // return expression not allowed + } +} + +//// [structConstructorFunctionTypeIsAssignableToBaseType2.js] +// the constructor function itself does not need to be a subtype of the base type constructor function +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + }); + function _ctor(x) { + } + function Base(x) { + var obj = new _Base(); + _ctor.call(obj ,); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor(x) { + _super.call(this, x); + } + function Derived(x) { + var obj = new _Derived(); + _ctor.call(obj ,); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var Derived2 = (function () { + var _Derived2 = new TypedObject.StructType({ + }); + // ok, not enforcing assignability relation on this + function _ctor(x) { + _super.call(this, x); + return 1; // return expression not allowed + } + function Derived2(x) { + var obj = new _Derived2(); + _ctor.call(obj ,); + return obj; + } + Derived2._TO = _Derived2; + return Derived2; +})(); diff --git a/tests/baselines/reference/structDerivedTypeDoesRequireExtendsClause.errors.txt b/tests/baselines/reference/structDerivedTypeDoesRequireExtendsClause.errors.txt new file mode 100644 index 0000000000000..65d8588ce1852 --- /dev/null +++ b/tests/baselines/reference/structDerivedTypeDoesRequireExtendsClause.errors.txt @@ -0,0 +1,24 @@ +tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structDerivedTypeDoesRequireExtendsClause.ts(17,1): error TS2322: Type 'Derived' is not assignable to type 'Base'. + + +==== tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structDerivedTypeDoesRequireExtendsClause.ts (1 errors) ==== + struct Base { + foo: string; + } + + struct Derived { + foo: string; + bar: number; + } + + struct Derived2 extends Base { + bar: string; + } + + var b: Base; + var d1: Derived; + var d2: Derived2; + b = d1; // error, not structural typing + ~ +!!! error TS2322: Type 'Derived' is not assignable to type 'Base'. + b = d2; // ok \ No newline at end of file diff --git a/tests/baselines/reference/structDerivedTypeDoesRequireExtendsClause.js b/tests/baselines/reference/structDerivedTypeDoesRequireExtendsClause.js new file mode 100644 index 0000000000000..b790a4a387cbc --- /dev/null +++ b/tests/baselines/reference/structDerivedTypeDoesRequireExtendsClause.js @@ -0,0 +1,74 @@ +//// [structDerivedTypeDoesRequireExtendsClause.ts] +struct Base { + foo: string; +} + +struct Derived { + foo: string; + bar: number; +} + +struct Derived2 extends Base { + bar: string; +} + +var b: Base; +var d1: Derived; +var d2: Derived2; +b = d1; // error, not structural typing +b = d2; // ok + +//// [structDerivedTypeDoesRequireExtendsClause.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + foo: TypedObject.string, + bar: TypedObject.float64 + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var Derived2 = (function () { + var _Derived2 = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function Derived2() { + var obj = new _Derived2(); + _ctor.call(obj); + return obj; + } + Derived2._TO = _Derived2; + return Derived2; +})(); +var b; +var d1; +var d2; +b = d1; // error, not structural typing +b = d2; // ok diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType.ts new file mode 100644 index 0000000000000..0c6056a01a9d1 --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType.ts @@ -0,0 +1,19 @@ +struct Base { + static foo: { + bar: Object; + } +} + +struct Derived extends Base { + // ok + static foo: { + bar: number; + } +} + +struct Derived2 extends Base { + // ok + static foo: { + bar: any; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType2.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType2.ts new file mode 100644 index 0000000000000..7ecae2bb13d5e --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structConstructorFunctionTypeIsAssignableToBaseType2.ts @@ -0,0 +1,32 @@ +// the constructor function itself does not need to be a subtype of the base type constructor function + +struct Base { + static foo: { + bar: Object; + } + constructor(x: Object) { + } +} + +struct Derived extends Base { + // ok + static foo: { + bar: number; + } + + constructor(x: number) { // ok + super(x); + } +} + +struct Derived2 extends Base { + static foo: { + bar: number; + } + + // ok, not enforcing assignability relation on this + constructor(x: any) { + super(x); + return 1; // return expression not allowed + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structDerivedTypeDoesRequireExtendsClause.ts b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structDerivedTypeDoesRequireExtendsClause.ts new file mode 100644 index 0000000000000..58a6494d143ca --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structHeritageSpecification/structDerivedTypeDoesRequireExtendsClause.ts @@ -0,0 +1,18 @@ +struct Base { + foo: string; +} + +struct Derived { + foo: string; + bar: number; +} + +struct Derived2 extends Base { + bar: string; +} + +var b: Base; +var d1: Derived; +var d2: Derived2; +b = d1; // error, not structural typing +b = d2; // ok \ No newline at end of file From 6f48862332ecfa676cdb6f2e6b258164bd5633b3 Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 16:04:29 -0800 Subject: [PATCH 30/43] check base types --- src/compiler/checker.ts | 43 +++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ad59a0ca11fd4..53b28320810e4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3011,15 +3011,13 @@ namespace ts { if (baseType === unknownType) { return; } - if (!(getTargetType(baseType).flags & (TypeFlags.Class | TypeFlags.Struct | TypeFlags.Interface))) { - if (isInClassDecl) { - error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); - return; - } - else { - error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_struct_type, typeToString(baseType)); - return; - } + if (isInClassDecl && !(getTargetType(baseType).flags & (TypeFlags.Class | TypeFlags.Interface))) { + error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); + return; + } + else if (!isInClassDecl && !(getTargetType(baseType).flags & TypeFlags.Struct)) { + error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_struct_type, typeToString(baseType)); + return; } if (type === baseType || hasBaseType(baseType, type)) { error(type.symbol.valueDeclaration, Diagnostics.Type_0_recursively_references_itself_as_a_base_type, @@ -5125,19 +5123,14 @@ parentSymbol = (declaration.parent).symbol; if (isBothStructType(source, target)) { // for struct assignability, check its inheritance chain let baseTypes = (apparentType).resolvedBaseTypes; - if (baseTypes) { - while (baseTypes && baseTypes.length > 0) { - if (baseTypes.indexOf(target) > -1) { // target is on source's inheritance chain - errorInfo = saveErrorInfo; - return Ternary.True; - } - else { - baseTypes = (baseTypes[0]).resolvedBaseTypes; // expand the inheritance chain - } + while (baseTypes && baseTypes.length > 0) { + if (baseTypes.indexOf(target) > -1) { // target is on source's inheritance chain + errorInfo = saveErrorInfo; + return Ternary.True; + } + else { + baseTypes = (baseTypes[0]).resolvedBaseTypes; // expand the inheritance chain } - } - else { // struct declaration - return Ternary.True; } } else { // structural comparison @@ -13791,9 +13784,9 @@ error(node.expression, Diagnostics.Struct_constructor_cannot_have_return_express } } } - checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType, type.thisType), node.name || node, Diagnostics.Struct_0_incorrectly_extends_base_struct_1); + /* checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType, type.thisType), node.name || node, Diagnostics.Struct_0_incorrectly_extends_base_struct_1); checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node, - Diagnostics.Struct_static_side_0_incorrectly_extends_base_struct_static_side_1); + Diagnostics.Struct_static_side_0_incorrectly_extends_base_struct_static_side_1); */ if (!(staticBaseType.symbol && staticBaseType.symbol.flags & SymbolFlags.Struct)) { // When the static base type is a "struct-like" constructor function (but not actually a struct), we verify @@ -13809,10 +13802,6 @@ error(node.expression, Diagnostics.Struct_constructor_cannot_have_return_express } } - if (produceDiagnostics) { - // checkIndexConstraints(type); - checkTypeForDuplicateIndexSignatures(node); - } } function checkClassLikeDeclaration(node: ClassLikeDeclaration) { From de083895d070fb986d8a189f4ba3a7a7656ee66e Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 16:56:12 -0800 Subject: [PATCH 31/43] instance and static members --- ...tructTypeOfThisInInstanceMember.errors.txt | 33 ++++++ .../structTypeOfThisInInstanceMember.js | 63 ++++++++++ .../structTypeOfThisInInstanceMember2.js | 64 ++++++++++ .../structTypeOfThisInInstanceMember2.symbols | 104 +++++++++++++++++ .../structTypeOfThisInInstanceMember2.types | 109 ++++++++++++++++++ .../structTypeOfThisInStaticMembers.js | 81 +++++++++++++ .../structTypeOfThisInStaticMembers.symbols | 67 +++++++++++ .../structTypeOfThisInStaticMembers.types | 73 ++++++++++++ ...tructTypeOfThisInStaticMembers2.errors.txt | 13 +++ .../structTypeOfThisInStaticMembers2.js | 26 +++++ .../structTypeOfThisInInstanceMember.ts | 27 +++++ .../structTypeOfThisInInstanceMember2.ts | 27 +++++ .../structTypeOfThisInStaticMembers.ts | 36 ++++++ .../structTypeOfThisInStaticMembers2.ts | 7 ++ 14 files changed, 730 insertions(+) create mode 100644 tests/baselines/reference/structTypeOfThisInInstanceMember.errors.txt create mode 100644 tests/baselines/reference/structTypeOfThisInInstanceMember.js create mode 100644 tests/baselines/reference/structTypeOfThisInInstanceMember2.js create mode 100644 tests/baselines/reference/structTypeOfThisInInstanceMember2.symbols create mode 100644 tests/baselines/reference/structTypeOfThisInInstanceMember2.types create mode 100644 tests/baselines/reference/structTypeOfThisInStaticMembers.js create mode 100644 tests/baselines/reference/structTypeOfThisInStaticMembers.symbols create mode 100644 tests/baselines/reference/structTypeOfThisInStaticMembers.types create mode 100644 tests/baselines/reference/structTypeOfThisInStaticMembers2.errors.txt create mode 100644 tests/baselines/reference/structTypeOfThisInStaticMembers2.js create mode 100644 tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember.ts create mode 100644 tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember2.ts create mode 100644 tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers.ts create mode 100644 tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers2.ts diff --git a/tests/baselines/reference/structTypeOfThisInInstanceMember.errors.txt b/tests/baselines/reference/structTypeOfThisInInstanceMember.errors.txt new file mode 100644 index 0000000000000..5b2dcd4c33332 --- /dev/null +++ b/tests/baselines/reference/structTypeOfThisInInstanceMember.errors.txt @@ -0,0 +1,33 @@ +tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember.ts(12,11): error TS2339: Property 'z' does not exist on type 'C'. + + +==== tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember.ts (1 errors) ==== + // doc 2.1 + // Within constructors and instance member functions, the type of this is the instance type of the sturct. + + struct C { + x = this; + foo() { + return this; + } + constructor(x: number) { + var t = this; + t.x; + t.z; // error, z doesn't exist. + ~ +!!! error TS2339: Property 'z' does not exist on type 'C'. + var r = t.foo(); + } + } + + var c: C; + // all ok + var r = c.x; + var ra = c.x.x.x; + var r3 = c.foo(); + var rs = [r, r3]; + + rs.forEach(x => { + x.foo; + x.x; + }); \ No newline at end of file diff --git a/tests/baselines/reference/structTypeOfThisInInstanceMember.js b/tests/baselines/reference/structTypeOfThisInInstanceMember.js new file mode 100644 index 0000000000000..a2be4f5731258 --- /dev/null +++ b/tests/baselines/reference/structTypeOfThisInInstanceMember.js @@ -0,0 +1,63 @@ +//// [structTypeOfThisInInstanceMember.ts] +// doc 2.1 +// Within constructors and instance member functions, the type of this is the instance type of the sturct. + +struct C { + x = this; + foo() { + return this; + } + constructor(x: number) { + var t = this; + t.x; + t.z; // error, z doesn't exist. + var r = t.foo(); + } +} + +var c: C; +// all ok +var r = c.x; +var ra = c.x.x.x; +var r3 = c.foo(); +var rs = [r, r3]; + +rs.forEach(x => { + x.foo; + x.x; +}); + +//// [structTypeOfThisInInstanceMember.js] +// doc 2.1 +// Within constructors and instance member functions, the type of this is the instance type of the sturct. +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.Object, + }); + function _ctor(x) { + var t = this; + t.x; + t.z; // error, z doesn't exist. + var r = t.foo(); + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + _C.prototype.foo = function () { + return this; + }; + return C; +})(); +var c; +// all ok +var r = c.x; +var ra = c.x.x.x; +var r3 = c.foo(); +var rs = [r, r3]; +rs.forEach(function (x) { + x.foo; + x.x; +}); diff --git a/tests/baselines/reference/structTypeOfThisInInstanceMember2.js b/tests/baselines/reference/structTypeOfThisInInstanceMember2.js new file mode 100644 index 0000000000000..9c0adf476f48b --- /dev/null +++ b/tests/baselines/reference/structTypeOfThisInInstanceMember2.js @@ -0,0 +1,64 @@ +//// [structTypeOfThisInInstanceMember2.ts] +struct C { + x = this; + foo() { + return this; + } + constructor(x: T) { + var t = this; + t.x; + t.z; + var r = t.foo(); + } + z: T; +} + +var c: C; +// all ok +var r = c.x; +var ra = c.x.x.x; +var r3 = c.foo(); +var r4 = c.z; +var rs = [r, r3]; + +rs.forEach(x => { + x.foo; + x.x; + x.z; +}); + +//// [structTypeOfThisInInstanceMember2.js] +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.Object, + z: TypedObject.Object + }); + function _ctor(x) { + var t = this; + t.x; + t.z; + var r = t.foo(); + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + _C.prototype.foo = function () { + return this; + }; + return C; +})(); +var c; +// all ok +var r = c.x; +var ra = c.x.x.x; +var r3 = c.foo(); +var r4 = c.z; +var rs = [r, r3]; +rs.forEach(function (x) { + x.foo; + x.x; + x.z; +}); diff --git a/tests/baselines/reference/structTypeOfThisInInstanceMember2.symbols b/tests/baselines/reference/structTypeOfThisInInstanceMember2.symbols new file mode 100644 index 0000000000000..19a6dcf8b99c1 --- /dev/null +++ b/tests/baselines/reference/structTypeOfThisInInstanceMember2.symbols @@ -0,0 +1,104 @@ +=== tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember2.ts === +struct C { +>C : Symbol(C, Decl(structTypeOfThisInInstanceMember2.ts, 0, 0)) +>T : Symbol(T, Decl(structTypeOfThisInInstanceMember2.ts, 0, 9)) + + x = this; +>x : Symbol(x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) +>this : Symbol(C, Decl(structTypeOfThisInInstanceMember2.ts, 0, 0)) + + foo() { +>foo : Symbol(foo, Decl(structTypeOfThisInInstanceMember2.ts, 1, 13)) + + return this; +>this : Symbol(C, Decl(structTypeOfThisInInstanceMember2.ts, 0, 0)) + } + constructor(x: T) { +>x : Symbol(x, Decl(structTypeOfThisInInstanceMember2.ts, 5, 16)) +>T : Symbol(T, Decl(structTypeOfThisInInstanceMember2.ts, 0, 9)) + + var t = this; +>t : Symbol(t, Decl(structTypeOfThisInInstanceMember2.ts, 6, 11)) +>this : Symbol(C, Decl(structTypeOfThisInInstanceMember2.ts, 0, 0)) + + t.x; +>t.x : Symbol(x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) +>t : Symbol(t, Decl(structTypeOfThisInInstanceMember2.ts, 6, 11)) +>x : Symbol(x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) + + t.z; +>t.z : Symbol(z, Decl(structTypeOfThisInInstanceMember2.ts, 10, 5)) +>t : Symbol(t, Decl(structTypeOfThisInInstanceMember2.ts, 6, 11)) +>z : Symbol(z, Decl(structTypeOfThisInInstanceMember2.ts, 10, 5)) + + var r = t.foo(); +>r : Symbol(r, Decl(structTypeOfThisInInstanceMember2.ts, 9, 11)) +>t.foo : Symbol(foo, Decl(structTypeOfThisInInstanceMember2.ts, 1, 13)) +>t : Symbol(t, Decl(structTypeOfThisInInstanceMember2.ts, 6, 11)) +>foo : Symbol(foo, Decl(structTypeOfThisInInstanceMember2.ts, 1, 13)) + } + z: T; +>z : Symbol(z, Decl(structTypeOfThisInInstanceMember2.ts, 10, 5)) +>T : Symbol(T, Decl(structTypeOfThisInInstanceMember2.ts, 0, 9)) +} + +var c: C; +>c : Symbol(c, Decl(structTypeOfThisInInstanceMember2.ts, 14, 3)) +>C : Symbol(C, Decl(structTypeOfThisInInstanceMember2.ts, 0, 0)) + +// all ok +var r = c.x; +>r : Symbol(r, Decl(structTypeOfThisInInstanceMember2.ts, 16, 3)) +>c.x : Symbol(C.x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) +>c : Symbol(c, Decl(structTypeOfThisInInstanceMember2.ts, 14, 3)) +>x : Symbol(C.x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) + +var ra = c.x.x.x; +>ra : Symbol(ra, Decl(structTypeOfThisInInstanceMember2.ts, 17, 3)) +>c.x.x.x : Symbol(C.x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) +>c.x.x : Symbol(C.x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) +>c.x : Symbol(C.x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) +>c : Symbol(c, Decl(structTypeOfThisInInstanceMember2.ts, 14, 3)) +>x : Symbol(C.x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) +>x : Symbol(C.x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) +>x : Symbol(C.x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) + +var r3 = c.foo(); +>r3 : Symbol(r3, Decl(structTypeOfThisInInstanceMember2.ts, 18, 3)) +>c.foo : Symbol(C.foo, Decl(structTypeOfThisInInstanceMember2.ts, 1, 13)) +>c : Symbol(c, Decl(structTypeOfThisInInstanceMember2.ts, 14, 3)) +>foo : Symbol(C.foo, Decl(structTypeOfThisInInstanceMember2.ts, 1, 13)) + +var r4 = c.z; +>r4 : Symbol(r4, Decl(structTypeOfThisInInstanceMember2.ts, 19, 3)) +>c.z : Symbol(C.z, Decl(structTypeOfThisInInstanceMember2.ts, 10, 5)) +>c : Symbol(c, Decl(structTypeOfThisInInstanceMember2.ts, 14, 3)) +>z : Symbol(C.z, Decl(structTypeOfThisInInstanceMember2.ts, 10, 5)) + +var rs = [r, r3]; +>rs : Symbol(rs, Decl(structTypeOfThisInInstanceMember2.ts, 20, 3)) +>r : Symbol(r, Decl(structTypeOfThisInInstanceMember2.ts, 16, 3)) +>r3 : Symbol(r3, Decl(structTypeOfThisInInstanceMember2.ts, 18, 3)) + +rs.forEach(x => { +>rs.forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --)) +>rs : Symbol(rs, Decl(structTypeOfThisInInstanceMember2.ts, 20, 3)) +>forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(structTypeOfThisInInstanceMember2.ts, 22, 11)) + + x.foo; +>x.foo : Symbol(C.foo, Decl(structTypeOfThisInInstanceMember2.ts, 1, 13)) +>x : Symbol(x, Decl(structTypeOfThisInInstanceMember2.ts, 22, 11)) +>foo : Symbol(C.foo, Decl(structTypeOfThisInInstanceMember2.ts, 1, 13)) + + x.x; +>x.x : Symbol(C.x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) +>x : Symbol(x, Decl(structTypeOfThisInInstanceMember2.ts, 22, 11)) +>x : Symbol(C.x, Decl(structTypeOfThisInInstanceMember2.ts, 0, 13)) + + x.z; +>x.z : Symbol(C.z, Decl(structTypeOfThisInInstanceMember2.ts, 10, 5)) +>x : Symbol(x, Decl(structTypeOfThisInInstanceMember2.ts, 22, 11)) +>z : Symbol(C.z, Decl(structTypeOfThisInInstanceMember2.ts, 10, 5)) + +}); diff --git a/tests/baselines/reference/structTypeOfThisInInstanceMember2.types b/tests/baselines/reference/structTypeOfThisInInstanceMember2.types new file mode 100644 index 0000000000000..f9c9ac3eb51e3 --- /dev/null +++ b/tests/baselines/reference/structTypeOfThisInInstanceMember2.types @@ -0,0 +1,109 @@ +=== tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember2.ts === +struct C { +>C : C +>T : T + + x = this; +>x : this +>this : this + + foo() { +>foo : () => this + + return this; +>this : this + } + constructor(x: T) { +>x : T +>T : T + + var t = this; +>t : this +>this : this + + t.x; +>t.x : this +>t : this +>x : this + + t.z; +>t.z : T +>t : this +>z : T + + var r = t.foo(); +>r : this +>t.foo() : this +>t.foo : () => this +>t : this +>foo : () => this + } + z: T; +>z : T +>T : T +} + +var c: C; +>c : C +>C : C + +// all ok +var r = c.x; +>r : C +>c.x : C +>c : C +>x : C + +var ra = c.x.x.x; +>ra : C +>c.x.x.x : C +>c.x.x : C +>c.x : C +>c : C +>x : C +>x : C +>x : C + +var r3 = c.foo(); +>r3 : C +>c.foo() : C +>c.foo : () => C +>c : C +>foo : () => C + +var r4 = c.z; +>r4 : string +>c.z : string +>c : C +>z : string + +var rs = [r, r3]; +>rs : C[] +>[r, r3] : C[] +>r : C +>r3 : C + +rs.forEach(x => { +>rs.forEach(x => { x.foo; x.x; x.z;}) : void +>rs.forEach : (callbackfn: (value: C, index: number, array: C[]) => void, thisArg?: any) => void +>rs : C[] +>forEach : (callbackfn: (value: C, index: number, array: C[]) => void, thisArg?: any) => void +>x => { x.foo; x.x; x.z;} : (x: C) => void +>x : C + + x.foo; +>x.foo : () => C +>x : C +>foo : () => C + + x.x; +>x.x : C +>x : C +>x : C + + x.z; +>x.z : string +>x : C +>z : string + +}); diff --git a/tests/baselines/reference/structTypeOfThisInStaticMembers.js b/tests/baselines/reference/structTypeOfThisInStaticMembers.js new file mode 100644 index 0000000000000..726ae9e7fa75d --- /dev/null +++ b/tests/baselines/reference/structTypeOfThisInStaticMembers.js @@ -0,0 +1,81 @@ +//// [structTypeOfThisInStaticMembers.ts] +// Within static member functions and static member accessors, the type of this is +// the constructor function type. + +struct C { + constructor(x: number) { } + static foo: number; + static bar() { + // type of this is the constructor function type + var t = this; + return this; + } +} + +var t = C.bar(); +// all ok +var r2 = t.foo + 1; +var r3 = t.bar(); +var r4 = new t(1); + +/* struct C2 { + static test: number; + constructor(x: string) { } + static foo: string; + static bar() { + // type of this is the constructor function type + var t = this; + return this; + } +} + +var t2 = C2.bar(); +// all ok +var r5 = t2.foo + 1; +var r6 = t2.bar(); +var r7 = new t2(''); */ + + + +//// [structTypeOfThisInStaticMembers.js] +// Within static member functions and static member accessors, the type of this is +// the constructor function type. +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor(x) { + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + _C.bar = function () { + // type of this is the constructor function type + var t = this; + return this; + }; + return C; +})(); +var t = C.bar(); +// all ok +var r2 = t.foo + 1; +var r3 = t.bar(); +var r4 = new t(1); +/* struct C2 { + static test: number; + constructor(x: string) { } + static foo: string; + static bar() { + // type of this is the constructor function type + var t = this; + return this; + } +} + +var t2 = C2.bar(); +// all ok +var r5 = t2.foo + 1; +var r6 = t2.bar(); +var r7 = new t2(''); */ diff --git a/tests/baselines/reference/structTypeOfThisInStaticMembers.symbols b/tests/baselines/reference/structTypeOfThisInStaticMembers.symbols new file mode 100644 index 0000000000000..00fa56dd02723 --- /dev/null +++ b/tests/baselines/reference/structTypeOfThisInStaticMembers.symbols @@ -0,0 +1,67 @@ +=== tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers.ts === +// Within static member functions and static member accessors, the type of this is +// the constructor function type. + +struct C { +>C : Symbol(C, Decl(structTypeOfThisInStaticMembers.ts, 0, 0)) + + constructor(x: number) { } +>x : Symbol(x, Decl(structTypeOfThisInStaticMembers.ts, 4, 16)) + + static foo: number; +>foo : Symbol(C.foo, Decl(structTypeOfThisInStaticMembers.ts, 4, 30)) + + static bar() { +>bar : Symbol(C.bar, Decl(structTypeOfThisInStaticMembers.ts, 5, 23)) + + // type of this is the constructor function type + var t = this; +>t : Symbol(t, Decl(structTypeOfThisInStaticMembers.ts, 8, 11)) +>this : Symbol(C, Decl(structTypeOfThisInStaticMembers.ts, 0, 0)) + + return this; +>this : Symbol(C, Decl(structTypeOfThisInStaticMembers.ts, 0, 0)) + } +} + +var t = C.bar(); +>t : Symbol(t, Decl(structTypeOfThisInStaticMembers.ts, 13, 3)) +>C.bar : Symbol(C.bar, Decl(structTypeOfThisInStaticMembers.ts, 5, 23)) +>C : Symbol(C, Decl(structTypeOfThisInStaticMembers.ts, 0, 0)) +>bar : Symbol(C.bar, Decl(structTypeOfThisInStaticMembers.ts, 5, 23)) + +// all ok +var r2 = t.foo + 1; +>r2 : Symbol(r2, Decl(structTypeOfThisInStaticMembers.ts, 15, 3)) +>t.foo : Symbol(C.foo, Decl(structTypeOfThisInStaticMembers.ts, 4, 30)) +>t : Symbol(t, Decl(structTypeOfThisInStaticMembers.ts, 13, 3)) +>foo : Symbol(C.foo, Decl(structTypeOfThisInStaticMembers.ts, 4, 30)) + +var r3 = t.bar(); +>r3 : Symbol(r3, Decl(structTypeOfThisInStaticMembers.ts, 16, 3)) +>t.bar : Symbol(C.bar, Decl(structTypeOfThisInStaticMembers.ts, 5, 23)) +>t : Symbol(t, Decl(structTypeOfThisInStaticMembers.ts, 13, 3)) +>bar : Symbol(C.bar, Decl(structTypeOfThisInStaticMembers.ts, 5, 23)) + +var r4 = new t(1); +>r4 : Symbol(r4, Decl(structTypeOfThisInStaticMembers.ts, 17, 3)) +>t : Symbol(t, Decl(structTypeOfThisInStaticMembers.ts, 13, 3)) + +/* struct C2 { + static test: number; + constructor(x: string) { } + static foo: string; + static bar() { + // type of this is the constructor function type + var t = this; + return this; + } +} + +var t2 = C2.bar(); +// all ok +var r5 = t2.foo + 1; +var r6 = t2.bar(); +var r7 = new t2(''); */ + + diff --git a/tests/baselines/reference/structTypeOfThisInStaticMembers.types b/tests/baselines/reference/structTypeOfThisInStaticMembers.types new file mode 100644 index 0000000000000..8eb055f7bd0c5 --- /dev/null +++ b/tests/baselines/reference/structTypeOfThisInStaticMembers.types @@ -0,0 +1,73 @@ +=== tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers.ts === +// Within static member functions and static member accessors, the type of this is +// the constructor function type. + +struct C { +>C : C + + constructor(x: number) { } +>x : number + + static foo: number; +>foo : number + + static bar() { +>bar : () => typeof C + + // type of this is the constructor function type + var t = this; +>t : typeof C +>this : typeof C + + return this; +>this : typeof C + } +} + +var t = C.bar(); +>t : typeof C +>C.bar() : typeof C +>C.bar : () => typeof C +>C : typeof C +>bar : () => typeof C + +// all ok +var r2 = t.foo + 1; +>r2 : number +>t.foo + 1 : number +>t.foo : number +>t : typeof C +>foo : number +>1 : number + +var r3 = t.bar(); +>r3 : typeof C +>t.bar() : typeof C +>t.bar : () => typeof C +>t : typeof C +>bar : () => typeof C + +var r4 = new t(1); +>r4 : C +>new t(1) : C +>t : typeof C +>1 : number + +/* struct C2 { + static test: number; + constructor(x: string) { } + static foo: string; + static bar() { + // type of this is the constructor function type + var t = this; + return this; + } +} + +var t2 = C2.bar(); +// all ok +var r5 = t2.foo + 1; +var r6 = t2.bar(); +var r7 = new t2(''); */ + + diff --git a/tests/baselines/reference/structTypeOfThisInStaticMembers2.errors.txt b/tests/baselines/reference/structTypeOfThisInStaticMembers2.errors.txt new file mode 100644 index 0000000000000..13e6e296f0783 --- /dev/null +++ b/tests/baselines/reference/structTypeOfThisInStaticMembers2.errors.txt @@ -0,0 +1,13 @@ +tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers2.ts(2,18): error TS2334: 'this' cannot be referenced in a static property initializer. + + +==== tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers2.ts (1 errors) ==== + struct C { + static foo = this; // error, 'this' cannot be referenced in a static property initializer. + ~~~~ +!!! error TS2334: 'this' cannot be referenced in a static property initializer. + } + + /* struct C2 { + static foo = this; // error + } */ \ No newline at end of file diff --git a/tests/baselines/reference/structTypeOfThisInStaticMembers2.js b/tests/baselines/reference/structTypeOfThisInStaticMembers2.js new file mode 100644 index 0000000000000..5759b36f98b59 --- /dev/null +++ b/tests/baselines/reference/structTypeOfThisInStaticMembers2.js @@ -0,0 +1,26 @@ +//// [structTypeOfThisInStaticMembers2.ts] +struct C { + static foo = this; // error, 'this' cannot be referenced in a static property initializer. +} + +/* struct C2 { + static foo = this; // error +} */ + +//// [structTypeOfThisInStaticMembers2.js] +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +/* struct C2 { + static foo = this; // error +} */ diff --git a/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember.ts b/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember.ts new file mode 100644 index 0000000000000..8e2be3f17cad1 --- /dev/null +++ b/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember.ts @@ -0,0 +1,27 @@ +// doc 2.1 +// Within constructors and instance member functions, the type of this is the instance type of the sturct. + +struct C { + x = this; + foo() { + return this; + } + constructor(x: number) { + var t = this; + t.x; + t.z; // error, z doesn't exist. + var r = t.foo(); + } +} + +var c: C; +// all ok +var r = c.x; +var ra = c.x.x.x; +var r3 = c.foo(); +var rs = [r, r3]; + +rs.forEach(x => { + x.foo; + x.x; +}); \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember2.ts b/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember2.ts new file mode 100644 index 0000000000000..088617d3f4f08 --- /dev/null +++ b/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInInstanceMember2.ts @@ -0,0 +1,27 @@ +struct C { + x = this; + foo() { + return this; + } + constructor(x: T) { + var t = this; + t.x; + t.z; + var r = t.foo(); + } + z: T; +} + +var c: C; +// all ok +var r = c.x; +var ra = c.x.x.x; +var r3 = c.foo(); +var r4 = c.z; +var rs = [r, r3]; + +rs.forEach(x => { + x.foo; + x.x; + x.z; +}); \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers.ts b/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers.ts new file mode 100644 index 0000000000000..568c0a9171e55 --- /dev/null +++ b/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers.ts @@ -0,0 +1,36 @@ +// Within static member functions and static member accessors, the type of this is +// the constructor function type. + +struct C { + constructor(x: number) { } + static foo: number; + static bar() { + // type of this is the constructor function type + var t = this; + return this; + } +} + +var t = C.bar(); +// all ok +var r2 = t.foo + 1; +var r3 = t.bar(); +var r4 = new t(1); + +/* struct C2 { + static test: number; + constructor(x: string) { } + static foo: string; + static bar() { + // type of this is the constructor function type + var t = this; + return this; + } +} + +var t2 = C2.bar(); +// all ok +var r5 = t2.foo + 1; +var r6 = t2.bar(); +var r7 = new t2(''); */ + diff --git a/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers2.ts b/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers2.ts new file mode 100644 index 0000000000000..20fa70286fe7f --- /dev/null +++ b/tests/cases/conformance/structs/members/instanceAndStaticMembers/structTypeOfThisInStaticMembers2.ts @@ -0,0 +1,7 @@ +struct C { + static foo = this; // error, 'this' cannot be referenced in a static property initializer. +} + +/* struct C2 { + static foo = this; // error +} */ \ No newline at end of file From 65fb76468fd9d178f7509fa04a78079990b2b4b9 Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 17:34:04 -0800 Subject: [PATCH 32/43] protected not allowed on struct members --- ...structPropertyNotAllowProtected.errors.txt | 25 +++++++++++++++ .../structPropertyNotAllowProtected.js | 31 +++++++++++++++++++ .../structPropertyNotAllowProtected.ts | 10 ++++++ 3 files changed, 66 insertions(+) create mode 100644 tests/baselines/reference/structPropertyNotAllowProtected.errors.txt create mode 100644 tests/baselines/reference/structPropertyNotAllowProtected.js create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyNotAllowProtected.ts diff --git a/tests/baselines/reference/structPropertyNotAllowProtected.errors.txt b/tests/baselines/reference/structPropertyNotAllowProtected.errors.txt new file mode 100644 index 0000000000000..d27d11dbb7d0b --- /dev/null +++ b/tests/baselines/reference/structPropertyNotAllowProtected.errors.txt @@ -0,0 +1,25 @@ +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyNotAllowProtected.ts(5,5): error TS20011: 'protected' modifier cannot appear on a struct element. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyNotAllowProtected.ts(6,5): error TS20011: 'protected' modifier cannot appear on a struct element. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyNotAllowProtected.ts(8,5): error TS20011: 'protected' modifier cannot appear on a struct element. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyNotAllowProtected.ts(9,5): error TS20011: 'protected' modifier cannot appear on a struct element. + + +==== tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyNotAllowProtected.ts (4 errors) ==== + // doc 2.2 + // no protected property members in struct. + + struct C { + protected x: string; // error + ~~~~~~~~~ +!!! error TS20011: 'protected' modifier cannot appear on a struct element. + protected foo() { } // error + ~~~~~~~~~ +!!! error TS20011: 'protected' modifier cannot appear on a struct element. + + protected static a: string; // error + ~~~~~~~~~ +!!! error TS20011: 'protected' modifier cannot appear on a struct element. + protected static foo() { } // error + ~~~~~~~~~ +!!! error TS20011: 'protected' modifier cannot appear on a struct element. + } \ No newline at end of file diff --git a/tests/baselines/reference/structPropertyNotAllowProtected.js b/tests/baselines/reference/structPropertyNotAllowProtected.js new file mode 100644 index 0000000000000..78f35ae2b34f5 --- /dev/null +++ b/tests/baselines/reference/structPropertyNotAllowProtected.js @@ -0,0 +1,31 @@ +//// [structPropertyNotAllowProtected.ts] +// doc 2.2 +// no protected property members in struct. + +struct C { + protected x: string; // error + protected foo() { } // error + + protected static a: string; // error + protected static foo() { } // error +} + +//// [structPropertyNotAllowProtected.js] +// doc 2.2 +// no protected property members in struct. +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.string, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.foo = function () { }; // error + _C.foo = function () { }; // error + return C; +})(); diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyNotAllowProtected.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyNotAllowProtected.ts new file mode 100644 index 0000000000000..4fa38f87727c9 --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyNotAllowProtected.ts @@ -0,0 +1,10 @@ +// doc 2.2 +// no protected property members in struct. + +struct C { + protected x: string; // error + protected foo() { } // error + + protected static a: string; // error + protected static foo() { } // error +} \ No newline at end of file From 7924ee96262627988c4a177ce5b6ce57510d290f Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 17:34:50 -0800 Subject: [PATCH 33/43] protected not allowed on struct members --- src/compiler/checker.ts | 3 +++ src/compiler/parser.ts | 17 +++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 53b28320810e4..3f84f0024c697 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16050,6 +16050,9 @@ getGlobalClassDecoratorType = memoize(() => getGlobalType("StructDecorator")); else if (node.parent.kind === SyntaxKind.ModuleBlock || node.parent.kind === SyntaxKind.SourceFile) { return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_module_element, text); } + else if (!isProtectedAllowedInStruct && (node.parent.kind === SyntaxKind.StructExpression || node.parent.kind === SyntaxKind.StructDeclaration)) { + return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_struct_element, text); + } else if (flags & NodeFlags.Abstract) { if (modifier.kind === SyntaxKind.PrivateKeyword) { return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, text, "abstract"); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index a0bdfce4a4ce8..5b0a2e5a3e156 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2,6 +2,9 @@ /// namespace ts { + // use for protected modifier in struct + export let isProtectedAllowedInStruct = false; + let nodeConstructors = new Array Node>(SyntaxKind.Count); /* @internal */ export let parseTime = 0; @@ -4898,10 +4901,12 @@ namespace ts { } function isStructMemberModifier(idToken: SyntaxKind) { + if ((idToken === SyntaxKind.ProtectedKeyword) && isProtectedAllowedInStruct) { + return true; + } switch (idToken) { case SyntaxKind.PublicKeyword: case SyntaxKind.PrivateKeyword: - // case SyntaxKind.ProtectedKeyword: case SyntaxKind.StaticKeyword: return true; default: @@ -5184,11 +5189,11 @@ namespace ts { node.typeParameters = parseTypeParameters(); node.heritageClauses = parseHeritageClauses(/*isStructHeritageClause*/ true); if (node.heritageClauses) { - for (let hc of node.heritageClauses) { - if (hc.token === SyntaxKind.ImplementsKeyword) { -parseErrorAtPosition(hc.pos, hc.end - hc.pos, Diagnostics.A_struct_can_not_implement_a_class_or_interface); - } - } + for (let hc of node.heritageClauses) { + if (hc.token === SyntaxKind.ImplementsKeyword) { + parseErrorAtPosition(hc.pos, hc.end - hc.pos, Diagnostics.A_struct_can_not_implement_a_class_or_interface); + } + } } if (parseExpected(SyntaxKind.OpenBraceToken)) { From 420998ddc1956d6c529f330cae18272ff6f05945 Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 19:19:24 -0800 Subject: [PATCH 34/43] struct private and public properties --- .../structPropertyAsPrivate.errors.txt | 33 +++++++++++++ .../reference/structPropertyAsPrivate.js | 45 ++++++++++++++++++ .../structPropertyIsPublicByDefault.js | 43 +++++++++++++++++ .../structPropertyIsPublicByDefault.symbols | 44 ++++++++++++++++++ .../structPropertyIsPublicByDefault.types | 46 +++++++++++++++++++ .../structPropertyAsPrivate.ts | 18 ++++++++ .../structPropertyIsPublicByDefault.ts | 17 +++++++ 7 files changed, 246 insertions(+) create mode 100644 tests/baselines/reference/structPropertyAsPrivate.errors.txt create mode 100644 tests/baselines/reference/structPropertyAsPrivate.js create mode 100644 tests/baselines/reference/structPropertyIsPublicByDefault.js create mode 100644 tests/baselines/reference/structPropertyIsPublicByDefault.symbols create mode 100644 tests/baselines/reference/structPropertyIsPublicByDefault.types create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyAsPrivate.ts create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyIsPublicByDefault.ts diff --git a/tests/baselines/reference/structPropertyAsPrivate.errors.txt b/tests/baselines/reference/structPropertyAsPrivate.errors.txt new file mode 100644 index 0000000000000..bc4517ac7cc6a --- /dev/null +++ b/tests/baselines/reference/structPropertyAsPrivate.errors.txt @@ -0,0 +1,33 @@ +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyAsPrivate.ts(14,1): error TS20018: Property 'x' is private and only accessible within struct 'C'. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyAsPrivate.ts(15,1): error TS20018: Property 'foo' is private and only accessible within struct 'C'. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyAsPrivate.ts(17,1): error TS20018: Property 'a' is private and only accessible within struct 'C'. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyAsPrivate.ts(18,1): error TS20018: Property 'foo' is private and only accessible within struct 'C'. + + +==== tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyAsPrivate.ts (4 errors) ==== + // doc 2.2 + // private property members can be accessed only within the struct body that contains their declaration + + struct C { + private x: string; + private foo() { } + + private static a: string; + private static foo() { } + } + + var c: C; + // all errors + c.x; + ~~~ +!!! error TS20018: Property 'x' is private and only accessible within struct 'C'. + c.foo(); + ~~~~~ +!!! error TS20018: Property 'foo' is private and only accessible within struct 'C'. + + C.a; + ~~~ +!!! error TS20018: Property 'a' is private and only accessible within struct 'C'. + C.foo(); + ~~~~~ +!!! error TS20018: Property 'foo' is private and only accessible within struct 'C'. \ No newline at end of file diff --git a/tests/baselines/reference/structPropertyAsPrivate.js b/tests/baselines/reference/structPropertyAsPrivate.js new file mode 100644 index 0000000000000..be53e1a4c185c --- /dev/null +++ b/tests/baselines/reference/structPropertyAsPrivate.js @@ -0,0 +1,45 @@ +//// [structPropertyAsPrivate.ts] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration + +struct C { + private x: string; + private foo() { } + + private static a: string; + private static foo() { } +} + +var c: C; +// all errors +c.x; +c.foo(); + +C.a; +C.foo(); + +//// [structPropertyAsPrivate.js] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.string, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.foo = function () { }; + _C.foo = function () { }; + return C; +})(); +var c; +// all errors +c.x; +c.foo(); +C.a; +C.foo(); diff --git a/tests/baselines/reference/structPropertyIsPublicByDefault.js b/tests/baselines/reference/structPropertyIsPublicByDefault.js new file mode 100644 index 0000000000000..f7877f5d72a17 --- /dev/null +++ b/tests/baselines/reference/structPropertyIsPublicByDefault.js @@ -0,0 +1,43 @@ +//// [structPropertyIsPublicByDefault.ts] +// doc 2.2 +// all ok + +struct C { + x: string; + foo() { } + + static a: string; + static foo() { } +} + +var c: C; +c.x; +c.foo(); + +C.a; +C.foo(); + +//// [structPropertyIsPublicByDefault.js] +// doc 2.2 +// all ok +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.string, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.foo = function () { }; + _C.foo = function () { }; + return C; +})(); +var c; +c.x; +c.foo(); +C.a; +C.foo(); diff --git a/tests/baselines/reference/structPropertyIsPublicByDefault.symbols b/tests/baselines/reference/structPropertyIsPublicByDefault.symbols new file mode 100644 index 0000000000000..369e1c66bc077 --- /dev/null +++ b/tests/baselines/reference/structPropertyIsPublicByDefault.symbols @@ -0,0 +1,44 @@ +=== tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyIsPublicByDefault.ts === +// doc 2.2 +// all ok + +struct C { +>C : Symbol(C, Decl(structPropertyIsPublicByDefault.ts, 0, 0)) + + x: string; +>x : Symbol(x, Decl(structPropertyIsPublicByDefault.ts, 3, 10)) + + foo() { } +>foo : Symbol(foo, Decl(structPropertyIsPublicByDefault.ts, 4, 14)) + + static a: string; +>a : Symbol(C.a, Decl(structPropertyIsPublicByDefault.ts, 5, 13)) + + static foo() { } +>foo : Symbol(C.foo, Decl(structPropertyIsPublicByDefault.ts, 7, 21)) +} + +var c: C; +>c : Symbol(c, Decl(structPropertyIsPublicByDefault.ts, 11, 3)) +>C : Symbol(C, Decl(structPropertyIsPublicByDefault.ts, 0, 0)) + +c.x; +>c.x : Symbol(C.x, Decl(structPropertyIsPublicByDefault.ts, 3, 10)) +>c : Symbol(c, Decl(structPropertyIsPublicByDefault.ts, 11, 3)) +>x : Symbol(C.x, Decl(structPropertyIsPublicByDefault.ts, 3, 10)) + +c.foo(); +>c.foo : Symbol(C.foo, Decl(structPropertyIsPublicByDefault.ts, 4, 14)) +>c : Symbol(c, Decl(structPropertyIsPublicByDefault.ts, 11, 3)) +>foo : Symbol(C.foo, Decl(structPropertyIsPublicByDefault.ts, 4, 14)) + +C.a; +>C.a : Symbol(C.a, Decl(structPropertyIsPublicByDefault.ts, 5, 13)) +>C : Symbol(C, Decl(structPropertyIsPublicByDefault.ts, 0, 0)) +>a : Symbol(C.a, Decl(structPropertyIsPublicByDefault.ts, 5, 13)) + +C.foo(); +>C.foo : Symbol(C.foo, Decl(structPropertyIsPublicByDefault.ts, 7, 21)) +>C : Symbol(C, Decl(structPropertyIsPublicByDefault.ts, 0, 0)) +>foo : Symbol(C.foo, Decl(structPropertyIsPublicByDefault.ts, 7, 21)) + diff --git a/tests/baselines/reference/structPropertyIsPublicByDefault.types b/tests/baselines/reference/structPropertyIsPublicByDefault.types new file mode 100644 index 0000000000000..01123e61d4dd1 --- /dev/null +++ b/tests/baselines/reference/structPropertyIsPublicByDefault.types @@ -0,0 +1,46 @@ +=== tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyIsPublicByDefault.ts === +// doc 2.2 +// all ok + +struct C { +>C : C + + x: string; +>x : string + + foo() { } +>foo : () => void + + static a: string; +>a : string + + static foo() { } +>foo : () => void +} + +var c: C; +>c : C +>C : C + +c.x; +>c.x : string +>c : C +>x : string + +c.foo(); +>c.foo() : void +>c.foo : () => void +>c : C +>foo : () => void + +C.a; +>C.a : string +>C : typeof C +>a : string + +C.foo(); +>C.foo() : void +>C.foo : () => void +>C : typeof C +>foo : () => void + diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyAsPrivate.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyAsPrivate.ts new file mode 100644 index 0000000000000..f5ede8e70c64f --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyAsPrivate.ts @@ -0,0 +1,18 @@ +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration + +struct C { + private x: string; + private foo() { } + + private static a: string; + private static foo() { } +} + +var c: C; +// all errors +c.x; +c.foo(); + +C.a; +C.foo(); \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyIsPublicByDefault.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyIsPublicByDefault.ts new file mode 100644 index 0000000000000..2c8cd909b474a --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPropertyIsPublicByDefault.ts @@ -0,0 +1,17 @@ +// doc 2.2 +// all ok + +struct C { + x: string; + foo() { } + + static a: string; + static foo() { } +} + +var c: C; +c.x; +c.foo(); + +C.a; +C.foo(); \ No newline at end of file From 1cf16abfc039ba4c13ea025c6b5a4d1b5990b25b Mon Sep 17 00:00:00 2001 From: jied Date: Fri, 6 Nov 2015 19:19:49 -0800 Subject: [PATCH 35/43] modifiers - only protected for struct --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3f84f0024c697..85d5d0c78fc4c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16050,7 +16050,7 @@ getGlobalClassDecoratorType = memoize(() => getGlobalType("StructDecorator")); else if (node.parent.kind === SyntaxKind.ModuleBlock || node.parent.kind === SyntaxKind.SourceFile) { return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_module_element, text); } - else if (!isProtectedAllowedInStruct && (node.parent.kind === SyntaxKind.StructExpression || node.parent.kind === SyntaxKind.StructDeclaration)) { + else if (modifier.kind === SyntaxKind.ProtectedKeyword && !isProtectedAllowedInStruct && (node.parent.kind === SyntaxKind.StructExpression || node.parent.kind === SyntaxKind.StructDeclaration)) { return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_struct_element, text); } else if (flags & NodeFlags.Abstract) { From a11f0ecfd3e964df926d46b49e0473da92cb90ca Mon Sep 17 00:00:00 2001 From: jied Date: Sat, 7 Nov 2015 00:26:02 -0800 Subject: [PATCH 36/43] struct private members --- ...vateInstanceMemberAccessibility.errors.txt | 36 +++++++++ ...tructPrivateInstanceMemberAccessibility.js | 62 ++++++++++++++++ ...ctPrivatePropertyAccessibleWithinStruct.js | 73 +++++++++++++++++++ ...vatePropertyAccessibleWithinStruct.symbols | 62 ++++++++++++++++ ...rivatePropertyAccessibleWithinStruct.types | 67 +++++++++++++++++ ...rivateStaticMemberAccessibility.errors.txt | 17 +++++ .../structPrivateStaticMemberAccessibility.js | 48 ++++++++++++ ...vateStaticNotAccessibleInModule.errors.txt | 17 +++++ ...tructPrivateStaticNotAccessibleInModule.js | 34 +++++++++ ...ateStaticNotAccessibleInModule2.errors.txt | 21 ++++++ ...ructPrivateStaticNotAccessibleInModule2.js | 57 +++++++++++++++ ...tructPrivateInstanceMemberAccessibility.ts | 18 +++++ ...ctPrivatePropertyAccessibleWithinStruct.ts | 22 ++++++ .../structPrivateStaticMemberAccessibility.ts | 11 +++ ...tructPrivateStaticNotAccessibleInModule.ts | 11 +++ ...ructPrivateStaticNotAccessibleInModule2.ts | 15 ++++ 16 files changed, 571 insertions(+) create mode 100644 tests/baselines/reference/structPrivateInstanceMemberAccessibility.errors.txt create mode 100644 tests/baselines/reference/structPrivateInstanceMemberAccessibility.js create mode 100644 tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.js create mode 100644 tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.symbols create mode 100644 tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.types create mode 100644 tests/baselines/reference/structPrivateStaticMemberAccessibility.errors.txt create mode 100644 tests/baselines/reference/structPrivateStaticMemberAccessibility.js create mode 100644 tests/baselines/reference/structPrivateStaticNotAccessibleInModule.errors.txt create mode 100644 tests/baselines/reference/structPrivateStaticNotAccessibleInModule.js create mode 100644 tests/baselines/reference/structPrivateStaticNotAccessibleInModule2.errors.txt create mode 100644 tests/baselines/reference/structPrivateStaticNotAccessibleInModule2.js create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateInstanceMemberAccessibility.ts create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivatePropertyAccessibleWithinStruct.ts create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticMemberAccessibility.ts create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule.ts create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule2.ts diff --git a/tests/baselines/reference/structPrivateInstanceMemberAccessibility.errors.txt b/tests/baselines/reference/structPrivateInstanceMemberAccessibility.errors.txt new file mode 100644 index 0000000000000..dd3b8e983a2ff --- /dev/null +++ b/tests/baselines/reference/structPrivateInstanceMemberAccessibility.errors.txt @@ -0,0 +1,36 @@ +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateInstanceMemberAccessibility.ts(10,15): error TS20017: Only public methods of the base struct are accessible via the 'super' keyword. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateInstanceMemberAccessibility.ts(13,22): error TS20017: Only public methods of the base struct are accessible via the 'super' keyword. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateInstanceMemberAccessibility.ts(15,15): error TS1003: Identifier expected. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateInstanceMemberAccessibility.ts(15,21): error TS20017: Only public methods of the base struct are accessible via the 'super' keyword. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateInstanceMemberAccessibility.ts(17,12): error TS1005: ';' expected. + + +==== tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateInstanceMemberAccessibility.ts (5 errors) ==== + // doc 2.2 + // private property members can be accessed only within the struct body that contains their declaration + + struct Base { + private foo: string; + } + + struct Derived extends Base { + // error, Only public methods of the base class are accessible via the 'super' keyword + x = super.foo; + ~~~ +!!! error TS20017: Only public methods of the base struct are accessible via the 'super' keyword. + + y() { + return super.foo; // error + ~~~ +!!! error TS20017: Only public methods of the base struct are accessible via the 'super' keyword. + } + z: typeof super.foo; // error + ~~~~~ +!!! error TS1003: Identifier expected. + ~~~ +!!! error TS20017: Only public methods of the base struct are accessible via the 'super' keyword. + + a: this.foo; // error + ~ +!!! error TS1005: ';' expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/structPrivateInstanceMemberAccessibility.js b/tests/baselines/reference/structPrivateInstanceMemberAccessibility.js new file mode 100644 index 0000000000000..e7feb1253d9f9 --- /dev/null +++ b/tests/baselines/reference/structPrivateInstanceMemberAccessibility.js @@ -0,0 +1,62 @@ +//// [structPrivateInstanceMemberAccessibility.ts] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration + +struct Base { + private foo: string; +} + +struct Derived extends Base { + // error, Only public methods of the base class are accessible via the 'super' keyword + x = super.foo; + + y() { + return super.foo; // error + } + z: typeof super.foo; // error + + a: this.foo; // error +} + +//// [structPrivateInstanceMemberAccessibility.js] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + x: TypedObject.string, + z: TypedObject.Any, + a: TypedObject.Object, + foo: TypedObject.Any + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + _Derived.prototype.y = function () { + return _super.prototype.foo; // error + }; + return Derived; +})(); diff --git a/tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.js b/tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.js new file mode 100644 index 0000000000000..c5feeb255b69e --- /dev/null +++ b/tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.js @@ -0,0 +1,73 @@ +//// [structPrivatePropertyAccessibleWithinStruct.ts] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration +// no errors + +struct C { + private x: string; + private foo() { return this.foo; } + + private static x: string; + private static foo() { return this.foo; } + private static bar() { this.foo(); } +} + +// added level of function nesting +struct C2 { + private x: string; + private foo() { () => this.foo; } + + private static x: string; + private static foo() { () => this.foo; } + private static bar() { () => this.foo(); } +} + + +//// [structPrivatePropertyAccessibleWithinStruct.js] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration +// no errors +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.string, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.foo = function () { return this.foo; }; + _C.foo = function () { return this.foo; }; + _C.bar = function () { this.foo(); }; + return C; +})(); +// added level of function nesting +var C2 = (function () { + var _C2 = new TypedObject.StructType({ + x: TypedObject.string, + }); + function _ctor() { + } + function C2() { + var obj = new _C2(); + _ctor.call(obj); + return obj; + } + C2._TO = _C2; + _C2.prototype.foo = function () { + var _this = this; + (function () { return _this.foo; }); + }; + _C2.foo = function () { + var _this = this; + (function () { return _this.foo; }); + }; + _C2.bar = function () { + var _this = this; + (function () { return _this.foo(); }); + }; + return C2; +})(); diff --git a/tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.symbols b/tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.symbols new file mode 100644 index 0000000000000..8d7516b35961f --- /dev/null +++ b/tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.symbols @@ -0,0 +1,62 @@ +=== tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivatePropertyAccessibleWithinStruct.ts === +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration +// no errors + +struct C { +>C : Symbol(C, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 0, 0)) + + private x: string; +>x : Symbol(x, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 4, 10)) + + private foo() { return this.foo; } +>foo : Symbol(foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 5, 22)) +>this.foo : Symbol(foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 5, 22)) +>this : Symbol(C, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 0, 0)) +>foo : Symbol(foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 5, 22)) + + private static x: string; +>x : Symbol(C.x, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 6, 38)) + + private static foo() { return this.foo; } +>foo : Symbol(C.foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 8, 29)) +>this.foo : Symbol(C.foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 8, 29)) +>this : Symbol(C, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 0, 0)) +>foo : Symbol(C.foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 8, 29)) + + private static bar() { this.foo(); } +>bar : Symbol(C.bar, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 9, 45)) +>this.foo : Symbol(C.foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 8, 29)) +>this : Symbol(C, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 0, 0)) +>foo : Symbol(C.foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 8, 29)) +} + +// added level of function nesting +struct C2 { +>C2 : Symbol(C2, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 11, 1)) + + private x: string; +>x : Symbol(x, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 14, 11)) + + private foo() { () => this.foo; } +>foo : Symbol(foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 15, 22)) +>this.foo : Symbol(foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 15, 22)) +>this : Symbol(C2, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 11, 1)) +>foo : Symbol(foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 15, 22)) + + private static x: string; +>x : Symbol(C2.x, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 16, 37)) + + private static foo() { () => this.foo; } +>foo : Symbol(C2.foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 18, 29)) +>this.foo : Symbol(C2.foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 18, 29)) +>this : Symbol(C2, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 11, 1)) +>foo : Symbol(C2.foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 18, 29)) + + private static bar() { () => this.foo(); } +>bar : Symbol(C2.bar, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 19, 44)) +>this.foo : Symbol(C2.foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 18, 29)) +>this : Symbol(C2, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 11, 1)) +>foo : Symbol(C2.foo, Decl(structPrivatePropertyAccessibleWithinStruct.ts, 18, 29)) +} + diff --git a/tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.types b/tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.types new file mode 100644 index 0000000000000..6e735283f7d6b --- /dev/null +++ b/tests/baselines/reference/structPrivatePropertyAccessibleWithinStruct.types @@ -0,0 +1,67 @@ +=== tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivatePropertyAccessibleWithinStruct.ts === +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration +// no errors + +struct C { +>C : C + + private x: string; +>x : string + + private foo() { return this.foo; } +>foo : () => any +>this.foo : () => any +>this : this +>foo : () => any + + private static x: string; +>x : string + + private static foo() { return this.foo; } +>foo : () => typeof C.foo +>this.foo : () => typeof C.foo +>this : typeof C +>foo : () => typeof C.foo + + private static bar() { this.foo(); } +>bar : () => void +>this.foo() : () => typeof C.foo +>this.foo : () => typeof C.foo +>this : typeof C +>foo : () => typeof C.foo +} + +// added level of function nesting +struct C2 { +>C2 : C2 + + private x: string; +>x : string + + private foo() { () => this.foo; } +>foo : () => void +>() => this.foo : () => () => void +>this.foo : () => void +>this : this +>foo : () => void + + private static x: string; +>x : string + + private static foo() { () => this.foo; } +>foo : () => void +>() => this.foo : () => () => void +>this.foo : () => void +>this : typeof C2 +>foo : () => void + + private static bar() { () => this.foo(); } +>bar : () => void +>() => this.foo() : () => void +>this.foo() : void +>this.foo : () => void +>this : typeof C2 +>foo : () => void +} + diff --git a/tests/baselines/reference/structPrivateStaticMemberAccessibility.errors.txt b/tests/baselines/reference/structPrivateStaticMemberAccessibility.errors.txt new file mode 100644 index 0000000000000..376fc4d222f25 --- /dev/null +++ b/tests/baselines/reference/structPrivateStaticMemberAccessibility.errors.txt @@ -0,0 +1,17 @@ +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticMemberAccessibility.ts(9,18): error TS20018: Property 'foo' is private and only accessible within struct 'Base'. + + +==== tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticMemberAccessibility.ts (1 errors) ==== + // doc 2.2 + // private property members can be accessed only within the struct body that contains their declaration + + struct Base { + private static foo: string; + } + + struct Derived extends Base { + static bar = Base.foo; // error + ~~~~~~~~ +!!! error TS20018: Property 'foo' is private and only accessible within struct 'Base'. + bing = () => Base.foo; // error + } \ No newline at end of file diff --git a/tests/baselines/reference/structPrivateStaticMemberAccessibility.js b/tests/baselines/reference/structPrivateStaticMemberAccessibility.js new file mode 100644 index 0000000000000..e46595487b7cb --- /dev/null +++ b/tests/baselines/reference/structPrivateStaticMemberAccessibility.js @@ -0,0 +1,48 @@ +//// [structPrivateStaticMemberAccessibility.ts] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration + +struct Base { + private static foo: string; +} + +struct Derived extends Base { + static bar = Base.foo; // error + bing = () => Base.foo; // error +} + +//// [structPrivateStaticMemberAccessibility.js] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + bing: TypedObject.Object + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); diff --git a/tests/baselines/reference/structPrivateStaticNotAccessibleInModule.errors.txt b/tests/baselines/reference/structPrivateStaticNotAccessibleInModule.errors.txt new file mode 100644 index 0000000000000..f0c8b62231374 --- /dev/null +++ b/tests/baselines/reference/structPrivateStaticNotAccessibleInModule.errors.txt @@ -0,0 +1,17 @@ +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule.ts(10,20): error TS20018: Property 'bar' is private and only accessible within struct 'C'. + + +==== tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule.ts (1 errors) ==== + // doc 2.2 + // private property members can be accessed only within the struct body that contains their declaration + + struct C { + private foo: string; + private static bar: string; + } + + module C { + export var y = C.bar; // error + ~~~~~ +!!! error TS20018: Property 'bar' is private and only accessible within struct 'C'. + } \ No newline at end of file diff --git a/tests/baselines/reference/structPrivateStaticNotAccessibleInModule.js b/tests/baselines/reference/structPrivateStaticNotAccessibleInModule.js new file mode 100644 index 0000000000000..cd7fc4bee80f5 --- /dev/null +++ b/tests/baselines/reference/structPrivateStaticNotAccessibleInModule.js @@ -0,0 +1,34 @@ +//// [structPrivateStaticNotAccessibleInModule.ts] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration + +struct C { + private foo: string; + private static bar: string; +} + +module C { + export var y = C.bar; // error +} + +//// [structPrivateStaticNotAccessibleInModule.js] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var C; +(function (C) { + C.y = C.bar; // error +})(C || (C = {})); diff --git a/tests/baselines/reference/structPrivateStaticNotAccessibleInModule2.errors.txt b/tests/baselines/reference/structPrivateStaticNotAccessibleInModule2.errors.txt new file mode 100644 index 0000000000000..506b3a252a0cd --- /dev/null +++ b/tests/baselines/reference/structPrivateStaticNotAccessibleInModule2.errors.txt @@ -0,0 +1,21 @@ +tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule2.ts(14,20): error TS20018: Property 'bar' is private and only accessible within struct 'C'. + + +==== tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule2.ts (1 errors) ==== + // doc 2.2 + // private property members can be accessed only within the struct body that contains their declaration + + struct C { + private foo: string; + private static bar: string; + } + + struct D extends C { + baz: number; + } + + module D { + export var y = D.bar; // error + ~~~~~ +!!! error TS20018: Property 'bar' is private and only accessible within struct 'C'. + } \ No newline at end of file diff --git a/tests/baselines/reference/structPrivateStaticNotAccessibleInModule2.js b/tests/baselines/reference/structPrivateStaticNotAccessibleInModule2.js new file mode 100644 index 0000000000000..4741ed2dae103 --- /dev/null +++ b/tests/baselines/reference/structPrivateStaticNotAccessibleInModule2.js @@ -0,0 +1,57 @@ +//// [structPrivateStaticNotAccessibleInModule2.ts] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration + +struct C { + private foo: string; + private static bar: string; +} + +struct D extends C { + baz: number; +} + +module D { + export var y = D.bar; // error +} + +//// [structPrivateStaticNotAccessibleInModule2.js] +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + baz: TypedObject.float64 + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); +var D; +(function (D) { + D.y = D.bar; // error +})(D || (D = {})); diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateInstanceMemberAccessibility.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateInstanceMemberAccessibility.ts new file mode 100644 index 0000000000000..88e40fcbe0f22 --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateInstanceMemberAccessibility.ts @@ -0,0 +1,18 @@ +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration + +struct Base { + private foo: string; +} + +struct Derived extends Base { + // error, Only public methods of the base class are accessible via the 'super' keyword + x = super.foo; + + y() { + return super.foo; // error + } + z: typeof super.foo; // error + + a: this.foo; // error +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivatePropertyAccessibleWithinStruct.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivatePropertyAccessibleWithinStruct.ts new file mode 100644 index 0000000000000..9788e69589504 --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivatePropertyAccessibleWithinStruct.ts @@ -0,0 +1,22 @@ +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration +// no errors + +struct C { + private x: string; + private foo() { return this.foo; } + + private static x: string; + private static foo() { return this.foo; } + private static bar() { this.foo(); } +} + +// added level of function nesting +struct C2 { + private x: string; + private foo() { () => this.foo; } + + private static x: string; + private static foo() { () => this.foo; } + private static bar() { () => this.foo(); } +} diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticMemberAccessibility.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticMemberAccessibility.ts new file mode 100644 index 0000000000000..c72979a89f5f0 --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticMemberAccessibility.ts @@ -0,0 +1,11 @@ +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration + +struct Base { + private static foo: string; +} + +struct Derived extends Base { + static bar = Base.foo; // error + bing = () => Base.foo; // error +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule.ts new file mode 100644 index 0000000000000..0d1c95e9a9b95 --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule.ts @@ -0,0 +1,11 @@ +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration + +struct C { + private foo: string; + private static bar: string; +} + +module C { + export var y = C.bar; // error +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule2.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule2.ts new file mode 100644 index 0000000000000..2af9a7c5eadd2 --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structPrivateStaticNotAccessibleInModule2.ts @@ -0,0 +1,15 @@ +// doc 2.2 +// private property members can be accessed only within the struct body that contains their declaration + +struct C { + private foo: string; + private static bar: string; +} + +struct D extends C { + baz: number; +} + +module D { + export var y = D.bar; // error +} \ No newline at end of file From 19db1ca95a81935592e41bf2c3c9b77ecd25337e Mon Sep 17 00:00:00 2001 From: jied Date: Tue, 24 Nov 2015 21:25:37 -0800 Subject: [PATCH 37/43] struct accessors and accessibility --- src/compiler/checker.ts | 5 ++- src/compiler/parser.ts | 36 ++++++++++------ .../structBodyWithStatements.errors.txt | 8 ++-- ...ctNotAllowInstanceMemberGetters.errors.txt | 31 +++++++++++++ .../structNotAllowInstanceMemberGetters.js | 34 +++++++++++++++ ...ctNotAllowInstanceMemberSetters.errors.txt | 43 +++++++++++++++++++ .../structNotAllowInstanceMemberSetters.js | 34 +++++++++++++++ ...ructNotAllowStaticMemberGetters.errors.txt | 20 +++++++++ .../structNotAllowStaticMemberGetters.js | 35 +++++++++++++++ ...ructNotAllowStaticMemberSetters.errors.txt | 20 +++++++++ .../structNotAllowStaticMemberSetters.js | 35 +++++++++++++++ .../structNotAllowInstanceMemberGetters.ts | 10 +++++ .../structNotAllowInstanceMemberSetters.ts | 10 +++++ .../structNotAllowStaticMemberGetters.ts | 10 +++++ .../structNotAllowStaticMemberSetters.ts | 10 +++++ 15 files changed, 321 insertions(+), 20 deletions(-) create mode 100644 tests/baselines/reference/structNotAllowInstanceMemberGetters.errors.txt create mode 100644 tests/baselines/reference/structNotAllowInstanceMemberGetters.js create mode 100644 tests/baselines/reference/structNotAllowInstanceMemberSetters.errors.txt create mode 100644 tests/baselines/reference/structNotAllowInstanceMemberSetters.js create mode 100644 tests/baselines/reference/structNotAllowStaticMemberGetters.errors.txt create mode 100644 tests/baselines/reference/structNotAllowStaticMemberGetters.js create mode 100644 tests/baselines/reference/structNotAllowStaticMemberSetters.errors.txt create mode 100644 tests/baselines/reference/structNotAllowStaticMemberSetters.js create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberGetters.ts create mode 100644 tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberSetters.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 85d5d0c78fc4c..546b9e4749c57 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8327,7 +8327,7 @@ parentSymbol = (declaration.parent).symbol; getNodeLinks(node).resolvedSymbol = prop; - if (prop.parent && prop.parent.flags & (SymbolFlags.Class | SymbolFlags.Struct)) { + if (prop.parent && (prop.parent.flags & (SymbolFlags.Class | SymbolFlags.Struct))) { checkClassOrStructPropertyAccess(node, left, apparentType, prop); } return getTypeOfSymbol(prop); @@ -8341,7 +8341,7 @@ parentSymbol = (declaration.parent).symbol; let type = checkExpression(left); if (type !== unknownType && !isTypeAny(type)) { let prop = getPropertyOfType(getWidenedType(type), propertyName); - if (prop && prop.parent && prop.parent.flags & (SymbolFlags.Class | SymbolFlags.Struct)) { + if (prop && prop.parent && (prop.parent.flags & (SymbolFlags.Class | SymbolFlags.Struct))) { return checkClassOrStructPropertyAccess(node, left, type, prop); } } @@ -16167,6 +16167,7 @@ return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_s flags |= NodeFlags.Async; lastAsync = modifier; break; + } } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 5b0a2e5a3e156..c20a72804edc3 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3,7 +3,7 @@ namespace ts { // use for protected modifier in struct - export let isProtectedAllowedInStruct = false; +export let isProtectedAllowedInStruct = false; let nodeConstructors = new Array Node>(SyntaxKind.Count); /* @internal */ export let parseTime = 0; @@ -1748,7 +1748,7 @@ namespace ts { case ParsingContext.SwitchClauseStatements: return Diagnostics.Statement_expected; case ParsingContext.TypeMembers: return Diagnostics.Property_or_signature_expected; case ParsingContext.ClassMembers: return Diagnostics.Unexpected_token_A_constructor_method_accessor_or_property_was_expected; - case ParsingContext.StructMembers: return Diagnostics.Unexpected_token_A_constructor_method_accessor_or_property_was_expected; + case ParsingContext.StructMembers: return Diagnostics.Unexpected_token_A_constructor_method_or_property_was_expected; case ParsingContext.EnumMembers: return Diagnostics.Enum_member_expected; case ParsingContext.HeritageClauseElement: return Diagnostics.Expression_expected; case ParsingContext.VariableDeclarations: return Diagnostics.Variable_declaration_expected; @@ -4901,9 +4901,9 @@ namespace ts { } function isStructMemberModifier(idToken: SyntaxKind) { - if ((idToken === SyntaxKind.ProtectedKeyword) && isProtectedAllowedInStruct) { - return true; - } + if ((idToken === SyntaxKind.ProtectedKeyword) && isProtectedAllowedInStruct) { +return true; + } switch (idToken) { case SyntaxKind.PublicKeyword: case SyntaxKind.PrivateKeyword: @@ -4961,6 +4961,8 @@ namespace ts { return true; } + + // If it *is* a keyword, but not an accessor, check a little farther along // to see if it should actually be parsed as a struct member. switch (token) { @@ -5100,14 +5102,20 @@ namespace ts { let decorators = parseDecorators(); let modifiers = parseModifiers(); + let accessor = tryParseAccessorDeclaration(fullStart, decorators, modifiers); + if (accessor) { + parseErrorAtPosition(accessor.pos, accessor.end - accessor.pos, Diagnostics.Unexpected_token_A_constructor_method_or_property_was_expected); + return accessor; + } + if (token === SyntaxKind.ConstructorKeyword) { return parseConstructorDeclaration(fullStart, decorators, modifiers); } - // index signature not allowed in struct + /* index signature not allowed in struct if (token === SyntaxKind.OpenBracketToken) { - } + } */ if (isIndexSignature()) { return parseIndexSignatureDeclaration(fullStart, decorators, modifiers); @@ -5188,13 +5196,13 @@ namespace ts { node.name = parseNameOfStructDeclarationOrExpression(); node.typeParameters = parseTypeParameters(); node.heritageClauses = parseHeritageClauses(/*isStructHeritageClause*/ true); - if (node.heritageClauses) { - for (let hc of node.heritageClauses) { - if (hc.token === SyntaxKind.ImplementsKeyword) { - parseErrorAtPosition(hc.pos, hc.end - hc.pos, Diagnostics.A_struct_can_not_implement_a_class_or_interface); - } - } - } + if (node.heritageClauses) { + for (let hc of node.heritageClauses) { + if (hc.token === SyntaxKind.ImplementsKeyword) { +parseErrorAtPosition(hc.pos, hc.end - hc.pos, Diagnostics.A_struct_can_not_implement_a_class_or_interface); + } + } + } if (parseExpected(SyntaxKind.OpenBraceToken)) { // StructTail[Yield,Await] : (Modified) See 14.5 diff --git a/tests/baselines/reference/structBodyWithStatements.errors.txt b/tests/baselines/reference/structBodyWithStatements.errors.txt index b49d56b1b6068..cd9dd0b35b5f1 100644 --- a/tests/baselines/reference/structBodyWithStatements.errors.txt +++ b/tests/baselines/reference/structBodyWithStatements.errors.txt @@ -1,6 +1,6 @@ -tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts(2,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected. +tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts(2,5): error TS20025: Unexpected token. A constructor, method, or property was expected. tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts(3,1): error TS1128: Declaration or statement expected. -tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts(6,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected. +tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts(6,5): error TS20025: Unexpected token. A constructor, method, or property was expected. tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStatements.ts(7,1): error TS1128: Declaration or statement expected. @@ -8,7 +8,7 @@ tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStat struct C { var x = 1; // error, variable statement ~~~ -!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected. +!!! error TS20025: Unexpected token. A constructor, method, or property was expected. } ~ !!! error TS1128: Declaration or statement expected. @@ -16,7 +16,7 @@ tests/cases/conformance/structs/structDeclarations/structBody/structBodyWithStat struct C2 { function foo() {} // error, function declaration ~~~~~~~~ -!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected. +!!! error TS20025: Unexpected token. A constructor, method, or property was expected. } ~ !!! error TS1128: Declaration or statement expected. diff --git a/tests/baselines/reference/structNotAllowInstanceMemberGetters.errors.txt b/tests/baselines/reference/structNotAllowInstanceMemberGetters.errors.txt new file mode 100644 index 0000000000000..b3202817f0123 --- /dev/null +++ b/tests/baselines/reference/structNotAllowInstanceMemberGetters.errors.txt @@ -0,0 +1,31 @@ +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts(7,5): error TS2304: Cannot find name 'get'. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts(7,5): error TS20025: Unexpected token. A constructor, method, or property was expected. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts(7,9): error TS1005: ';' expected. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts(7,9): error TS2304: Cannot find name 'foo'. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts(7,15): error TS1005: ';' expected. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts(10,1): error TS1128: Declaration or statement expected. + + +==== tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts (6 errors) ==== + // doc 2.2 + // Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + + struct C { + private _foo: string; + + get foo() { // error, getters are not allowed in struct + ~~~ +!!! error TS2304: Cannot find name 'get'. + ~~~ +!!! error TS20025: Unexpected token. A constructor, method, or property was expected. + ~~~ +!!! error TS1005: ';' expected. + ~~~ +!!! error TS2304: Cannot find name 'foo'. + ~ +!!! error TS1005: ';' expected. + return this._foo; + } + } + ~ +!!! error TS1128: Declaration or statement expected. \ No newline at end of file diff --git a/tests/baselines/reference/structNotAllowInstanceMemberGetters.js b/tests/baselines/reference/structNotAllowInstanceMemberGetters.js new file mode 100644 index 0000000000000..4c4078caad895 --- /dev/null +++ b/tests/baselines/reference/structNotAllowInstanceMemberGetters.js @@ -0,0 +1,34 @@ +//// [structNotAllowInstanceMemberGetters.ts] +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + +struct C { + private _foo: string; + + get foo() { // error, getters are not allowed in struct + return this._foo; + } +} + +//// [structNotAllowInstanceMemberGetters.js] +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. +var C = (function () { + var _C = new TypedObject.StructType({ + _foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +get; +foo(); +{ + return this._foo; +} diff --git a/tests/baselines/reference/structNotAllowInstanceMemberSetters.errors.txt b/tests/baselines/reference/structNotAllowInstanceMemberSetters.errors.txt new file mode 100644 index 0000000000000..934cba17efbdc --- /dev/null +++ b/tests/baselines/reference/structNotAllowInstanceMemberSetters.errors.txt @@ -0,0 +1,43 @@ +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts(7,5): error TS2304: Cannot find name 'set'. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts(7,5): error TS20025: Unexpected token. A constructor, method, or property was expected. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts(7,9): error TS1005: ';' expected. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts(7,9): error TS2304: Cannot find name 'foo'. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts(7,13): error TS2304: Cannot find name 'new_foo'. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts(7,20): error TS1005: ',' expected. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts(7,22): error TS2304: Cannot find name 'string'. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts(7,30): error TS1005: ';' expected. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts(8,21): error TS2304: Cannot find name 'new_foo'. +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts(10,1): error TS1128: Declaration or statement expected. + + +==== tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts (10 errors) ==== + // doc 2.2 + // Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + + struct C { + private _foo: string; + + set foo(new_foo: string) { // error, setters are not allowed in struct + ~~~ +!!! error TS2304: Cannot find name 'set'. + ~~~ +!!! error TS20025: Unexpected token. A constructor, method, or property was expected. + ~~~ +!!! error TS1005: ';' expected. + ~~~ +!!! error TS2304: Cannot find name 'foo'. + ~~~~~~~ +!!! error TS2304: Cannot find name 'new_foo'. + ~ +!!! error TS1005: ',' expected. + ~~~~~~ +!!! error TS2304: Cannot find name 'string'. + ~ +!!! error TS1005: ';' expected. + this._foo = new_foo; + ~~~~~~~ +!!! error TS2304: Cannot find name 'new_foo'. + } + } + ~ +!!! error TS1128: Declaration or statement expected. \ No newline at end of file diff --git a/tests/baselines/reference/structNotAllowInstanceMemberSetters.js b/tests/baselines/reference/structNotAllowInstanceMemberSetters.js new file mode 100644 index 0000000000000..4670bd5b65ece --- /dev/null +++ b/tests/baselines/reference/structNotAllowInstanceMemberSetters.js @@ -0,0 +1,34 @@ +//// [structNotAllowInstanceMemberSetters.ts] +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + +struct C { + private _foo: string; + + set foo(new_foo: string) { // error, setters are not allowed in struct + this._foo = new_foo; + } +} + +//// [structNotAllowInstanceMemberSetters.js] +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. +var C = (function () { + var _C = new TypedObject.StructType({ + _foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +set; +foo(new_foo, string); +{ + this._foo = new_foo; +} diff --git a/tests/baselines/reference/structNotAllowStaticMemberGetters.errors.txt b/tests/baselines/reference/structNotAllowStaticMemberGetters.errors.txt new file mode 100644 index 0000000000000..48cd253351cb1 --- /dev/null +++ b/tests/baselines/reference/structNotAllowStaticMemberGetters.errors.txt @@ -0,0 +1,20 @@ +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberGetters.ts(5,22): error TS20025: Unexpected token. A constructor, method, or property was expected. + + +==== tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberGetters.ts (1 errors) ==== + // doc 2.2 + // Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + + struct C { + static _bar: number; + + + + static get bar() { // error, getters are not allowed in struct + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + return C._bar; + ~~~~~~~~~~~~~~~~ + } + ~~ +!!! error TS20025: Unexpected token. A constructor, method, or property was expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/structNotAllowStaticMemberGetters.js b/tests/baselines/reference/structNotAllowStaticMemberGetters.js new file mode 100644 index 0000000000000..4a852f5ded6ef --- /dev/null +++ b/tests/baselines/reference/structNotAllowStaticMemberGetters.js @@ -0,0 +1,35 @@ +//// [structNotAllowStaticMemberGetters.ts] +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + +struct C { + static _bar: number; + + static get bar() { // error, getters are not allowed in struct + return C._bar; + } +} + +//// [structNotAllowStaticMemberGetters.js] +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + Object.defineProperty(C, "bar", { + get: function () { + return C._bar; + }, + enumerable: true, + configurable: true + }); + return C; +})(); diff --git a/tests/baselines/reference/structNotAllowStaticMemberSetters.errors.txt b/tests/baselines/reference/structNotAllowStaticMemberSetters.errors.txt new file mode 100644 index 0000000000000..8f0c91287dba9 --- /dev/null +++ b/tests/baselines/reference/structNotAllowStaticMemberSetters.errors.txt @@ -0,0 +1,20 @@ +tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberSetters.ts(5,25): error TS20025: Unexpected token. A constructor, method, or property was expected. + + +==== tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberSetters.ts (1 errors) ==== + // doc 2.2 + // Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + + struct C { + static _bar: number; + + + + static set bar(new_bar: number) { // error, setters are not allowed in struct + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + C._bar = new_bar; + ~~~~~~~~~~~~~~~~~~~ + } + ~~ +!!! error TS20025: Unexpected token. A constructor, method, or property was expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/structNotAllowStaticMemberSetters.js b/tests/baselines/reference/structNotAllowStaticMemberSetters.js new file mode 100644 index 0000000000000..32d8b5880060d --- /dev/null +++ b/tests/baselines/reference/structNotAllowStaticMemberSetters.js @@ -0,0 +1,35 @@ +//// [structNotAllowStaticMemberSetters.ts] +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + +struct C { + static _bar: number; + + static set bar(new_bar: number) { // error, setters are not allowed in struct + C._bar = new_bar; + } +} + +//// [structNotAllowStaticMemberSetters.js] +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + Object.defineProperty(C, "bar", { + set: function (new_bar) { + C._bar = new_bar; + }, + enumerable: true, + configurable: true + }); + return C; +})(); diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts new file mode 100644 index 0000000000000..28498229aa828 --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberGetters.ts @@ -0,0 +1,10 @@ +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + +struct C { + private _foo: string; + + get foo() { // error, getters are not allowed in struct + return this._foo; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts new file mode 100644 index 0000000000000..28cc04d9cde67 --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowInstanceMemberSetters.ts @@ -0,0 +1,10 @@ +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + +struct C { + private _foo: string; + + set foo(new_foo: string) { // error, setters are not allowed in struct + this._foo = new_foo; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberGetters.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberGetters.ts new file mode 100644 index 0000000000000..91032dec9c97d --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberGetters.ts @@ -0,0 +1,10 @@ +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + +struct C { + static _bar: number; + + static get bar() { // error, getters are not allowed in struct + return C._bar; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberSetters.ts b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberSetters.ts new file mode 100644 index 0000000000000..fac12be51c1cf --- /dev/null +++ b/tests/cases/conformance/structs/members/accessibilityAndAccessor/structNotAllowStaticMemberSetters.ts @@ -0,0 +1,10 @@ +// doc 2.2 +// Due to Typed Objects being nonextensible and non-configurable, accessors are not allowed. + +struct C { + static _bar: number; + + static set bar(new_bar: number) { // error, setters are not allowed in struct + C._bar = new_bar; + } +} \ No newline at end of file From 966e102e2f3901c3616754c69d496ea7ea9403db Mon Sep 17 00:00:00 2001 From: jied Date: Wed, 25 Nov 2015 13:39:05 -0800 Subject: [PATCH 38/43] struct not allow index members --- src/compiler/parser.ts | 10 +- .../structNotAllowIndexMember.errors.txt | 80 ++++++++++ .../reference/structNotAllowIndexMember.js | 148 ++++++++++++++++++ .../structs/structNotAllowIndexMember.ts | 48 ++++++ 4 files changed, 280 insertions(+), 6 deletions(-) create mode 100644 tests/baselines/reference/structNotAllowIndexMember.errors.txt create mode 100644 tests/baselines/reference/structNotAllowIndexMember.js create mode 100644 tests/cases/conformance/structs/structNotAllowIndexMember.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index c20a72804edc3..d8ebf8ac0f5a8 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5112,13 +5112,11 @@ return true; return parseConstructorDeclaration(fullStart, decorators, modifiers); } - /* index signature not allowed in struct - if (token === SyntaxKind.OpenBracketToken) { - - } */ - + // index signature not allowed in struct if (isIndexSignature()) { - return parseIndexSignatureDeclaration(fullStart, decorators, modifiers); + let indexSingature: IndexSignatureDeclaration = parseIndexSignatureDeclaration(fullStart, decorators, modifiers); + parseErrorAtPosition(indexSingature.pos, indexSingature.end - indexSingature.pos, Diagnostics.A_struct_can_not_have_index_members); + return indexSingature } // It is very important that we check this *after* checking indexers because diff --git a/tests/baselines/reference/structNotAllowIndexMember.errors.txt b/tests/baselines/reference/structNotAllowIndexMember.errors.txt new file mode 100644 index 0000000000000..934bf1c205cd4 --- /dev/null +++ b/tests/baselines/reference/structNotAllowIndexMember.errors.txt @@ -0,0 +1,80 @@ +tests/cases/conformance/structs/structNotAllowIndexMember.ts(3,11): error TS20026: A struct can not have index members. +tests/cases/conformance/structs/structNotAllowIndexMember.ts(4,22): error TS20026: A struct can not have index members. +tests/cases/conformance/structs/structNotAllowIndexMember.ts(10,12): error TS20026: A struct can not have index members. +tests/cases/conformance/structs/structNotAllowIndexMember.ts(24,12): error TS20026: A struct can not have index members. +tests/cases/conformance/structs/structNotAllowIndexMember.ts(28,12): error TS20026: A struct can not have index members. +tests/cases/conformance/structs/structNotAllowIndexMember.ts(38,12): error TS20026: A struct can not have index members. +tests/cases/conformance/structs/structNotAllowIndexMember.ts(42,12): error TS20026: A struct can not have index members. + + +==== tests/cases/conformance/structs/structNotAllowIndexMember.ts (7 errors) ==== + // doc 5 + // no indexers are allowed + struct S { + + [x: string]: string; + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS20026: A struct can not have index members. + + [y: number]: string; + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS20026: A struct can not have index members. + } + + // private indexers not allowed + + struct C1 { + + private [x: string]: string; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS20026: A struct can not have index members. + } + + /* struct D1 { + private [x: number]: string; + } + + struct E1 { + private [x: string]: T; + } */ + + // public indexers not allowed + + struct C2 { + + public [x: string]: string; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS20026: A struct can not have index members. + } + + struct D2 { + + public [x: number]: string; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS20026: A struct can not have index members. + } + + /* struct E2 { + public [x: string]: T; + } */ + + // static indexers not allowed + + struct C3 { + + static [x: string]: string; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS20026: A struct can not have index members. + } + + struct D3 { + + static [x: number]: string; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS20026: A struct can not have index members. + } + + /* struct E3 { + static [x: string]: T; + } */ + \ No newline at end of file diff --git a/tests/baselines/reference/structNotAllowIndexMember.js b/tests/baselines/reference/structNotAllowIndexMember.js new file mode 100644 index 0000000000000..e16fbc5e97cc2 --- /dev/null +++ b/tests/baselines/reference/structNotAllowIndexMember.js @@ -0,0 +1,148 @@ +//// [structNotAllowIndexMember.ts] +// doc 5 +// no indexers are allowed +struct S { + [x: string]: string; + [y: number]: string; +} + +// private indexers not allowed + +struct C1 { + private [x: string]: string; +} + +/* struct D1 { + private [x: number]: string; +} + +struct E1 { + private [x: string]: T; +} */ + +// public indexers not allowed + +struct C2 { + public [x: string]: string; +} + +struct D2 { + public [x: number]: string; +} + +/* struct E2 { + public [x: string]: T; +} */ + +// static indexers not allowed + +struct C3 { + static [x: string]: string; +} + +struct D3 { + static [x: number]: string; +} + +/* struct E3 { + static [x: string]: T; +} */ + + +//// [structNotAllowIndexMember.js] +// doc 5 +// no indexers are allowed +var S = (function () { + var _S = new TypedObject.StructType({ + }); + function _ctor() { + } + function S() { + var obj = new _S(); + _ctor.call(obj); + return obj; + } + S._TO = _S; + return S; +})(); +// private indexers not allowed +var C1 = (function () { + var _C1 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C1() { + var obj = new _C1(); + _ctor.call(obj); + return obj; + } + C1._TO = _C1; + return C1; +})(); +/* struct D1 { + private [x: number]: string; +} + +struct E1 { + private [x: string]: T; +} */ +// public indexers not allowed +var C2 = (function () { + var _C2 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C2() { + var obj = new _C2(); + _ctor.call(obj); + return obj; + } + C2._TO = _C2; + return C2; +})(); +var D2 = (function () { + var _D2 = new TypedObject.StructType({ + }); + function _ctor() { + } + function D2() { + var obj = new _D2(); + _ctor.call(obj); + return obj; + } + D2._TO = _D2; + return D2; +})(); +/* struct E2 { + public [x: string]: T; +} */ +// static indexers not allowed +var C3 = (function () { + var _C3 = new TypedObject.StructType({ + }); + function _ctor() { + } + function C3() { + var obj = new _C3(); + _ctor.call(obj); + return obj; + } + C3._TO = _C3; + return C3; +})(); +var D3 = (function () { + var _D3 = new TypedObject.StructType({ + }); + function _ctor() { + } + function D3() { + var obj = new _D3(); + _ctor.call(obj); + return obj; + } + D3._TO = _D3; + return D3; +})(); +/* struct E3 { + static [x: string]: T; +} */ diff --git a/tests/cases/conformance/structs/structNotAllowIndexMember.ts b/tests/cases/conformance/structs/structNotAllowIndexMember.ts new file mode 100644 index 0000000000000..0948f133025a0 --- /dev/null +++ b/tests/cases/conformance/structs/structNotAllowIndexMember.ts @@ -0,0 +1,48 @@ +// doc 5 +// no indexers are allowed +struct S { + [x: string]: string; + [y: number]: string; +} + +// private indexers not allowed + +struct C1 { + private [x: string]: string; +} + +/* struct D1 { + private [x: number]: string; +} + +struct E1 { + private [x: string]: T; +} */ + +// public indexers not allowed + +struct C2 { + public [x: string]: string; +} + +struct D2 { + public [x: number]: string; +} + +/* struct E2 { + public [x: string]: T; +} */ + +// static indexers not allowed + +struct C3 { + static [x: string]: string; +} + +struct D3 { + static [x: number]: string; +} + +/* struct E3 { + static [x: string]: T; +} */ From 1c985696e9c738bff31503aa0be037fbbd8f0399 Mon Sep 17 00:00:00 2001 From: jied Date: Wed, 25 Nov 2015 21:17:07 -0800 Subject: [PATCH 39/43] struct tests --- .../derivedStructAssignability.errors.txt | 34 +++++++ .../reference/derivedStructAssignability.js | 92 ++++++++++++++++++ .../structArrayAssignment.errors.txt | 35 +++++++ .../reference/structArrayAssignment.js | 89 +++++++++++++++++ ...derivedStructWithoutExplicitConstructor.ts | 30 ++++++ ...erivedStructWithoutExplicitConstructor2.ts | 50 ++++++++++ .../structWithoutExplicitConstructor.ts | 19 ++++ ...ConstructorDefaultValuesReferencingThis.ts | 14 +++ ...structorImplementationWithDefaultValues.ts | 22 +++++ ...tructorImplementationWithDefaultValues2.ts | 23 +++++ ...ctConstructorOverloadsWithDefaultValues.ts | 23 +++++ .../structConstructorParameterNotOptional.ts | 14 +++ .../structConstructorParameterProperties.ts | 24 +++++ .../structConstructorParameterProperties2.ts | 26 +++++ ...ameterPropertyDeclarationCannotOverride.ts | 24 +++++ ...tructConstructorParametersAccessibility.ts | 23 +++++ .../structConstructorAccessibility.ts | 36 +++++++ ...nstructorWithAssignableReturnExpression.ts | 36 +++++++ ...ructConstructorWithExpressionLessReturn.ts | 28 ++++++ ...erivedStructConstructorWithoutSuperCall.ts | 33 +++++++ .../derivedStructParameterProperties.ts | 97 +++++++++++++++++++ ...StructSuperCallsInNonConstructorMembers.ts | 18 ++++ .../derivedStructSuperCallsWithThisArg.ts | 31 ++++++ ...erCallInStructConstructorWithNoBaseType.ts | 14 +++ .../structWithTwoConstructorDefinitions.ts | 12 +++ .../structConstructorHasPrototypeProperty.ts | 36 +++++++ .../structWithBaseStructButNoConstructor.ts | 42 ++++++++ .../structWithConstructors.ts | 54 +++++++++++ .../structWithNoConstructorOrBaseStruct.ts | 21 ++++ .../structWithStaticMembers.ts | 18 ++++ .../derivedStructAssignability.ts | 28 ++++++ ...ctCannotOverrideInstanceMemberVariables.ts | 30 ++++++ .../derivedStructIncludesInheritedMembers.ts | 22 +++++ ...edStructOverrideInstanceFunctionMembers.ts | 32 ++++++ ...rivedStructOverrideStaticProperyMembers.ts | 37 +++++++ ...hPrivateInstanceShadowingPublicInstance.ts | 21 ++++ ...tWithPrivateStaticShadowingPublicStatic.ts | 23 +++++ ...stancePropertiesInheritedIntoStructType.ts | 32 ++++++ .../instancePropertyInStructType.ts | 28 ++++++ .../staticPropertyNotInStructType.ts | 41 ++++++++ .../structArrays/structArrayAssignment.ts | 23 +++++ .../structArrays/structArrayInitialization.ts | 15 +++ .../structWithPredefinedTypesAsNames2.ts | 3 + .../structs/structNotAddNonDeclaredMember.ts | 6 ++ .../structNotHaveOptionalProperties.ts | 15 +++ .../structAccessibilityModifiers.ts | 25 +++++ ...InstancePropertyAndFunctionWithSameName.ts | 21 ++++ .../instanceMemberAssignsToStructPrototype.ts | 16 +++ ...signsToStructConstructorFunctionMembers.ts | 16 +++ ...sesHiddenBaseCallViaSuperPropertyAccess.ts | 21 ++++ ...FunctionOverloadMixingStaticAndInstance.ts | 22 +++++ ...ructMemberFunctionsWithPrivateOverloads.ts | 54 +++++++++++ ...tructMemberFunctionsWithPublicOverloads.ts | 44 +++++++++ ...mberFunctionsWithPublicPrivateOverloads.ts | 57 +++++++++++ .../structStaticFactory1.ts | 17 ++++ .../typeOfThisInStructMemberFunctions.ts | 35 +++++++ ...tConstructorParameterShadowsOuterScopes.ts | 23 +++++ ...InitializerReferencingConstructorLocals.ts | 24 +++++ ...ializerReferencingConstructorParameters.ts | 26 +++++ .../structInstanceMemberInitialization.ts | 11 +++ .../structStaticMemberInitialization.ts | 9 ++ .../structThisInInstanceMemberInitializer.ts | 12 +++ .../structPropertyNamedPrototype.ts | 7 ++ ...uctStaticAndNonStaticPropertiesSameName.ts | 12 +++ ...ctStaticPropertyAndFunctionWithSameName.ts | 14 +++ .../AssignableOnStructInheritanceChain.ts | 30 ++++++ .../types/NotAssignableBetweenAnyAndStruct.ts | 14 +++ .../NotAssignableBetweenClassAndStruct.ts | 28 ++++++ .../NotAssignableBetweenInterfaceAndStruct.ts | 22 +++++ .../NotAssignableBetweenObjectAndStruct.ts | 14 +++ ...ignableBetweenStructsWithoutInheritance.ts | 19 ++++ .../conformance/structs/types/returnType.ts | 11 +++ .../structs/types/structInitialization.ts | 14 +++ 73 files changed, 1992 insertions(+) create mode 100644 tests/baselines/reference/derivedStructAssignability.errors.txt create mode 100644 tests/baselines/reference/derivedStructAssignability.js create mode 100644 tests/baselines/reference/structArrayAssignment.errors.txt create mode 100644 tests/baselines/reference/structArrayAssignment.js create mode 100644 tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor2.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/structWithoutExplicitConstructor.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorDefaultValuesReferencingThis.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues2.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorOverloadsWithDefaultValues.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterNotOptional.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties2.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterPropertyDeclarationCannotOverride.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParametersAccessibility.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/structConstructorAccessibility.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/structConstructorWithAssignableReturnExpression.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/structConstructorWithExpressionLessReturn.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructConstructorWithoutSuperCall.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructParameterProperties.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsWithThisArg.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/superCallInStructConstructorWithNoBaseType.ts create mode 100644 tests/cases/conformance/structs/constructorDeclarations/structWithTwoConstructorDefinitions.ts create mode 100644 tests/cases/conformance/structs/members/constructorFunctionTypes/structConstructorHasPrototypeProperty.ts create mode 100644 tests/cases/conformance/structs/members/constructorFunctionTypes/structWithBaseStructButNoConstructor.ts create mode 100644 tests/cases/conformance/structs/members/constructorFunctionTypes/structWithConstructors.ts create mode 100644 tests/cases/conformance/structs/members/constructorFunctionTypes/structWithNoConstructorOrBaseStruct.ts create mode 100644 tests/cases/conformance/structs/members/constructorFunctionTypes/structWithStaticMembers.ts create mode 100644 tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructAssignability.ts create mode 100644 tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructCannotOverrideInstanceMemberVariables.ts create mode 100644 tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructIncludesInheritedMembers.ts create mode 100644 tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideInstanceFunctionMembers.ts create mode 100644 tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideStaticProperyMembers.ts create mode 100644 tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateInstanceShadowingPublicInstance.ts create mode 100644 tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateStaticShadowingPublicStatic.ts create mode 100644 tests/cases/conformance/structs/members/structTypes/instancePropertiesInheritedIntoStructType.ts create mode 100644 tests/cases/conformance/structs/members/structTypes/instancePropertyInStructType.ts create mode 100644 tests/cases/conformance/structs/members/structTypes/staticPropertyNotInStructType.ts create mode 100644 tests/cases/conformance/structs/structArrays/structArrayAssignment.ts create mode 100644 tests/cases/conformance/structs/structArrays/structArrayInitialization.ts create mode 100644 tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames2.ts create mode 100644 tests/cases/conformance/structs/structNotAddNonDeclaredMember.ts create mode 100644 tests/cases/conformance/structs/structNotHaveOptionalProperties.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/instanceMemberAssignsToStructPrototype.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/staticMemberAssignsToStructConstructorFunctionMembers.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionOverloadMixingStaticAndInstance.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPrivateOverloads.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicOverloads.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicPrivateOverloads.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structStaticFactory1.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/typeOfThisInStructMemberFunctions.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structConstructorParameterShadowsOuterScopes.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorParameters.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInstanceMemberInitialization.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structStaticMemberInitialization.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structThisInInstanceMemberInitializer.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structPropertyNamedPrototype.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticAndNonStaticPropertiesSameName.ts create mode 100644 tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticPropertyAndFunctionWithSameName.ts create mode 100644 tests/cases/conformance/structs/types/AssignableOnStructInheritanceChain.ts create mode 100644 tests/cases/conformance/structs/types/NotAssignableBetweenAnyAndStruct.ts create mode 100644 tests/cases/conformance/structs/types/NotAssignableBetweenClassAndStruct.ts create mode 100644 tests/cases/conformance/structs/types/NotAssignableBetweenInterfaceAndStruct.ts create mode 100644 tests/cases/conformance/structs/types/NotAssignableBetweenObjectAndStruct.ts create mode 100644 tests/cases/conformance/structs/types/NotAssignableBetweenStructsWithoutInheritance.ts create mode 100644 tests/cases/conformance/structs/types/returnType.ts create mode 100644 tests/cases/conformance/structs/types/structInitialization.ts diff --git a/tests/baselines/reference/derivedStructAssignability.errors.txt b/tests/baselines/reference/derivedStructAssignability.errors.txt new file mode 100644 index 0000000000000..014ad8c55ec17 --- /dev/null +++ b/tests/baselines/reference/derivedStructAssignability.errors.txt @@ -0,0 +1,34 @@ +tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructAssignability.ts(27,12): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. + + +==== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructAssignability.ts (1 errors) ==== + // doc 2.3 + // struct type assignability is based on inheritance declaration. + // The type of an overriding property member doesn’t need to be assignable to the type of + // the overridden property member. + + struct C { + foo(x: number) { } + } + + struct D extends C { + foo() { } // ok + } + + struct E extends D { + foo(x?: string) { } // ok + } + + var c: C; + var d: D; + var e: E; + c = d; // ok + c = e; // ok + + var r = c.foo(1); + var r2 = e.foo(''); + r2 = e.foo(); + r2 = e.foo(1); // error, not match + ~ +!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. + r2 = (e).foo(1); // ok \ No newline at end of file diff --git a/tests/baselines/reference/derivedStructAssignability.js b/tests/baselines/reference/derivedStructAssignability.js new file mode 100644 index 0000000000000..c3297b7cbe987 --- /dev/null +++ b/tests/baselines/reference/derivedStructAssignability.js @@ -0,0 +1,92 @@ +//// [derivedStructAssignability.ts] +// doc 2.3 +// struct type assignability is based on inheritance declaration. +// The type of an overriding property member doesn’t need to be assignable to the type of +// the overridden property member. + +struct C { + foo(x: number) { } +} + +struct D extends C { + foo() { } // ok +} + +struct E extends D { + foo(x?: string) { } // ok +} + +var c: C; +var d: D; +var e: E; +c = d; // ok +c = e; // ok + +var r = c.foo(1); +var r2 = e.foo(''); +r2 = e.foo(); +r2 = e.foo(1); // error, not match +r2 = (e).foo(1); // ok + +//// [derivedStructAssignability.js] +// doc 2.3 +// struct type assignability is based on inheritance declaration. +// The type of an overriding property member doesn’t need to be assignable to the type of +// the overridden property member. +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.foo = function (x) { }; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + _D.prototype.foo = function () { }; // ok + return D; +})(); +var E = (function () { + var _E = new TypedObject.StructType({ + }); + function _ctor() { + } + function E() { + var obj = new _E(); + _ctor.call(obj); + return obj; + } + E._TO = _E; + _E.prototype.foo = function (x) { }; // ok + return E; +})(); +var c; +var d; +var e; +c = d; // ok +c = e; // ok +var r = c.foo(1); +var r2 = e.foo(''); +r2 = e.foo(); +r2 = e.foo(1); // error, not match +r2 = e.foo(1); // ok diff --git a/tests/baselines/reference/structArrayAssignment.errors.txt b/tests/baselines/reference/structArrayAssignment.errors.txt new file mode 100644 index 0000000000000..617fc89d24b3d --- /dev/null +++ b/tests/baselines/reference/structArrayAssignment.errors.txt @@ -0,0 +1,35 @@ +tests/cases/conformance/structs/structArrays/structArrayAssignment.ts(19,1): error TS2322: Type 'A' is not assignable to type 'C'. +tests/cases/conformance/structs/structArrays/structArrayAssignment.ts(20,1): error TS2322: Type 'number' is not assignable to type 'C'. +tests/cases/conformance/structs/structArrays/structArrayAssignment.ts(23,1): error TS2322: Type 'E' is not assignable to type 'C'. + + +==== tests/cases/conformance/structs/structArrays/structArrayAssignment.ts (3 errors) ==== + // doc 6 + // If struct array members are assigned with values of incompatible type, type error will be issued. + + class A {} + var a: A; + var b = 1; + + struct C { } + var c = new C(); + struct D extends C {} + var d = new D(); + + struct E {} + var e: E; + + var structArr: C[]; + structArr = new C[5]; + + structArr[0] = a; // error + ~~~~~~~~~~~~ +!!! error TS2322: Type 'A' is not assignable to type 'C'. + structArr[1] = b; // error + ~~~~~~~~~~~~ +!!! error TS2322: Type 'number' is not assignable to type 'C'. + structArr[2] = c; // ok + structArr[3] = d; // ok + structArr[4] = e; // error + ~~~~~~~~~~~~ +!!! error TS2322: Type 'E' is not assignable to type 'C'. \ No newline at end of file diff --git a/tests/baselines/reference/structArrayAssignment.js b/tests/baselines/reference/structArrayAssignment.js new file mode 100644 index 0000000000000..4bc2984df0d55 --- /dev/null +++ b/tests/baselines/reference/structArrayAssignment.js @@ -0,0 +1,89 @@ +//// [structArrayAssignment.ts] +// doc 6 +// If struct array members are assigned with values of incompatible type, type error will be issued. + +class A {} +var a: A; +var b = 1; + +struct C { } +var c = new C(); +struct D extends C {} +var d = new D(); + +struct E {} +var e: E; + +var structArr: C[]; +structArr = new C[5]; + +structArr[0] = a; // error +structArr[1] = b; // error +structArr[2] = c; // ok +structArr[3] = d; // ok +structArr[4] = e; // error + +//// [structArrayAssignment.js] +// doc 6 +// If struct array members are assigned with values of incompatible type, type error will be issued. +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var A = (function () { + function A() { + } + return A; +})(); +var a; +var b = 1; +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var c = new C(); +var D = (function () { + var _D = new TypedObject.StructType({ + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); +var d = new D(); +var E = (function () { + var _E = new TypedObject.StructType({ + }); + function _ctor() { + } + function E() { + var obj = new _E(); + _ctor.call(obj); + return obj; + } + E._TO = _E; + return E; +})(); +var e; +var structArr; +structArr = new StructArray(5); +structArr[0] = a; // error +structArr[1] = b; // error +structArr[2] = c; // ok +structArr[3] = d; // ok +structArr[4] = e; // error diff --git a/tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor.ts b/tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor.ts new file mode 100644 index 0000000000000..1f1fba5735d12 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor.ts @@ -0,0 +1,30 @@ +// doc 3.3 +// In a derived struct, the automatic constructor has the same parameter list +// (and possibly overloads) as the base struct constructor. + +struct Base { + a = 1; + constructor(x: number) { this.a = x; } +} + +struct Derived extends Base { + x = 1 + y = 'hello'; +} + +var r = new Derived(); // error, Supplied parameters do not match any signature of call target +var r2 = new Derived(1); + +/* struct Base2 { + a: T; + constructor(x: T) { this.a = x; } +} + +struct D extends Base2 { + x = 2 + y: T = null; +} + +var d = new D(); // error, Supplied parameters do not match any signature of call target +var d2 = new D(new Date()); // ok +*/ \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor2.ts b/tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor2.ts new file mode 100644 index 0000000000000..d4ed5516988e8 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor2.ts @@ -0,0 +1,50 @@ +// doc 3.3 +// In a derived struct, the automatic constructor has the same parameter list +// (and possibly overloads) as the base struct constructor. +// automatic constructors with a struct hieararchy of depth > 2 + +struct Base { + a = 1; + constructor(x: number) { this.a = x; } +} + +struct Derived extends Base { + b = ''; + constructor(y: string, z: string) { + super(2); + this.b = y; + } +} + +struct Derived2 extends Derived { + x = 1 + y = 'hello'; +} + +var r = new Derived(); // error, Supplied parameters do not match any signature of call target. +var r2 = new Derived2(1); // error, Supplied parameters do not match any signature of call target. +var r3 = new Derived('', ''); + +/* struct Base2 { + a: T; + constructor(x: T) { this.a = x; } +} + +struct D extends Base { + b: T = null; + constructor(y: T, z: T) { + super(2); + this.b = y; + } +} + + +struct D2 extends D { + x = 2 + y: T = null; +} + +var d = new D2(); // error, Supplied parameters do not match any signature of call target. +var d2 = new D2(new Date()); // error, Supplied parameters do not match any signature of call target. +var d3 = new D2(new Date(), new Date()); // ok + */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/structWithoutExplicitConstructor.ts b/tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/structWithoutExplicitConstructor.ts new file mode 100644 index 0000000000000..4c4db364c6183 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/structWithoutExplicitConstructor.ts @@ -0,0 +1,19 @@ +// doc 3.3 +// If a struct omits a constructor declaration, an automatic constructor is provided. + +struct C { + x = 1 + y = 'hello'; +} + +var c = new C(); +var c2 = new C(null); // error, Supplied parameters do not match any signature of call target + +/* struct D { + x = 2 + y: T = null; +} + +var d = new D(); +var d2 = new D(null); // error, Supplied parameters do not match any signature of call target + */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorDefaultValuesReferencingThis.ts b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorDefaultValuesReferencingThis.ts new file mode 100644 index 0000000000000..9f6d2c96f5f04 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorDefaultValuesReferencingThis.ts @@ -0,0 +1,14 @@ +// doc 3.1 +// 'this' cannot be referenced in constructor arguments + +struct C { + constructor(x = this) { } // error +} + +/* struct D { + constructor(x = this) { } // error +} + +struct E { + constructor(public x = this) { } // error +}*/ \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues.ts b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues.ts new file mode 100644 index 0000000000000..723cbb1fa6e5c --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues.ts @@ -0,0 +1,22 @@ +// doc3.1 + +struct C { + constructor(x); + constructor(x = 1) { + var y = x; + } +} + +/* struct D { + constructor(x); + constructor(x:T = null) { + var y = x; + } +} + +struct E { + constructor(x); + constructor(x: T = null) { + var y = x; + } +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues2.ts b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues2.ts new file mode 100644 index 0000000000000..4af1f75835ec3 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues2.ts @@ -0,0 +1,23 @@ +// doc 3.1 + +struct C { + constructor(x); + constructor(public x: string = 1) { // error, Type 'number' is not assignable to type 'string' + var y = x; + } +} + +/* struct D { + constructor(x: T, y: U); + constructor(x: T = 1, public y: U = x) { // error, Type 'number' is not assignable to type 'T'. + // Type 'T' is not assignable to type 'U' + var z = x; + } +} + +struct E { + constructor(x); + constructor(x: T = new Date()) { // error, Type 'Date' is not assignable to type 'T' + var y = x; + } +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorOverloadsWithDefaultValues.ts b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorOverloadsWithDefaultValues.ts new file mode 100644 index 0000000000000..9a8e1b793e33c --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorOverloadsWithDefaultValues.ts @@ -0,0 +1,23 @@ +// doc 3.1 +// A parameter initializer is only allowed in a function or constructor implementation + +struct C { + foo: string; + constructor(x = 1); // error + constructor() { + } +} + +struct D { + foo: string; + constructor(); + constructor(x = 1) { // ok + } +} + +/* struct D { + foo: string; + constructor(x = 1); // error + constructor() { + } +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterNotOptional.ts b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterNotOptional.ts new file mode 100644 index 0000000000000..bee8a0c7d03cf --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterNotOptional.ts @@ -0,0 +1,14 @@ +// doc 3.1 +// optional parameters are not allowed for struct constructor. + +struct C1 { + constructor(public x?: number) { } // error +} + +struct C2 { + constructor(private p?: number) { } // error +} + +struct C3 { + constructor(protected p?: number) { } // error +} diff --git a/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties.ts b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties.ts new file mode 100644 index 0000000000000..cad7b2c1fccea --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties.ts @@ -0,0 +1,24 @@ +// doc 3.1 +// modifiers in parameter property declaration define accessibility for parameters + +struct C { + y: string; + constructor(private x: string) { } +} + +var c: C; +var r = c.y; +var r2 = c.x; // error, Property 'x' is private and only accessible within struct 'C' + + +/* struct D { + y: T; + constructor(a: T, private x: T, protected z: T) { } +} + +var d: D; +var r = d.y; +var r2 = d.x; // error +var r3 = d.a; // error, Property 'a' does not exist on type 'D' +var r4 = d.z; // error +*/ diff --git a/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties2.ts b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties2.ts new file mode 100644 index 0000000000000..6c930e73e4269 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties2.ts @@ -0,0 +1,26 @@ +// doc 3.1 + +struct C { + y: number; + constructor(y: number) { } // ok +} + +var c: C; +var r = c.y; + +struct D { + y: number; + constructor(public y: number) { } // error, Duplicate identifier 'y' +} + +var d: D; +var r2 = d.y; + +struct E { + y: number; + constructor(private y: number) { } // error, Duplicate identifier 'y' +} + +var e: E; +var r3 = e.y; // error + diff --git a/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterPropertyDeclarationCannotOverride.ts b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterPropertyDeclarationCannotOverride.ts new file mode 100644 index 0000000000000..87d3660670fe0 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterPropertyDeclarationCannotOverride.ts @@ -0,0 +1,24 @@ +// doc 3.1 +// derived struct cannot override inherited member variables from base struct + +struct Base { + constructor(public x: number) { } +} + +struct Derived extends Base { + constructor(public x: number) { // cannot overrides inherited member variables + super(x); + this.x; // OK + } +} + +struct Derived1 extends Base { + constructor(public y: number) { // ok + super(y); + this.x; // OK + this.y; // OK + } +} + +var d: Derived1; +d.x; // public, OK \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParametersAccessibility.ts b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParametersAccessibility.ts new file mode 100644 index 0000000000000..be1258299fcde --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParametersAccessibility.ts @@ -0,0 +1,23 @@ +// doc 3.1 +// Only public and private modifiers are allowed for constructor parameters, but not protected. + +struct C1 { + constructor(public x: number) { } +} +var c1: C1; +c1.x // OK + + +struct C2 { + constructor(private p: number) { } +} +var c2: C2; +c2.p // private, error + + +struct C3 { + constructor(protected p: number) { } // error +} +var c3: C3; +c3.p // protected, error + diff --git a/tests/cases/conformance/structs/constructorDeclarations/structConstructorAccessibility.ts b/tests/cases/conformance/structs/constructorDeclarations/structConstructorAccessibility.ts new file mode 100644 index 0000000000000..1ca7e2cfe3381 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/structConstructorAccessibility.ts @@ -0,0 +1,36 @@ +// doc 3 +// Only public constructors are supported. Private and protected constructors result in an error + +struct C { + public constructor(public x: number) { } +} + +struct D { + private constructor(public x: number) { } // error +} + +struct E { + protected constructor(public x: number) { } // error +} + +var c = new C(1); +var d = new D(1); +var e = new E(1); + +/* module Generic { + struct C { + public constructor(public x: T) { } + } + + struct D { + private constructor(public x: T) { } // error + } + + struct E { + protected constructor(public x: T) { } // error + } + + var c = new C(1); + var d = new D(1); + var e = new E(1); +} */ diff --git a/tests/cases/conformance/structs/constructorDeclarations/structConstructorWithAssignableReturnExpression.ts b/tests/cases/conformance/structs/constructorDeclarations/structConstructorWithAssignableReturnExpression.ts new file mode 100644 index 0000000000000..69d6002b07c96 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/structConstructorWithAssignableReturnExpression.ts @@ -0,0 +1,36 @@ +// doc 3 +// A struct constructor canot return an expression. + +struct C { + constructor() { + return 1; // error + } +} + +struct D { + x: number; + constructor() { + return 1; // error + } +} + +struct E { + x: number; + constructor() { + return { x: 1 }; // error + } +} + +/* struct F { + x: T; + constructor() { + return { x: 1 }; // error + } +} + +struct G { + x: T; + constructor() { + return { x: null }; // error + } +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/structConstructorWithExpressionLessReturn.ts b/tests/cases/conformance/structs/constructorDeclarations/structConstructorWithExpressionLessReturn.ts new file mode 100644 index 0000000000000..d4faecdca13ab --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/structConstructorWithExpressionLessReturn.ts @@ -0,0 +1,28 @@ +// doc 3 +// just return keyword is ok. +// no error + +struct C { + constructor() { + return; + } +} + +struct D { + x: number; + constructor() { + return; + } +} + +struct E { + constructor(public x: number) { + return; + } +} + +struct F { + constructor(public x: T) { + return; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructConstructorWithoutSuperCall.ts b/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructConstructorWithoutSuperCall.ts new file mode 100644 index 0000000000000..d9434df287139 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructConstructorWithoutSuperCall.ts @@ -0,0 +1,33 @@ +// doc 3.2 +// derived struct constructors must contain a super call + +struct Base { + x: string; +} + +struct Derived extends Base { + constructor() { // error + } +} + +struct Base2 { + x: any; +} + +struct Derived2 extends Base2 { + constructor() { // error for no super call (nested scopes don't count) + var r2 = () => super(); // error for misplaced super call (nested function) + } +} + +struct Derived3 extends Base2 { + constructor() { // error + var r = function () { super() } // error + } +} + +struct Derived4 extends Base2 { + constructor() { + var r = super(); // ok + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructParameterProperties.ts b/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructParameterProperties.ts new file mode 100644 index 0000000000000..23f33761cd8e4 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructParameterProperties.ts @@ -0,0 +1,97 @@ +// doc 3.2 +// ordering of super calls in derived constructors matters depending on other struct contents +// A 'super' call must be the first statement in the constructor when a struct contains +// initialized properties or has parameter properties. + +struct Base { + x: string; +} + +struct Derived extends Base { + constructor(y: string) { + var a = 1; + super(); // ok + } +} + +struct Derived2 extends Base { + constructor(public y: string) { // has parameter properties + var a = 1; + super(); // error + } +} + +struct Derived3 extends Base { + constructor(public y: string) { + super(); // ok + var a = 1; + } +} + +struct Derived4 extends Base { + a = 1; // contains initialized properties + constructor(y: string) { + var b = 2; + super(); // error + } +} + +struct Derived5 extends Base { + a = 1; + constructor(y: string) { + super(); // ok + var b = 2; + } +} + +struct Derived6 extends Base { + a: number; + constructor(y: string) { + this.a = 1; + var b = 2; + super(); // ok + } +} + +struct Derived7 extends Base { + a = 1; // contains initialized properties + b: number; + constructor(y: string) { + this.a = 3; + this.b = 3; + super(); // error + } +} + +struct Derived8 extends Base { + a = 1; + b: number; + constructor(y: string) { + super(); // ok + this.a = 3; + this.b = 3; + } +} + +/* generic cases of Derived7 and Derived8 +struct Base2 { x: T; } + +struct Derived9 extends Base2 { + a = 1; // contains initialized properties + b: number; + constructor(y: string) { + this.a = 3; + this.b = 3; + super(); // error + } +} + +struct Derived10 extends Base2 { + a = 1; + b: number; + constructor(y: string) { + super(); // ok + this.a = 3; + this.b = 3; + } +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts b/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts new file mode 100644 index 0000000000000..00ba28a1e689d --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts @@ -0,0 +1,18 @@ +// doc 3.2 +// error to use super calls outside a constructor or in nested functions inside constructors + +struct Base { + x: string; +} + +struct Derived extends Base { + a: super(); // error + b() { + super(); // error + } + + static a: super(); // error + static b() { + super(); // error + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsWithThisArg.ts b/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsWithThisArg.ts new file mode 100644 index 0000000000000..3bba5fbebea00 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsWithThisArg.ts @@ -0,0 +1,31 @@ +// doc 3.2 +// it is a compile-time error for argument expressions to reference this. + +struct Base { + x: string; + constructor(a) { } +} + +struct Derived extends Base { + constructor() { + super(this); // error, not assignable + } +} + +struct Derived2 extends Base { + constructor(public a: string) { + super(this); // error, 'this' cannot be referenced in current location + } +} + +struct Derived3 extends Base { + constructor(public a: string) { + super(() => this); // error, 'this' cannot be referenced in current location + } +} + +struct Derived4 extends Base { + constructor(public a: string) { + super(function () { return this; }); // ok + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/superCallInStructConstructorWithNoBaseType.ts b/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/superCallInStructConstructorWithNoBaseType.ts new file mode 100644 index 0000000000000..f1589db63402b --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/superCallInStructConstructorWithNoBaseType.ts @@ -0,0 +1,14 @@ +// doc 3.2 +// 'super' can only be referenced in a derived struct + +struct C { + constructor() { + super(); // error + } +} + +/* struct D { + public constructor(public x: T) { + super(); // error + } +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/constructorDeclarations/structWithTwoConstructorDefinitions.ts b/tests/cases/conformance/structs/constructorDeclarations/structWithTwoConstructorDefinitions.ts new file mode 100644 index 0000000000000..7c1c4542c66c2 --- /dev/null +++ b/tests/cases/conformance/structs/constructorDeclarations/structWithTwoConstructorDefinitions.ts @@ -0,0 +1,12 @@ +// doc 3 +// Multiple constructor implementations are not allowed + +struct C { + constructor() { } // error + constructor(x) { } // error +} + +/* struct D { + constructor(x: T) { } // error + constructor(x: T, y: T) { } // error +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/constructorFunctionTypes/structConstructorHasPrototypeProperty.ts b/tests/cases/conformance/structs/members/constructorFunctionTypes/structConstructorHasPrototypeProperty.ts new file mode 100644 index 0000000000000..d81b34b140bc0 --- /dev/null +++ b/tests/cases/conformance/structs/members/constructorFunctionTypes/structConstructorHasPrototypeProperty.ts @@ -0,0 +1,36 @@ +// doc 2.5 +// Every struct automatically contains a static property member named ‘prototype’, +// the type of which is the containing struct with type Any substituted for each type parameter. +// ok + +module NonGeneric { + struct C { + foo: string; + } + + struct D extends C { + bar: string; + } + + var r = C.prototype; + r.foo; // any + var r2 = D.prototype; + r2.bar; // any +} + +/* module Generic { + struct C { + foo: T; + bar: U; + } + + struct D extends C { + baz: T; + bing: U; + } + + var r = C.prototype; // C + var ra = r.foo; // any + var r2 = D.prototype; // D + var rb = r2.baz; // any +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithBaseStructButNoConstructor.ts b/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithBaseStructButNoConstructor.ts new file mode 100644 index 0000000000000..d98cadbba3207 --- /dev/null +++ b/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithBaseStructButNoConstructor.ts @@ -0,0 +1,42 @@ +// doc 2.5 + +struct Base { + constructor(x: number) { } +} + +struct C extends Base { + foo: string; +} + +var r = C; +var c = new C(); // error +var c2 = new C(1); // ok + +/* struct Base2 { + constructor(x: T) { } +} + +struct D extends Base2 { + foo: U; +} + +var r2 = D; +var d = new D(); // error +var d2 = new D(1); // ok + +// specialized base struct +struct D2 extends Base2 { + foo: U; +} + +var r3 = D2; +var d3 = new D(); // error +var d4 = new D(1); // ok + +struct D3 extends Base2 { + foo: string; +} + +var r4 = D3; +var d5 = new D(); // error +var d6 = new D(1); // ok */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithConstructors.ts b/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithConstructors.ts new file mode 100644 index 0000000000000..c16bfed84ae86 --- /dev/null +++ b/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithConstructors.ts @@ -0,0 +1,54 @@ +// doc 2.5 +// If the struct contains a constructor declaration with overloads, a set of construct signatures with +// the parameter lists of the overloads, all having the same type parameters as the struct and returning +// the instance type of the struct. + +module NonGeneric { + struct C { + constructor(x: string) { } + } + + var c = new C(); // error + var c2 = new C(''); // ok + + struct C2 { + constructor(x: number); + constructor(x: string); + constructor(x: any) { } + } + + var c3 = new C2(); // error + var c4 = new C2(''); // ok + var c5 = new C2(1); // ok + + struct D extends C2 { } + + var d = new D(); // error + var d2 = new D(1); // ok + var d3 = new D(''); // ok +} + +/* module Generics { + struct C { + constructor(x: T) { } + } + + var c = new C(); // error + var c2 = new C(''); // ok + + struct C2 { + constructor(x: T); + constructor(x: T, y: U); + constructor(x: any) { } + } + + var c3 = new C2(); // error + var c4 = new C2(''); // ok + var c5 = new C2(1, 2); // ok + + struct D extends C2 { } + + var d = new D(); // error + var d2 = new D(1); // ok + var d3 = new D(''); // ok +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithNoConstructorOrBaseStruct.ts b/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithNoConstructorOrBaseStruct.ts new file mode 100644 index 0000000000000..e5b9616c9e884 --- /dev/null +++ b/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithNoConstructorOrBaseStruct.ts @@ -0,0 +1,21 @@ +// doc 2.5 +// If the struct contains no constructor declaration and has no base struct, a single construct signature +// with no parameters, having the same type parameters as the struct and returning the instance type of +// the struct. +// ok + +struct C { + x: string; +} + +var c = new C(); +var r = C; + +/* struct D { + x: T; + y: U; +} + +var d = new D(); +var d2 = new D(); +var r2 = D; */ diff --git a/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithStaticMembers.ts b/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithStaticMembers.ts new file mode 100644 index 0000000000000..f77ada728b596 --- /dev/null +++ b/tests/cases/conformance/structs/members/constructorFunctionTypes/structWithStaticMembers.ts @@ -0,0 +1,18 @@ +// doc 2.5 +// ok + +struct C { + static fn() { return this; } + constructor(public a: number, private b: number) { } + static foo: string; +} + +var r = C.fn(); +var r2 = r.foo; + +struct D extends C { + bar: string; +} + +var r = D.fn(); +var r2 = r.foo; \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructAssignability.ts b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructAssignability.ts new file mode 100644 index 0000000000000..1d5bfc3d07d31 --- /dev/null +++ b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructAssignability.ts @@ -0,0 +1,28 @@ +// doc 2.3 +// struct type assignability is based on inheritance declaration. +// The type of an overriding property member doesn’t need to be assignable to the type of +// the overridden property member. + +struct C { + foo(x: number) { } +} + +struct D extends C { + foo() { } // ok +} + +struct E extends D { + foo(x?: string) { } // ok +} + +var c: C; +var d: D; +var e: E; +c = d; // ok +c = e; // ok + +var r = c.foo(1); +var r2 = e.foo(''); +r2 = e.foo(); +r2 = e.foo(1); // error, not match +r2 = (e).foo(1); // ok \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructCannotOverrideInstanceMemberVariables.ts b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructCannotOverrideInstanceMemberVariables.ts new file mode 100644 index 0000000000000..4c27afd2cd821 --- /dev/null +++ b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructCannotOverrideInstanceMemberVariables.ts @@ -0,0 +1,30 @@ +// doc 2.3 +// only public function members can be overridden. + +struct Base { + private x: { foo: string }; +} + +struct Derived extends Base { + private x: { foo: string; bar: string; }; // error +} + +struct Base1 { + public foo: any; +} + +struct Derived1 extends Base1 { + public foo: string; // error} +} + +struct Base2 { + static y: { foo: string }; +} + +struct Derived2 extends Base2 { + static y: { foo: string; bar: string; }; // ok +} + +struct Derived3 extends Base2 { + static y: { foo: any }; // ok +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructIncludesInheritedMembers.ts b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructIncludesInheritedMembers.ts new file mode 100644 index 0000000000000..2ee9d0dad31e6 --- /dev/null +++ b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructIncludesInheritedMembers.ts @@ -0,0 +1,22 @@ +// doc 2.3 +// A derived struct inherits all members from its base struct it doesn’t override. +// all ok + +struct Base { + a: string; + b() { } + + static r: string; + static s() { } + + constructor(x) { } +} + +struct Derived extends Base { +} + +var d: Derived = new Derived(1); +var r1 = d.a; +var r2 = d.b(); +var r3 = Derived.r; +var r4 = Derived.s(); diff --git a/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideInstanceFunctionMembers.ts b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideInstanceFunctionMembers.ts new file mode 100644 index 0000000000000..6affe35478ec1 --- /dev/null +++ b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideInstanceFunctionMembers.ts @@ -0,0 +1,32 @@ +// doc 2.3 +// derived struct can override base struct's instance function members + +var x: { foo: string; } +var y: { foo: string; bar: string; } + +struct Base { + a: typeof x; + b(a: typeof x) { } + constructor(a: typeof x) { } +} + +struct Derived extends Base { + b(a: typeof y) { return 1; } // ok + constructor(a: typeof y) { super(x) } +} + + +/* struct Base2 { + [i: string]: Object; + [i: number]: typeof x; +} + +struct Derived2 extends Base2 { + [i: string]: typeof x; + [i: number]: typeof y; +} + +var d2: Derived2; +var r7 = d2['']; +var r8 = d2[1]; */ + diff --git a/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideStaticProperyMembers.ts b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideStaticProperyMembers.ts new file mode 100644 index 0000000000000..b890040233e1e --- /dev/null +++ b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideStaticProperyMembers.ts @@ -0,0 +1,37 @@ +// doc 2.3 +// derived struct can override base struct's static property members + +var x: { foo: string; } +var y: { foo: string; bar: string; } + +struct Base { + static r: typeof x; + static s(a: typeof x) { return 1; } + static u: (a: typeof x) => any; + + constructor(a: typeof x) { } +} + +struct Derived extends Base { + static r: typeof y; // ok + static s(a: typeof y) { } // ok + static u: (a: typeof y) => void; // ok + + constructor(a: typeof y) { super(x) } +} + + +/* struct Base2 { + [i: string]: Object; + [i: number]: typeof x; +} + +struct Derived2 extends Base2 { + [i: string]: typeof x; + [i: number]: typeof y; +} + +var d2: Derived2; +var r7 = d2['']; +var r8 = d2[1]; */ + diff --git a/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateInstanceShadowingPublicInstance.ts b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateInstanceShadowingPublicInstance.ts new file mode 100644 index 0000000000000..1d8739ec4479c --- /dev/null +++ b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateInstanceShadowingPublicInstance.ts @@ -0,0 +1,21 @@ +// doc 2.3 + +struct Base { + public fn(): string { + return ''; + } +} + +// error, not a subtype +struct Derived extends Base { + private fn(): string { + return ''; + } +} + +var b: Base; +var d: Derived; + +var r1 = b.fn(); // ok +var r2 = d.fn(); // error +var r3 = (d).fn; // ok \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateStaticShadowingPublicStatic.ts b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateStaticShadowingPublicStatic.ts new file mode 100644 index 0000000000000..26086e88bc30b --- /dev/null +++ b/tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateStaticShadowingPublicStatic.ts @@ -0,0 +1,23 @@ +// doc 2.3 + +struct Base { + public static x: string; + public static fn(): string { + return ''; + } +} + +struct Derived extends Base { + private static x: string; + private static fn(): string { + return ''; + } +} + +var b: typeof Base; +var d: typeof Derived; +var r = b.x; // ok +var r2 = d.x; // error + +var r3 = b.fn(); // ok +var r4 = d.fn(); // error \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/structTypes/instancePropertiesInheritedIntoStructType.ts b/tests/cases/conformance/structs/members/structTypes/instancePropertiesInheritedIntoStructType.ts new file mode 100644 index 0000000000000..fd01a82c3dc1d --- /dev/null +++ b/tests/cases/conformance/structs/members/structTypes/instancePropertiesInheritedIntoStructType.ts @@ -0,0 +1,32 @@ +// doc 2.4 +// All base struct instance type property that are not overridden in the struct +// ok + +module NonGeneric { + struct C { + x: string; + fn() { return this; } + constructor(public a: number, private b: number) { } + } + + struct D extends C { e: string; } + + var d = new D(1, 2); + var r = d.fn(); + var r2 = r.x; + +} + +/* module Generic { + struct C { + x: T; + fn() { return this; } + constructor(public a: T, private b: U) { } + } + + struct D extends C { e: T; } + + var d = new D(1, ''); + var r = d.fn(); + var r2 = r.x; +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/structTypes/instancePropertyInStructType.ts b/tests/cases/conformance/structs/members/structTypes/instancePropertyInStructType.ts new file mode 100644 index 0000000000000..9b3374a675da2 --- /dev/null +++ b/tests/cases/conformance/structs/members/structTypes/instancePropertyInStructType.ts @@ -0,0 +1,28 @@ +// doc 2.4 +// struct type contains all instance member variables and function members. +// ok + +module NonGeneric { + struct C { + x: string; + fn() { return this; } + constructor(public a: number, private b: number) { } + } + + var c = new C(1, 2); + var r = c.fn(); + var r2 = r.x; + c.a; +} + +/* module Generic { + struct C { + x: T; + fn() { return this; } + constructor(public a: T, private b: U) { } + } + + var c = new C(1, ''); + var r = c.fn(); + var r2 = r.x; +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/members/structTypes/staticPropertyNotInStructType.ts b/tests/cases/conformance/structs/members/structTypes/staticPropertyNotInStructType.ts new file mode 100644 index 0000000000000..a2b45c55fd87f --- /dev/null +++ b/tests/cases/conformance/structs/members/structTypes/staticPropertyNotInStructType.ts @@ -0,0 +1,41 @@ +// doc 2.4 +// Static members are declared using the static modifier and are members of the constructor function type. + +module NonGeneric { + struct C { + fn() { return this; } + constructor(public a: number, private b: number) { } + static foo: string; // not reflected in struct type + } + + module C { + export var bar = ''; // not reflected in struct type + } + + var c = new C(1, 2); + var r = c.fn(); + var r4 = c.foo; // error + var r5 = c.bar; // error + + var cs: typeof C; + r = cs.fn(); // error + r4 = cs.foo; // ok + r5 = cs.bar; // ok +} + +/* module Generic { + struct C { + fn() { return this; } + constructor(public a: T, private b: U) { } + static foo: T; // not reflected in struct type + } + + module C { + export var bar = ''; // not reflected in struct type + } + + var c = new C(1, ''); + var r = c.fn(); + var r4 = c.foo; // error + var r5 = c.bar; // error +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/structArrays/structArrayAssignment.ts b/tests/cases/conformance/structs/structArrays/structArrayAssignment.ts new file mode 100644 index 0000000000000..8723884b59a7c --- /dev/null +++ b/tests/cases/conformance/structs/structArrays/structArrayAssignment.ts @@ -0,0 +1,23 @@ +// doc 6 +// If struct array members are assigned with values of incompatible type, type error will be issued. + +class A {} +var a: A; +var b = 1; + +struct C { } +var c = new C(); +struct D extends C {} +var d = new D(); + +struct E {} +var e: E; + +var structArr: C[]; +structArr = new C[5]; + +structArr[0] = a; // error +structArr[1] = b; // error +structArr[2] = c; // ok +structArr[3] = d; // ok +structArr[4] = e; // error \ No newline at end of file diff --git a/tests/cases/conformance/structs/structArrays/structArrayInitialization.ts b/tests/cases/conformance/structs/structArrays/structArrayInitialization.ts new file mode 100644 index 0000000000000..77e19697f9667 --- /dev/null +++ b/tests/cases/conformance/structs/structArrays/structArrayInitialization.ts @@ -0,0 +1,15 @@ +// doc 6 +// Struct array length is fixed that if programmers try to access array element that is out of bound, +// compiler will complain “array index out of bound”. +// Struct array only has one member function ‘length’. + +struct C { } +var c = new C(); + +var structArr: C[]; +structArr = new C[2]; + +structArr[0] = c; // ok +structArr[2]; // error, array index out of bound +structArr.length; // ok +structArr.push(c); // error, push is not a member of struct array diff --git a/tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames2.ts b/tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames2.ts new file mode 100644 index 0000000000000..2abd78e6bef1d --- /dev/null +++ b/tests/cases/conformance/structs/structDeclarations/structWithPredefinedTypesAsNames2.ts @@ -0,0 +1,3 @@ +// structs cannot use predefined types as names + +struct void {} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structNotAddNonDeclaredMember.ts b/tests/cases/conformance/structs/structNotAddNonDeclaredMember.ts new file mode 100644 index 0000000000000..2c8437b081326 --- /dev/null +++ b/tests/cases/conformance/structs/structNotAddNonDeclaredMember.ts @@ -0,0 +1,6 @@ +struct C { + constructor(public firstName: string, public age: number) {} +} + +var c = new C("John", 25); +c.lastName = c.firstName; // error, cannot add undefined members to struct \ No newline at end of file diff --git a/tests/cases/conformance/structs/structNotHaveOptionalProperties.ts b/tests/cases/conformance/structs/structNotHaveOptionalProperties.ts new file mode 100644 index 0000000000000..ec1a312003f68 --- /dev/null +++ b/tests/cases/conformance/structs/structNotHaveOptionalProperties.ts @@ -0,0 +1,15 @@ +/** + * inspired by conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties + */ + +interface I { + x?: number; // ok +} + +class C { + x?: number; // error, cannot be declared optional +} + +struct S { + x?: number; // error, cannot be declared optional +} diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts new file mode 100644 index 0000000000000..6317b8fce2051 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts @@ -0,0 +1,25 @@ +// doc 4 + +// No errors +struct C { + private static privateProperty; + private static privateMethod() { } + + public static publicProperty; + public static publicMethod() { } +} + +// Errors, accessibility modifiers must precede static +struct D { + static private privateProperty; + static private privateMethod() { } + + static public publicProperty; + static public publicMethod() { } +} + +// Errors, multiple accessibility modifier +struct E { + private public property; + public private method() { } +} diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts new file mode 100644 index 0000000000000..aaf549d28ffca --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts @@ -0,0 +1,21 @@ +// doc 4 +// Instance property member declarations declare properties in the struct instance type, +// and must specify names that are unique among all instance property member +// and parameter property declarations in the containing struct. + +struct C { + x: number; + x() { // error + return 1; + } +} + +struct D { + x: number; + x(v) { } // error +} + +struct E { + x: number; + constructor(private x: string) {} +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/instanceMemberAssignsToStructPrototype.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/instanceMemberAssignsToStructPrototype.ts new file mode 100644 index 0000000000000..c35ff1fea63b8 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/instanceMemberAssignsToStructPrototype.ts @@ -0,0 +1,16 @@ +// doc 4.2 +// An instance member function declaration declares a property in the struct instance type and +// assigns a function object to a property on the prototype object of the struct. + +struct C { + foo() { + C.prototype.foo = () => { } + } + + bar(x: number): number { + C.prototype.bar = () => { } // error, Type '() => void' is not assignable to type '(x: number) => number' + C.prototype.bar = (x) => x; // ok + C.prototype.bar = (x: number) => 1; // ok + return 1; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/staticMemberAssignsToStructConstructorFunctionMembers.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/staticMemberAssignsToStructConstructorFunctionMembers.ts new file mode 100644 index 0000000000000..5b936e9eba7bc --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/staticMemberAssignsToStructConstructorFunctionMembers.ts @@ -0,0 +1,16 @@ +// doc 4.2 +// A static member function declaration declares a property in the constructor function type and +// assigns a function object to a property on the constructor function object. + +struct C { + static foo() { + C.foo = () => { } + } + + static bar(x: number): number { + C.bar = () => { } // error, Type '() => void' is not assignable to type '(x: number) => number' + C.bar = (x) => x; // ok + C.bar = (x: number) => 1; // ok + return 1; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.ts new file mode 100644 index 0000000000000..96163e8d31d57 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.ts @@ -0,0 +1,21 @@ +// doc 4.2 +// A member function can access overridden base struct members using a super property access. + +struct Base { + foo(x: { a: number }): { a: number } { + return null; + } +} + +struct Derived extends Base { + foo(x: { a: number; b: number }): { a: number; b: number } { + return null; + } + + bar() { + var r = super.foo({ a: 1 }); // { a: number } + var r2 = this.foo({a: 1}); // error, Argument of type '{ a: number; }' is not assignable to parameter of type '{ a: number; b: number; }'. + var r3 = super.foo({ a: 1, b: 2 }); // { a: number } + var r4 = this.foo({ a: 1, b: 2 }); // { a: number; b: number; } + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionOverloadMixingStaticAndInstance.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionOverloadMixingStaticAndInstance.ts new file mode 100644 index 0000000000000..ab81a4962634a --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionOverloadMixingStaticAndInstance.ts @@ -0,0 +1,22 @@ +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +struct C { + foo(); + static foo(); // error, Function overload must not be static. +} + +struct D { + static foo(); + foo(); // error, Function overload must be static. +} + +/* struct E { + foo(x: T); + static foo(x: number); // error, Function overload must not be static. +} + +struct F { + static foo(x: number); + foo(x: T); // error, Function overload must be static. +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPrivateOverloads.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPrivateOverloads.ts new file mode 100644 index 0000000000000..b02a1be41cb69 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPrivateOverloads.ts @@ -0,0 +1,54 @@ +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +struct C { + private foo(x: number); + private foo(x: number, y: string); + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + private bar(x: string); + private bar(x: number, y: string); + private bar(x: any, y?: any) { } + + private static foo(x: number); + private static foo(x: number, y: string); + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + private static bar(x: string); + private static bar(x: number, y: string); + private static bar(x: any, y?: any) { } +} + +var c: C; +var r1 = c.foo(1); // error +var r2 = C.foo(1); // error + +/* struct D { + private foo(x: number); + private foo(x: T, y: T); + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + private bar(x: string); + private bar(x: T, y: T); + private bar(x: any, y?: any) { } + + private static foo(x: number); + private static foo(x: number, y: number); + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + private static bar(x: string); + private static bar(x: number, y: number); + private static bar(x: any, y?: any) { } + +} + + + +var d: D; +var r2 = d.foo(2); // error +var r4 = D.bar(''); // error +*/ \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicOverloads.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicOverloads.ts new file mode 100644 index 0000000000000..8e946e4184694 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicOverloads.ts @@ -0,0 +1,44 @@ +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +// ok +struct C { + public foo(x: number); + public foo(x: number, y: string); + public foo(x: any, y?: any) { } + + public bar(x: 'hi'); + public bar(x: string); + public bar(x: number, y: string); + public bar(x: any, y?: any) { } + + public static foo(x: number); + public static foo(x: number, y: string); + public static foo(x: any, y?: any) { } + + public static bar(x: 'hi'); + public static bar(x: string); + public static bar(x: number, y: string); + public static bar(x: any, y?: any) { } +} + +/* struct D { + public foo(x: number); + public foo(x: T, y: T); + public foo(x: any, y?: any) { } + + public bar(x: 'hi'); + public bar(x: string); + public bar(x: T, y: T); + public bar(x: any, y?: any) { } + + public static foo(x: number); + public static foo(x: number, y: string); + public static foo(x: any, y?: any) { } + + public static bar(x: 'hi'); + public static bar(x: string); + public static bar(x: number, y: string); + public static bar(x: any, y?: any) { } + +}*/ \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicPrivateOverloads.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicPrivateOverloads.ts new file mode 100644 index 0000000000000..f198316d8d691 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicPrivateOverloads.ts @@ -0,0 +1,57 @@ +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +struct C { + private foo(x: number); + public foo(x: number, y: string); // error + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + public bar(x: string); // error + private bar(x: number, y: string); + private bar(x: any, y?: any) { } + + private static foo(x: number); + public static foo(x: number, y: string); // error + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + public static bar(x: string); // error + private static bar(x: number, y: string); + private static bar(x: any, y?: any) { } +} + +var c: C; +var r = c.foo(1); // error + +/* struct D { + private foo(x: number); + public foo(x: T, y: T); // error + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + public bar(x: string); // error + private bar(x: T, y: T); + private bar(x: any, y?: any) { } + + private baz(x: string); + protected baz(x: number, y: string); // error + private baz(x: any, y?: any) { } + + private static foo(x: number); + public static foo(x: number, y: string); // error + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + public static bar(x: string); // error + private static bar(x: number, y: string); + private static bar(x: any, y?: any) { } + + public static baz(x: string); // error + protected static baz(x: number, y: string); + protected static baz(x: any, y?: any) { } +} + +var d: D; +var r2 = d.foo(2); // error +*/ \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structStaticFactory1.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structStaticFactory1.ts new file mode 100644 index 0000000000000..895000fab1561 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structStaticFactory1.ts @@ -0,0 +1,17 @@ +// doc 4.2 +// In a static member function, this represents the constructor function object on which +// the static member function was invoked. Thus, a call to ‘new this()’ may actually invoke +// a derived struct constructor. +// ok +struct Base { + foo() { return 1; } + static create() { + return new this(); + } +} + +struct Derived extends Base { + foo() { return 2; } +} +var d = Derived.create(); // new B() +d.foo(); // 2 \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/typeOfThisInStructMemberFunctions.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/typeOfThisInStructMemberFunctions.ts new file mode 100644 index 0000000000000..1179b5a227aed --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/typeOfThisInStructMemberFunctions.ts @@ -0,0 +1,35 @@ +// doc 4.2 +// In the body of a static member function declaration, the type of this is the constructor +// function type. +// ok +struct C { + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} + +/* struct D { + x: T; + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} + +struct E { + x: T; + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structConstructorParameterShadowsOuterScopes.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structConstructorParameterShadowsOuterScopes.ts new file mode 100644 index 0000000000000..52ba209ec47e0 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structConstructorParameterShadowsOuterScopes.ts @@ -0,0 +1,23 @@ +// doc 4.1 +// Initializer expressions for instance member variables are evaluated in the scope of the struct +// constructor body, but are not permitted to reference parameters or local variables of +// the constructor. +// This effectively means that entities from outer scopes by the same name as a constructor +// parameter or local variable are inaccessible in initializer expressions for instance member +// variables. + +var x = 1; +struct C { + b = x; // error, evaluated in scope of constructor, cannot reference x + constructor(x: string) { + x = 2; // error, x is string + } +} + +var y = 1; +struct D { + b = y; // error, evaluated in scope of constructor, cannot reference y + constructor(x: string) { + var y = ""; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts new file mode 100644 index 0000000000000..3c726732f1fc0 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts @@ -0,0 +1,24 @@ +// doc 4.1 +// Initializer expressions for instance member variables are evaluated in the scope of the +// struct constructor body but are not permitted to reference parameters or local variables +// of the constructor. + +struct C { + a = z; // error + b: typeof z; // error + c = this.z; // error + d: typeof this.z; // error + constructor(x) { + z = 1; + } +} + +/* struct D { + a = z; // error + b: typeof z; // error + c = this.z; // error + d: typeof this.z; // error + constructor(x: T) { + z = 1; + } +} */ \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorParameters.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorParameters.ts new file mode 100644 index 0000000000000..c55c6b54a7fb4 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorParameters.ts @@ -0,0 +1,26 @@ +// doc 4.1 +// Initializer expressions for instance member variables are evaluated in the scope of the struct constructor body but are not permitted to reference parameters or local variables of the constructor. + +struct C { + a = x; // error + b: typeof x; // error + constructor(x) { } +} + +struct D { + a = x; // error + b: typeof x; // error + constructor(public x) { } +} + +struct E { + a = this.x; // ok + b: typeof this.x; // error + constructor(public x) { } +} + +/* struct F { + a = this.x; // ok + b = x; // error + constructor(public x: T) { } +}*/ \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInstanceMemberInitialization.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInstanceMemberInitialization.ts new file mode 100644 index 0000000000000..b2573c3750d40 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInstanceMemberInitialization.ts @@ -0,0 +1,11 @@ +// 4.1 +// ok + +struct C { + x = 1; +} + +var c = new C(); +c.x = 3; +var c2 = new C(); +var r = c.x === c2.x; \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structStaticMemberInitialization.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structStaticMemberInitialization.ts new file mode 100644 index 0000000000000..79c32064eab7a --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structStaticMemberInitialization.ts @@ -0,0 +1,9 @@ +// doc 4.1 +// ok + +struct C { + static x = 1; +} + +var c = new C(); +var r = C.x; \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structThisInInstanceMemberInitializer.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structThisInInstanceMemberInitializer.ts new file mode 100644 index 0000000000000..0417a20965367 --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structThisInInstanceMemberInitializer.ts @@ -0,0 +1,12 @@ +// doc 4.1 +// In an initializer expression for an instance member variable, this is of the struct instance type. +// ok + +struct C { + x = this; +} + +/* struct D { + x = this; + y: T; +}*/ \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structPropertyNamedPrototype.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structPropertyNamedPrototype.ts new file mode 100644 index 0000000000000..426e194f55e0f --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structPropertyNamedPrototype.ts @@ -0,0 +1,7 @@ +// doc 4 +// It is an error to explicitly declare a static property member with the name ‘prototype’. + +struct C { + prototype: number; // ok + static prototype: C; // error +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticAndNonStaticPropertiesSameName.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticAndNonStaticPropertiesSameName.ts new file mode 100644 index 0000000000000..1f7ebdd223fab --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticAndNonStaticPropertiesSameName.ts @@ -0,0 +1,12 @@ +// doc 4 +// the declaration spaces of instance and static property members are separate. +// Thus, it is possible to have instance and static property members with the same name +// ok + +struct C { + x: number; + static x: number; + + f() { } + static f() { } +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticPropertyAndFunctionWithSameName.ts b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticPropertyAndFunctionWithSameName.ts new file mode 100644 index 0000000000000..bea80a75136ef --- /dev/null +++ b/tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticPropertyAndFunctionWithSameName.ts @@ -0,0 +1,14 @@ +// doc 4 +// Static property member declarations declare properties in the constructor function type, +// and must specify names that are unique among all static property member declarations +// in the containing struct + +struct C { + static f: number; + static f() {} // error +} + +struct D { + f: number; + static f: number; +} \ No newline at end of file diff --git a/tests/cases/conformance/structs/types/AssignableOnStructInheritanceChain.ts b/tests/cases/conformance/structs/types/AssignableOnStructInheritanceChain.ts new file mode 100644 index 0000000000000..593a477efaaf6 --- /dev/null +++ b/tests/cases/conformance/structs/types/AssignableOnStructInheritanceChain.ts @@ -0,0 +1,30 @@ +// doc 8 +// A subtype struct can be assigned to a supertype struct. + +struct Point2 { + x: number; + y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y + } +} + +struct Point3 extends Point2{ + z: number; + constructor(x: number, y: number, z: number) { + super(x, y); + this.z = z; + } +} + +var p2: Point2; +var p3: Point3; +var p21 = new Point2(1,1); +var p31 = new Point3(1,1,1); + +p2 = p21; // ok +p2 = p31; // ok +p3 = p21; // error +p3 = p31; // ok + diff --git a/tests/cases/conformance/structs/types/NotAssignableBetweenAnyAndStruct.ts b/tests/cases/conformance/structs/types/NotAssignableBetweenAnyAndStruct.ts new file mode 100644 index 0000000000000..26d84ee8631db --- /dev/null +++ b/tests/cases/conformance/structs/types/NotAssignableBetweenAnyAndStruct.ts @@ -0,0 +1,14 @@ +// doc 8 +// A struct cannot be assigned or cast to any type, and vice versa. + +var a: any; + +struct S { + foo: string; +} +var as: S; + +as = a; // error +as = a; // error +a = as; // error +a = as; // error diff --git a/tests/cases/conformance/structs/types/NotAssignableBetweenClassAndStruct.ts b/tests/cases/conformance/structs/types/NotAssignableBetweenClassAndStruct.ts new file mode 100644 index 0000000000000..b940afce7b078 --- /dev/null +++ b/tests/cases/conformance/structs/types/NotAssignableBetweenClassAndStruct.ts @@ -0,0 +1,28 @@ +// doc 8 +// A struct cannot be assigned or cast to class and vice versa, even if their type shapes are similar. + +class Point { + x: number; + y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y + } +} +var cp: Point = new Point(1,1); + +struct Point2 { + x: number; + y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y + } +} +var sp: Point2 = new Point2(2,2); + +cp = sp; // error +cp = sp; // error +sp = cp; // error +sp = cp; // error + diff --git a/tests/cases/conformance/structs/types/NotAssignableBetweenInterfaceAndStruct.ts b/tests/cases/conformance/structs/types/NotAssignableBetweenInterfaceAndStruct.ts new file mode 100644 index 0000000000000..4b1023eeae3a3 --- /dev/null +++ b/tests/cases/conformance/structs/types/NotAssignableBetweenInterfaceAndStruct.ts @@ -0,0 +1,22 @@ +// doc 8 +// A struct cannot be assigned to interface and vice versa, even if their type shapes are similar. +interface P { + x: number; + y: number; +} +var ip: P; + +struct Point2 { + x: number; + y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y + } +} +var sp: Point2 = new Point2(2,2); + +ip = sp; // error +ip =

sp; // error +sp = ip; // error +sp = ip; // error diff --git a/tests/cases/conformance/structs/types/NotAssignableBetweenObjectAndStruct.ts b/tests/cases/conformance/structs/types/NotAssignableBetweenObjectAndStruct.ts new file mode 100644 index 0000000000000..7c910dcff4bd6 --- /dev/null +++ b/tests/cases/conformance/structs/types/NotAssignableBetweenObjectAndStruct.ts @@ -0,0 +1,14 @@ +// doc 8 +// A struct cannot be assigned to object type, and vice versa. + +var o: {}; + +struct S { + foo: string; +} +var os: S; + +os = o; // error +os = o; // error +o = os; // error +o = os; // error diff --git a/tests/cases/conformance/structs/types/NotAssignableBetweenStructsWithoutInheritance.ts b/tests/cases/conformance/structs/types/NotAssignableBetweenStructsWithoutInheritance.ts new file mode 100644 index 0000000000000..57038cc849516 --- /dev/null +++ b/tests/cases/conformance/structs/types/NotAssignableBetweenStructsWithoutInheritance.ts @@ -0,0 +1,19 @@ +// doc 8 +// Only a subtype struct can be assigned to a supertype struct. + +struct Animal { + feet: number; + constructor(name: string, numFeet: number) { } +} + +struct Size { + feet: number; + constructor(numFeet: number) { } +} + +var a: Animal; +var s: Size; + +a = s; //error +s = a; //error + diff --git a/tests/cases/conformance/structs/types/returnType.ts b/tests/cases/conformance/structs/types/returnType.ts new file mode 100644 index 0000000000000..630851310568a --- /dev/null +++ b/tests/cases/conformance/structs/types/returnType.ts @@ -0,0 +1,11 @@ +// doc 8 + +struct Person { + constructor(public name: string, public age: number) {} +} + +var p: Person; +var test = (name: string, age: number) => new Person(name, age); +p = test("John", 25); +test = function (n, a) {return {name: n, age: a} }; // error, type not compatible + diff --git a/tests/cases/conformance/structs/types/structInitialization.ts b/tests/cases/conformance/structs/types/structInitialization.ts new file mode 100644 index 0000000000000..afb20c691feff --- /dev/null +++ b/tests/cases/conformance/structs/types/structInitialization.ts @@ -0,0 +1,14 @@ +// doc 7 +// Struct initialization requires explicit declaration and the sequence of the arguments matters. +struct Person { + constructor(public name: string, public age: number) {} +} +var john = new Person("John", 25); // ok +var george = new Person(25, "George"); // error, sequence matters +var jg = new Person("John", "George", 25); // error, wrong call signature +var john1 = new Person({name: "John", age: 25}); // error, wrong call signature + +var test = function (person: Person) {}; +test(john); // ok +test({ name: "John", age: 25 }); // error, type incompatibility. need an explicit declaration + From 405c9fa9fcc997e24138a09e2acf6d0f7201f0e2 Mon Sep 17 00:00:00 2001 From: jied Date: Wed, 25 Nov 2015 21:17:57 -0800 Subject: [PATCH 40/43] struct array --- src/compiler/checker.ts | 110 ++++++++++++++++++++++++++++++++++++++++ src/compiler/parser.ts | 2 +- src/compiler/types.ts | 6 ++- src/lib/core.d.ts | 21 ++++++++ 4 files changed, 137 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 546b9e4749c57..9ad4593acc70a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -142,6 +142,7 @@ namespace ts { let globalObjectType: ObjectType; let globalFunctionType: ObjectType; let globalArrayType: GenericType; + let globalStructArrayType: GenericType; let globalStringType: ObjectType; let globalNumberType: ObjectType; let globalBooleanType: ObjectType; @@ -3221,6 +3222,8 @@ namespace ts { return true; case SyntaxKind.ArrayType: return isIndependentType((node).elementType); + case SyntaxKind.StructArrayType: + return isIndependentType((node).elementType); case SyntaxKind.TypeReference: return isIndependentTypeReference(node); } @@ -4368,6 +4371,18 @@ parentSymbol = (declaration.parent).symbol; return createTypeFromGenericGlobalType(globalIterableIteratorType, [elementType]); } + function createStructArrayType(elementType: Type): Type { + return createTypeFromGenericGlobalType(globalStructArrayType, [elementType]); + } + + function getTypeFromStructArrayTypeNode(node: StructArrayTypeNode): Type { + let links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = createStructArrayType(getTypeFromTypeNode(node.elementType)); + } + return links.resolvedType; + } + function createArrayType(elementType: Type): Type { return createTypeFromGenericGlobalType(globalArrayType, [elementType]); } @@ -4620,6 +4635,8 @@ parentSymbol = (declaration.parent).symbol; return getTypeFromTypeQueryNode(node); case SyntaxKind.ArrayType: return getTypeFromArrayTypeNode(node); + case SyntaxKind.StructArrayType: + return getTypeFromStructArrayTypeNode(node); case SyntaxKind.TupleType: return getTypeFromTupleTypeNode(node); case SyntaxKind.UnionType: @@ -8348,6 +8365,75 @@ parentSymbol = (declaration.parent).symbol; return true; } + // replace IndexedAccess node to StructArray Node (refer to parseCallAndAccess in parser - how to build an Array Node) + // node.parent === NewExpression + // current parent node structure: + // node.parent.func = IndexedAccess (object + index) + // desired parent node structure: + // parent.func = Identifier (text: StructArray) + // parent.typeArguments = [TypeReference: T] <-- IndexedAccess.object + // parent.arguments = [NumericLiteral: 2] <-- IndexedAccess.index + // return StructArray type. the rest will be handled in resolveNewExpression. + function checkStructArrayInitialization(node: ElementAccessExpression): Type { + var parentNode = node.parent; + var indexedAccessNode = node; + var objectNode = indexedAccessNode.expression; + var indexNode = indexedAccessNode.argumentExpression; + + // reassign func to be StructArray + // no position info since we artificially add StructArray node + var func = new (getNodeConstructor(SyntaxKind.Identifier))(); + func.text = "StructArray"; + func.parent = parentNode; + parentNode.expression = func; + + // reassign typeArguments + var typeArguments = >[]; + typeArguments.pos = objectNode.pos; + typeArguments.end = objectNode.end; + parentNode.typeArguments = typeArguments; + + // reassign structType (should be TypeReference) + var structTypeReference = new (getNodeConstructor(SyntaxKind.TypeReference))(); + structTypeReference.pos = typeArguments.pos; + structTypeReference.end = typeArguments.end; + structTypeReference.parent = parentNode; + var structTypeName = objectNode; + structTypeReference.typeName = structTypeName; + structTypeName.parent = structTypeReference; + typeArguments.push(structTypeReference); + + // reassign arguments (store StructArray length) + var arguments = >[]; + arguments.pos = indexNode.pos; + arguments.end = indexNode.end; + parentNode.arguments = arguments; + var structArrayLength = indexNode; + structArrayLength.parent = parentNode; + arguments.push(structArrayLength); + + node = null; + return checkExpression(func); + } + + // check struct array index access bound + function checkStructArrayAccess(node: ElementAccessExpression) { + var objectNode = node.expression; + var indexNode = node.argumentExpression; + var symbol = getResolvedSymbol(objectNode); + var valueDeclaration = symbol.valueDeclaration; + var newExpression: NewExpression; + if (valueDeclaration && valueDeclaration.initializer) newExpression = valueDeclaration.initializer; + var arguments: NodeArray; + if (newExpression && newExpression.arguments) arguments = newExpression.arguments; + var arrayLength: string; + if (arguments) arrayLength = (arguments[0]).text; + var index = (indexNode).text; + if (+index >= +arrayLength) { + error(node, Diagnostics.Struct_array_index_out_of_bound, symbolToString(symbol), arrayLength); + } + } + function checkIndexedAccess(node: ElementAccessExpression): Type { // Grammar checking if (!node.argumentExpression) { @@ -8372,6 +8458,19 @@ parentSymbol = (declaration.parent).symbol; // Obtain base constraint such that we can bail out if the constraint is an unknown type let originObjectType = checkExpression(node.expression); let objectType = getApparentType(originObjectType); + + // check if node's actual type is struct array + if (objectType && objectType.symbol && getDeclarationKindFromSymbol(objectType.symbol) === SyntaxKind.StructDeclaration) { + if (node.parent.kind === SyntaxKind.NewExpression) { // struct array declaration + // write("struct array declaration"); + return checkStructArrayInitialization(node); + } + } + if (objectType && objectType.symbol && objectType.symbol.name === 'StructArray') { // struct array access + // write("struct array access"); + checkStructArrayAccess(node); + } + let indexType = node.argumentExpression ? checkExpression(node.argumentExpression) : unknownType; if (objectType === unknownType) { @@ -11741,6 +11840,10 @@ error(node, Diagnostics.Constructors_for_derived_structs_must_contain_a_super_ca checkTypeArgumentConstraints(typeParameters, node.typeArguments); } } + // check if structArray declaration + if (node.parent.kind === SyntaxKind.ArrayType && type.flags & TypeFlags.Struct) { + Object.getPrototypeOf(node.parent).kind = SyntaxKind.StructArrayType; + } } function checkTypeQuery(node: TypeQueryNode) { @@ -11760,6 +11863,10 @@ error(node, Diagnostics.Constructors_for_derived_structs_must_contain_a_super_ca checkSourceElement(node.elementType); } + function checkStructArrayType(node: StructArrayTypeNode) { + checkSourceElement(node.elementType); + } + function checkTupleType(node: TupleTypeNode) { // Grammar checking let hasErrorFromDisallowedTrailingComma = checkGrammarForDisallowedTrailingComma(node.elementTypes); @@ -14753,6 +14860,8 @@ getNodeLinks(node).flags |= NodeCheckFlags.LexicalModuleMergesWithStruct; return checkTypeLiteral(node); case SyntaxKind.ArrayType: return checkArrayType(node); + case SyntaxKind.StructArrayType: + return checkStructArrayType(node); case SyntaxKind.TupleType: return checkTupleType(node); case SyntaxKind.UnionType: @@ -15885,6 +15994,7 @@ getNodeLinks(node).flags |= NodeCheckFlags.LexicalModuleMergesWithStruct; // Initialize special types globalArrayType = getGlobalType("Array", /*arity*/ 1); + globalStructArrayType = getGlobalType("StructArray", /*arity*/ 1); globalObjectType = getGlobalType("Object"); globalFunctionType = getGlobalType("Function"); globalStringType = getGlobalType("String"); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index d8ebf8ac0f5a8..0a96b7bc5a3ae 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5116,7 +5116,7 @@ return true; if (isIndexSignature()) { let indexSingature: IndexSignatureDeclaration = parseIndexSignatureDeclaration(fullStart, decorators, modifiers); parseErrorAtPosition(indexSingature.pos, indexSingature.end - indexSingature.pos, Diagnostics.A_struct_can_not_have_index_members); - return indexSingature + return indexSingature; } // It is very important that we check this *after* checking indexers because diff --git a/src/compiler/types.ts b/src/compiler/types.ts index a89a77f159eba..84d043e8e9860 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -124,6 +124,7 @@ namespace ts { NewKeyword, NullKeyword, ReturnKeyword, + StructKeyword, SuperKeyword, SwitchKeyword, ThisKeyword, @@ -204,6 +205,7 @@ namespace ts { TypeQuery, TypeLiteral, ArrayType, + StructArrayType, TupleType, UnionType, IntersectionType, @@ -258,7 +260,6 @@ namespace ts { ContinueStatement, BreakStatement, ReturnStatement, - StructKeyword, WithStatement, SwitchStatement, LabeledStatement, @@ -687,6 +688,9 @@ namespace ts { elementType: TypeNode; } + export interface StructArrayTypeNode extends ArrayTypeNode { + } + export interface TupleTypeNode extends TypeNode { elementTypes: NodeArray; } diff --git a/src/lib/core.d.ts b/src/lib/core.d.ts index 7259ff5081c31..00e552d07df51 100644 --- a/src/lib/core.d.ts +++ b/src/lib/core.d.ts @@ -1156,6 +1156,27 @@ interface ArrayConstructor { declare var Array: ArrayConstructor; +interface StructArray { + /** + * Gets or sets the length of the array. This is a number one higher than the highest element defined in an array. + */ + length: number; + /** + * Returns a string representation of an array. + */ + toString(): string; + toLocaleString(): string; + + [n: number]: T; +} + +interface StructArrayConstructor { + new (arrayLength: number): StructArray; + (arrayLength: number): StructArray; +} + +declare var StructArray: StructArrayConstructor; + interface TypedPropertyDescriptor { enumerable?: boolean; configurable?: boolean; From 8998314409b05e68188f6f56a5e7b76abbd8292e Mon Sep 17 00:00:00 2001 From: jied Date: Wed, 25 Nov 2015 21:19:30 -0800 Subject: [PATCH 41/43] automatic generated changes --- lib/tsc.js | 38 +++++++++++++-------------- lib/tsserver.js | 42 +++++++++++++++--------------- lib/typescript.js | 42 +++++++++++++++--------------- lib/typescriptServices.js | 42 +++++++++++++++--------------- src/compiler/declarationEmitter.ts | 2 +- src/harness/typeWriter.ts | 2 +- src/services/services.ts | 4 +-- 7 files changed, 86 insertions(+), 86 deletions(-) diff --git a/lib/tsc.js b/lib/tsc.js index 07ff723b17cb7..cfa83695eb9d3 100644 --- a/lib/tsc.js +++ b/lib/tsc.js @@ -1321,7 +1321,7 @@ var ts; Constraint_of_a_type_parameter_cannot_reference_any_type_parameter_from_the_same_type_parameter_list: { code: 2313, category: ts.DiagnosticCategory.Error, key: "Constraint of a type parameter cannot reference any type parameter from the same type parameter list." }, Generic_type_0_requires_1_type_argument_s: { code: 2314, category: ts.DiagnosticCategory.Error, key: "Generic type '{0}' requires {1} type argument(s)." }, Type_0_is_not_generic: { code: 2315, category: ts.DiagnosticCategory.Error, key: "Type '{0}' is not generic." }, - Global_type_0_must_be_a_class_or_interface_type: { code: 2316, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must be a class or interface type." }, + Global_type_0_must_be_a_class_or_struct_or_interface_type: { code: 2316, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must be a class or interface type." }, Global_type_0_must_have_1_type_parameter_s: { code: 2317, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must have {1} type parameter(s)." }, Cannot_find_global_type_0: { code: 2318, category: ts.DiagnosticCategory.Error, key: "Cannot find global type '{0}'." }, Named_property_0_of_types_1_and_2_are_not_identical: { code: 2319, category: ts.DiagnosticCategory.Error, key: "Named property '{0}' of types '{1}' and '{2}' are not identical." }, @@ -1428,8 +1428,8 @@ var ts; Interface_0_incorrectly_extends_interface_1: { code: 2430, category: ts.DiagnosticCategory.Error, key: "Interface '{0}' incorrectly extends interface '{1}'." }, Enum_name_cannot_be_0: { code: 2431, category: ts.DiagnosticCategory.Error, key: "Enum name cannot be '{0}'" }, In_an_enum_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_its_first_enum_element: { code: 2432, category: ts.DiagnosticCategory.Error, key: "In an enum with multiple declarations, only one declaration can omit an initializer for its first enum element." }, - A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged: { code: 2433, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be in a different file from a class or function with which it is merged" }, - A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged: { code: 2434, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be located prior to a class or function with which it is merged" }, + A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_struct_or_function_with_which_it_is_merged: { code: 2433, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be in a different file from a class or function with which it is merged" }, + A_namespace_declaration_cannot_be_located_prior_to_a_class_or_struct_or_function_with_which_it_is_merged: { code: 2434, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be located prior to a class or function with which it is merged" }, Ambient_modules_cannot_be_nested_in_other_modules_or_namespaces: { code: 2435, category: ts.DiagnosticCategory.Error, key: "Ambient modules cannot be nested in other modules or namespaces." }, Ambient_module_declaration_cannot_specify_relative_module_name: { code: 2436, category: ts.DiagnosticCategory.Error, key: "Ambient module declaration cannot specify relative module name." }, Module_0_is_hidden_by_a_local_declaration_with_the_same_name: { code: 2437, category: ts.DiagnosticCategory.Error, key: "Module '{0}' is hidden by a local declaration with the same name" }, @@ -4916,7 +4916,7 @@ var ts; var heritageClause = getHeritageClause(node.heritageClauses, 83); return heritageClause && heritageClause.types.length > 0 ? heritageClause.types[0] : undefined; } - ts.getClassExtendsHeritageClauseElement = getClassExtendsHeritageClauseElement; + ts.getClassOrStructExtendsHeritageClauseElement = getClassExtendsHeritageClauseElement; function getClassImplementsHeritageClauseElements(node) { var heritageClause = getHeritageClause(node.heritageClauses, 106); return heritageClause ? heritageClause.types : undefined; @@ -5532,7 +5532,7 @@ var ts; node.parent.token === 83 && isClassLike(node.parent.parent); } - ts.isExpressionWithTypeArgumentsInClassExtendsClause = isExpressionWithTypeArgumentsInClassExtendsClause; + ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause = isExpressionWithTypeArgumentsInClassExtendsClause; function isSupportedExpressionWithTypeArguments(node) { return isSupportedExpressionWithTypeArgumentsRest(node.expression); } @@ -12593,7 +12593,7 @@ var ts; return type.flags & 80896 && getSignaturesOfType(type, 1).length > 0; } function getBaseTypeNodeOfClass(type) { - return ts.getClassExtendsHeritageClauseElement(type.symbol.valueDeclaration); + return ts.getClassOrStructExtendsHeritageClauseElement(type.symbol.valueDeclaration); } function getConstructorsForTypeArguments(type, typeArgumentNodes) { var typeArgCount = typeArgumentNodes ? typeArgumentNodes.length : 0; @@ -13740,7 +13740,7 @@ var ts; } var type = getDeclaredTypeOfSymbol(symbol); if (!(type.flags & 80896)) { - error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); + error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_struct_or_interface_type, symbol.name); return arity ? emptyGenericType : emptyObjectType; } if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { @@ -15855,7 +15855,7 @@ var ts; } } if (!baseClassType) { - if (!classDeclaration || !ts.getClassExtendsHeritageClauseElement(classDeclaration)) { + if (!classDeclaration || !ts.getClassOrStructExtendsHeritageClauseElement(classDeclaration)) { error(node, ts.Diagnostics.super_can_only_be_referenced_in_a_derived_class); } return unknownType; @@ -17452,7 +17452,7 @@ var ts; if (node.expression.kind === 95) { var superType = checkSuperExpression(node.expression); if (superType !== unknownType) { - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(ts.getContainingClass(node)); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(ts.getContainingClass(node)); var baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments); return resolveCall(node, baseConstructors, candidatesOutArray); } @@ -18757,7 +18757,7 @@ var ts; !!n.initializer; } var containingClassDecl = node.parent; - if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { + if (ts.getClassOrStructExtendsHeritageClauseElement(containingClassDecl)) { var containingClassSymbol = getSymbolOfNode(containingClassDecl); var containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); var baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); @@ -19481,7 +19481,7 @@ var ts; if (!enclosingClass || ts.isInAmbientContext(enclosingClass)) { return; } - if (ts.getClassExtendsHeritageClauseElement(enclosingClass)) { + if (ts.getClassOrStructExtendsHeritageClauseElement(enclosingClass)) { var isDeclaration_2 = node.kind !== 69; if (isDeclaration_2) { error(node, ts.Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); @@ -20121,7 +20121,7 @@ var ts; var type = getDeclaredTypeOfSymbol(symbol); var typeWithThis = getTypeWithThisArgument(type); var staticType = getTypeOfSymbol(symbol); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitExtends = emitExtends || !ts.isInAmbientContext(node); var baseTypes = getBaseTypes(type); @@ -20606,10 +20606,10 @@ var ts; var firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); if (firstNonAmbientClassOrFunc) { if (ts.getSourceFileOfNode(node) !== ts.getSourceFileOfNode(firstNonAmbientClassOrFunc)) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged); + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_struct_or_function_with_which_it_is_merged); } else if (node.pos < firstNonAmbientClassOrFunc.pos) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged); + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_struct_or_function_with_which_it_is_merged); } } var mergedClass = ts.getDeclarationOfKind(symbol, 214); @@ -21265,7 +21265,7 @@ var ts; var meaning = 0; if (entityName.parent.kind === 188) { meaning = 793056; - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { + if (ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(entityName.parent)) { meaning |= 107455; } } @@ -21391,7 +21391,7 @@ var ts; if (ts.isExpression(node)) { return getTypeOfExpression(node); } - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(node)) { + if (ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node)) { return getBaseTypes(getDeclaredTypeOfSymbol(getSymbolOfNode(node.parent.parent)))[0]; } if (isTypeDeclaration(node)) { @@ -23484,7 +23484,7 @@ var ts; var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitHeritageClause([baseTypeNode], false); } @@ -27749,7 +27749,7 @@ var ts; write(" "); emitDeclarationName(node); } - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { write(" extends "); emit(baseTypeNode.expression); @@ -27810,7 +27810,7 @@ var ts; write(" = "); } write("(function ("); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { write("_super"); } diff --git a/lib/tsserver.js b/lib/tsserver.js index 2832b8b535ac6..c71783025b36b 100644 --- a/lib/tsserver.js +++ b/lib/tsserver.js @@ -1321,7 +1321,7 @@ var ts; Constraint_of_a_type_parameter_cannot_reference_any_type_parameter_from_the_same_type_parameter_list: { code: 2313, category: ts.DiagnosticCategory.Error, key: "Constraint of a type parameter cannot reference any type parameter from the same type parameter list." }, Generic_type_0_requires_1_type_argument_s: { code: 2314, category: ts.DiagnosticCategory.Error, key: "Generic type '{0}' requires {1} type argument(s)." }, Type_0_is_not_generic: { code: 2315, category: ts.DiagnosticCategory.Error, key: "Type '{0}' is not generic." }, - Global_type_0_must_be_a_class_or_interface_type: { code: 2316, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must be a class or interface type." }, + Global_type_0_must_be_a_class_or_struct_or_interface_type: { code: 2316, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must be a class or interface type." }, Global_type_0_must_have_1_type_parameter_s: { code: 2317, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must have {1} type parameter(s)." }, Cannot_find_global_type_0: { code: 2318, category: ts.DiagnosticCategory.Error, key: "Cannot find global type '{0}'." }, Named_property_0_of_types_1_and_2_are_not_identical: { code: 2319, category: ts.DiagnosticCategory.Error, key: "Named property '{0}' of types '{1}' and '{2}' are not identical." }, @@ -1428,8 +1428,8 @@ var ts; Interface_0_incorrectly_extends_interface_1: { code: 2430, category: ts.DiagnosticCategory.Error, key: "Interface '{0}' incorrectly extends interface '{1}'." }, Enum_name_cannot_be_0: { code: 2431, category: ts.DiagnosticCategory.Error, key: "Enum name cannot be '{0}'" }, In_an_enum_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_its_first_enum_element: { code: 2432, category: ts.DiagnosticCategory.Error, key: "In an enum with multiple declarations, only one declaration can omit an initializer for its first enum element." }, - A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged: { code: 2433, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be in a different file from a class or function with which it is merged" }, - A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged: { code: 2434, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be located prior to a class or function with which it is merged" }, + A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_struct_or_function_with_which_it_is_merged: { code: 2433, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be in a different file from a class or function with which it is merged" }, + A_namespace_declaration_cannot_be_located_prior_to_a_class_or_struct_or_function_with_which_it_is_merged: { code: 2434, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be located prior to a class or function with which it is merged" }, Ambient_modules_cannot_be_nested_in_other_modules_or_namespaces: { code: 2435, category: ts.DiagnosticCategory.Error, key: "Ambient modules cannot be nested in other modules or namespaces." }, Ambient_module_declaration_cannot_specify_relative_module_name: { code: 2436, category: ts.DiagnosticCategory.Error, key: "Ambient module declaration cannot specify relative module name." }, Module_0_is_hidden_by_a_local_declaration_with_the_same_name: { code: 2437, category: ts.DiagnosticCategory.Error, key: "Module '{0}' is hidden by a local declaration with the same name" }, @@ -4649,7 +4649,7 @@ var ts; var heritageClause = getHeritageClause(node.heritageClauses, 83); return heritageClause && heritageClause.types.length > 0 ? heritageClause.types[0] : undefined; } - ts.getClassExtendsHeritageClauseElement = getClassExtendsHeritageClauseElement; + ts.getClassOrStructExtendsHeritageClauseElement = getClassExtendsHeritageClauseElement; function getClassImplementsHeritageClauseElements(node) { var heritageClause = getHeritageClause(node.heritageClauses, 106); return heritageClause ? heritageClause.types : undefined; @@ -5265,7 +5265,7 @@ var ts; node.parent.token === 83 && isClassLike(node.parent.parent); } - ts.isExpressionWithTypeArgumentsInClassExtendsClause = isExpressionWithTypeArgumentsInClassExtendsClause; + ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause = isExpressionWithTypeArgumentsInClassExtendsClause; function isSupportedExpressionWithTypeArguments(node) { return isSupportedExpressionWithTypeArgumentsRest(node.expression); } @@ -13056,7 +13056,7 @@ var ts; return type.flags & 80896 && getSignaturesOfType(type, 1).length > 0; } function getBaseTypeNodeOfClass(type) { - return ts.getClassExtendsHeritageClauseElement(type.symbol.valueDeclaration); + return ts.getClassOrStructExtendsHeritageClauseElement(type.symbol.valueDeclaration); } function getConstructorsForTypeArguments(type, typeArgumentNodes) { var typeArgCount = typeArgumentNodes ? typeArgumentNodes.length : 0; @@ -14203,7 +14203,7 @@ var ts; } var type = getDeclaredTypeOfSymbol(symbol); if (!(type.flags & 80896)) { - error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); + error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_struct_or_interface_type, symbol.name); return arity ? emptyGenericType : emptyObjectType; } if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { @@ -16318,7 +16318,7 @@ var ts; } } if (!baseClassType) { - if (!classDeclaration || !ts.getClassExtendsHeritageClauseElement(classDeclaration)) { + if (!classDeclaration || !ts.getClassOrStructExtendsHeritageClauseElement(classDeclaration)) { error(node, ts.Diagnostics.super_can_only_be_referenced_in_a_derived_class); } return unknownType; @@ -17915,7 +17915,7 @@ var ts; if (node.expression.kind === 95) { var superType = checkSuperExpression(node.expression); if (superType !== unknownType) { - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(ts.getContainingClass(node)); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(ts.getContainingClass(node)); var baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments); return resolveCall(node, baseConstructors, candidatesOutArray); } @@ -19220,7 +19220,7 @@ var ts; !!n.initializer; } var containingClassDecl = node.parent; - if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { + if (ts.getClassOrStructExtendsHeritageClauseElement(containingClassDecl)) { var containingClassSymbol = getSymbolOfNode(containingClassDecl); var containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); var baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); @@ -19944,7 +19944,7 @@ var ts; if (!enclosingClass || ts.isInAmbientContext(enclosingClass)) { return; } - if (ts.getClassExtendsHeritageClauseElement(enclosingClass)) { + if (ts.getClassOrStructExtendsHeritageClauseElement(enclosingClass)) { var isDeclaration_2 = node.kind !== 69; if (isDeclaration_2) { error(node, ts.Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); @@ -20584,7 +20584,7 @@ var ts; var type = getDeclaredTypeOfSymbol(symbol); var typeWithThis = getTypeWithThisArgument(type); var staticType = getTypeOfSymbol(symbol); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitExtends = emitExtends || !ts.isInAmbientContext(node); var baseTypes = getBaseTypes(type); @@ -21069,10 +21069,10 @@ var ts; var firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); if (firstNonAmbientClassOrFunc) { if (ts.getSourceFileOfNode(node) !== ts.getSourceFileOfNode(firstNonAmbientClassOrFunc)) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged); + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_struct_or_function_with_which_it_is_merged); } else if (node.pos < firstNonAmbientClassOrFunc.pos) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged); + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_struct_or_function_with_which_it_is_merged); } } var mergedClass = ts.getDeclarationOfKind(symbol, 214); @@ -21728,7 +21728,7 @@ var ts; var meaning = 0; if (entityName.parent.kind === 188) { meaning = 793056; - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { + if (ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(entityName.parent)) { meaning |= 107455; } } @@ -21854,7 +21854,7 @@ var ts; if (ts.isExpression(node)) { return getTypeOfExpression(node); } - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(node)) { + if (ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node)) { return getBaseTypes(getDeclaredTypeOfSymbol(getSymbolOfNode(node.parent.parent)))[0]; } if (isTypeDeclaration(node)) { @@ -23947,7 +23947,7 @@ var ts; var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitHeritageClause([baseTypeNode], false); } @@ -28212,7 +28212,7 @@ var ts; write(" "); emitDeclarationName(node); } - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { write(" extends "); emit(baseTypeNode.expression); @@ -28273,7 +28273,7 @@ var ts; write(" = "); } write("(function ("); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { write("_super"); } @@ -39888,7 +39888,7 @@ var ts; if (symbol && symbol.flags & (32 | 64)) { ts.forEach(symbol.getDeclarations(), function (declaration) { if (declaration.kind === 214) { - getPropertySymbolFromTypeReference(ts.getClassExtendsHeritageClauseElement(declaration)); + getPropertySymbolFromTypeReference(ts.getClassOrStructExtendsHeritageClauseElement(declaration)); ts.forEach(ts.getClassImplementsHeritageClauseElements(declaration), getPropertySymbolFromTypeReference); } else if (declaration.kind === 215) { @@ -40095,7 +40095,7 @@ var ts; node = node.parent; } return node.parent.kind === 151 || - (node.parent.kind === 188 && !ts.isExpressionWithTypeArgumentsInClassExtendsClause(node.parent)) || + (node.parent.kind === 188 && !ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node.parent)) || node.kind === 97 && !ts.isExpression(node); } function isNamespaceReference(node) { diff --git a/lib/typescript.js b/lib/typescript.js index 7f9c5c293ad9e..c818d2c83df48 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -2236,7 +2236,7 @@ var ts; Constraint_of_a_type_parameter_cannot_reference_any_type_parameter_from_the_same_type_parameter_list: { code: 2313, category: ts.DiagnosticCategory.Error, key: "Constraint of a type parameter cannot reference any type parameter from the same type parameter list." }, Generic_type_0_requires_1_type_argument_s: { code: 2314, category: ts.DiagnosticCategory.Error, key: "Generic type '{0}' requires {1} type argument(s)." }, Type_0_is_not_generic: { code: 2315, category: ts.DiagnosticCategory.Error, key: "Type '{0}' is not generic." }, - Global_type_0_must_be_a_class_or_interface_type: { code: 2316, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must be a class or interface type." }, + Global_type_0_must_be_a_class_or_struct_or_interface_type: { code: 2316, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must be a class or interface type." }, Global_type_0_must_have_1_type_parameter_s: { code: 2317, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must have {1} type parameter(s)." }, Cannot_find_global_type_0: { code: 2318, category: ts.DiagnosticCategory.Error, key: "Cannot find global type '{0}'." }, Named_property_0_of_types_1_and_2_are_not_identical: { code: 2319, category: ts.DiagnosticCategory.Error, key: "Named property '{0}' of types '{1}' and '{2}' are not identical." }, @@ -2343,8 +2343,8 @@ var ts; Interface_0_incorrectly_extends_interface_1: { code: 2430, category: ts.DiagnosticCategory.Error, key: "Interface '{0}' incorrectly extends interface '{1}'." }, Enum_name_cannot_be_0: { code: 2431, category: ts.DiagnosticCategory.Error, key: "Enum name cannot be '{0}'" }, In_an_enum_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_its_first_enum_element: { code: 2432, category: ts.DiagnosticCategory.Error, key: "In an enum with multiple declarations, only one declaration can omit an initializer for its first enum element." }, - A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged: { code: 2433, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be in a different file from a class or function with which it is merged" }, - A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged: { code: 2434, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be located prior to a class or function with which it is merged" }, + A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_struct_or_function_with_which_it_is_merged: { code: 2433, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be in a different file from a class or function with which it is merged" }, + A_namespace_declaration_cannot_be_located_prior_to_a_class_or_struct_or_function_with_which_it_is_merged: { code: 2434, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be located prior to a class or function with which it is merged" }, Ambient_modules_cannot_be_nested_in_other_modules_or_namespaces: { code: 2435, category: ts.DiagnosticCategory.Error, key: "Ambient modules cannot be nested in other modules or namespaces." }, Ambient_module_declaration_cannot_specify_relative_module_name: { code: 2436, category: ts.DiagnosticCategory.Error, key: "Ambient module declaration cannot specify relative module name." }, Module_0_is_hidden_by_a_local_declaration_with_the_same_name: { code: 2437, category: ts.DiagnosticCategory.Error, key: "Module '{0}' is hidden by a local declaration with the same name" }, @@ -6351,7 +6351,7 @@ var ts; var heritageClause = getHeritageClause(node.heritageClauses, 83 /* ExtendsKeyword */); return heritageClause && heritageClause.types.length > 0 ? heritageClause.types[0] : undefined; } - ts.getClassExtendsHeritageClauseElement = getClassExtendsHeritageClauseElement; + ts.getClassOrStructExtendsHeritageClauseElement = getClassExtendsHeritageClauseElement; function getClassImplementsHeritageClauseElements(node) { var heritageClause = getHeritageClause(node.heritageClauses, 106 /* ImplementsKeyword */); return heritageClause ? heritageClause.types : undefined; @@ -7023,7 +7023,7 @@ var ts; node.parent.token === 83 /* ExtendsKeyword */ && isClassLike(node.parent.parent); } - ts.isExpressionWithTypeArgumentsInClassExtendsClause = isExpressionWithTypeArgumentsInClassExtendsClause; + ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause = isExpressionWithTypeArgumentsInClassExtendsClause; // Returns false if this heritage clause element's expression contains something unsupported // (i.e. not a name or dotted name). function isSupportedExpressionWithTypeArguments(node) { @@ -15765,7 +15765,7 @@ var ts; return type.flags & 80896 /* ObjectType */ && getSignaturesOfType(type, 1 /* Construct */).length > 0; } function getBaseTypeNodeOfClass(type) { - return ts.getClassExtendsHeritageClauseElement(type.symbol.valueDeclaration); + return ts.getClassOrStructExtendsHeritageClauseElement(type.symbol.valueDeclaration); } function getConstructorsForTypeArguments(type, typeArgumentNodes) { var typeArgCount = typeArgumentNodes ? typeArgumentNodes.length : 0; @@ -17033,7 +17033,7 @@ var ts; } var type = getDeclaredTypeOfSymbol(symbol); if (!(type.flags & 80896 /* ObjectType */)) { - error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); + error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_struct_or_interface_type, symbol.name); return arity ? emptyGenericType : emptyObjectType; } if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { @@ -19394,7 +19394,7 @@ var ts; } } if (!baseClassType) { - if (!classDeclaration || !ts.getClassExtendsHeritageClauseElement(classDeclaration)) { + if (!classDeclaration || !ts.getClassOrStructExtendsHeritageClauseElement(classDeclaration)) { error(node, ts.Diagnostics.super_can_only_be_referenced_in_a_derived_class); } return unknownType; @@ -21526,7 +21526,7 @@ var ts; if (superType !== unknownType) { // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated // with the type arguments specified in the extends clause. - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(ts.getContainingClass(node)); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(ts.getContainingClass(node)); var baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments); return resolveCall(node, baseConstructors, candidatesOutArray); } @@ -23096,7 +23096,7 @@ var ts; // Constructors of classes with no extends clause may not contain super calls, whereas // constructors of derived classes must contain at least one super call somewhere in their function body. var containingClassDecl = node.parent; - if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { + if (ts.getClassOrStructExtendsHeritageClauseElement(containingClassDecl)) { var containingClassSymbol = getSymbolOfNode(containingClassDecl); var containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); var baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); @@ -24050,7 +24050,7 @@ var ts; if (!enclosingClass || ts.isInAmbientContext(enclosingClass)) { return; } - if (ts.getClassExtendsHeritageClauseElement(enclosingClass)) { + if (ts.getClassOrStructExtendsHeritageClauseElement(enclosingClass)) { var isDeclaration_2 = node.kind !== 69 /* Identifier */; if (isDeclaration_2) { error(node, ts.Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); @@ -24882,7 +24882,7 @@ var ts; var type = getDeclaredTypeOfSymbol(symbol); var typeWithThis = getTypeWithThisArgument(type); var staticType = getTypeOfSymbol(symbol); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitExtends = emitExtends || !ts.isInAmbientContext(node); var baseTypes = getBaseTypes(type); @@ -25433,10 +25433,10 @@ var ts; var firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); if (firstNonAmbientClassOrFunc) { if (ts.getSourceFileOfNode(node) !== ts.getSourceFileOfNode(firstNonAmbientClassOrFunc)) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged); + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_struct_or_function_with_which_it_is_merged); } else if (node.pos < firstNonAmbientClassOrFunc.pos) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged); + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_struct_or_function_with_which_it_is_merged); } } // if the module merges with a class declaration in the same lexical scope, @@ -26152,7 +26152,7 @@ var ts; if (entityName.parent.kind === 188 /* ExpressionWithTypeArguments */) { meaning = 793056 /* Type */; // In a class 'extends' clause we are also looking for a value. - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { + if (ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(entityName.parent)) { meaning |= 107455 /* Value */; } } @@ -26294,7 +26294,7 @@ var ts; if (ts.isExpression(node)) { return getTypeOfExpression(node); } - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(node)) { + if (ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node)) { // A SyntaxKind.ExpressionWithTypeArguments is considered a type node, except when it occurs in the // extends clause of a class. We handle that case here. return getBaseTypes(getDeclaredTypeOfSymbol(getSymbolOfNode(node.parent.parent)))[0]; @@ -28547,7 +28547,7 @@ var ts; var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitHeritageClause([baseTypeNode], /*isImplementsList*/ false); } @@ -33525,7 +33525,7 @@ var ts; write(" "); emitDeclarationName(node); } - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { write(" extends "); emit(baseTypeNode.expression); @@ -33603,7 +33603,7 @@ var ts; write(" = "); } write("(function ("); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { write("_super"); } @@ -47414,7 +47414,7 @@ var ts; if (symbol && symbol.flags & (32 /* Class */ | 64 /* Interface */)) { ts.forEach(symbol.getDeclarations(), function (declaration) { if (declaration.kind === 214 /* ClassDeclaration */) { - getPropertySymbolFromTypeReference(ts.getClassExtendsHeritageClauseElement(declaration)); + getPropertySymbolFromTypeReference(ts.getClassOrStructExtendsHeritageClauseElement(declaration)); ts.forEach(ts.getClassImplementsHeritageClauseElements(declaration), getPropertySymbolFromTypeReference); } else if (declaration.kind === 215 /* InterfaceDeclaration */) { @@ -47649,7 +47649,7 @@ var ts; node = node.parent; } return node.parent.kind === 151 /* TypeReference */ || - (node.parent.kind === 188 /* ExpressionWithTypeArguments */ && !ts.isExpressionWithTypeArgumentsInClassExtendsClause(node.parent)) || + (node.parent.kind === 188 /* ExpressionWithTypeArguments */ && !ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node.parent)) || node.kind === 97 /* ThisKeyword */ && !ts.isExpression(node); } function isNamespaceReference(node) { diff --git a/lib/typescriptServices.js b/lib/typescriptServices.js index 7f9c5c293ad9e..c818d2c83df48 100644 --- a/lib/typescriptServices.js +++ b/lib/typescriptServices.js @@ -2236,7 +2236,7 @@ var ts; Constraint_of_a_type_parameter_cannot_reference_any_type_parameter_from_the_same_type_parameter_list: { code: 2313, category: ts.DiagnosticCategory.Error, key: "Constraint of a type parameter cannot reference any type parameter from the same type parameter list." }, Generic_type_0_requires_1_type_argument_s: { code: 2314, category: ts.DiagnosticCategory.Error, key: "Generic type '{0}' requires {1} type argument(s)." }, Type_0_is_not_generic: { code: 2315, category: ts.DiagnosticCategory.Error, key: "Type '{0}' is not generic." }, - Global_type_0_must_be_a_class_or_interface_type: { code: 2316, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must be a class or interface type." }, + Global_type_0_must_be_a_class_or_struct_or_interface_type: { code: 2316, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must be a class or interface type." }, Global_type_0_must_have_1_type_parameter_s: { code: 2317, category: ts.DiagnosticCategory.Error, key: "Global type '{0}' must have {1} type parameter(s)." }, Cannot_find_global_type_0: { code: 2318, category: ts.DiagnosticCategory.Error, key: "Cannot find global type '{0}'." }, Named_property_0_of_types_1_and_2_are_not_identical: { code: 2319, category: ts.DiagnosticCategory.Error, key: "Named property '{0}' of types '{1}' and '{2}' are not identical." }, @@ -2343,8 +2343,8 @@ var ts; Interface_0_incorrectly_extends_interface_1: { code: 2430, category: ts.DiagnosticCategory.Error, key: "Interface '{0}' incorrectly extends interface '{1}'." }, Enum_name_cannot_be_0: { code: 2431, category: ts.DiagnosticCategory.Error, key: "Enum name cannot be '{0}'" }, In_an_enum_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_its_first_enum_element: { code: 2432, category: ts.DiagnosticCategory.Error, key: "In an enum with multiple declarations, only one declaration can omit an initializer for its first enum element." }, - A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged: { code: 2433, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be in a different file from a class or function with which it is merged" }, - A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged: { code: 2434, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be located prior to a class or function with which it is merged" }, + A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_struct_or_function_with_which_it_is_merged: { code: 2433, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be in a different file from a class or function with which it is merged" }, + A_namespace_declaration_cannot_be_located_prior_to_a_class_or_struct_or_function_with_which_it_is_merged: { code: 2434, category: ts.DiagnosticCategory.Error, key: "A namespace declaration cannot be located prior to a class or function with which it is merged" }, Ambient_modules_cannot_be_nested_in_other_modules_or_namespaces: { code: 2435, category: ts.DiagnosticCategory.Error, key: "Ambient modules cannot be nested in other modules or namespaces." }, Ambient_module_declaration_cannot_specify_relative_module_name: { code: 2436, category: ts.DiagnosticCategory.Error, key: "Ambient module declaration cannot specify relative module name." }, Module_0_is_hidden_by_a_local_declaration_with_the_same_name: { code: 2437, category: ts.DiagnosticCategory.Error, key: "Module '{0}' is hidden by a local declaration with the same name" }, @@ -6351,7 +6351,7 @@ var ts; var heritageClause = getHeritageClause(node.heritageClauses, 83 /* ExtendsKeyword */); return heritageClause && heritageClause.types.length > 0 ? heritageClause.types[0] : undefined; } - ts.getClassExtendsHeritageClauseElement = getClassExtendsHeritageClauseElement; + ts.getClassOrStructExtendsHeritageClauseElement = getClassExtendsHeritageClauseElement; function getClassImplementsHeritageClauseElements(node) { var heritageClause = getHeritageClause(node.heritageClauses, 106 /* ImplementsKeyword */); return heritageClause ? heritageClause.types : undefined; @@ -7023,7 +7023,7 @@ var ts; node.parent.token === 83 /* ExtendsKeyword */ && isClassLike(node.parent.parent); } - ts.isExpressionWithTypeArgumentsInClassExtendsClause = isExpressionWithTypeArgumentsInClassExtendsClause; + ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause = isExpressionWithTypeArgumentsInClassExtendsClause; // Returns false if this heritage clause element's expression contains something unsupported // (i.e. not a name or dotted name). function isSupportedExpressionWithTypeArguments(node) { @@ -15765,7 +15765,7 @@ var ts; return type.flags & 80896 /* ObjectType */ && getSignaturesOfType(type, 1 /* Construct */).length > 0; } function getBaseTypeNodeOfClass(type) { - return ts.getClassExtendsHeritageClauseElement(type.symbol.valueDeclaration); + return ts.getClassOrStructExtendsHeritageClauseElement(type.symbol.valueDeclaration); } function getConstructorsForTypeArguments(type, typeArgumentNodes) { var typeArgCount = typeArgumentNodes ? typeArgumentNodes.length : 0; @@ -17033,7 +17033,7 @@ var ts; } var type = getDeclaredTypeOfSymbol(symbol); if (!(type.flags & 80896 /* ObjectType */)) { - error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); + error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_struct_or_interface_type, symbol.name); return arity ? emptyGenericType : emptyObjectType; } if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { @@ -19394,7 +19394,7 @@ var ts; } } if (!baseClassType) { - if (!classDeclaration || !ts.getClassExtendsHeritageClauseElement(classDeclaration)) { + if (!classDeclaration || !ts.getClassOrStructExtendsHeritageClauseElement(classDeclaration)) { error(node, ts.Diagnostics.super_can_only_be_referenced_in_a_derived_class); } return unknownType; @@ -21526,7 +21526,7 @@ var ts; if (superType !== unknownType) { // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated // with the type arguments specified in the extends clause. - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(ts.getContainingClass(node)); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(ts.getContainingClass(node)); var baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments); return resolveCall(node, baseConstructors, candidatesOutArray); } @@ -23096,7 +23096,7 @@ var ts; // Constructors of classes with no extends clause may not contain super calls, whereas // constructors of derived classes must contain at least one super call somewhere in their function body. var containingClassDecl = node.parent; - if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { + if (ts.getClassOrStructExtendsHeritageClauseElement(containingClassDecl)) { var containingClassSymbol = getSymbolOfNode(containingClassDecl); var containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); var baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); @@ -24050,7 +24050,7 @@ var ts; if (!enclosingClass || ts.isInAmbientContext(enclosingClass)) { return; } - if (ts.getClassExtendsHeritageClauseElement(enclosingClass)) { + if (ts.getClassOrStructExtendsHeritageClauseElement(enclosingClass)) { var isDeclaration_2 = node.kind !== 69 /* Identifier */; if (isDeclaration_2) { error(node, ts.Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); @@ -24882,7 +24882,7 @@ var ts; var type = getDeclaredTypeOfSymbol(symbol); var typeWithThis = getTypeWithThisArgument(type); var staticType = getTypeOfSymbol(symbol); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitExtends = emitExtends || !ts.isInAmbientContext(node); var baseTypes = getBaseTypes(type); @@ -25433,10 +25433,10 @@ var ts; var firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); if (firstNonAmbientClassOrFunc) { if (ts.getSourceFileOfNode(node) !== ts.getSourceFileOfNode(firstNonAmbientClassOrFunc)) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged); + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_struct_or_function_with_which_it_is_merged); } else if (node.pos < firstNonAmbientClassOrFunc.pos) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged); + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_struct_or_function_with_which_it_is_merged); } } // if the module merges with a class declaration in the same lexical scope, @@ -26152,7 +26152,7 @@ var ts; if (entityName.parent.kind === 188 /* ExpressionWithTypeArguments */) { meaning = 793056 /* Type */; // In a class 'extends' clause we are also looking for a value. - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { + if (ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(entityName.parent)) { meaning |= 107455 /* Value */; } } @@ -26294,7 +26294,7 @@ var ts; if (ts.isExpression(node)) { return getTypeOfExpression(node); } - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(node)) { + if (ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node)) { // A SyntaxKind.ExpressionWithTypeArguments is considered a type node, except when it occurs in the // extends clause of a class. We handle that case here. return getBaseTypes(getDeclaredTypeOfSymbol(getSymbolOfNode(node.parent.parent)))[0]; @@ -28547,7 +28547,7 @@ var ts; var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitHeritageClause([baseTypeNode], /*isImplementsList*/ false); } @@ -33525,7 +33525,7 @@ var ts; write(" "); emitDeclarationName(node); } - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { write(" extends "); emit(baseTypeNode.expression); @@ -33603,7 +33603,7 @@ var ts; write(" = "); } write("(function ("); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + var baseTypeNode = ts.getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { write("_super"); } @@ -47414,7 +47414,7 @@ var ts; if (symbol && symbol.flags & (32 /* Class */ | 64 /* Interface */)) { ts.forEach(symbol.getDeclarations(), function (declaration) { if (declaration.kind === 214 /* ClassDeclaration */) { - getPropertySymbolFromTypeReference(ts.getClassExtendsHeritageClauseElement(declaration)); + getPropertySymbolFromTypeReference(ts.getClassOrStructExtendsHeritageClauseElement(declaration)); ts.forEach(ts.getClassImplementsHeritageClauseElements(declaration), getPropertySymbolFromTypeReference); } else if (declaration.kind === 215 /* InterfaceDeclaration */) { @@ -47649,7 +47649,7 @@ var ts; node = node.parent; } return node.parent.kind === 151 /* TypeReference */ || - (node.parent.kind === 188 /* ExpressionWithTypeArguments */ && !ts.isExpressionWithTypeArgumentsInClassExtendsClause(node.parent)) || + (node.parent.kind === 188 /* ExpressionWithTypeArguments */ && !ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node.parent)) || node.kind === 97 /* ThisKeyword */ && !ts.isExpression(node); } function isNamespaceReference(node) { diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 00085f1608679..47ae019e5b6e0 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -961,7 +961,7 @@ namespace ts { let prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); - let baseTypeNode = getClassExtendsHeritageClauseElement(node); + let baseTypeNode = getClassOrStructExtendsHeritageClauseElement(node); if (baseTypeNode) { emitHeritageClause([baseTypeNode], /*isImplementsList*/ false); } diff --git a/src/harness/typeWriter.ts b/src/harness/typeWriter.ts index 9db4fbcd41cac..3a109f03089ef 100644 --- a/src/harness/typeWriter.ts +++ b/src/harness/typeWriter.ts @@ -43,7 +43,7 @@ class TypeWriterWalker { // Workaround to ensure we output 'C' instead of 'typeof C' for base class expressions // let type = this.checker.getTypeAtLocation(node); - let type = node.parent && ts.isExpressionWithTypeArgumentsInClassExtendsClause(node.parent) && this.checker.getTypeAtLocation(node.parent) || this.checker.getTypeAtLocation(node); + let type = node.parent && ts.isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node.parent) && this.checker.getTypeAtLocation(node.parent) || this.checker.getTypeAtLocation(node); ts.Debug.assert(type !== undefined, "type doesn't exist"); let symbol = this.checker.getSymbolAtLocation(node); diff --git a/src/services/services.ts b/src/services/services.ts index 15c6e3df56f8f..8a7631153f64d 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -6025,7 +6025,7 @@ namespace ts { if (symbol && symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface)) { forEach(symbol.getDeclarations(), declaration => { if (declaration.kind === SyntaxKind.ClassDeclaration) { - getPropertySymbolFromTypeReference(getClassExtendsHeritageClauseElement(declaration)); + getPropertySymbolFromTypeReference(getClassOrStructExtendsHeritageClauseElement(declaration)); forEach(getClassImplementsHeritageClauseElements(declaration), getPropertySymbolFromTypeReference); } else if (declaration.kind === SyntaxKind.InterfaceDeclaration) { @@ -6297,7 +6297,7 @@ namespace ts { } return node.parent.kind === SyntaxKind.TypeReference || - (node.parent.kind === SyntaxKind.ExpressionWithTypeArguments && !isExpressionWithTypeArgumentsInClassExtendsClause(node.parent)) || + (node.parent.kind === SyntaxKind.ExpressionWithTypeArguments && !isExpressionWithTypeArgumentsInClassOrStructExtendsClause(node.parent)) || node.kind === SyntaxKind.ThisKeyword && !isExpression(node); } From 2f0539ed75600d3f9a7efea9b1b5f16e0a0f1206 Mon Sep 17 00:00:00 2001 From: jied Date: Wed, 25 Nov 2015 21:47:09 -0800 Subject: [PATCH 42/43] lint --- src/compiler/checker.ts | 410 ++++++++++++++++++++-------------------- src/compiler/parser.ts | 4 +- 2 files changed, 207 insertions(+), 207 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9ad4593acc70a..6ca79ba3fea72 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3013,8 +3013,8 @@ namespace ts { return; } if (isInClassDecl && !(getTargetType(baseType).flags & (TypeFlags.Class | TypeFlags.Interface))) { - error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); - return; +error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); +return; } else if (!isInClassDecl && !(getTargetType(baseType).flags & TypeFlags.Struct)) { error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_a_struct_type, typeToString(baseType)); @@ -3222,8 +3222,8 @@ namespace ts { return true; case SyntaxKind.ArrayType: return isIndependentType((node).elementType); - case SyntaxKind.StructArrayType: - return isIndependentType((node).elementType); +case SyntaxKind.StructArrayType: +return isIndependentType((node).elementType); case SyntaxKind.TypeReference: return isIndependentTypeReference(node); } @@ -4371,17 +4371,17 @@ parentSymbol = (declaration.parent).symbol; return createTypeFromGenericGlobalType(globalIterableIteratorType, [elementType]); } - function createStructArrayType(elementType: Type): Type { - return createTypeFromGenericGlobalType(globalStructArrayType, [elementType]); - } +function createStructArrayType(elementType: Type): Type { +return createTypeFromGenericGlobalType(globalStructArrayType, [elementType]); +} - function getTypeFromStructArrayTypeNode(node: StructArrayTypeNode): Type { - let links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = createStructArrayType(getTypeFromTypeNode(node.elementType)); - } - return links.resolvedType; - } +function getTypeFromStructArrayTypeNode(node: StructArrayTypeNode): Type { +let links = getNodeLinks(node); +if (!links.resolvedType) { +links.resolvedType = createStructArrayType(getTypeFromTypeNode(node.elementType)); +} +return links.resolvedType; +} function createArrayType(elementType: Type): Type { return createTypeFromGenericGlobalType(globalArrayType, [elementType]); @@ -4635,8 +4635,8 @@ parentSymbol = (declaration.parent).symbol; return getTypeFromTypeQueryNode(node); case SyntaxKind.ArrayType: return getTypeFromArrayTypeNode(node); - case SyntaxKind.StructArrayType: - return getTypeFromStructArrayTypeNode(node); +case SyntaxKind.StructArrayType: +return getTypeFromStructArrayTypeNode(node); case SyntaxKind.TupleType: return getTypeFromTupleTypeNode(node); case SyntaxKind.UnionType: @@ -5137,25 +5137,25 @@ parentSymbol = (declaration.parent).symbol; if (apparentType.flags & (TypeFlags.ObjectType | TypeFlags.Intersection) && target.flags & TypeFlags.ObjectType) { // Report structural errors only if we haven't reported any errors yet let reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo; - if (isBothStructType(source, target)) { - // for struct assignability, check its inheritance chain - let baseTypes = (apparentType).resolvedBaseTypes; - while (baseTypes && baseTypes.length > 0) { - if (baseTypes.indexOf(target) > -1) { // target is on source's inheritance chain - errorInfo = saveErrorInfo; - return Ternary.True; - } - else { - baseTypes = (baseTypes[0]).resolvedBaseTypes; // expand the inheritance chain - } - } - } - else { // structural comparison - if (result = objectTypeRelatedTo(apparentType, target, reportStructuralErrors)) { - errorInfo = saveErrorInfo; - return result; - } - } +if (isBothStructType(source, target)) { +// for struct assignability, check its inheritance chain +let baseTypes = (apparentType).resolvedBaseTypes; +while (baseTypes && baseTypes.length > 0) { +if (baseTypes.indexOf(target) > -1) { // target is on source's inheritance chain +errorInfo = saveErrorInfo; +return Ternary.True; +} +else { +baseTypes = (baseTypes[0]).resolvedBaseTypes; // expand the inheritance chain +} +} +} +else { // structural comparison +if (result = objectTypeRelatedTo(apparentType, target, reportStructuralErrors)) { +errorInfo = saveErrorInfo; +return result; +} +} } } @@ -6962,12 +6962,12 @@ parentSymbol = (declaration.parent).symbol; } if (!baseClassOrStructType && (!classOrStructDeclaration || !getClassOrStructExtendsHeritageClauseElement(classOrStructDeclaration))) { - if (isInClassDecl) { - error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_class); - } - else if (!isInClassDecl) { - error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_struct); - } +if (isInClassDecl) { +error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_class); +} +else if (!isInClassDecl) { +error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_struct); +} return unknownType; } @@ -7029,19 +7029,19 @@ parentSymbol = (declaration.parent).symbol; container.kind === SyntaxKind.Constructor; } } - else if (container && isStructLike(container.parent)) { - if (container.flags & NodeFlags.Static) { - return container.kind === SyntaxKind.MethodDeclaration || - container.kind === SyntaxKind.MethodSignature; - } - else { - return container.kind === SyntaxKind.MethodDeclaration || - container.kind === SyntaxKind.MethodSignature || - container.kind === SyntaxKind.PropertyDeclaration || - container.kind === SyntaxKind.PropertySignature || - container.kind === SyntaxKind.Constructor; - } - } +else if (container && isStructLike(container.parent)) { +if (container.flags & NodeFlags.Static) { +return container.kind === SyntaxKind.MethodDeclaration || +container.kind === SyntaxKind.MethodSignature; +} +else { +return container.kind === SyntaxKind.MethodDeclaration || +container.kind === SyntaxKind.MethodSignature || +container.kind === SyntaxKind.PropertyDeclaration || +container.kind === SyntaxKind.PropertySignature || +container.kind === SyntaxKind.Constructor; +} +} } return false; @@ -8218,8 +8218,8 @@ parentSymbol = (declaration.parent).symbol; */ function checkClassOrStructPropertyAccess(node: PropertyAccessExpression | QualifiedName, left: Expression | QualifiedName, type: Type, prop: Symbol): boolean { let flags = getDeclarationFlagsFromSymbol(prop); - let isInClassDecl = true; - if (prop.parent.flags & SymbolFlags.Struct) isInClassDecl = false; + let isInClassDecl = true; + if (prop.parent.flags & SymbolFlags.Struct) isInClassDecl = false; let declaringClassOrStruct = getDeclaredTypeOfSymbol(prop.parent); if (left.kind === SyntaxKind.SuperKeyword) { @@ -8237,12 +8237,12 @@ parentSymbol = (declaration.parent).symbol; if (getDeclarationKindFromSymbol(prop) !== SyntaxKind.MethodDeclaration) { // `prop` refers to a *property* declared in the super class/struct // rather than a *method*, so it does not satisfy the above criteria. - if (isInClassDecl) { - error(errorNode, Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); - } - else { - error(errorNode, Diagnostics.Only_public_methods_of_the_base_struct_are_accessible_via_the_super_keyword); - } +if (isInClassDecl) { +error(errorNode, Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); +} +else { +error(errorNode, Diagnostics.Only_public_methods_of_the_base_struct_are_accessible_via_the_super_keyword); +} return false; } @@ -8270,12 +8270,12 @@ parentSymbol = (declaration.parent).symbol; // Private property is accessible if declaring and enclosing class are the same if (flags & NodeFlags.Private) { if (declaringClassOrStruct !== enclosingClassOrStruct) { - if (isInClassDecl) { - error(node, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClassOrStruct)); - } - else { - error(node, Diagnostics.Property_0_is_private_and_only_accessible_within_struct_1, symbolToString(prop), typeToString(declaringClassOrStruct)); - } + if (isInClassDecl) { +error(node, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClassOrStruct)); + } + else { +error(node, Diagnostics.Property_0_is_private_and_only_accessible_within_struct_1, symbolToString(prop), typeToString(declaringClassOrStruct)); + } return false; } return true; @@ -8289,12 +8289,12 @@ parentSymbol = (declaration.parent).symbol; } // A protected property is accessible in the declaring class and classes derived from it if (!enclosingClassOrStruct || !hasBaseType(enclosingClassOrStruct, declaringClassOrStruct)) { - if (isInClassDecl) { - error(node, Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(declaringClassOrStruct)); - } - else { - error(node, Diagnostics.Property_0_is_protected_and_only_accessible_within_struct_1_and_its_substructs, symbolToString(prop), typeToString(declaringClassOrStruct)); - } + if (isInClassDecl) { +error(node, Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(declaringClassOrStruct)); + } + else { +error(node, Diagnostics.Property_0_is_protected_and_only_accessible_within_struct_1_and_its_substructs, symbolToString(prop), typeToString(declaringClassOrStruct)); + } return false; } // No further restrictions for static properties @@ -8374,65 +8374,65 @@ parentSymbol = (declaration.parent).symbol; // parent.typeArguments = [TypeReference: T] <-- IndexedAccess.object // parent.arguments = [NumericLiteral: 2] <-- IndexedAccess.index // return StructArray type. the rest will be handled in resolveNewExpression. - function checkStructArrayInitialization(node: ElementAccessExpression): Type { - var parentNode = node.parent; - var indexedAccessNode = node; - var objectNode = indexedAccessNode.expression; - var indexNode = indexedAccessNode.argumentExpression; - - // reassign func to be StructArray - // no position info since we artificially add StructArray node - var func = new (getNodeConstructor(SyntaxKind.Identifier))(); - func.text = "StructArray"; - func.parent = parentNode; - parentNode.expression = func; - - // reassign typeArguments - var typeArguments = >[]; - typeArguments.pos = objectNode.pos; - typeArguments.end = objectNode.end; - parentNode.typeArguments = typeArguments; - - // reassign structType (should be TypeReference) - var structTypeReference = new (getNodeConstructor(SyntaxKind.TypeReference))(); - structTypeReference.pos = typeArguments.pos; - structTypeReference.end = typeArguments.end; - structTypeReference.parent = parentNode; - var structTypeName = objectNode; - structTypeReference.typeName = structTypeName; - structTypeName.parent = structTypeReference; - typeArguments.push(structTypeReference); - - // reassign arguments (store StructArray length) - var arguments = >[]; - arguments.pos = indexNode.pos; - arguments.end = indexNode.end; - parentNode.arguments = arguments; - var structArrayLength = indexNode; - structArrayLength.parent = parentNode; - arguments.push(structArrayLength); - - node = null; - return checkExpression(func); - } - - // check struct array index access bound - function checkStructArrayAccess(node: ElementAccessExpression) { - var objectNode = node.expression; - var indexNode = node.argumentExpression; - var symbol = getResolvedSymbol(objectNode); - var valueDeclaration = symbol.valueDeclaration; - var newExpression: NewExpression; - if (valueDeclaration && valueDeclaration.initializer) newExpression = valueDeclaration.initializer; - var arguments: NodeArray; - if (newExpression && newExpression.arguments) arguments = newExpression.arguments; - var arrayLength: string; - if (arguments) arrayLength = (arguments[0]).text; - var index = (indexNode).text; - if (+index >= +arrayLength) { - error(node, Diagnostics.Struct_array_index_out_of_bound, symbolToString(symbol), arrayLength); - } - } +function checkStructArrayInitialization(node: ElementAccessExpression): Type { +let parentNode = node.parent; +let indexedAccessNode = node; +let objectNode = indexedAccessNode.expression; +let indexNode = indexedAccessNode.argumentExpression; + +// reassign func to be StructArray +// no position info since we artificially add StructArray node +let func = new (getNodeConstructor(SyntaxKind.Identifier))(); +func.text = "StructArray"; +func.parent = parentNode; +parentNode.expression = func; + +// reassign typeArguments +let typeArguments = >[]; +typeArguments.pos = objectNode.pos; +typeArguments.end = objectNode.end; +parentNode.typeArguments = typeArguments; + +// reassign structType (should be TypeReference) +let structTypeReference = new (getNodeConstructor(SyntaxKind.TypeReference))(); +structTypeReference.pos = typeArguments.pos; +structTypeReference.end = typeArguments.end; +structTypeReference.parent = parentNode; +let structTypeName = objectNode; +structTypeReference.typeName = structTypeName; +structTypeName.parent = structTypeReference; +typeArguments.push(structTypeReference); + +// reassign arguments (store StructArray length) +let arguments = >[]; +arguments.pos = indexNode.pos; +arguments.end = indexNode.end; +parentNode.arguments = arguments; +let structArrayLength = indexNode; +structArrayLength.parent = parentNode; +arguments.push(structArrayLength); + +node = undefined; +return checkExpression(func); +} + +// check struct array index access bound +function checkStructArrayAccess(node: ElementAccessExpression) { +let objectNode = node.expression; +let indexNode = node.argumentExpression; +let symbol = getResolvedSymbol(objectNode); +let valueDeclaration = symbol.valueDeclaration; +let newExpression: NewExpression; +if (valueDeclaration && valueDeclaration.initializer) newExpression = valueDeclaration.initializer; +let arguments: NodeArray; +if (newExpression && newExpression.arguments) arguments = newExpression.arguments; +let arrayLength: string; +if (arguments) arrayLength = (arguments[0]).text; +let index = (indexNode).text; +if (+index >= +arrayLength) { +error(node, Diagnostics.Struct_array_index_out_of_bound, symbolToString(symbol), arrayLength); +} +} function checkIndexedAccess(node: ElementAccessExpression): Type { // Grammar checking @@ -8460,16 +8460,16 @@ parentSymbol = (declaration.parent).symbol; let objectType = getApparentType(originObjectType); // check if node's actual type is struct array - if (objectType && objectType.symbol && getDeclarationKindFromSymbol(objectType.symbol) === SyntaxKind.StructDeclaration) { - if (node.parent.kind === SyntaxKind.NewExpression) { // struct array declaration +if (objectType && objectType.symbol && getDeclarationKindFromSymbol(objectType.symbol) === SyntaxKind.StructDeclaration) { +if (node.parent.kind === SyntaxKind.NewExpression) { // struct array declaration // write("struct array declaration"); - return checkStructArrayInitialization(node); - } - } - if (objectType && objectType.symbol && objectType.symbol.name === 'StructArray') { // struct array access +return checkStructArrayInitialization(node); +} +} +if (objectType && objectType.symbol && objectType.symbol.flags & SymbolFlags.Struct) { // struct array access // write("struct array access"); - checkStructArrayAccess(node); - } +checkStructArrayAccess(node); +} let indexType = node.argumentExpression ? checkExpression(node.argumentExpression) : unknownType; @@ -9497,13 +9497,13 @@ parentSymbol = (declaration.parent).symbol; if (superType !== unknownType) { // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated // with the type arguments specified in the extends clause. - let baseTypeNode: ExpressionWithTypeArguments; - if (getContainingClass(node)) { - baseTypeNode = getClassOrStructExtendsHeritageClauseElement(getContainingClass(node)); - } - else { - baseTypeNode = getClassOrStructExtendsHeritageClauseElement(getContainingStruct(node)); - } + let baseTypeNode: ExpressionWithTypeArguments; + if (getContainingClass(node)) { +baseTypeNode = getClassOrStructExtendsHeritageClauseElement(getContainingClass(node)); + } + else { +baseTypeNode = getClassOrStructExtendsHeritageClauseElement(getContainingStruct(node)); + } let baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments); return resolveCall(node, baseConstructors, candidatesOutArray); } @@ -11675,53 +11675,53 @@ return forEach((n).members, containsSuperCallAsComputedPr // Constructors of classes with no extends clause may not contain super calls, whereas // constructors of derived classes must contain at least one super call somewhere in their function body. if (node.parent.kind === SyntaxKind.ClassDeclaration) { - let containingClassDecl = node.parent; - if (getClassOrStructExtendsHeritageClauseElement(containingClassDecl)) { - let containingClassSymbol = getSymbolOfNode(containingClassDecl); - let containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); - let baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); - - if (containsSuperCall(node.body)) { - if (baseConstructorType === nullType) { - error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); - } - - // The first statement in the body of a constructor (excluding prologue directives) must be a super call - // if both of the following are true: - // - The containing class is a derived class. - // - The constructor declares parameter properties - // or the containing class declares instance member variables with initializers. - let superCallShouldBeFirst = - forEach((node.parent).members, isInstancePropertyWithInitializer) || - forEach(node.parameters, p => p.flags & (NodeFlags.Public | NodeFlags.Private | NodeFlags.Protected)); - - // Skip past any prologue directives to find the first statement - // to ensure that it was a super call. - if (superCallShouldBeFirst) { - let statements = (node.body).statements; - let superCallStatement: ExpressionStatement; - for (let statement of statements) { - if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { - superCallStatement = statement; - break; - } - if (!isPrologueDirective(statement)) { - break; - } - } - if (!superCallStatement) { - error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); - } - else { - // In such a required super call, it is a compile-time error for argument expressions to reference this. - markThisReferencesAsErrors(superCallStatement.expression); - } - } - } - else if (baseConstructorType !== nullType) { - error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); - } - } +let containingClassDecl = node.parent; +if (getClassOrStructExtendsHeritageClauseElement(containingClassDecl)) { +let containingClassSymbol = getSymbolOfNode(containingClassDecl); +let containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); +let baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); + +if (containsSuperCall(node.body)) { +if (baseConstructorType === nullType) { +error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); +} + + // The first statement in the body of a constructor (excluding prologue directives) must be a super call + // if both of the following are true: + // - The containing class is a derived class. + // - The constructor declares parameter properties + // or the containing class declares instance member variables with initializers. +let superCallShouldBeFirst = +forEach((node.parent).members, isInstancePropertyWithInitializer) || +forEach(node.parameters, p => p.flags & (NodeFlags.Public | NodeFlags.Private | NodeFlags.Protected)); + + // Skip past any prologue directives to find the first statement + // to ensure that it was a super call. +if (superCallShouldBeFirst) { +let statements = (node.body).statements; +let superCallStatement: ExpressionStatement; +for (let statement of statements) { +if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { +superCallStatement = statement; +break; + } + if (!isPrologueDirective(statement)) { +break; + } + } +if (!superCallStatement) { +error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); +} +else { + // In such a required super call, it is a compile-time error for argument expressions to reference this. +markThisReferencesAsErrors(superCallStatement.expression); + } + } + } +else if (baseConstructorType !== nullType) { +error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); +} + } } else if (node.parent.kind === SyntaxKind.StructDeclaration) { let containingStructDecl = node.parent; @@ -11841,9 +11841,9 @@ error(node, Diagnostics.Constructors_for_derived_structs_must_contain_a_super_ca } } // check if structArray declaration - if (node.parent.kind === SyntaxKind.ArrayType && type.flags & TypeFlags.Struct) { - Object.getPrototypeOf(node.parent).kind = SyntaxKind.StructArrayType; - } +if (node.parent.kind === SyntaxKind.ArrayType && type.flags & TypeFlags.Struct) { +Object.getPrototypeOf(node.parent).kind = SyntaxKind.StructArrayType; +} } function checkTypeQuery(node: TypeQueryNode) { @@ -11863,9 +11863,9 @@ error(node, Diagnostics.Constructors_for_derived_structs_must_contain_a_super_ca checkSourceElement(node.elementType); } - function checkStructArrayType(node: StructArrayTypeNode) { - checkSourceElement(node.elementType); - } +function checkStructArrayType(node: StructArrayTypeNode) { +checkSourceElement(node.elementType); +} function checkTupleType(node: TupleTypeNode) { // Grammar checking @@ -14860,8 +14860,8 @@ getNodeLinks(node).flags |= NodeCheckFlags.LexicalModuleMergesWithStruct; return checkTypeLiteral(node); case SyntaxKind.ArrayType: return checkArrayType(node); - case SyntaxKind.StructArrayType: - return checkStructArrayType(node); +case SyntaxKind.StructArrayType: +return checkStructArrayType(node); case SyntaxKind.TupleType: return checkTupleType(node); case SyntaxKind.UnionType: @@ -15994,7 +15994,7 @@ getNodeLinks(node).flags |= NodeCheckFlags.LexicalModuleMergesWithStruct; // Initialize special types globalArrayType = getGlobalType("Array", /*arity*/ 1); - globalStructArrayType = getGlobalType("StructArray", /*arity*/ 1); +globalStructArrayType = getGlobalType("StructArray", /*arity*/ 1); globalObjectType = getGlobalType("Object"); globalFunctionType = getGlobalType("Function"); globalStringType = getGlobalType("String"); @@ -16161,7 +16161,7 @@ getGlobalClassDecoratorType = memoize(() => getGlobalType("StructDecorator")); return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_module_element, text); } else if (modifier.kind === SyntaxKind.ProtectedKeyword && !isProtectedAllowedInStruct && (node.parent.kind === SyntaxKind.StructExpression || node.parent.kind === SyntaxKind.StructDeclaration)) { - return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_struct_element, text); +return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_struct_element, text); } else if (flags & NodeFlags.Abstract) { if (modifier.kind === SyntaxKind.PrivateKeyword) { diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 0a96b7bc5a3ae..129f1d5bc8c20 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5114,8 +5114,8 @@ return true; // index signature not allowed in struct if (isIndexSignature()) { - let indexSingature: IndexSignatureDeclaration = parseIndexSignatureDeclaration(fullStart, decorators, modifiers); - parseErrorAtPosition(indexSingature.pos, indexSingature.end - indexSingature.pos, Diagnostics.A_struct_can_not_have_index_members); + let indexSingature: IndexSignatureDeclaration = parseIndexSignatureDeclaration(fullStart, decorators, modifiers); + parseErrorAtPosition(indexSingature.pos, indexSingature.end - indexSingature.pos, Diagnostics.A_struct_can_not_have_index_members); return indexSingature; } From a714a0315c352e2907d34a7b92a639d3da1bd0b9 Mon Sep 17 00:00:00 2001 From: jied Date: Wed, 25 Nov 2015 22:07:06 -0800 Subject: [PATCH 43/43] test baseline --- ...ignableOnStructInheritanceChain.errors.txt | 37 +++ .../AssignableOnStructInheritanceChain.js | 82 ++++++ ...etweenStructsWithoutInheritance.errors.txt | 29 ++ ...ignableBetweenStructsWithoutInheritance.js | 57 ++++ ...ctCannotOverrideInstanceMemberVariables.js | 135 +++++++++ ...notOverrideInstanceMemberVariables.symbols | 63 ++++ ...annotOverrideInstanceMemberVariables.types | 63 ++++ ...ructConstructorWithoutSuperCall.errors.txt | 56 ++++ ...erivedStructConstructorWithoutSuperCall.js | 126 ++++++++ .../derivedStructIncludesInheritedMembers.js | 68 +++++ ...ivedStructIncludesInheritedMembers.symbols | 58 ++++ ...erivedStructIncludesInheritedMembers.types | 62 ++++ ...edStructOverrideInstanceFunctionMembers.js | 88 ++++++ ...uctOverrideInstanceFunctionMembers.symbols | 62 ++++ ...tructOverrideInstanceFunctionMembers.types | 64 ++++ ...rivedStructOverrideStaticProperyMembers.js | 92 ++++++ ...StructOverrideStaticProperyMembers.symbols | 76 +++++ ...edStructOverrideStaticProperyMembers.types | 78 +++++ ...erivedStructParameterProperties.errors.txt | 119 ++++++++ .../derivedStructParameterProperties.js | 277 ++++++++++++++++++ ...perCallsInNonConstructorMembers.errors.txt | 39 +++ ...StructSuperCallsInNonConstructorMembers.js | 62 ++++ ...ivedStructSuperCallsWithThisArg.errors.txt | 40 +++ .../derivedStructSuperCallsWithThisArg.js | 115 ++++++++ ...InstanceShadowingPublicInstance.errors.txt | 27 ++ ...hPrivateInstanceShadowingPublicInstance.js | 68 +++++ ...vateStaticShadowingPublicStatic.errors.txt | 32 ++ ...tWithPrivateStaticShadowingPublicStatic.js | 70 +++++ ...tructWithoutExplicitConstructor.errors.txt | 36 +++ ...derivedStructWithoutExplicitConstructor.js | 86 ++++++ ...ructWithoutExplicitConstructor2.errors.txt | 59 ++++ ...erivedStructWithoutExplicitConstructor2.js | 134 +++++++++ ...eMemberAssignsToStructPrototype.errors.txt | 24 ++ .../instanceMemberAssignsToStructPrototype.js | 44 +++ ...stancePropertiesInheritedIntoStructType.js | 93 ++++++ ...ePropertiesInheritedIntoStructType.symbols | 59 ++++ ...ncePropertiesInheritedIntoStructType.types | 63 ++++ .../reference/instancePropertyInStructType.js | 69 +++++ .../instancePropertyInStructType.symbols | 56 ++++ .../instancePropertyInStructType.types | 60 ++++ .../baselines/reference/returnType.errors.txt | 20 ++ tests/baselines/reference/returnType.js | 35 +++ ...tructConstructorFunctionMembers.errors.txt | 24 ++ ...signsToStructConstructorFunctionMembers.js | 44 +++ .../staticPropertyNotInStructType.errors.txt | 56 ++++ .../staticPropertyNotInStructType.js | 93 ++++++ .../structAccessibilityModifiers.errors.txt | 47 +++ .../reference/structAccessibilityModifiers.js | 78 +++++ .../structConstructorAccessibility.errors.txt | 46 +++ .../structConstructorAccessibility.js | 104 +++++++ ...torDefaultValuesReferencingThis.errors.txt | 20 ++ ...ConstructorDefaultValuesReferencingThis.js | 40 +++ .../structConstructorHasPrototypeProperty.js | 99 +++++++ ...uctConstructorHasPrototypeProperty.symbols | 63 ++++ ...tructConstructorHasPrototypeProperty.types | 63 ++++ ...structorImplementationWithDefaultValues.js | 54 ++++ ...torImplementationWithDefaultValues.symbols | 31 ++ ...uctorImplementationWithDefaultValues.types | 32 ++ ...mplementationWithDefaultValues2.errors.txt | 29 ++ ...tructorImplementationWithDefaultValues2.js | 57 ++++ ...uctorOverloadsWithDefaultValues.errors.txt | 29 ++ ...ctConstructorOverloadsWithDefaultValues.js | 63 ++++ .../structConstructorParameterNotOptional.js | 62 ++++ ...uctConstructorParameterNotOptional.symbols | 25 ++ ...tructConstructorParameterNotOptional.types | 25 ++ ...tConstructorParameterProperties.errors.txt | 31 ++ .../structConstructorParameterProperties.js | 59 ++++ ...ConstructorParameterProperties2.errors.txt | 42 +++ .../structConstructorParameterProperties2.js | 81 +++++ ...ameterPropertyDeclarationCannotOverride.js | 83 ++++++ ...rPropertyDeclarationCannotOverride.symbols | 61 ++++ ...terPropertyDeclarationCannotOverride.types | 63 ++++ ...ctorParameterShadowsOuterScopes.errors.txt | 35 +++ ...tConstructorParameterShadowsOuterScopes.js | 65 ++++ ...structorParametersAccessibility.errors.txt | 33 +++ ...tructConstructorParametersAccessibility.js | 77 +++++ ...rWithAssignableReturnExpression.errors.txt | 48 +++ ...nstructorWithAssignableReturnExpression.js | 98 +++++++ ...ructConstructorWithExpressionLessReturn.js | 93 ++++++ ...onstructorWithExpressionLessReturn.symbols | 45 +++ ...tConstructorWithExpressionLessReturn.types | 45 +++ ...nBaseCallViaSuperPropertyAccess.errors.txt | 34 +++ ...sesHiddenBaseCallViaSuperPropertyAccess.js | 69 +++++ ...zerReferencingConstructorLocals.errors.txt | 45 +++ ...InitializerReferencingConstructorLocals.js | 58 ++++ ...eferencingConstructorParameters.errors.txt | 44 +++ ...ializerReferencingConstructorParameters.js | 83 ++++++ .../structInstanceMemberInitialization.js | 34 +++ ...structInstanceMemberInitialization.symbols | 33 +++ .../structInstanceMemberInitialization.types | 39 +++ ...PropertyAndFunctionWithSameName.errors.txt | 42 +++ ...InstancePropertyAndFunctionWithSameName.js | 75 +++++ ...OverloadMixingStaticAndInstance.errors.txt | 37 +++ ...FunctionOverloadMixingStaticAndInstance.js | 63 ++++ ...erFunctionsWithPrivateOverloads.errors.txt | 63 ++++ ...ructMemberFunctionsWithPrivateOverloads.js | 107 +++++++ ...tructMemberFunctionsWithPublicOverloads.js | 88 ++++++ ...MemberFunctionsWithPublicOverloads.symbols | 93 ++++++ ...ctMemberFunctionsWithPublicOverloads.types | 93 ++++++ ...tionsWithPublicPrivateOverloads.errors.txt | 75 +++++ ...mberFunctionsWithPublicPrivateOverloads.js | 113 +++++++ .../structNotAddNonDeclaredMember.errors.txt | 12 + .../structNotAddNonDeclaredMember.js | 26 ++ ...structNotHaveOptionalProperties.errors.txt | 25 ++ .../structNotHaveOptionalProperties.js | 41 +++ .../structPropertyNamedPrototype.errors.txt | 13 + .../reference/structPropertyNamedPrototype.js | 26 ++ ...uctStaticAndNonStaticPropertiesSameName.js | 35 +++ ...aticAndNonStaticPropertiesSameName.symbols | 21 ++ ...StaticAndNonStaticPropertiesSameName.types | 21 ++ .../reference/structStaticFactory1.js | 63 ++++ .../reference/structStaticFactory1.symbols | 38 +++ .../reference/structStaticFactory1.types | 43 +++ .../structStaticMemberInitialization.js | 29 ++ .../structStaticMemberInitialization.symbols | 21 ++ .../structStaticMemberInitialization.types | 23 ++ ...PropertyAndFunctionWithSameName.errors.txt | 23 ++ ...ctStaticPropertyAndFunctionWithSameName.js | 49 ++++ .../structThisInInstanceMemberInitializer.js | 36 +++ ...uctThisInInstanceMemberInitializer.symbols | 17 ++ ...tructThisInInstanceMemberInitializer.types | 17 ++ ...tWithBaseStructButNoConstructor.errors.txt | 48 +++ .../structWithBaseStructButNoConstructor.js | 109 +++++++ .../structWithConstructors.errors.txt | 66 +++++ .../reference/structWithConstructors.js | 140 +++++++++ .../structWithNoConstructorOrBaseStruct.js | 54 ++++ ...tructWithNoConstructorOrBaseStruct.symbols | 31 ++ .../structWithNoConstructorOrBaseStruct.types | 32 ++ .../reference/structWithStaticMembers.js | 62 ++++ .../reference/structWithStaticMembers.symbols | 51 ++++ .../reference/structWithStaticMembers.types | 53 ++++ ...ctWithTwoConstructorDefinitions.errors.txt | 21 ++ .../structWithTwoConstructorDefinitions.js | 34 +++ ...tructWithoutExplicitConstructor.errors.txt | 25 ++ .../structWithoutExplicitConstructor.js | 49 ++++ ...StructConstructorWithNoBaseType.errors.txt | 20 ++ ...erCallInStructConstructorWithNoBaseType.js | 38 +++ .../typeOfThisInStructMemberFunctions.js | 82 ++++++ .../typeOfThisInStructMemberFunctions.symbols | 46 +++ .../typeOfThisInStructMemberFunctions.types | 46 +++ 140 files changed, 8000 insertions(+) create mode 100644 tests/baselines/reference/AssignableOnStructInheritanceChain.errors.txt create mode 100644 tests/baselines/reference/AssignableOnStructInheritanceChain.js create mode 100644 tests/baselines/reference/NotAssignableBetweenStructsWithoutInheritance.errors.txt create mode 100644 tests/baselines/reference/NotAssignableBetweenStructsWithoutInheritance.js create mode 100644 tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.js create mode 100644 tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.symbols create mode 100644 tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.types create mode 100644 tests/baselines/reference/derivedStructConstructorWithoutSuperCall.errors.txt create mode 100644 tests/baselines/reference/derivedStructConstructorWithoutSuperCall.js create mode 100644 tests/baselines/reference/derivedStructIncludesInheritedMembers.js create mode 100644 tests/baselines/reference/derivedStructIncludesInheritedMembers.symbols create mode 100644 tests/baselines/reference/derivedStructIncludesInheritedMembers.types create mode 100644 tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.js create mode 100644 tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.symbols create mode 100644 tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.types create mode 100644 tests/baselines/reference/derivedStructOverrideStaticProperyMembers.js create mode 100644 tests/baselines/reference/derivedStructOverrideStaticProperyMembers.symbols create mode 100644 tests/baselines/reference/derivedStructOverrideStaticProperyMembers.types create mode 100644 tests/baselines/reference/derivedStructParameterProperties.errors.txt create mode 100644 tests/baselines/reference/derivedStructParameterProperties.js create mode 100644 tests/baselines/reference/derivedStructSuperCallsInNonConstructorMembers.errors.txt create mode 100644 tests/baselines/reference/derivedStructSuperCallsInNonConstructorMembers.js create mode 100644 tests/baselines/reference/derivedStructSuperCallsWithThisArg.errors.txt create mode 100644 tests/baselines/reference/derivedStructSuperCallsWithThisArg.js create mode 100644 tests/baselines/reference/derivedStructWithPrivateInstanceShadowingPublicInstance.errors.txt create mode 100644 tests/baselines/reference/derivedStructWithPrivateInstanceShadowingPublicInstance.js create mode 100644 tests/baselines/reference/derivedStructWithPrivateStaticShadowingPublicStatic.errors.txt create mode 100644 tests/baselines/reference/derivedStructWithPrivateStaticShadowingPublicStatic.js create mode 100644 tests/baselines/reference/derivedStructWithoutExplicitConstructor.errors.txt create mode 100644 tests/baselines/reference/derivedStructWithoutExplicitConstructor.js create mode 100644 tests/baselines/reference/derivedStructWithoutExplicitConstructor2.errors.txt create mode 100644 tests/baselines/reference/derivedStructWithoutExplicitConstructor2.js create mode 100644 tests/baselines/reference/instanceMemberAssignsToStructPrototype.errors.txt create mode 100644 tests/baselines/reference/instanceMemberAssignsToStructPrototype.js create mode 100644 tests/baselines/reference/instancePropertiesInheritedIntoStructType.js create mode 100644 tests/baselines/reference/instancePropertiesInheritedIntoStructType.symbols create mode 100644 tests/baselines/reference/instancePropertiesInheritedIntoStructType.types create mode 100644 tests/baselines/reference/instancePropertyInStructType.js create mode 100644 tests/baselines/reference/instancePropertyInStructType.symbols create mode 100644 tests/baselines/reference/instancePropertyInStructType.types create mode 100644 tests/baselines/reference/returnType.errors.txt create mode 100644 tests/baselines/reference/returnType.js create mode 100644 tests/baselines/reference/staticMemberAssignsToStructConstructorFunctionMembers.errors.txt create mode 100644 tests/baselines/reference/staticMemberAssignsToStructConstructorFunctionMembers.js create mode 100644 tests/baselines/reference/staticPropertyNotInStructType.errors.txt create mode 100644 tests/baselines/reference/staticPropertyNotInStructType.js create mode 100644 tests/baselines/reference/structAccessibilityModifiers.errors.txt create mode 100644 tests/baselines/reference/structAccessibilityModifiers.js create mode 100644 tests/baselines/reference/structConstructorAccessibility.errors.txt create mode 100644 tests/baselines/reference/structConstructorAccessibility.js create mode 100644 tests/baselines/reference/structConstructorDefaultValuesReferencingThis.errors.txt create mode 100644 tests/baselines/reference/structConstructorDefaultValuesReferencingThis.js create mode 100644 tests/baselines/reference/structConstructorHasPrototypeProperty.js create mode 100644 tests/baselines/reference/structConstructorHasPrototypeProperty.symbols create mode 100644 tests/baselines/reference/structConstructorHasPrototypeProperty.types create mode 100644 tests/baselines/reference/structConstructorImplementationWithDefaultValues.js create mode 100644 tests/baselines/reference/structConstructorImplementationWithDefaultValues.symbols create mode 100644 tests/baselines/reference/structConstructorImplementationWithDefaultValues.types create mode 100644 tests/baselines/reference/structConstructorImplementationWithDefaultValues2.errors.txt create mode 100644 tests/baselines/reference/structConstructorImplementationWithDefaultValues2.js create mode 100644 tests/baselines/reference/structConstructorOverloadsWithDefaultValues.errors.txt create mode 100644 tests/baselines/reference/structConstructorOverloadsWithDefaultValues.js create mode 100644 tests/baselines/reference/structConstructorParameterNotOptional.js create mode 100644 tests/baselines/reference/structConstructorParameterNotOptional.symbols create mode 100644 tests/baselines/reference/structConstructorParameterNotOptional.types create mode 100644 tests/baselines/reference/structConstructorParameterProperties.errors.txt create mode 100644 tests/baselines/reference/structConstructorParameterProperties.js create mode 100644 tests/baselines/reference/structConstructorParameterProperties2.errors.txt create mode 100644 tests/baselines/reference/structConstructorParameterProperties2.js create mode 100644 tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.js create mode 100644 tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.symbols create mode 100644 tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.types create mode 100644 tests/baselines/reference/structConstructorParameterShadowsOuterScopes.errors.txt create mode 100644 tests/baselines/reference/structConstructorParameterShadowsOuterScopes.js create mode 100644 tests/baselines/reference/structConstructorParametersAccessibility.errors.txt create mode 100644 tests/baselines/reference/structConstructorParametersAccessibility.js create mode 100644 tests/baselines/reference/structConstructorWithAssignableReturnExpression.errors.txt create mode 100644 tests/baselines/reference/structConstructorWithAssignableReturnExpression.js create mode 100644 tests/baselines/reference/structConstructorWithExpressionLessReturn.js create mode 100644 tests/baselines/reference/structConstructorWithExpressionLessReturn.symbols create mode 100644 tests/baselines/reference/structConstructorWithExpressionLessReturn.types create mode 100644 tests/baselines/reference/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.errors.txt create mode 100644 tests/baselines/reference/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.js create mode 100644 tests/baselines/reference/structInitializerReferencingConstructorLocals.errors.txt create mode 100644 tests/baselines/reference/structInitializerReferencingConstructorLocals.js create mode 100644 tests/baselines/reference/structInitializerReferencingConstructorParameters.errors.txt create mode 100644 tests/baselines/reference/structInitializerReferencingConstructorParameters.js create mode 100644 tests/baselines/reference/structInstanceMemberInitialization.js create mode 100644 tests/baselines/reference/structInstanceMemberInitialization.symbols create mode 100644 tests/baselines/reference/structInstanceMemberInitialization.types create mode 100644 tests/baselines/reference/structInstancePropertyAndFunctionWithSameName.errors.txt create mode 100644 tests/baselines/reference/structInstancePropertyAndFunctionWithSameName.js create mode 100644 tests/baselines/reference/structMemberFunctionOverloadMixingStaticAndInstance.errors.txt create mode 100644 tests/baselines/reference/structMemberFunctionOverloadMixingStaticAndInstance.js create mode 100644 tests/baselines/reference/structMemberFunctionsWithPrivateOverloads.errors.txt create mode 100644 tests/baselines/reference/structMemberFunctionsWithPrivateOverloads.js create mode 100644 tests/baselines/reference/structMemberFunctionsWithPublicOverloads.js create mode 100644 tests/baselines/reference/structMemberFunctionsWithPublicOverloads.symbols create mode 100644 tests/baselines/reference/structMemberFunctionsWithPublicOverloads.types create mode 100644 tests/baselines/reference/structMemberFunctionsWithPublicPrivateOverloads.errors.txt create mode 100644 tests/baselines/reference/structMemberFunctionsWithPublicPrivateOverloads.js create mode 100644 tests/baselines/reference/structNotAddNonDeclaredMember.errors.txt create mode 100644 tests/baselines/reference/structNotAddNonDeclaredMember.js create mode 100644 tests/baselines/reference/structNotHaveOptionalProperties.errors.txt create mode 100644 tests/baselines/reference/structNotHaveOptionalProperties.js create mode 100644 tests/baselines/reference/structPropertyNamedPrototype.errors.txt create mode 100644 tests/baselines/reference/structPropertyNamedPrototype.js create mode 100644 tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.js create mode 100644 tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.symbols create mode 100644 tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.types create mode 100644 tests/baselines/reference/structStaticFactory1.js create mode 100644 tests/baselines/reference/structStaticFactory1.symbols create mode 100644 tests/baselines/reference/structStaticFactory1.types create mode 100644 tests/baselines/reference/structStaticMemberInitialization.js create mode 100644 tests/baselines/reference/structStaticMemberInitialization.symbols create mode 100644 tests/baselines/reference/structStaticMemberInitialization.types create mode 100644 tests/baselines/reference/structStaticPropertyAndFunctionWithSameName.errors.txt create mode 100644 tests/baselines/reference/structStaticPropertyAndFunctionWithSameName.js create mode 100644 tests/baselines/reference/structThisInInstanceMemberInitializer.js create mode 100644 tests/baselines/reference/structThisInInstanceMemberInitializer.symbols create mode 100644 tests/baselines/reference/structThisInInstanceMemberInitializer.types create mode 100644 tests/baselines/reference/structWithBaseStructButNoConstructor.errors.txt create mode 100644 tests/baselines/reference/structWithBaseStructButNoConstructor.js create mode 100644 tests/baselines/reference/structWithConstructors.errors.txt create mode 100644 tests/baselines/reference/structWithConstructors.js create mode 100644 tests/baselines/reference/structWithNoConstructorOrBaseStruct.js create mode 100644 tests/baselines/reference/structWithNoConstructorOrBaseStruct.symbols create mode 100644 tests/baselines/reference/structWithNoConstructorOrBaseStruct.types create mode 100644 tests/baselines/reference/structWithStaticMembers.js create mode 100644 tests/baselines/reference/structWithStaticMembers.symbols create mode 100644 tests/baselines/reference/structWithStaticMembers.types create mode 100644 tests/baselines/reference/structWithTwoConstructorDefinitions.errors.txt create mode 100644 tests/baselines/reference/structWithTwoConstructorDefinitions.js create mode 100644 tests/baselines/reference/structWithoutExplicitConstructor.errors.txt create mode 100644 tests/baselines/reference/structWithoutExplicitConstructor.js create mode 100644 tests/baselines/reference/superCallInStructConstructorWithNoBaseType.errors.txt create mode 100644 tests/baselines/reference/superCallInStructConstructorWithNoBaseType.js create mode 100644 tests/baselines/reference/typeOfThisInStructMemberFunctions.js create mode 100644 tests/baselines/reference/typeOfThisInStructMemberFunctions.symbols create mode 100644 tests/baselines/reference/typeOfThisInStructMemberFunctions.types diff --git a/tests/baselines/reference/AssignableOnStructInheritanceChain.errors.txt b/tests/baselines/reference/AssignableOnStructInheritanceChain.errors.txt new file mode 100644 index 0000000000000..db1d5a6fbbc4d --- /dev/null +++ b/tests/baselines/reference/AssignableOnStructInheritanceChain.errors.txt @@ -0,0 +1,37 @@ +tests/cases/conformance/structs/types/AssignableOnStructInheritanceChain.ts(28,1): error TS2322: Type 'Point2' is not assignable to type 'Point3'. + + +==== tests/cases/conformance/structs/types/AssignableOnStructInheritanceChain.ts (1 errors) ==== + // doc 8 + // A subtype struct can be assigned to a supertype struct. + + struct Point2 { + x: number; + y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y + } + } + + struct Point3 extends Point2{ + z: number; + constructor(x: number, y: number, z: number) { + super(x, y); + this.z = z; + } + } + + var p2: Point2; + var p3: Point3; + var p21 = new Point2(1,1); + var p31 = new Point3(1,1,1); + + p2 = p21; // ok + p2 = p31; // ok + p3 = p21; // error + ~~ +!!! error TS2322: Type 'Point2' is not assignable to type 'Point3'. + p3 = p31; // ok + + \ No newline at end of file diff --git a/tests/baselines/reference/AssignableOnStructInheritanceChain.js b/tests/baselines/reference/AssignableOnStructInheritanceChain.js new file mode 100644 index 0000000000000..1b1602c71502c --- /dev/null +++ b/tests/baselines/reference/AssignableOnStructInheritanceChain.js @@ -0,0 +1,82 @@ +//// [AssignableOnStructInheritanceChain.ts] +// doc 8 +// A subtype struct can be assigned to a supertype struct. + +struct Point2 { + x: number; + y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y + } +} + +struct Point3 extends Point2{ + z: number; + constructor(x: number, y: number, z: number) { + super(x, y); + this.z = z; + } +} + +var p2: Point2; +var p3: Point3; +var p21 = new Point2(1,1); +var p31 = new Point3(1,1,1); + +p2 = p21; // ok +p2 = p31; // ok +p3 = p21; // error +p3 = p31; // ok + + + +//// [AssignableOnStructInheritanceChain.js] +// doc 8 +// A subtype struct can be assigned to a supertype struct. +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Point2 = (function () { + var _Point2 = new TypedObject.StructType({ + x: TypedObject.float64, + y: TypedObject.float64, + }); + function _ctor(x, y) { + this.x = x; + this.y = y; + } + function Point2(x, y) { + var obj = new _Point2(); + _ctor.call(obj ,); + return obj; + } + Point2._TO = _Point2; + return Point2; +})(); +var Point3 = (function () { + var _Point3 = new TypedObject.StructType({ + z: TypedObject.float64, + }); + function _ctor(x, y, z) { + _super.call(this, x, y); + this.z = z; + } + function Point3(x, y, z) { + var obj = new _Point3(); + _ctor.call(obj ,); + return obj; + } + Point3._TO = _Point3; + return Point3; +})(); +var p2; +var p3; +var p21 = new Point2(1, 1); +var p31 = new Point3(1, 1, 1); +p2 = p21; // ok +p2 = p31; // ok +p3 = p21; // error +p3 = p31; // ok diff --git a/tests/baselines/reference/NotAssignableBetweenStructsWithoutInheritance.errors.txt b/tests/baselines/reference/NotAssignableBetweenStructsWithoutInheritance.errors.txt new file mode 100644 index 0000000000000..f46c7ad146938 --- /dev/null +++ b/tests/baselines/reference/NotAssignableBetweenStructsWithoutInheritance.errors.txt @@ -0,0 +1,29 @@ +tests/cases/conformance/structs/types/NotAssignableBetweenStructsWithoutInheritance.ts(17,1): error TS2322: Type 'Size' is not assignable to type 'Animal'. +tests/cases/conformance/structs/types/NotAssignableBetweenStructsWithoutInheritance.ts(18,1): error TS2322: Type 'Animal' is not assignable to type 'Size'. + + +==== tests/cases/conformance/structs/types/NotAssignableBetweenStructsWithoutInheritance.ts (2 errors) ==== + // doc 8 + // Only a subtype struct can be assigned to a supertype struct. + + struct Animal { + feet: number; + constructor(name: string, numFeet: number) { } + } + + struct Size { + feet: number; + constructor(numFeet: number) { } + } + + var a: Animal; + var s: Size; + + a = s; //error + ~ +!!! error TS2322: Type 'Size' is not assignable to type 'Animal'. + s = a; //error + ~ +!!! error TS2322: Type 'Animal' is not assignable to type 'Size'. + + \ No newline at end of file diff --git a/tests/baselines/reference/NotAssignableBetweenStructsWithoutInheritance.js b/tests/baselines/reference/NotAssignableBetweenStructsWithoutInheritance.js new file mode 100644 index 0000000000000..82adbe0649d05 --- /dev/null +++ b/tests/baselines/reference/NotAssignableBetweenStructsWithoutInheritance.js @@ -0,0 +1,57 @@ +//// [NotAssignableBetweenStructsWithoutInheritance.ts] +// doc 8 +// Only a subtype struct can be assigned to a supertype struct. + +struct Animal { + feet: number; + constructor(name: string, numFeet: number) { } +} + +struct Size { + feet: number; + constructor(numFeet: number) { } +} + +var a: Animal; +var s: Size; + +a = s; //error +s = a; //error + + + +//// [NotAssignableBetweenStructsWithoutInheritance.js] +// doc 8 +// Only a subtype struct can be assigned to a supertype struct. +var Animal = (function () { + var _Animal = new TypedObject.StructType({ + feet: TypedObject.float64, + }); + function _ctor(name, numFeet) { + } + function Animal(name, numFeet) { + var obj = new _Animal(); + _ctor.call(obj ,); + return obj; + } + Animal._TO = _Animal; + return Animal; +})(); +var Size = (function () { + var _Size = new TypedObject.StructType({ + feet: TypedObject.float64, + }); + function _ctor(numFeet) { + } + function Size(numFeet) { + var obj = new _Size(); + _ctor.call(obj ,); + return obj; + } + Size._TO = _Size; + return Size; +})(); +var a; +var s; +a = s; //error +s = a; //error diff --git a/tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.js b/tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.js new file mode 100644 index 0000000000000..2d06019209e54 --- /dev/null +++ b/tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.js @@ -0,0 +1,135 @@ +//// [derivedStructCannotOverrideInstanceMemberVariables.ts] +// doc 2.3 +// only public function members can be overridden. + +struct Base { + private x: { foo: string }; +} + +struct Derived extends Base { + private x: { foo: string; bar: string; }; // error +} + +struct Base1 { + public foo: any; +} + +struct Derived1 extends Base1 { + public foo: string; // error} +} + +struct Base2 { + static y: { foo: string }; +} + +struct Derived2 extends Base2 { + static y: { foo: string; bar: string; }; // ok +} + +struct Derived3 extends Base2 { + static y: { foo: any }; // ok +} + +//// [derivedStructCannotOverrideInstanceMemberVariables.js] +// doc 2.3 +// only public function members can be overridden. +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + x: TypedObject.Object + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + x: TypedObject.Object + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var Base1 = (function () { + var _Base1 = new TypedObject.StructType({ + foo: TypedObject.Any + }); + function _ctor() { + } + function Base1() { + var obj = new _Base1(); + _ctor.call(obj); + return obj; + } + Base1._TO = _Base1; + return Base1; +})(); +var Derived1 = (function () { + var _Derived1 = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function Derived1() { + var obj = new _Derived1(); + _ctor.call(obj); + return obj; + } + Derived1._TO = _Derived1; + return Derived1; +})(); +var Base2 = (function () { + var _Base2 = new TypedObject.StructType({ + }); + function _ctor() { + } + function Base2() { + var obj = new _Base2(); + _ctor.call(obj); + return obj; + } + Base2._TO = _Base2; + return Base2; +})(); +var Derived2 = (function () { + var _Derived2 = new TypedObject.StructType({ + }); + function _ctor() { + } + function Derived2() { + var obj = new _Derived2(); + _ctor.call(obj); + return obj; + } + Derived2._TO = _Derived2; + return Derived2; +})(); +var Derived3 = (function () { + var _Derived3 = new TypedObject.StructType({ + }); + function _ctor() { + } + function Derived3() { + var obj = new _Derived3(); + _ctor.call(obj); + return obj; + } + Derived3._TO = _Derived3; + return Derived3; +})(); diff --git a/tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.symbols b/tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.symbols new file mode 100644 index 0000000000000..8d58ae54f947a --- /dev/null +++ b/tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.symbols @@ -0,0 +1,63 @@ +=== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructCannotOverrideInstanceMemberVariables.ts === +// doc 2.3 +// only public function members can be overridden. + +struct Base { +>Base : Symbol(Base, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 0, 0)) + + private x: { foo: string }; +>x : Symbol(x, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 3, 13)) +>foo : Symbol(foo, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 4, 16)) +} + +struct Derived extends Base { +>Derived : Symbol(Derived, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 5, 1)) +>Base : Symbol(Base, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 0, 0)) + + private x: { foo: string; bar: string; }; // error +>x : Symbol(x, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 7, 29)) +>foo : Symbol(foo, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 8, 16)) +>bar : Symbol(bar, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 8, 29)) +} + +struct Base1 { +>Base1 : Symbol(Base1, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 9, 1)) + + public foo: any; +>foo : Symbol(foo, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 11, 14)) +} + +struct Derived1 extends Base1 { +>Derived1 : Symbol(Derived1, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 13, 1)) +>Base1 : Symbol(Base1, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 9, 1)) + + public foo: string; // error} +>foo : Symbol(foo, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 15, 31)) +} + +struct Base2 { +>Base2 : Symbol(Base2, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 17, 1)) + + static y: { foo: string }; +>y : Symbol(Base2.y, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 19, 14)) +>foo : Symbol(foo, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 20, 12)) +} + +struct Derived2 extends Base2 { +>Derived2 : Symbol(Derived2, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 21, 1)) +>Base2 : Symbol(Base2, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 17, 1)) + + static y: { foo: string; bar: string; }; // ok +>y : Symbol(Derived2.y, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 23, 31)) +>foo : Symbol(foo, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 24, 12)) +>bar : Symbol(bar, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 24, 25)) +} + +struct Derived3 extends Base2 { +>Derived3 : Symbol(Derived3, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 25, 1)) +>Base2 : Symbol(Base2, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 17, 1)) + + static y: { foo: any }; // ok +>y : Symbol(Derived3.y, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 27, 31)) +>foo : Symbol(foo, Decl(derivedStructCannotOverrideInstanceMemberVariables.ts, 28, 12)) +} diff --git a/tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.types b/tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.types new file mode 100644 index 0000000000000..25ad59ecbdf05 --- /dev/null +++ b/tests/baselines/reference/derivedStructCannotOverrideInstanceMemberVariables.types @@ -0,0 +1,63 @@ +=== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructCannotOverrideInstanceMemberVariables.ts === +// doc 2.3 +// only public function members can be overridden. + +struct Base { +>Base : Base + + private x: { foo: string }; +>x : { foo: string; } +>foo : string +} + +struct Derived extends Base { +>Derived : Derived +>Base : Base + + private x: { foo: string; bar: string; }; // error +>x : { foo: string; bar: string; } +>foo : string +>bar : string +} + +struct Base1 { +>Base1 : Base1 + + public foo: any; +>foo : any +} + +struct Derived1 extends Base1 { +>Derived1 : Derived1 +>Base1 : Base1 + + public foo: string; // error} +>foo : string +} + +struct Base2 { +>Base2 : Base2 + + static y: { foo: string }; +>y : { foo: string; } +>foo : string +} + +struct Derived2 extends Base2 { +>Derived2 : Derived2 +>Base2 : Base2 + + static y: { foo: string; bar: string; }; // ok +>y : { foo: string; bar: string; } +>foo : string +>bar : string +} + +struct Derived3 extends Base2 { +>Derived3 : Derived3 +>Base2 : Base2 + + static y: { foo: any }; // ok +>y : { foo: any; } +>foo : any +} diff --git a/tests/baselines/reference/derivedStructConstructorWithoutSuperCall.errors.txt b/tests/baselines/reference/derivedStructConstructorWithoutSuperCall.errors.txt new file mode 100644 index 0000000000000..43c321ab5b9e1 --- /dev/null +++ b/tests/baselines/reference/derivedStructConstructorWithoutSuperCall.errors.txt @@ -0,0 +1,56 @@ +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructConstructorWithoutSuperCall.ts(9,5): error TS20008: Constructors for derived structs must contain a 'super' call. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructConstructorWithoutSuperCall.ts(18,5): error TS20008: Constructors for derived structs must contain a 'super' call. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructConstructorWithoutSuperCall.ts(19,24): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructConstructorWithoutSuperCall.ts(24,5): error TS20008: Constructors for derived structs must contain a 'super' call. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructConstructorWithoutSuperCall.ts(25,31): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. + + +==== tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructConstructorWithoutSuperCall.ts (5 errors) ==== + // doc 3.2 + // derived struct constructors must contain a super call + + struct Base { + x: string; + } + + struct Derived extends Base { + constructor() { // error + ~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS20008: Constructors for derived structs must contain a 'super' call. + } + + struct Base2 { + x: any; + } + + struct Derived2 extends Base2 { + constructor() { // error for no super call (nested scopes don't count) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + var r2 = () => super(); // error for misplaced super call (nested function) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~ +!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. + } + ~~~~~ +!!! error TS20008: Constructors for derived structs must contain a 'super' call. + } + + struct Derived3 extends Base2 { + constructor() { // error + ~~~~~~~~~~~~~~~~~~~~~~~~ + var r = function () { super() } // error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~ +!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. + } + ~~~~~ +!!! error TS20008: Constructors for derived structs must contain a 'super' call. + } + + struct Derived4 extends Base2 { + constructor() { + var r = super(); // ok + } + } \ No newline at end of file diff --git a/tests/baselines/reference/derivedStructConstructorWithoutSuperCall.js b/tests/baselines/reference/derivedStructConstructorWithoutSuperCall.js new file mode 100644 index 0000000000000..7bb23002b7c2a --- /dev/null +++ b/tests/baselines/reference/derivedStructConstructorWithoutSuperCall.js @@ -0,0 +1,126 @@ +//// [derivedStructConstructorWithoutSuperCall.ts] +// doc 3.2 +// derived struct constructors must contain a super call + +struct Base { + x: string; +} + +struct Derived extends Base { + constructor() { // error + } +} + +struct Base2 { + x: any; +} + +struct Derived2 extends Base2 { + constructor() { // error for no super call (nested scopes don't count) + var r2 = () => super(); // error for misplaced super call (nested function) + } +} + +struct Derived3 extends Base2 { + constructor() { // error + var r = function () { super() } // error + } +} + +struct Derived4 extends Base2 { + constructor() { + var r = super(); // ok + } +} + +//// [derivedStructConstructorWithoutSuperCall.js] +// doc 3.2 +// derived struct constructors must contain a super call +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + x: TypedObject.string + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var Base2 = (function () { + var _Base2 = new TypedObject.StructType({ + x: TypedObject.Any + }); + function _ctor() { + } + function Base2() { + var obj = new _Base2(); + _ctor.call(obj); + return obj; + } + Base2._TO = _Base2; + return Base2; +})(); +var Derived2 = (function () { + var _Derived2 = new TypedObject.StructType({ + }); + function _ctor() { + var r2 = function () { return _super.call(this); }; // error for misplaced super call (nested function) + } + function Derived2() { + var obj = new _Derived2(); + _ctor.call(obj); + return obj; + } + Derived2._TO = _Derived2; + return Derived2; +})(); +var Derived3 = (function () { + var _Derived3 = new TypedObject.StructType({ + }); + function _ctor() { + var r = function () { _super.call(this); }; // error + } + function Derived3() { + var obj = new _Derived3(); + _ctor.call(obj); + return obj; + } + Derived3._TO = _Derived3; + return Derived3; +})(); +var Derived4 = (function () { + var _Derived4 = new TypedObject.StructType({ + }); + function _ctor() { + var r = _super.call(this); // ok + } + function Derived4() { + var obj = new _Derived4(); + _ctor.call(obj); + return obj; + } + Derived4._TO = _Derived4; + return Derived4; +})(); diff --git a/tests/baselines/reference/derivedStructIncludesInheritedMembers.js b/tests/baselines/reference/derivedStructIncludesInheritedMembers.js new file mode 100644 index 0000000000000..ee4b56f1cba7b --- /dev/null +++ b/tests/baselines/reference/derivedStructIncludesInheritedMembers.js @@ -0,0 +1,68 @@ +//// [derivedStructIncludesInheritedMembers.ts] +// doc 2.3 +// A derived struct inherits all members from its base struct it doesn’t override. +// all ok + +struct Base { + a: string; + b() { } + + static r: string; + static s() { } + + constructor(x) { } +} + +struct Derived extends Base { +} + +var d: Derived = new Derived(1); +var r1 = d.a; +var r2 = d.b(); +var r3 = Derived.r; +var r4 = Derived.s(); + + +//// [derivedStructIncludesInheritedMembers.js] +// doc 2.3 +// A derived struct inherits all members from its base struct it doesn’t override. +// all ok +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + a: TypedObject.string, + }); + function _ctor(x) { + } + function Base(x) { + var obj = new _Base(); + _ctor.call(obj ,); + return obj; + } + Base._TO = _Base; + _Base.prototype.b = function () { }; + _Base.s = function () { }; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var d = new Derived(1); +var r1 = d.a; +var r2 = d.b(); +var r3 = Derived.r; +var r4 = Derived.s(); diff --git a/tests/baselines/reference/derivedStructIncludesInheritedMembers.symbols b/tests/baselines/reference/derivedStructIncludesInheritedMembers.symbols new file mode 100644 index 0000000000000..ab1ef3369e5b1 --- /dev/null +++ b/tests/baselines/reference/derivedStructIncludesInheritedMembers.symbols @@ -0,0 +1,58 @@ +=== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructIncludesInheritedMembers.ts === +// doc 2.3 +// A derived struct inherits all members from its base struct it doesn’t override. +// all ok + +struct Base { +>Base : Symbol(Base, Decl(derivedStructIncludesInheritedMembers.ts, 0, 0)) + + a: string; +>a : Symbol(a, Decl(derivedStructIncludesInheritedMembers.ts, 4, 13)) + + b() { } +>b : Symbol(b, Decl(derivedStructIncludesInheritedMembers.ts, 5, 14)) + + static r: string; +>r : Symbol(Base.r, Decl(derivedStructIncludesInheritedMembers.ts, 6, 11)) + + static s() { } +>s : Symbol(Base.s, Decl(derivedStructIncludesInheritedMembers.ts, 8, 21)) + + constructor(x) { } +>x : Symbol(x, Decl(derivedStructIncludesInheritedMembers.ts, 11, 16)) +} + +struct Derived extends Base { +>Derived : Symbol(Derived, Decl(derivedStructIncludesInheritedMembers.ts, 12, 1)) +>Base : Symbol(Base, Decl(derivedStructIncludesInheritedMembers.ts, 0, 0)) +} + +var d: Derived = new Derived(1); +>d : Symbol(d, Decl(derivedStructIncludesInheritedMembers.ts, 17, 3)) +>Derived : Symbol(Derived, Decl(derivedStructIncludesInheritedMembers.ts, 12, 1)) +>Derived : Symbol(Derived, Decl(derivedStructIncludesInheritedMembers.ts, 12, 1)) + +var r1 = d.a; +>r1 : Symbol(r1, Decl(derivedStructIncludesInheritedMembers.ts, 18, 3)) +>d.a : Symbol(Base.a, Decl(derivedStructIncludesInheritedMembers.ts, 4, 13)) +>d : Symbol(d, Decl(derivedStructIncludesInheritedMembers.ts, 17, 3)) +>a : Symbol(Base.a, Decl(derivedStructIncludesInheritedMembers.ts, 4, 13)) + +var r2 = d.b(); +>r2 : Symbol(r2, Decl(derivedStructIncludesInheritedMembers.ts, 19, 3)) +>d.b : Symbol(Base.b, Decl(derivedStructIncludesInheritedMembers.ts, 5, 14)) +>d : Symbol(d, Decl(derivedStructIncludesInheritedMembers.ts, 17, 3)) +>b : Symbol(Base.b, Decl(derivedStructIncludesInheritedMembers.ts, 5, 14)) + +var r3 = Derived.r; +>r3 : Symbol(r3, Decl(derivedStructIncludesInheritedMembers.ts, 20, 3)) +>Derived.r : Symbol(Base.r, Decl(derivedStructIncludesInheritedMembers.ts, 6, 11)) +>Derived : Symbol(Derived, Decl(derivedStructIncludesInheritedMembers.ts, 12, 1)) +>r : Symbol(Base.r, Decl(derivedStructIncludesInheritedMembers.ts, 6, 11)) + +var r4 = Derived.s(); +>r4 : Symbol(r4, Decl(derivedStructIncludesInheritedMembers.ts, 21, 3)) +>Derived.s : Symbol(Base.s, Decl(derivedStructIncludesInheritedMembers.ts, 8, 21)) +>Derived : Symbol(Derived, Decl(derivedStructIncludesInheritedMembers.ts, 12, 1)) +>s : Symbol(Base.s, Decl(derivedStructIncludesInheritedMembers.ts, 8, 21)) + diff --git a/tests/baselines/reference/derivedStructIncludesInheritedMembers.types b/tests/baselines/reference/derivedStructIncludesInheritedMembers.types new file mode 100644 index 0000000000000..b4fc6f5a2bb7b --- /dev/null +++ b/tests/baselines/reference/derivedStructIncludesInheritedMembers.types @@ -0,0 +1,62 @@ +=== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructIncludesInheritedMembers.ts === +// doc 2.3 +// A derived struct inherits all members from its base struct it doesn’t override. +// all ok + +struct Base { +>Base : Base + + a: string; +>a : string + + b() { } +>b : () => void + + static r: string; +>r : string + + static s() { } +>s : () => void + + constructor(x) { } +>x : any +} + +struct Derived extends Base { +>Derived : Derived +>Base : Base +} + +var d: Derived = new Derived(1); +>d : Derived +>Derived : Derived +>new Derived(1) : Derived +>Derived : typeof Derived +>1 : number + +var r1 = d.a; +>r1 : string +>d.a : string +>d : Derived +>a : string + +var r2 = d.b(); +>r2 : void +>d.b() : void +>d.b : () => void +>d : Derived +>b : () => void + +var r3 = Derived.r; +>r3 : string +>Derived.r : string +>Derived : typeof Derived +>r : string + +var r4 = Derived.s(); +>r4 : void +>Derived.s() : void +>Derived.s : () => void +>Derived : typeof Derived +>s : () => void + diff --git a/tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.js b/tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.js new file mode 100644 index 0000000000000..b2e9f43e7e1bb --- /dev/null +++ b/tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.js @@ -0,0 +1,88 @@ +//// [derivedStructOverrideInstanceFunctionMembers.ts] +// doc 2.3 +// derived struct can override base struct's instance function members + +var x: { foo: string; } +var y: { foo: string; bar: string; } + +struct Base { + a: typeof x; + b(a: typeof x) { } + constructor(a: typeof x) { } +} + +struct Derived extends Base { + b(a: typeof y) { return 1; } // ok + constructor(a: typeof y) { super(x) } +} + + +/* struct Base2 { + [i: string]: Object; + [i: number]: typeof x; +} + +struct Derived2 extends Base2 { + [i: string]: typeof x; + [i: number]: typeof y; +} + +var d2: Derived2; +var r7 = d2['']; +var r8 = d2[1]; */ + + + +//// [derivedStructOverrideInstanceFunctionMembers.js] +// doc 2.3 +// derived struct can override base struct's instance function members +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var x; +var y; +var Base = (function () { + var _Base = new TypedObject.StructType({ + a: TypedObject.Object, + }); + function _ctor(a) { + } + function Base(a) { + var obj = new _Base(); + _ctor.call(obj ,); + return obj; + } + Base._TO = _Base; + _Base.prototype.b = function (a) { }; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor(a) { + _super.call(this, x); + } + function Derived(a) { + var obj = new _Derived(); + _ctor.call(obj ,); + return obj; + } + Derived._TO = _Derived; + _Derived.prototype.b = function (a) { return 1; }; // ok + return Derived; +})(); +/* struct Base2 { + [i: string]: Object; + [i: number]: typeof x; +} + +struct Derived2 extends Base2 { + [i: string]: typeof x; + [i: number]: typeof y; +} + +var d2: Derived2; +var r7 = d2['']; +var r8 = d2[1]; */ diff --git a/tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.symbols b/tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.symbols new file mode 100644 index 0000000000000..2d2388dec4357 --- /dev/null +++ b/tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.symbols @@ -0,0 +1,62 @@ +=== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideInstanceFunctionMembers.ts === +// doc 2.3 +// derived struct can override base struct's instance function members + +var x: { foo: string; } +>x : Symbol(x, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 3, 3)) +>foo : Symbol(foo, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 3, 8)) + +var y: { foo: string; bar: string; } +>y : Symbol(y, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 4, 3)) +>foo : Symbol(foo, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 4, 8)) +>bar : Symbol(bar, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 4, 21)) + +struct Base { +>Base : Symbol(Base, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 4, 36)) + + a: typeof x; +>a : Symbol(a, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 6, 13)) +>x : Symbol(x, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 3, 3)) + + b(a: typeof x) { } +>b : Symbol(b, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 7, 16)) +>a : Symbol(a, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 8, 6)) +>x : Symbol(x, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 3, 3)) + + constructor(a: typeof x) { } +>a : Symbol(a, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 9, 16)) +>x : Symbol(x, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 3, 3)) +} + +struct Derived extends Base { +>Derived : Symbol(Derived, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 10, 1)) +>Base : Symbol(Base, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 4, 36)) + + b(a: typeof y) { return 1; } // ok +>b : Symbol(b, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 12, 29)) +>a : Symbol(a, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 13, 6)) +>y : Symbol(y, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 4, 3)) + + constructor(a: typeof y) { super(x) } +>a : Symbol(a, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 14, 16)) +>y : Symbol(y, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 4, 3)) +>super : Symbol(Base, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 4, 36)) +>x : Symbol(x, Decl(derivedStructOverrideInstanceFunctionMembers.ts, 3, 3)) +} + + +/* struct Base2 { + [i: string]: Object; + [i: number]: typeof x; +} + +struct Derived2 extends Base2 { + [i: string]: typeof x; + [i: number]: typeof y; +} + +var d2: Derived2; +var r7 = d2['']; +var r8 = d2[1]; */ + + diff --git a/tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.types b/tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.types new file mode 100644 index 0000000000000..2c4c0fb0df289 --- /dev/null +++ b/tests/baselines/reference/derivedStructOverrideInstanceFunctionMembers.types @@ -0,0 +1,64 @@ +=== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideInstanceFunctionMembers.ts === +// doc 2.3 +// derived struct can override base struct's instance function members + +var x: { foo: string; } +>x : { foo: string; } +>foo : string + +var y: { foo: string; bar: string; } +>y : { foo: string; bar: string; } +>foo : string +>bar : string + +struct Base { +>Base : Base + + a: typeof x; +>a : { foo: string; } +>x : { foo: string; } + + b(a: typeof x) { } +>b : (a: { foo: string; }) => void +>a : { foo: string; } +>x : { foo: string; } + + constructor(a: typeof x) { } +>a : { foo: string; } +>x : { foo: string; } +} + +struct Derived extends Base { +>Derived : Derived +>Base : Base + + b(a: typeof y) { return 1; } // ok +>b : (a: { foo: string; bar: string; }) => number +>a : { foo: string; bar: string; } +>y : { foo: string; bar: string; } +>1 : number + + constructor(a: typeof y) { super(x) } +>a : { foo: string; bar: string; } +>y : { foo: string; bar: string; } +>super(x) : void +>super : typeof Base +>x : { foo: string; } +} + + +/* struct Base2 { + [i: string]: Object; + [i: number]: typeof x; +} + +struct Derived2 extends Base2 { + [i: string]: typeof x; + [i: number]: typeof y; +} + +var d2: Derived2; +var r7 = d2['']; +var r8 = d2[1]; */ + + diff --git a/tests/baselines/reference/derivedStructOverrideStaticProperyMembers.js b/tests/baselines/reference/derivedStructOverrideStaticProperyMembers.js new file mode 100644 index 0000000000000..93e11be0f4d26 --- /dev/null +++ b/tests/baselines/reference/derivedStructOverrideStaticProperyMembers.js @@ -0,0 +1,92 @@ +//// [derivedStructOverrideStaticProperyMembers.ts] +// doc 2.3 +// derived struct can override base struct's static property members + +var x: { foo: string; } +var y: { foo: string; bar: string; } + +struct Base { + static r: typeof x; + static s(a: typeof x) { return 1; } + static u: (a: typeof x) => any; + + constructor(a: typeof x) { } +} + +struct Derived extends Base { + static r: typeof y; // ok + static s(a: typeof y) { } // ok + static u: (a: typeof y) => void; // ok + + constructor(a: typeof y) { super(x) } +} + + +/* struct Base2 { + [i: string]: Object; + [i: number]: typeof x; +} + +struct Derived2 extends Base2 { + [i: string]: typeof x; + [i: number]: typeof y; +} + +var d2: Derived2; +var r7 = d2['']; +var r8 = d2[1]; */ + + + +//// [derivedStructOverrideStaticProperyMembers.js] +// doc 2.3 +// derived struct can override base struct's static property members +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var x; +var y; +var Base = (function () { + var _Base = new TypedObject.StructType({ + }); + function _ctor(a) { + } + function Base(a) { + var obj = new _Base(); + _ctor.call(obj ,); + return obj; + } + Base._TO = _Base; + _Base.s = function (a) { return 1; }; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor(a) { + _super.call(this, x); + } + function Derived(a) { + var obj = new _Derived(); + _ctor.call(obj ,); + return obj; + } + Derived._TO = _Derived; + _Derived.s = function (a) { }; // ok + return Derived; +})(); +/* struct Base2 { + [i: string]: Object; + [i: number]: typeof x; +} + +struct Derived2 extends Base2 { + [i: string]: typeof x; + [i: number]: typeof y; +} + +var d2: Derived2; +var r7 = d2['']; +var r8 = d2[1]; */ diff --git a/tests/baselines/reference/derivedStructOverrideStaticProperyMembers.symbols b/tests/baselines/reference/derivedStructOverrideStaticProperyMembers.symbols new file mode 100644 index 0000000000000..f22d68782821f --- /dev/null +++ b/tests/baselines/reference/derivedStructOverrideStaticProperyMembers.symbols @@ -0,0 +1,76 @@ +=== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideStaticProperyMembers.ts === +// doc 2.3 +// derived struct can override base struct's static property members + +var x: { foo: string; } +>x : Symbol(x, Decl(derivedStructOverrideStaticProperyMembers.ts, 3, 3)) +>foo : Symbol(foo, Decl(derivedStructOverrideStaticProperyMembers.ts, 3, 8)) + +var y: { foo: string; bar: string; } +>y : Symbol(y, Decl(derivedStructOverrideStaticProperyMembers.ts, 4, 3)) +>foo : Symbol(foo, Decl(derivedStructOverrideStaticProperyMembers.ts, 4, 8)) +>bar : Symbol(bar, Decl(derivedStructOverrideStaticProperyMembers.ts, 4, 21)) + +struct Base { +>Base : Symbol(Base, Decl(derivedStructOverrideStaticProperyMembers.ts, 4, 36)) + + static r: typeof x; +>r : Symbol(Base.r, Decl(derivedStructOverrideStaticProperyMembers.ts, 6, 13)) +>x : Symbol(x, Decl(derivedStructOverrideStaticProperyMembers.ts, 3, 3)) + + static s(a: typeof x) { return 1; } +>s : Symbol(Base.s, Decl(derivedStructOverrideStaticProperyMembers.ts, 7, 23)) +>a : Symbol(a, Decl(derivedStructOverrideStaticProperyMembers.ts, 8, 13)) +>x : Symbol(x, Decl(derivedStructOverrideStaticProperyMembers.ts, 3, 3)) + + static u: (a: typeof x) => any; +>u : Symbol(Base.u, Decl(derivedStructOverrideStaticProperyMembers.ts, 8, 39)) +>a : Symbol(a, Decl(derivedStructOverrideStaticProperyMembers.ts, 9, 15)) +>x : Symbol(x, Decl(derivedStructOverrideStaticProperyMembers.ts, 3, 3)) + + constructor(a: typeof x) { } +>a : Symbol(a, Decl(derivedStructOverrideStaticProperyMembers.ts, 11, 16)) +>x : Symbol(x, Decl(derivedStructOverrideStaticProperyMembers.ts, 3, 3)) +} + +struct Derived extends Base { +>Derived : Symbol(Derived, Decl(derivedStructOverrideStaticProperyMembers.ts, 12, 1)) +>Base : Symbol(Base, Decl(derivedStructOverrideStaticProperyMembers.ts, 4, 36)) + + static r: typeof y; // ok +>r : Symbol(Derived.r, Decl(derivedStructOverrideStaticProperyMembers.ts, 14, 29)) +>y : Symbol(y, Decl(derivedStructOverrideStaticProperyMembers.ts, 4, 3)) + + static s(a: typeof y) { } // ok +>s : Symbol(Derived.s, Decl(derivedStructOverrideStaticProperyMembers.ts, 15, 23)) +>a : Symbol(a, Decl(derivedStructOverrideStaticProperyMembers.ts, 16, 13)) +>y : Symbol(y, Decl(derivedStructOverrideStaticProperyMembers.ts, 4, 3)) + + static u: (a: typeof y) => void; // ok +>u : Symbol(Derived.u, Decl(derivedStructOverrideStaticProperyMembers.ts, 16, 29)) +>a : Symbol(a, Decl(derivedStructOverrideStaticProperyMembers.ts, 17, 15)) +>y : Symbol(y, Decl(derivedStructOverrideStaticProperyMembers.ts, 4, 3)) + + constructor(a: typeof y) { super(x) } +>a : Symbol(a, Decl(derivedStructOverrideStaticProperyMembers.ts, 19, 16)) +>y : Symbol(y, Decl(derivedStructOverrideStaticProperyMembers.ts, 4, 3)) +>super : Symbol(Base, Decl(derivedStructOverrideStaticProperyMembers.ts, 4, 36)) +>x : Symbol(x, Decl(derivedStructOverrideStaticProperyMembers.ts, 3, 3)) +} + + +/* struct Base2 { + [i: string]: Object; + [i: number]: typeof x; +} + +struct Derived2 extends Base2 { + [i: string]: typeof x; + [i: number]: typeof y; +} + +var d2: Derived2; +var r7 = d2['']; +var r8 = d2[1]; */ + + diff --git a/tests/baselines/reference/derivedStructOverrideStaticProperyMembers.types b/tests/baselines/reference/derivedStructOverrideStaticProperyMembers.types new file mode 100644 index 0000000000000..65054b395b645 --- /dev/null +++ b/tests/baselines/reference/derivedStructOverrideStaticProperyMembers.types @@ -0,0 +1,78 @@ +=== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructOverrideStaticProperyMembers.ts === +// doc 2.3 +// derived struct can override base struct's static property members + +var x: { foo: string; } +>x : { foo: string; } +>foo : string + +var y: { foo: string; bar: string; } +>y : { foo: string; bar: string; } +>foo : string +>bar : string + +struct Base { +>Base : Base + + static r: typeof x; +>r : { foo: string; } +>x : { foo: string; } + + static s(a: typeof x) { return 1; } +>s : (a: { foo: string; }) => number +>a : { foo: string; } +>x : { foo: string; } +>1 : number + + static u: (a: typeof x) => any; +>u : (a: { foo: string; }) => any +>a : { foo: string; } +>x : { foo: string; } + + constructor(a: typeof x) { } +>a : { foo: string; } +>x : { foo: string; } +} + +struct Derived extends Base { +>Derived : Derived +>Base : Base + + static r: typeof y; // ok +>r : { foo: string; bar: string; } +>y : { foo: string; bar: string; } + + static s(a: typeof y) { } // ok +>s : (a: { foo: string; bar: string; }) => void +>a : { foo: string; bar: string; } +>y : { foo: string; bar: string; } + + static u: (a: typeof y) => void; // ok +>u : (a: { foo: string; bar: string; }) => void +>a : { foo: string; bar: string; } +>y : { foo: string; bar: string; } + + constructor(a: typeof y) { super(x) } +>a : { foo: string; bar: string; } +>y : { foo: string; bar: string; } +>super(x) : void +>super : typeof Base +>x : { foo: string; } +} + + +/* struct Base2 { + [i: string]: Object; + [i: number]: typeof x; +} + +struct Derived2 extends Base2 { + [i: string]: typeof x; + [i: number]: typeof y; +} + +var d2: Derived2; +var r7 = d2['']; +var r8 = d2[1]; */ + + diff --git a/tests/baselines/reference/derivedStructParameterProperties.errors.txt b/tests/baselines/reference/derivedStructParameterProperties.errors.txt new file mode 100644 index 0000000000000..a340db6a256ee --- /dev/null +++ b/tests/baselines/reference/derivedStructParameterProperties.errors.txt @@ -0,0 +1,119 @@ +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructParameterProperties.ts(18,5): error TS20007: A 'super' call must be the first statement in the constructor when a struct contains initialized properties or has parameter properties. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructParameterProperties.ts(33,5): error TS20007: A 'super' call must be the first statement in the constructor when a struct contains initialized properties or has parameter properties. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructParameterProperties.ts(59,5): error TS20007: A 'super' call must be the first statement in the constructor when a struct contains initialized properties or has parameter properties. + + +==== tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructParameterProperties.ts (3 errors) ==== + // doc 3.2 + // ordering of super calls in derived constructors matters depending on other struct contents + // A 'super' call must be the first statement in the constructor when a struct contains + // initialized properties or has parameter properties. + + struct Base { + x: string; + } + + struct Derived extends Base { + constructor(y: string) { + var a = 1; + super(); // ok + } + } + + struct Derived2 extends Base { + constructor(public y: string) { // has parameter properties + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + var a = 1; + ~~~~~~~~~~~~~~~~~~ + super(); // error + ~~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS20007: A 'super' call must be the first statement in the constructor when a struct contains initialized properties or has parameter properties. + } + + struct Derived3 extends Base { + constructor(public y: string) { + super(); // ok + var a = 1; + } + } + + struct Derived4 extends Base { + a = 1; // contains initialized properties + constructor(y: string) { + ~~~~~~~~~~~~~~~~~~~~~~~~ + var b = 2; + ~~~~~~~~~~~~~~~~~~ + super(); // error + ~~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS20007: A 'super' call must be the first statement in the constructor when a struct contains initialized properties or has parameter properties. + } + + struct Derived5 extends Base { + a = 1; + constructor(y: string) { + super(); // ok + var b = 2; + } + } + + struct Derived6 extends Base { + a: number; + constructor(y: string) { + this.a = 1; + var b = 2; + super(); // ok + } + } + + struct Derived7 extends Base { + a = 1; // contains initialized properties + b: number; + constructor(y: string) { + ~~~~~~~~~~~~~~~~~~~~~~~~ + this.a = 3; + ~~~~~~~~~~~~~~~~~~~ + this.b = 3; + ~~~~~~~~~~~~~~~~~~~ + super(); // error + ~~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS20007: A 'super' call must be the first statement in the constructor when a struct contains initialized properties or has parameter properties. + } + + struct Derived8 extends Base { + a = 1; + b: number; + constructor(y: string) { + super(); // ok + this.a = 3; + this.b = 3; + } + } + + /* generic cases of Derived7 and Derived8 + struct Base2 { x: T; } + + struct Derived9 extends Base2 { + a = 1; // contains initialized properties + b: number; + constructor(y: string) { + this.a = 3; + this.b = 3; + super(); // error + } + } + + struct Derived10 extends Base2 { + a = 1; + b: number; + constructor(y: string) { + super(); // ok + this.a = 3; + this.b = 3; + } + } */ \ No newline at end of file diff --git a/tests/baselines/reference/derivedStructParameterProperties.js b/tests/baselines/reference/derivedStructParameterProperties.js new file mode 100644 index 0000000000000..fab384fb761cd --- /dev/null +++ b/tests/baselines/reference/derivedStructParameterProperties.js @@ -0,0 +1,277 @@ +//// [derivedStructParameterProperties.ts] +// doc 3.2 +// ordering of super calls in derived constructors matters depending on other struct contents +// A 'super' call must be the first statement in the constructor when a struct contains +// initialized properties or has parameter properties. + +struct Base { + x: string; +} + +struct Derived extends Base { + constructor(y: string) { + var a = 1; + super(); // ok + } +} + +struct Derived2 extends Base { + constructor(public y: string) { // has parameter properties + var a = 1; + super(); // error + } +} + +struct Derived3 extends Base { + constructor(public y: string) { + super(); // ok + var a = 1; + } +} + +struct Derived4 extends Base { + a = 1; // contains initialized properties + constructor(y: string) { + var b = 2; + super(); // error + } +} + +struct Derived5 extends Base { + a = 1; + constructor(y: string) { + super(); // ok + var b = 2; + } +} + +struct Derived6 extends Base { + a: number; + constructor(y: string) { + this.a = 1; + var b = 2; + super(); // ok + } +} + +struct Derived7 extends Base { + a = 1; // contains initialized properties + b: number; + constructor(y: string) { + this.a = 3; + this.b = 3; + super(); // error + } +} + +struct Derived8 extends Base { + a = 1; + b: number; + constructor(y: string) { + super(); // ok + this.a = 3; + this.b = 3; + } +} + +/* generic cases of Derived7 and Derived8 +struct Base2 { x: T; } + +struct Derived9 extends Base2 { + a = 1; // contains initialized properties + b: number; + constructor(y: string) { + this.a = 3; + this.b = 3; + super(); // error + } +} + +struct Derived10 extends Base2 { + a = 1; + b: number; + constructor(y: string) { + super(); // ok + this.a = 3; + this.b = 3; + } +} */ + +//// [derivedStructParameterProperties.js] +// doc 3.2 +// ordering of super calls in derived constructors matters depending on other struct contents +// A 'super' call must be the first statement in the constructor when a struct contains +// initialized properties or has parameter properties. +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + x: TypedObject.string + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor(y) { + var a = 1; + _super.call(this); // ok + } + function Derived(y) { + var obj = new _Derived(); + _ctor.call(obj ,); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var Derived2 = (function () { + var _Derived2 = new TypedObject.StructType({ + }); + function _ctor(y) { + this.y = y; + var a = 1; + _super.call(this); // error + } + function Derived2(y) { + var obj = new _Derived2(); + _ctor.call(obj ,); + return obj; + } + Derived2._TO = _Derived2; + return Derived2; +})(); +var Derived3 = (function () { + var _Derived3 = new TypedObject.StructType({ + }); + function _ctor(y) { + this.y = y; + _super.call(this); // ok + var a = 1; + } + function Derived3(y) { + var obj = new _Derived3(); + _ctor.call(obj ,); + return obj; + } + Derived3._TO = _Derived3; + return Derived3; +})(); +var Derived4 = (function () { + var _Derived4 = new TypedObject.StructType({ + a: TypedObject.float64, + }); + function _ctor(y) { + var b = 2; + _super.call(this); // error + } + function Derived4(y) { + var obj = new _Derived4(); + _ctor.call(obj ,); + return obj; + } + Derived4._TO = _Derived4; + return Derived4; +})(); +var Derived5 = (function () { + var _Derived5 = new TypedObject.StructType({ + a: TypedObject.float64, + }); + function _ctor(y) { + _super.call(this); // ok + var b = 2; + } + function Derived5(y) { + var obj = new _Derived5(); + _ctor.call(obj ,); + return obj; + } + Derived5._TO = _Derived5; + return Derived5; +})(); +var Derived6 = (function () { + var _Derived6 = new TypedObject.StructType({ + a: TypedObject.float64, + }); + function _ctor(y) { + this.a = 1; + var b = 2; + _super.call(this); // ok + } + function Derived6(y) { + var obj = new _Derived6(); + _ctor.call(obj ,); + return obj; + } + Derived6._TO = _Derived6; + return Derived6; +})(); +var Derived7 = (function () { + var _Derived7 = new TypedObject.StructType({ + a: TypedObject.float64, + b: TypedObject.float64, + }); + function _ctor(y) { + this.a = 3; + this.b = 3; + _super.call(this); // error + } + function Derived7(y) { + var obj = new _Derived7(); + _ctor.call(obj ,); + return obj; + } + Derived7._TO = _Derived7; + return Derived7; +})(); +var Derived8 = (function () { + var _Derived8 = new TypedObject.StructType({ + a: TypedObject.float64, + b: TypedObject.float64, + }); + function _ctor(y) { + _super.call(this); // ok + this.a = 3; + this.b = 3; + } + function Derived8(y) { + var obj = new _Derived8(); + _ctor.call(obj ,); + return obj; + } + Derived8._TO = _Derived8; + return Derived8; +})(); +/* generic cases of Derived7 and Derived8 +struct Base2 { x: T; } + +struct Derived9 extends Base2 { + a = 1; // contains initialized properties + b: number; + constructor(y: string) { + this.a = 3; + this.b = 3; + super(); // error + } +} + +struct Derived10 extends Base2 { + a = 1; + b: number; + constructor(y: string) { + super(); // ok + this.a = 3; + this.b = 3; + } +} */ diff --git a/tests/baselines/reference/derivedStructSuperCallsInNonConstructorMembers.errors.txt b/tests/baselines/reference/derivedStructSuperCallsInNonConstructorMembers.errors.txt new file mode 100644 index 0000000000000..d9c1774342b13 --- /dev/null +++ b/tests/baselines/reference/derivedStructSuperCallsInNonConstructorMembers.errors.txt @@ -0,0 +1,39 @@ +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts(9,8): error TS1110: Type expected. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts(9,8): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts(11,9): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts(14,15): error TS1110: Type expected. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts(14,15): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts(16,9): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. + + +==== tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsInNonConstructorMembers.ts (6 errors) ==== + // doc 3.2 + // error to use super calls outside a constructor or in nested functions inside constructors + + struct Base { + x: string; + } + + struct Derived extends Base { + a: super(); // error + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. + b() { + super(); // error + ~~~~~ +!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. + } + + static a: super(); // error + ~~~~~ +!!! error TS1110: Type expected. + ~~~~~ +!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. + static b() { + super(); // error + ~~~~~ +!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/derivedStructSuperCallsInNonConstructorMembers.js b/tests/baselines/reference/derivedStructSuperCallsInNonConstructorMembers.js new file mode 100644 index 0000000000000..8432bfb2f2cea --- /dev/null +++ b/tests/baselines/reference/derivedStructSuperCallsInNonConstructorMembers.js @@ -0,0 +1,62 @@ +//// [derivedStructSuperCallsInNonConstructorMembers.ts] +// doc 3.2 +// error to use super calls outside a constructor or in nested functions inside constructors + +struct Base { + x: string; +} + +struct Derived extends Base { + a: super(); // error + b() { + super(); // error + } + + static a: super(); // error + static b() { + super(); // error + } +} + +//// [derivedStructSuperCallsInNonConstructorMembers.js] +// doc 3.2 +// error to use super calls outside a constructor or in nested functions inside constructors +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + x: TypedObject.string + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + a: TypedObject.Any, + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + _Derived.prototype.b = function () { + _super.call(this); // error + }; + _Derived.b = function () { + _super.call(this); // error + }; + return Derived; +})(); diff --git a/tests/baselines/reference/derivedStructSuperCallsWithThisArg.errors.txt b/tests/baselines/reference/derivedStructSuperCallsWithThisArg.errors.txt new file mode 100644 index 0000000000000..c066ce7947619 --- /dev/null +++ b/tests/baselines/reference/derivedStructSuperCallsWithThisArg.errors.txt @@ -0,0 +1,40 @@ +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsWithThisArg.ts(17,15): error TS2332: 'this' cannot be referenced in current location. +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsWithThisArg.ts(23,21): error TS2332: 'this' cannot be referenced in current location. + + +==== tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/derivedStructSuperCallsWithThisArg.ts (2 errors) ==== + // doc 3.2 + // it is a compile-time error for argument expressions to reference this. + + struct Base { + x: string; + constructor(a) { } + } + + struct Derived extends Base { + constructor() { + super(this); // error, not assignable + } + } + + struct Derived2 extends Base { + constructor(public a: string) { + super(this); // error, 'this' cannot be referenced in current location + ~~~~ +!!! error TS2332: 'this' cannot be referenced in current location. + } + } + + struct Derived3 extends Base { + constructor(public a: string) { + super(() => this); // error, 'this' cannot be referenced in current location + ~~~~ +!!! error TS2332: 'this' cannot be referenced in current location. + } + } + + struct Derived4 extends Base { + constructor(public a: string) { + super(function () { return this; }); // ok + } + } \ No newline at end of file diff --git a/tests/baselines/reference/derivedStructSuperCallsWithThisArg.js b/tests/baselines/reference/derivedStructSuperCallsWithThisArg.js new file mode 100644 index 0000000000000..85de3b99eacee --- /dev/null +++ b/tests/baselines/reference/derivedStructSuperCallsWithThisArg.js @@ -0,0 +1,115 @@ +//// [derivedStructSuperCallsWithThisArg.ts] +// doc 3.2 +// it is a compile-time error for argument expressions to reference this. + +struct Base { + x: string; + constructor(a) { } +} + +struct Derived extends Base { + constructor() { + super(this); // error, not assignable + } +} + +struct Derived2 extends Base { + constructor(public a: string) { + super(this); // error, 'this' cannot be referenced in current location + } +} + +struct Derived3 extends Base { + constructor(public a: string) { + super(() => this); // error, 'this' cannot be referenced in current location + } +} + +struct Derived4 extends Base { + constructor(public a: string) { + super(function () { return this; }); // ok + } +} + +//// [derivedStructSuperCallsWithThisArg.js] +// doc 3.2 +// it is a compile-time error for argument expressions to reference this. +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + x: TypedObject.string, + }); + function _ctor(a) { + } + function Base(a) { + var obj = new _Base(); + _ctor.call(obj ,); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor() { + _super.call(this, this); // error, not assignable + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var Derived2 = (function () { + var _Derived2 = new TypedObject.StructType({ + }); + function _ctor(a) { + this.a = a; + _super.call(this, this); // error, 'this' cannot be referenced in current location + } + function Derived2(a) { + var obj = new _Derived2(); + _ctor.call(obj ,); + return obj; + } + Derived2._TO = _Derived2; + return Derived2; +})(); +var Derived3 = (function () { + var _Derived3 = new TypedObject.StructType({ + }); + function _ctor(a) { + var _this = this; + this.a = a; + _super.call(this, function () { return _this; }); // error, 'this' cannot be referenced in current location + } + function Derived3(a) { + var obj = new _Derived3(); + _ctor.call(obj ,); + return obj; + } + Derived3._TO = _Derived3; + return Derived3; +})(); +var Derived4 = (function () { + var _Derived4 = new TypedObject.StructType({ + }); + function _ctor(a) { + this.a = a; + _super.call(this, function () { return this; }); // ok + } + function Derived4(a) { + var obj = new _Derived4(); + _ctor.call(obj ,); + return obj; + } + Derived4._TO = _Derived4; + return Derived4; +})(); diff --git a/tests/baselines/reference/derivedStructWithPrivateInstanceShadowingPublicInstance.errors.txt b/tests/baselines/reference/derivedStructWithPrivateInstanceShadowingPublicInstance.errors.txt new file mode 100644 index 0000000000000..ca290eaa0b915 --- /dev/null +++ b/tests/baselines/reference/derivedStructWithPrivateInstanceShadowingPublicInstance.errors.txt @@ -0,0 +1,27 @@ +tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateInstanceShadowingPublicInstance.ts(20,10): error TS20018: Property 'fn' is private and only accessible within struct 'Derived'. + + +==== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateInstanceShadowingPublicInstance.ts (1 errors) ==== + // doc 2.3 + + struct Base { + public fn(): string { + return ''; + } + } + + // error, not a subtype + struct Derived extends Base { + private fn(): string { + return ''; + } + } + + var b: Base; + var d: Derived; + + var r1 = b.fn(); // ok + var r2 = d.fn(); // error + ~~~~ +!!! error TS20018: Property 'fn' is private and only accessible within struct 'Derived'. + var r3 = (d).fn; // ok \ No newline at end of file diff --git a/tests/baselines/reference/derivedStructWithPrivateInstanceShadowingPublicInstance.js b/tests/baselines/reference/derivedStructWithPrivateInstanceShadowingPublicInstance.js new file mode 100644 index 0000000000000..7f9c6aca4756e --- /dev/null +++ b/tests/baselines/reference/derivedStructWithPrivateInstanceShadowingPublicInstance.js @@ -0,0 +1,68 @@ +//// [derivedStructWithPrivateInstanceShadowingPublicInstance.ts] +// doc 2.3 + +struct Base { + public fn(): string { + return ''; + } +} + +// error, not a subtype +struct Derived extends Base { + private fn(): string { + return ''; + } +} + +var b: Base; +var d: Derived; + +var r1 = b.fn(); // ok +var r2 = d.fn(); // error +var r3 = (d).fn; // ok + +//// [derivedStructWithPrivateInstanceShadowingPublicInstance.js] +// doc 2.3 +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + _Base.prototype.fn = function () { + return ''; + }; + return Base; +})(); +// error, not a subtype +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + _Derived.prototype.fn = function () { + return ''; + }; + return Derived; +})(); +var b; +var d; +var r1 = b.fn(); // ok +var r2 = d.fn(); // error +var r3 = d.fn; // ok diff --git a/tests/baselines/reference/derivedStructWithPrivateStaticShadowingPublicStatic.errors.txt b/tests/baselines/reference/derivedStructWithPrivateStaticShadowingPublicStatic.errors.txt new file mode 100644 index 0000000000000..46c1b8aee40f3 --- /dev/null +++ b/tests/baselines/reference/derivedStructWithPrivateStaticShadowingPublicStatic.errors.txt @@ -0,0 +1,32 @@ +tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateStaticShadowingPublicStatic.ts(20,10): error TS20018: Property 'x' is private and only accessible within struct 'Derived'. +tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateStaticShadowingPublicStatic.ts(23,10): error TS20018: Property 'fn' is private and only accessible within struct 'Derived'. + + +==== tests/cases/conformance/structs/members/inheritanceAndOverriding/derivedStructWithPrivateStaticShadowingPublicStatic.ts (2 errors) ==== + // doc 2.3 + + struct Base { + public static x: string; + public static fn(): string { + return ''; + } + } + + struct Derived extends Base { + private static x: string; + private static fn(): string { + return ''; + } + } + + var b: typeof Base; + var d: typeof Derived; + var r = b.x; // ok + var r2 = d.x; // error + ~~~ +!!! error TS20018: Property 'x' is private and only accessible within struct 'Derived'. + + var r3 = b.fn(); // ok + var r4 = d.fn(); // error + ~~~~ +!!! error TS20018: Property 'fn' is private and only accessible within struct 'Derived'. \ No newline at end of file diff --git a/tests/baselines/reference/derivedStructWithPrivateStaticShadowingPublicStatic.js b/tests/baselines/reference/derivedStructWithPrivateStaticShadowingPublicStatic.js new file mode 100644 index 0000000000000..bda5a4e938c82 --- /dev/null +++ b/tests/baselines/reference/derivedStructWithPrivateStaticShadowingPublicStatic.js @@ -0,0 +1,70 @@ +//// [derivedStructWithPrivateStaticShadowingPublicStatic.ts] +// doc 2.3 + +struct Base { + public static x: string; + public static fn(): string { + return ''; + } +} + +struct Derived extends Base { + private static x: string; + private static fn(): string { + return ''; + } +} + +var b: typeof Base; +var d: typeof Derived; +var r = b.x; // ok +var r2 = d.x; // error + +var r3 = b.fn(); // ok +var r4 = d.fn(); // error + +//// [derivedStructWithPrivateStaticShadowingPublicStatic.js] +// doc 2.3 +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + _Base.fn = function () { + return ''; + }; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + _Derived.fn = function () { + return ''; + }; + return Derived; +})(); +var b; +var d; +var r = b.x; // ok +var r2 = d.x; // error +var r3 = b.fn(); // ok +var r4 = d.fn(); // error diff --git a/tests/baselines/reference/derivedStructWithoutExplicitConstructor.errors.txt b/tests/baselines/reference/derivedStructWithoutExplicitConstructor.errors.txt new file mode 100644 index 0000000000000..fe327729d5a7a --- /dev/null +++ b/tests/baselines/reference/derivedStructWithoutExplicitConstructor.errors.txt @@ -0,0 +1,36 @@ +tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor.ts(15,9): error TS2346: Supplied parameters do not match any signature of call target. + + +==== tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor.ts (1 errors) ==== + // doc 3.3 + // In a derived struct, the automatic constructor has the same parameter list + // (and possibly overloads) as the base struct constructor. + + struct Base { + a = 1; + constructor(x: number) { this.a = x; } + } + + struct Derived extends Base { + x = 1 + y = 'hello'; + } + + var r = new Derived(); // error, Supplied parameters do not match any signature of call target + ~~~~~~~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + var r2 = new Derived(1); + + /* struct Base2 { + a: T; + constructor(x: T) { this.a = x; } + } + + struct D extends Base2 { + x = 2 + y: T = null; + } + + var d = new D(); // error, Supplied parameters do not match any signature of call target + var d2 = new D(new Date()); // ok + */ \ No newline at end of file diff --git a/tests/baselines/reference/derivedStructWithoutExplicitConstructor.js b/tests/baselines/reference/derivedStructWithoutExplicitConstructor.js new file mode 100644 index 0000000000000..c94645d220ffe --- /dev/null +++ b/tests/baselines/reference/derivedStructWithoutExplicitConstructor.js @@ -0,0 +1,86 @@ +//// [derivedStructWithoutExplicitConstructor.ts] +// doc 3.3 +// In a derived struct, the automatic constructor has the same parameter list +// (and possibly overloads) as the base struct constructor. + +struct Base { + a = 1; + constructor(x: number) { this.a = x; } +} + +struct Derived extends Base { + x = 1 + y = 'hello'; +} + +var r = new Derived(); // error, Supplied parameters do not match any signature of call target +var r2 = new Derived(1); + +/* struct Base2 { + a: T; + constructor(x: T) { this.a = x; } +} + +struct D extends Base2 { + x = 2 + y: T = null; +} + +var d = new D(); // error, Supplied parameters do not match any signature of call target +var d2 = new D(new Date()); // ok +*/ + +//// [derivedStructWithoutExplicitConstructor.js] +// doc 3.3 +// In a derived struct, the automatic constructor has the same parameter list +// (and possibly overloads) as the base struct constructor. +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + a: TypedObject.float64, + }); + function _ctor(x) { + this.a = x; + } + function Base(x) { + var obj = new _Base(); + _ctor.call(obj ,); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + x: TypedObject.float64, + y: TypedObject.string + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var r = new Derived(); // error, Supplied parameters do not match any signature of call target +var r2 = new Derived(1); +/* struct Base2 { + a: T; + constructor(x: T) { this.a = x; } +} + +struct D extends Base2 { + x = 2 + y: T = null; +} + +var d = new D(); // error, Supplied parameters do not match any signature of call target +var d2 = new D(new Date()); // ok +*/ diff --git a/tests/baselines/reference/derivedStructWithoutExplicitConstructor2.errors.txt b/tests/baselines/reference/derivedStructWithoutExplicitConstructor2.errors.txt new file mode 100644 index 0000000000000..156b5fcb0071d --- /dev/null +++ b/tests/baselines/reference/derivedStructWithoutExplicitConstructor2.errors.txt @@ -0,0 +1,59 @@ +tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor2.ts(24,9): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor2.ts(25,10): error TS2346: Supplied parameters do not match any signature of call target. + + +==== tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/derivedStructWithoutExplicitConstructor2.ts (2 errors) ==== + // doc 3.3 + // In a derived struct, the automatic constructor has the same parameter list + // (and possibly overloads) as the base struct constructor. + // automatic constructors with a struct hieararchy of depth > 2 + + struct Base { + a = 1; + constructor(x: number) { this.a = x; } + } + + struct Derived extends Base { + b = ''; + constructor(y: string, z: string) { + super(2); + this.b = y; + } + } + + struct Derived2 extends Derived { + x = 1 + y = 'hello'; + } + + var r = new Derived(); // error, Supplied parameters do not match any signature of call target. + ~~~~~~~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + var r2 = new Derived2(1); // error, Supplied parameters do not match any signature of call target. + ~~~~~~~~~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + var r3 = new Derived('', ''); + + /* struct Base2 { + a: T; + constructor(x: T) { this.a = x; } + } + + struct D extends Base { + b: T = null; + constructor(y: T, z: T) { + super(2); + this.b = y; + } + } + + + struct D2 extends D { + x = 2 + y: T = null; + } + + var d = new D2(); // error, Supplied parameters do not match any signature of call target. + var d2 = new D2(new Date()); // error, Supplied parameters do not match any signature of call target. + var d3 = new D2(new Date(), new Date()); // ok + */ \ No newline at end of file diff --git a/tests/baselines/reference/derivedStructWithoutExplicitConstructor2.js b/tests/baselines/reference/derivedStructWithoutExplicitConstructor2.js new file mode 100644 index 0000000000000..feafe867b003f --- /dev/null +++ b/tests/baselines/reference/derivedStructWithoutExplicitConstructor2.js @@ -0,0 +1,134 @@ +//// [derivedStructWithoutExplicitConstructor2.ts] +// doc 3.3 +// In a derived struct, the automatic constructor has the same parameter list +// (and possibly overloads) as the base struct constructor. +// automatic constructors with a struct hieararchy of depth > 2 + +struct Base { + a = 1; + constructor(x: number) { this.a = x; } +} + +struct Derived extends Base { + b = ''; + constructor(y: string, z: string) { + super(2); + this.b = y; + } +} + +struct Derived2 extends Derived { + x = 1 + y = 'hello'; +} + +var r = new Derived(); // error, Supplied parameters do not match any signature of call target. +var r2 = new Derived2(1); // error, Supplied parameters do not match any signature of call target. +var r3 = new Derived('', ''); + +/* struct Base2 { + a: T; + constructor(x: T) { this.a = x; } +} + +struct D extends Base { + b: T = null; + constructor(y: T, z: T) { + super(2); + this.b = y; + } +} + + +struct D2 extends D { + x = 2 + y: T = null; +} + +var d = new D2(); // error, Supplied parameters do not match any signature of call target. +var d2 = new D2(new Date()); // error, Supplied parameters do not match any signature of call target. +var d3 = new D2(new Date(), new Date()); // ok + */ + +//// [derivedStructWithoutExplicitConstructor2.js] +// doc 3.3 +// In a derived struct, the automatic constructor has the same parameter list +// (and possibly overloads) as the base struct constructor. +// automatic constructors with a struct hieararchy of depth > 2 +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + a: TypedObject.float64, + }); + function _ctor(x) { + this.a = x; + } + function Base(x) { + var obj = new _Base(); + _ctor.call(obj ,); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + b: TypedObject.string, + }); + function _ctor(y, z) { + _super.call(this, 2); + this.b = y; + } + function Derived(y, z) { + var obj = new _Derived(); + _ctor.call(obj ,); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var Derived2 = (function () { + var _Derived2 = new TypedObject.StructType({ + x: TypedObject.float64, + y: TypedObject.string + }); + function _ctor() { + } + function Derived2() { + var obj = new _Derived2(); + _ctor.call(obj); + return obj; + } + Derived2._TO = _Derived2; + return Derived2; +})(); +var r = new Derived(); // error, Supplied parameters do not match any signature of call target. +var r2 = new Derived2(1); // error, Supplied parameters do not match any signature of call target. +var r3 = new Derived('', ''); +/* struct Base2 { + a: T; + constructor(x: T) { this.a = x; } +} + +struct D extends Base { + b: T = null; + constructor(y: T, z: T) { + super(2); + this.b = y; + } +} + + +struct D2 extends D { + x = 2 + y: T = null; +} + +var d = new D2(); // error, Supplied parameters do not match any signature of call target. +var d2 = new D2(new Date()); // error, Supplied parameters do not match any signature of call target. +var d3 = new D2(new Date(), new Date()); // ok + */ diff --git a/tests/baselines/reference/instanceMemberAssignsToStructPrototype.errors.txt b/tests/baselines/reference/instanceMemberAssignsToStructPrototype.errors.txt new file mode 100644 index 0000000000000..5670ad42bc010 --- /dev/null +++ b/tests/baselines/reference/instanceMemberAssignsToStructPrototype.errors.txt @@ -0,0 +1,24 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/instanceMemberAssignsToStructPrototype.ts(11,9): error TS2322: Type '() => void' is not assignable to type '(x: number) => number'. + Type 'void' is not assignable to type 'number'. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/instanceMemberAssignsToStructPrototype.ts (1 errors) ==== + // doc 4.2 + // An instance member function declaration declares a property in the struct instance type and + // assigns a function object to a property on the prototype object of the struct. + + struct C { + foo() { + C.prototype.foo = () => { } + } + + bar(x: number): number { + C.prototype.bar = () => { } // error, Type '() => void' is not assignable to type '(x: number) => number' + ~~~~~~~~~~~~~~~ +!!! error TS2322: Type '() => void' is not assignable to type '(x: number) => number'. +!!! error TS2322: Type 'void' is not assignable to type 'number'. + C.prototype.bar = (x) => x; // ok + C.prototype.bar = (x: number) => 1; // ok + return 1; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/instanceMemberAssignsToStructPrototype.js b/tests/baselines/reference/instanceMemberAssignsToStructPrototype.js new file mode 100644 index 0000000000000..b2882b9592bf8 --- /dev/null +++ b/tests/baselines/reference/instanceMemberAssignsToStructPrototype.js @@ -0,0 +1,44 @@ +//// [instanceMemberAssignsToStructPrototype.ts] +// doc 4.2 +// An instance member function declaration declares a property in the struct instance type and +// assigns a function object to a property on the prototype object of the struct. + +struct C { + foo() { + C.prototype.foo = () => { } + } + + bar(x: number): number { + C.prototype.bar = () => { } // error, Type '() => void' is not assignable to type '(x: number) => number' + C.prototype.bar = (x) => x; // ok + C.prototype.bar = (x: number) => 1; // ok + return 1; + } +} + +//// [instanceMemberAssignsToStructPrototype.js] +// doc 4.2 +// An instance member function declaration declares a property in the struct instance type and +// assigns a function object to a property on the prototype object of the struct. +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.foo = function () { + C.prototype.foo = function () { }; + }; + _C.prototype.bar = function (x) { + C.prototype.bar = function () { }; // error, Type '() => void' is not assignable to type '(x: number) => number' + C.prototype.bar = function (x) { return x; }; // ok + C.prototype.bar = function (x) { return 1; }; // ok + return 1; + }; + return C; +})(); diff --git a/tests/baselines/reference/instancePropertiesInheritedIntoStructType.js b/tests/baselines/reference/instancePropertiesInheritedIntoStructType.js new file mode 100644 index 0000000000000..b7402d89f26e3 --- /dev/null +++ b/tests/baselines/reference/instancePropertiesInheritedIntoStructType.js @@ -0,0 +1,93 @@ +//// [instancePropertiesInheritedIntoStructType.ts] +// doc 2.4 +// All base struct instance type property that are not overridden in the struct +// ok + +module NonGeneric { + struct C { + x: string; + fn() { return this; } + constructor(public a: number, private b: number) { } + } + + struct D extends C { e: string; } + + var d = new D(1, 2); + var r = d.fn(); + var r2 = r.x; + +} + +/* module Generic { + struct C { + x: T; + fn() { return this; } + constructor(public a: T, private b: U) { } + } + + struct D extends C { e: T; } + + var d = new D(1, ''); + var r = d.fn(); + var r2 = r.x; +} */ + +//// [instancePropertiesInheritedIntoStructType.js] +// doc 2.4 +// All base struct instance type property that are not overridden in the struct +// ok +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var NonGeneric; +(function (NonGeneric) { + var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.string, + }); + function _ctor(a, b) { + this.a = a; + this.b = b; + } + function C(a, b) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + _C.prototype.fn = function () { return this; }; + return C; + })(); + var D = (function () { + var _D = new TypedObject.StructType({ + e: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; + })(); + var d = new D(1, 2); + var r = d.fn(); + var r2 = r.x; +})(NonGeneric || (NonGeneric = {})); +/* module Generic { + struct C { + x: T; + fn() { return this; } + constructor(public a: T, private b: U) { } + } + + struct D extends C { e: T; } + + var d = new D(1, ''); + var r = d.fn(); + var r2 = r.x; +} */ diff --git a/tests/baselines/reference/instancePropertiesInheritedIntoStructType.symbols b/tests/baselines/reference/instancePropertiesInheritedIntoStructType.symbols new file mode 100644 index 0000000000000..84692d2536b61 --- /dev/null +++ b/tests/baselines/reference/instancePropertiesInheritedIntoStructType.symbols @@ -0,0 +1,59 @@ +=== tests/cases/conformance/structs/members/structTypes/instancePropertiesInheritedIntoStructType.ts === +// doc 2.4 +// All base struct instance type property that are not overridden in the struct +// ok + +module NonGeneric { +>NonGeneric : Symbol(NonGeneric, Decl(instancePropertiesInheritedIntoStructType.ts, 0, 0)) + + struct C { +>C : Symbol(C, Decl(instancePropertiesInheritedIntoStructType.ts, 4, 19)) + + x: string; +>x : Symbol(x, Decl(instancePropertiesInheritedIntoStructType.ts, 5, 14)) + + fn() { return this; } +>fn : Symbol(fn, Decl(instancePropertiesInheritedIntoStructType.ts, 6, 18)) +>this : Symbol(C, Decl(instancePropertiesInheritedIntoStructType.ts, 4, 19)) + + constructor(public a: number, private b: number) { } +>a : Symbol(a, Decl(instancePropertiesInheritedIntoStructType.ts, 8, 20)) +>b : Symbol(b, Decl(instancePropertiesInheritedIntoStructType.ts, 8, 37)) + } + + struct D extends C { e: string; } +>D : Symbol(D, Decl(instancePropertiesInheritedIntoStructType.ts, 9, 5)) +>C : Symbol(C, Decl(instancePropertiesInheritedIntoStructType.ts, 4, 19)) +>e : Symbol(e, Decl(instancePropertiesInheritedIntoStructType.ts, 11, 24)) + + var d = new D(1, 2); +>d : Symbol(d, Decl(instancePropertiesInheritedIntoStructType.ts, 13, 7)) +>D : Symbol(D, Decl(instancePropertiesInheritedIntoStructType.ts, 9, 5)) + + var r = d.fn(); +>r : Symbol(r, Decl(instancePropertiesInheritedIntoStructType.ts, 14, 7)) +>d.fn : Symbol(C.fn, Decl(instancePropertiesInheritedIntoStructType.ts, 6, 18)) +>d : Symbol(d, Decl(instancePropertiesInheritedIntoStructType.ts, 13, 7)) +>fn : Symbol(C.fn, Decl(instancePropertiesInheritedIntoStructType.ts, 6, 18)) + + var r2 = r.x; +>r2 : Symbol(r2, Decl(instancePropertiesInheritedIntoStructType.ts, 15, 7)) +>r.x : Symbol(C.x, Decl(instancePropertiesInheritedIntoStructType.ts, 5, 14)) +>r : Symbol(r, Decl(instancePropertiesInheritedIntoStructType.ts, 14, 7)) +>x : Symbol(C.x, Decl(instancePropertiesInheritedIntoStructType.ts, 5, 14)) + +} + +/* module Generic { + struct C { + x: T; + fn() { return this; } + constructor(public a: T, private b: U) { } + } + + struct D extends C { e: T; } + + var d = new D(1, ''); + var r = d.fn(); + var r2 = r.x; +} */ diff --git a/tests/baselines/reference/instancePropertiesInheritedIntoStructType.types b/tests/baselines/reference/instancePropertiesInheritedIntoStructType.types new file mode 100644 index 0000000000000..182d71885182f --- /dev/null +++ b/tests/baselines/reference/instancePropertiesInheritedIntoStructType.types @@ -0,0 +1,63 @@ +=== tests/cases/conformance/structs/members/structTypes/instancePropertiesInheritedIntoStructType.ts === +// doc 2.4 +// All base struct instance type property that are not overridden in the struct +// ok + +module NonGeneric { +>NonGeneric : typeof NonGeneric + + struct C { +>C : C + + x: string; +>x : string + + fn() { return this; } +>fn : () => this +>this : this + + constructor(public a: number, private b: number) { } +>a : number +>b : number + } + + struct D extends C { e: string; } +>D : D +>C : C +>e : string + + var d = new D(1, 2); +>d : D +>new D(1, 2) : D +>D : typeof D +>1 : number +>2 : number + + var r = d.fn(); +>r : D +>d.fn() : D +>d.fn : () => D +>d : D +>fn : () => D + + var r2 = r.x; +>r2 : string +>r.x : string +>r : D +>x : string + +} + +/* module Generic { + struct C { + x: T; + fn() { return this; } + constructor(public a: T, private b: U) { } + } + + struct D extends C { e: T; } + + var d = new D(1, ''); + var r = d.fn(); + var r2 = r.x; +} */ diff --git a/tests/baselines/reference/instancePropertyInStructType.js b/tests/baselines/reference/instancePropertyInStructType.js new file mode 100644 index 0000000000000..25250d21f50a2 --- /dev/null +++ b/tests/baselines/reference/instancePropertyInStructType.js @@ -0,0 +1,69 @@ +//// [instancePropertyInStructType.ts] +// doc 2.4 +// struct type contains all instance member variables and function members. +// ok + +module NonGeneric { + struct C { + x: string; + fn() { return this; } + constructor(public a: number, private b: number) { } + } + + var c = new C(1, 2); + var r = c.fn(); + var r2 = r.x; + c.a; +} + +/* module Generic { + struct C { + x: T; + fn() { return this; } + constructor(public a: T, private b: U) { } + } + + var c = new C(1, ''); + var r = c.fn(); + var r2 = r.x; +} */ + +//// [instancePropertyInStructType.js] +// doc 2.4 +// struct type contains all instance member variables and function members. +// ok +var NonGeneric; +(function (NonGeneric) { + var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.string, + }); + function _ctor(a, b) { + this.a = a; + this.b = b; + } + function C(a, b) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + _C.prototype.fn = function () { return this; }; + return C; + })(); + var c = new C(1, 2); + var r = c.fn(); + var r2 = r.x; + c.a; +})(NonGeneric || (NonGeneric = {})); +/* module Generic { + struct C { + x: T; + fn() { return this; } + constructor(public a: T, private b: U) { } + } + + var c = new C(1, ''); + var r = c.fn(); + var r2 = r.x; +} */ diff --git a/tests/baselines/reference/instancePropertyInStructType.symbols b/tests/baselines/reference/instancePropertyInStructType.symbols new file mode 100644 index 0000000000000..d35c618cecd23 --- /dev/null +++ b/tests/baselines/reference/instancePropertyInStructType.symbols @@ -0,0 +1,56 @@ +=== tests/cases/conformance/structs/members/structTypes/instancePropertyInStructType.ts === +// doc 2.4 +// struct type contains all instance member variables and function members. +// ok + +module NonGeneric { +>NonGeneric : Symbol(NonGeneric, Decl(instancePropertyInStructType.ts, 0, 0)) + + struct C { +>C : Symbol(C, Decl(instancePropertyInStructType.ts, 4, 19)) + + x: string; +>x : Symbol(x, Decl(instancePropertyInStructType.ts, 5, 14)) + + fn() { return this; } +>fn : Symbol(fn, Decl(instancePropertyInStructType.ts, 6, 18)) +>this : Symbol(C, Decl(instancePropertyInStructType.ts, 4, 19)) + + constructor(public a: number, private b: number) { } +>a : Symbol(a, Decl(instancePropertyInStructType.ts, 8, 20)) +>b : Symbol(b, Decl(instancePropertyInStructType.ts, 8, 37)) + } + + var c = new C(1, 2); +>c : Symbol(c, Decl(instancePropertyInStructType.ts, 11, 7)) +>C : Symbol(C, Decl(instancePropertyInStructType.ts, 4, 19)) + + var r = c.fn(); +>r : Symbol(r, Decl(instancePropertyInStructType.ts, 12, 7)) +>c.fn : Symbol(C.fn, Decl(instancePropertyInStructType.ts, 6, 18)) +>c : Symbol(c, Decl(instancePropertyInStructType.ts, 11, 7)) +>fn : Symbol(C.fn, Decl(instancePropertyInStructType.ts, 6, 18)) + + var r2 = r.x; +>r2 : Symbol(r2, Decl(instancePropertyInStructType.ts, 13, 7)) +>r.x : Symbol(C.x, Decl(instancePropertyInStructType.ts, 5, 14)) +>r : Symbol(r, Decl(instancePropertyInStructType.ts, 12, 7)) +>x : Symbol(C.x, Decl(instancePropertyInStructType.ts, 5, 14)) + + c.a; +>c.a : Symbol(C.a, Decl(instancePropertyInStructType.ts, 8, 20)) +>c : Symbol(c, Decl(instancePropertyInStructType.ts, 11, 7)) +>a : Symbol(C.a, Decl(instancePropertyInStructType.ts, 8, 20)) +} + +/* module Generic { + struct C { + x: T; + fn() { return this; } + constructor(public a: T, private b: U) { } + } + + var c = new C(1, ''); + var r = c.fn(); + var r2 = r.x; +} */ diff --git a/tests/baselines/reference/instancePropertyInStructType.types b/tests/baselines/reference/instancePropertyInStructType.types new file mode 100644 index 0000000000000..f90057c925528 --- /dev/null +++ b/tests/baselines/reference/instancePropertyInStructType.types @@ -0,0 +1,60 @@ +=== tests/cases/conformance/structs/members/structTypes/instancePropertyInStructType.ts === +// doc 2.4 +// struct type contains all instance member variables and function members. +// ok + +module NonGeneric { +>NonGeneric : typeof NonGeneric + + struct C { +>C : C + + x: string; +>x : string + + fn() { return this; } +>fn : () => this +>this : this + + constructor(public a: number, private b: number) { } +>a : number +>b : number + } + + var c = new C(1, 2); +>c : C +>new C(1, 2) : C +>C : typeof C +>1 : number +>2 : number + + var r = c.fn(); +>r : C +>c.fn() : C +>c.fn : () => C +>c : C +>fn : () => C + + var r2 = r.x; +>r2 : string +>r.x : string +>r : C +>x : string + + c.a; +>c.a : number +>c : C +>a : number +} + +/* module Generic { + struct C { + x: T; + fn() { return this; } + constructor(public a: T, private b: U) { } + } + + var c = new C(1, ''); + var r = c.fn(); + var r2 = r.x; +} */ diff --git a/tests/baselines/reference/returnType.errors.txt b/tests/baselines/reference/returnType.errors.txt new file mode 100644 index 0000000000000..fac559a249a68 --- /dev/null +++ b/tests/baselines/reference/returnType.errors.txt @@ -0,0 +1,20 @@ +tests/cases/conformance/structs/types/returnType.ts(10,1): error TS2322: Type '(n: string, a: number) => { name: string; age: number; }' is not assignable to type '(name: string, age: number) => Person'. + Type '{ name: string; age: number; }' is not assignable to type 'Person'. + + +==== tests/cases/conformance/structs/types/returnType.ts (1 errors) ==== + // doc 8 + + struct Person { + constructor(public name: string, public age: number) {} + } + + var p: Person; + var test = (name: string, age: number) => new Person(name, age); + p = test("John", 25); + test = function (n, a) {return {name: n, age: a} }; // error, type not compatible + ~~~~ +!!! error TS2322: Type '(n: string, a: number) => { name: string; age: number; }' is not assignable to type '(name: string, age: number) => Person'. +!!! error TS2322: Type '{ name: string; age: number; }' is not assignable to type 'Person'. + + \ No newline at end of file diff --git a/tests/baselines/reference/returnType.js b/tests/baselines/reference/returnType.js new file mode 100644 index 0000000000000..e94d726a2e0a1 --- /dev/null +++ b/tests/baselines/reference/returnType.js @@ -0,0 +1,35 @@ +//// [returnType.ts] +// doc 8 + +struct Person { + constructor(public name: string, public age: number) {} +} + +var p: Person; +var test = (name: string, age: number) => new Person(name, age); +p = test("John", 25); +test = function (n, a) {return {name: n, age: a} }; // error, type not compatible + + + +//// [returnType.js] +// doc 8 +var Person = (function () { + var _Person = new TypedObject.StructType({ + }); + function _ctor(name, age) { + this.name = name; + this.age = age; + } + function Person(name, age) { + var obj = new _Person(); + _ctor.call(obj ,); + return obj; + } + Person._TO = _Person; + return Person; +})(); +var p; +var test = function (name, age) { return new Person(name, age); }; +p = test("John", 25); +test = function (n, a) { return { name: n, age: a }; }; // error, type not compatible diff --git a/tests/baselines/reference/staticMemberAssignsToStructConstructorFunctionMembers.errors.txt b/tests/baselines/reference/staticMemberAssignsToStructConstructorFunctionMembers.errors.txt new file mode 100644 index 0000000000000..d8845b67c353c --- /dev/null +++ b/tests/baselines/reference/staticMemberAssignsToStructConstructorFunctionMembers.errors.txt @@ -0,0 +1,24 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/staticMemberAssignsToStructConstructorFunctionMembers.ts(11,9): error TS2322: Type '() => void' is not assignable to type '(x: number) => number'. + Type 'void' is not assignable to type 'number'. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/staticMemberAssignsToStructConstructorFunctionMembers.ts (1 errors) ==== + // doc 4.2 + // A static member function declaration declares a property in the constructor function type and + // assigns a function object to a property on the constructor function object. + + struct C { + static foo() { + C.foo = () => { } + } + + static bar(x: number): number { + C.bar = () => { } // error, Type '() => void' is not assignable to type '(x: number) => number' + ~~~~~ +!!! error TS2322: Type '() => void' is not assignable to type '(x: number) => number'. +!!! error TS2322: Type 'void' is not assignable to type 'number'. + C.bar = (x) => x; // ok + C.bar = (x: number) => 1; // ok + return 1; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/staticMemberAssignsToStructConstructorFunctionMembers.js b/tests/baselines/reference/staticMemberAssignsToStructConstructorFunctionMembers.js new file mode 100644 index 0000000000000..4a7115516b9d0 --- /dev/null +++ b/tests/baselines/reference/staticMemberAssignsToStructConstructorFunctionMembers.js @@ -0,0 +1,44 @@ +//// [staticMemberAssignsToStructConstructorFunctionMembers.ts] +// doc 4.2 +// A static member function declaration declares a property in the constructor function type and +// assigns a function object to a property on the constructor function object. + +struct C { + static foo() { + C.foo = () => { } + } + + static bar(x: number): number { + C.bar = () => { } // error, Type '() => void' is not assignable to type '(x: number) => number' + C.bar = (x) => x; // ok + C.bar = (x: number) => 1; // ok + return 1; + } +} + +//// [staticMemberAssignsToStructConstructorFunctionMembers.js] +// doc 4.2 +// A static member function declaration declares a property in the constructor function type and +// assigns a function object to a property on the constructor function object. +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.foo = function () { + C.foo = function () { }; + }; + _C.bar = function (x) { + C.bar = function () { }; // error, Type '() => void' is not assignable to type '(x: number) => number' + C.bar = function (x) { return x; }; // ok + C.bar = function (x) { return 1; }; // ok + return 1; + }; + return C; +})(); diff --git a/tests/baselines/reference/staticPropertyNotInStructType.errors.txt b/tests/baselines/reference/staticPropertyNotInStructType.errors.txt new file mode 100644 index 0000000000000..c7bc80eb2adad --- /dev/null +++ b/tests/baselines/reference/staticPropertyNotInStructType.errors.txt @@ -0,0 +1,56 @@ +tests/cases/conformance/structs/members/structTypes/staticPropertyNotInStructType.ts(17,16): error TS2339: Property 'foo' does not exist on type 'C'. +tests/cases/conformance/structs/members/structTypes/staticPropertyNotInStructType.ts(18,16): error TS2339: Property 'bar' does not exist on type 'C'. +tests/cases/conformance/structs/members/structTypes/staticPropertyNotInStructType.ts(21,2): error TS2322: Type 'any' is not assignable to type 'C'. +tests/cases/conformance/structs/members/structTypes/staticPropertyNotInStructType.ts(21,9): error TS2339: Property 'fn' does not exist on type 'typeof C'. + + +==== tests/cases/conformance/structs/members/structTypes/staticPropertyNotInStructType.ts (4 errors) ==== + // doc 2.4 + // Static members are declared using the static modifier and are members of the constructor function type. + + module NonGeneric { + struct C { + fn() { return this; } + constructor(public a: number, private b: number) { } + static foo: string; // not reflected in struct type + } + + module C { + export var bar = ''; // not reflected in struct type + } + + var c = new C(1, 2); + var r = c.fn(); + var r4 = c.foo; // error + ~~~ +!!! error TS2339: Property 'foo' does not exist on type 'C'. + var r5 = c.bar; // error + ~~~ +!!! error TS2339: Property 'bar' does not exist on type 'C'. + + var cs: typeof C; + r = cs.fn(); // error + ~ +!!! error TS2322: Type 'any' is not assignable to type 'C'. + ~~ +!!! error TS2339: Property 'fn' does not exist on type 'typeof C'. + r4 = cs.foo; // ok + r5 = cs.bar; // ok + } + + /* module Generic { + struct C { + fn() { return this; } + constructor(public a: T, private b: U) { } + static foo: T; // not reflected in struct type + } + + module C { + export var bar = ''; // not reflected in struct type + } + + var c = new C(1, ''); + var r = c.fn(); + var r4 = c.foo; // error + var r5 = c.bar; // error + } */ \ No newline at end of file diff --git a/tests/baselines/reference/staticPropertyNotInStructType.js b/tests/baselines/reference/staticPropertyNotInStructType.js new file mode 100644 index 0000000000000..30cd6d8815b92 --- /dev/null +++ b/tests/baselines/reference/staticPropertyNotInStructType.js @@ -0,0 +1,93 @@ +//// [staticPropertyNotInStructType.ts] +// doc 2.4 +// Static members are declared using the static modifier and are members of the constructor function type. + +module NonGeneric { + struct C { + fn() { return this; } + constructor(public a: number, private b: number) { } + static foo: string; // not reflected in struct type + } + + module C { + export var bar = ''; // not reflected in struct type + } + + var c = new C(1, 2); + var r = c.fn(); + var r4 = c.foo; // error + var r5 = c.bar; // error + + var cs: typeof C; + r = cs.fn(); // error + r4 = cs.foo; // ok + r5 = cs.bar; // ok +} + +/* module Generic { + struct C { + fn() { return this; } + constructor(public a: T, private b: U) { } + static foo: T; // not reflected in struct type + } + + module C { + export var bar = ''; // not reflected in struct type + } + + var c = new C(1, ''); + var r = c.fn(); + var r4 = c.foo; // error + var r5 = c.bar; // error +} */ + +//// [staticPropertyNotInStructType.js] +// doc 2.4 +// Static members are declared using the static modifier and are members of the constructor function type. +var NonGeneric; +(function (NonGeneric) { + var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor(a, b) { + this.a = a; + this.b = b; + } + function C(a, b) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + _C.prototype.fn = function () { return this; }; + return C; + })(); + var C; + (function (C) { + C.bar = ''; // not reflected in struct type + })(C || (C = {})); + var c = new C(1, 2); + var r = c.fn(); + var r4 = c.foo; // error + var r5 = c.bar; // error + var cs; + r = cs.fn(); // error + r4 = cs.foo; // ok + r5 = cs.bar; // ok +})(NonGeneric || (NonGeneric = {})); +/* module Generic { + struct C { + fn() { return this; } + constructor(public a: T, private b: U) { } + static foo: T; // not reflected in struct type + } + + module C { + export var bar = ''; // not reflected in struct type + } + + var c = new C(1, ''); + var r = c.fn(); + var r4 = c.foo; // error + var r5 = c.bar; // error +} */ diff --git a/tests/baselines/reference/structAccessibilityModifiers.errors.txt b/tests/baselines/reference/structAccessibilityModifiers.errors.txt new file mode 100644 index 0000000000000..1ab5fb650bdb7 --- /dev/null +++ b/tests/baselines/reference/structAccessibilityModifiers.errors.txt @@ -0,0 +1,47 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts(14,12): error TS1029: 'private' modifier must precede 'static' modifier. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts(15,12): error TS1029: 'private' modifier must precede 'static' modifier. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts(17,12): error TS1029: 'public' modifier must precede 'static' modifier. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts(18,12): error TS1029: 'public' modifier must precede 'static' modifier. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts(23,13): error TS1028: Accessibility modifier already seen. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts(24,12): error TS1028: Accessibility modifier already seen. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structAccessibilityModifiers.ts (6 errors) ==== + // doc 4 + + // No errors + struct C { + private static privateProperty; + private static privateMethod() { } + + public static publicProperty; + public static publicMethod() { } + } + + // Errors, accessibility modifiers must precede static + struct D { + static private privateProperty; + ~~~~~~~ +!!! error TS1029: 'private' modifier must precede 'static' modifier. + static private privateMethod() { } + ~~~~~~~ +!!! error TS1029: 'private' modifier must precede 'static' modifier. + + static public publicProperty; + ~~~~~~ +!!! error TS1029: 'public' modifier must precede 'static' modifier. + static public publicMethod() { } + ~~~~~~ +!!! error TS1029: 'public' modifier must precede 'static' modifier. + } + + // Errors, multiple accessibility modifier + struct E { + private public property; + ~~~~~~ +!!! error TS1028: Accessibility modifier already seen. + public private method() { } + ~~~~~~~ +!!! error TS1028: Accessibility modifier already seen. + } + \ No newline at end of file diff --git a/tests/baselines/reference/structAccessibilityModifiers.js b/tests/baselines/reference/structAccessibilityModifiers.js new file mode 100644 index 0000000000000..e7f66849b6142 --- /dev/null +++ b/tests/baselines/reference/structAccessibilityModifiers.js @@ -0,0 +1,78 @@ +//// [structAccessibilityModifiers.ts] +// doc 4 + +// No errors +struct C { + private static privateProperty; + private static privateMethod() { } + + public static publicProperty; + public static publicMethod() { } +} + +// Errors, accessibility modifiers must precede static +struct D { + static private privateProperty; + static private privateMethod() { } + + static public publicProperty; + static public publicMethod() { } +} + +// Errors, multiple accessibility modifier +struct E { + private public property; + public private method() { } +} + + +//// [structAccessibilityModifiers.js] +// doc 4 +// No errors +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.privateMethod = function () { }; + _C.publicMethod = function () { }; + return C; +})(); +// Errors, accessibility modifiers must precede static +var D = (function () { + var _D = new TypedObject.StructType({ + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + _D.privateMethod = function () { }; + _D.publicMethod = function () { }; + return D; +})(); +// Errors, multiple accessibility modifier +var E = (function () { + var _E = new TypedObject.StructType({ + property: TypedObject.Any, + }); + function _ctor() { + } + function E() { + var obj = new _E(); + _ctor.call(obj); + return obj; + } + E._TO = _E; + _E.prototype.method = function () { }; + return E; +})(); diff --git a/tests/baselines/reference/structConstructorAccessibility.errors.txt b/tests/baselines/reference/structConstructorAccessibility.errors.txt new file mode 100644 index 0000000000000..994a3aca2c2b1 --- /dev/null +++ b/tests/baselines/reference/structConstructorAccessibility.errors.txt @@ -0,0 +1,46 @@ +tests/cases/conformance/structs/constructorDeclarations/structConstructorAccessibility.ts(9,5): error TS1089: 'private' modifier cannot appear on a constructor declaration. +tests/cases/conformance/structs/constructorDeclarations/structConstructorAccessibility.ts(13,5): error TS20011: 'protected' modifier cannot appear on a struct element. + + +==== tests/cases/conformance/structs/constructorDeclarations/structConstructorAccessibility.ts (2 errors) ==== + // doc 3 + // Only public constructors are supported. Private and protected constructors result in an error + + struct C { + public constructor(public x: number) { } + } + + struct D { + private constructor(public x: number) { } // error + ~~~~~~~ +!!! error TS1089: 'private' modifier cannot appear on a constructor declaration. + } + + struct E { + protected constructor(public x: number) { } // error + ~~~~~~~~~ +!!! error TS20011: 'protected' modifier cannot appear on a struct element. + } + + var c = new C(1); + var d = new D(1); + var e = new E(1); + + /* module Generic { + struct C { + public constructor(public x: T) { } + } + + struct D { + private constructor(public x: T) { } // error + } + + struct E { + protected constructor(public x: T) { } // error + } + + var c = new C(1); + var d = new D(1); + var e = new E(1); + } */ + \ No newline at end of file diff --git a/tests/baselines/reference/structConstructorAccessibility.js b/tests/baselines/reference/structConstructorAccessibility.js new file mode 100644 index 0000000000000..640bf301b2556 --- /dev/null +++ b/tests/baselines/reference/structConstructorAccessibility.js @@ -0,0 +1,104 @@ +//// [structConstructorAccessibility.ts] +// doc 3 +// Only public constructors are supported. Private and protected constructors result in an error + +struct C { + public constructor(public x: number) { } +} + +struct D { + private constructor(public x: number) { } // error +} + +struct E { + protected constructor(public x: number) { } // error +} + +var c = new C(1); +var d = new D(1); +var e = new E(1); + +/* module Generic { + struct C { + public constructor(public x: T) { } + } + + struct D { + private constructor(public x: T) { } // error + } + + struct E { + protected constructor(public x: T) { } // error + } + + var c = new C(1); + var d = new D(1); + var e = new E(1); +} */ + + +//// [structConstructorAccessibility.js] +// doc 3 +// Only public constructors are supported. Private and protected constructors result in an error +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor(x) { + this.x = x; + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + }); + function _ctor(x) { + this.x = x; + } // error + function D(x) { + var obj = new _D(); + _ctor.call(obj ,); + return obj; + } + D._TO = _D; + return D; +})(); +var E = (function () { + var _E = new TypedObject.StructType({ + }); + function _ctor(x) { + this.x = x; + } // error + function E(x) { + var obj = new _E(); + _ctor.call(obj ,); + return obj; + } + E._TO = _E; + return E; +})(); +var c = new C(1); +var d = new D(1); +var e = new E(1); +/* module Generic { + struct C { + public constructor(public x: T) { } + } + + struct D { + private constructor(public x: T) { } // error + } + + struct E { + protected constructor(public x: T) { } // error + } + + var c = new C(1); + var d = new D(1); + var e = new E(1); +} */ diff --git a/tests/baselines/reference/structConstructorDefaultValuesReferencingThis.errors.txt b/tests/baselines/reference/structConstructorDefaultValuesReferencingThis.errors.txt new file mode 100644 index 0000000000000..5c3d11b585cf1 --- /dev/null +++ b/tests/baselines/reference/structConstructorDefaultValuesReferencingThis.errors.txt @@ -0,0 +1,20 @@ +tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorDefaultValuesReferencingThis.ts(5,21): error TS2333: 'this' cannot be referenced in constructor arguments. + + +==== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorDefaultValuesReferencingThis.ts (1 errors) ==== + // doc 3.1 + // 'this' cannot be referenced in constructor arguments + + struct C { + constructor(x = this) { } // error + ~~~~ +!!! error TS2333: 'this' cannot be referenced in constructor arguments. + } + + /* struct D { + constructor(x = this) { } // error + } + + struct E { + constructor(public x = this) { } // error + }*/ \ No newline at end of file diff --git a/tests/baselines/reference/structConstructorDefaultValuesReferencingThis.js b/tests/baselines/reference/structConstructorDefaultValuesReferencingThis.js new file mode 100644 index 0000000000000..8004340d1a701 --- /dev/null +++ b/tests/baselines/reference/structConstructorDefaultValuesReferencingThis.js @@ -0,0 +1,40 @@ +//// [structConstructorDefaultValuesReferencingThis.ts] +// doc 3.1 +// 'this' cannot be referenced in constructor arguments + +struct C { + constructor(x = this) { } // error +} + +/* struct D { + constructor(x = this) { } // error +} + +struct E { + constructor(public x = this) { } // error +}*/ + +//// [structConstructorDefaultValuesReferencingThis.js] +// doc 3.1 +// 'this' cannot be referenced in constructor arguments +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor(x) { + if (x === void 0) { x = this; } + } // error + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; +})(); +/* struct D { + constructor(x = this) { } // error +} + +struct E { + constructor(public x = this) { } // error +}*/ diff --git a/tests/baselines/reference/structConstructorHasPrototypeProperty.js b/tests/baselines/reference/structConstructorHasPrototypeProperty.js new file mode 100644 index 0000000000000..eeb0feb7148bb --- /dev/null +++ b/tests/baselines/reference/structConstructorHasPrototypeProperty.js @@ -0,0 +1,99 @@ +//// [structConstructorHasPrototypeProperty.ts] +// doc 2.5 +// Every struct automatically contains a static property member named ‘prototype’, +// the type of which is the containing struct with type Any substituted for each type parameter. +// ok + +module NonGeneric { + struct C { + foo: string; + } + + struct D extends C { + bar: string; + } + + var r = C.prototype; + r.foo; // any + var r2 = D.prototype; + r2.bar; // any +} + +/* module Generic { + struct C { + foo: T; + bar: U; + } + + struct D extends C { + baz: T; + bing: U; + } + + var r = C.prototype; // C + var ra = r.foo; // any + var r2 = D.prototype; // D + var rb = r2.baz; // any +} */ + +//// [structConstructorHasPrototypeProperty.js] +// doc 2.5 +// Every struct automatically contains a static property member named ‘prototype’, +// the type of which is the containing struct with type Any substituted for each type parameter. +// ok +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var NonGeneric; +(function (NonGeneric) { + var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; + })(); + var D = (function () { + var _D = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; + })(); + var r = C.prototype; + r.foo; // any + var r2 = D.prototype; + r2.bar; // any +})(NonGeneric || (NonGeneric = {})); +/* module Generic { + struct C { + foo: T; + bar: U; + } + + struct D extends C { + baz: T; + bing: U; + } + + var r = C.prototype; // C + var ra = r.foo; // any + var r2 = D.prototype; // D + var rb = r2.baz; // any +} */ diff --git a/tests/baselines/reference/structConstructorHasPrototypeProperty.symbols b/tests/baselines/reference/structConstructorHasPrototypeProperty.symbols new file mode 100644 index 0000000000000..29e12d2e2809c --- /dev/null +++ b/tests/baselines/reference/structConstructorHasPrototypeProperty.symbols @@ -0,0 +1,63 @@ +=== tests/cases/conformance/structs/members/constructorFunctionTypes/structConstructorHasPrototypeProperty.ts === +// doc 2.5 +// Every struct automatically contains a static property member named ‘prototype’, +// the type of which is the containing struct with type Any substituted for each type parameter. +// ok + +module NonGeneric { +>NonGeneric : Symbol(NonGeneric, Decl(structConstructorHasPrototypeProperty.ts, 0, 0)) + + struct C { +>C : Symbol(C, Decl(structConstructorHasPrototypeProperty.ts, 5, 19)) + + foo: string; +>foo : Symbol(foo, Decl(structConstructorHasPrototypeProperty.ts, 6, 14)) + } + + struct D extends C { +>D : Symbol(D, Decl(structConstructorHasPrototypeProperty.ts, 8, 5)) +>C : Symbol(C, Decl(structConstructorHasPrototypeProperty.ts, 5, 19)) + + bar: string; +>bar : Symbol(bar, Decl(structConstructorHasPrototypeProperty.ts, 10, 24)) + } + + var r = C.prototype; +>r : Symbol(r, Decl(structConstructorHasPrototypeProperty.ts, 14, 7)) +>C.prototype : Symbol(C.prototype) +>C : Symbol(C, Decl(structConstructorHasPrototypeProperty.ts, 5, 19)) +>prototype : Symbol(C.prototype) + + r.foo; // any +>r.foo : Symbol(C.foo, Decl(structConstructorHasPrototypeProperty.ts, 6, 14)) +>r : Symbol(r, Decl(structConstructorHasPrototypeProperty.ts, 14, 7)) +>foo : Symbol(C.foo, Decl(structConstructorHasPrototypeProperty.ts, 6, 14)) + + var r2 = D.prototype; +>r2 : Symbol(r2, Decl(structConstructorHasPrototypeProperty.ts, 16, 7)) +>D.prototype : Symbol(D.prototype) +>D : Symbol(D, Decl(structConstructorHasPrototypeProperty.ts, 8, 5)) +>prototype : Symbol(D.prototype) + + r2.bar; // any +>r2.bar : Symbol(D.bar, Decl(structConstructorHasPrototypeProperty.ts, 10, 24)) +>r2 : Symbol(r2, Decl(structConstructorHasPrototypeProperty.ts, 16, 7)) +>bar : Symbol(D.bar, Decl(structConstructorHasPrototypeProperty.ts, 10, 24)) +} + +/* module Generic { + struct C { + foo: T; + bar: U; + } + + struct D extends C { + baz: T; + bing: U; + } + + var r = C.prototype; // C + var ra = r.foo; // any + var r2 = D.prototype; // D + var rb = r2.baz; // any +} */ diff --git a/tests/baselines/reference/structConstructorHasPrototypeProperty.types b/tests/baselines/reference/structConstructorHasPrototypeProperty.types new file mode 100644 index 0000000000000..bcc4db58ab210 --- /dev/null +++ b/tests/baselines/reference/structConstructorHasPrototypeProperty.types @@ -0,0 +1,63 @@ +=== tests/cases/conformance/structs/members/constructorFunctionTypes/structConstructorHasPrototypeProperty.ts === +// doc 2.5 +// Every struct automatically contains a static property member named ‘prototype’, +// the type of which is the containing struct with type Any substituted for each type parameter. +// ok + +module NonGeneric { +>NonGeneric : typeof NonGeneric + + struct C { +>C : C + + foo: string; +>foo : string + } + + struct D extends C { +>D : D +>C : C + + bar: string; +>bar : string + } + + var r = C.prototype; +>r : C +>C.prototype : C +>C : typeof C +>prototype : C + + r.foo; // any +>r.foo : string +>r : C +>foo : string + + var r2 = D.prototype; +>r2 : D +>D.prototype : D +>D : typeof D +>prototype : D + + r2.bar; // any +>r2.bar : string +>r2 : D +>bar : string +} + +/* module Generic { + struct C { + foo: T; + bar: U; + } + + struct D extends C { + baz: T; + bing: U; + } + + var r = C.prototype; // C + var ra = r.foo; // any + var r2 = D.prototype; // D + var rb = r2.baz; // any +} */ diff --git a/tests/baselines/reference/structConstructorImplementationWithDefaultValues.js b/tests/baselines/reference/structConstructorImplementationWithDefaultValues.js new file mode 100644 index 0000000000000..9bf9074cbed95 --- /dev/null +++ b/tests/baselines/reference/structConstructorImplementationWithDefaultValues.js @@ -0,0 +1,54 @@ +//// [structConstructorImplementationWithDefaultValues.ts] +// doc3.1 + +struct C { + constructor(x); + constructor(x = 1) { + var y = x; + } +} + +/* struct D { + constructor(x); + constructor(x:T = null) { + var y = x; + } +} + +struct E { + constructor(x); + constructor(x: T = null) { + var y = x; + } +} */ + +//// [structConstructorImplementationWithDefaultValues.js] +// doc3.1 +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor(x) { + if (x === void 0) { x = 1; } + var y = x; + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; +})(); +/* struct D { + constructor(x); + constructor(x:T = null) { + var y = x; + } +} + +struct E { + constructor(x); + constructor(x: T = null) { + var y = x; + } +} */ diff --git a/tests/baselines/reference/structConstructorImplementationWithDefaultValues.symbols b/tests/baselines/reference/structConstructorImplementationWithDefaultValues.symbols new file mode 100644 index 0000000000000..0a863ad2b749a --- /dev/null +++ b/tests/baselines/reference/structConstructorImplementationWithDefaultValues.symbols @@ -0,0 +1,31 @@ +=== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues.ts === +// doc3.1 + +struct C { +>C : Symbol(C, Decl(structConstructorImplementationWithDefaultValues.ts, 0, 0)) + + constructor(x); +>x : Symbol(x, Decl(structConstructorImplementationWithDefaultValues.ts, 3, 16)) + + constructor(x = 1) { +>x : Symbol(x, Decl(structConstructorImplementationWithDefaultValues.ts, 4, 16)) + + var y = x; +>y : Symbol(y, Decl(structConstructorImplementationWithDefaultValues.ts, 5, 11)) +>x : Symbol(x, Decl(structConstructorImplementationWithDefaultValues.ts, 4, 16)) + } +} + +/* struct D { + constructor(x); + constructor(x:T = null) { + var y = x; + } +} + +struct E { + constructor(x); + constructor(x: T = null) { + var y = x; + } +} */ diff --git a/tests/baselines/reference/structConstructorImplementationWithDefaultValues.types b/tests/baselines/reference/structConstructorImplementationWithDefaultValues.types new file mode 100644 index 0000000000000..359ef4cd97fad --- /dev/null +++ b/tests/baselines/reference/structConstructorImplementationWithDefaultValues.types @@ -0,0 +1,32 @@ +=== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues.ts === +// doc3.1 + +struct C { +>C : C + + constructor(x); +>x : any + + constructor(x = 1) { +>x : number +>1 : number + + var y = x; +>y : number +>x : number + } +} + +/* struct D { + constructor(x); + constructor(x:T = null) { + var y = x; + } +} + +struct E { + constructor(x); + constructor(x: T = null) { + var y = x; + } +} */ diff --git a/tests/baselines/reference/structConstructorImplementationWithDefaultValues2.errors.txt b/tests/baselines/reference/structConstructorImplementationWithDefaultValues2.errors.txt new file mode 100644 index 0000000000000..c4ea474261079 --- /dev/null +++ b/tests/baselines/reference/structConstructorImplementationWithDefaultValues2.errors.txt @@ -0,0 +1,29 @@ +tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues2.ts(5,17): error TS2322: Type 'number' is not assignable to type 'string'. + + +==== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorImplementationWithDefaultValues2.ts (1 errors) ==== + // doc 3.1 + + struct C { + constructor(x); + constructor(public x: string = 1) { // error, Type 'number' is not assignable to type 'string' + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type 'number' is not assignable to type 'string'. + var y = x; + } + } + + /* struct D { + constructor(x: T, y: U); + constructor(x: T = 1, public y: U = x) { // error, Type 'number' is not assignable to type 'T'. + // Type 'T' is not assignable to type 'U' + var z = x; + } + } + + struct E { + constructor(x); + constructor(x: T = new Date()) { // error, Type 'Date' is not assignable to type 'T' + var y = x; + } + } */ \ No newline at end of file diff --git a/tests/baselines/reference/structConstructorImplementationWithDefaultValues2.js b/tests/baselines/reference/structConstructorImplementationWithDefaultValues2.js new file mode 100644 index 0000000000000..cf6c9c69ef7dd --- /dev/null +++ b/tests/baselines/reference/structConstructorImplementationWithDefaultValues2.js @@ -0,0 +1,57 @@ +//// [structConstructorImplementationWithDefaultValues2.ts] +// doc 3.1 + +struct C { + constructor(x); + constructor(public x: string = 1) { // error, Type 'number' is not assignable to type 'string' + var y = x; + } +} + +/* struct D { + constructor(x: T, y: U); + constructor(x: T = 1, public y: U = x) { // error, Type 'number' is not assignable to type 'T'. + // Type 'T' is not assignable to type 'U' + var z = x; + } +} + +struct E { + constructor(x); + constructor(x: T = new Date()) { // error, Type 'Date' is not assignable to type 'T' + var y = x; + } +} */ + +//// [structConstructorImplementationWithDefaultValues2.js] +// doc 3.1 +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor(x) { + if (x === void 0) { x = 1; } + this.x = x; + var y = x; + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; +})(); +/* struct D { + constructor(x: T, y: U); + constructor(x: T = 1, public y: U = x) { // error, Type 'number' is not assignable to type 'T'. + // Type 'T' is not assignable to type 'U' + var z = x; + } +} + +struct E { + constructor(x); + constructor(x: T = new Date()) { // error, Type 'Date' is not assignable to type 'T' + var y = x; + } +} */ diff --git a/tests/baselines/reference/structConstructorOverloadsWithDefaultValues.errors.txt b/tests/baselines/reference/structConstructorOverloadsWithDefaultValues.errors.txt new file mode 100644 index 0000000000000..66e21b02f5448 --- /dev/null +++ b/tests/baselines/reference/structConstructorOverloadsWithDefaultValues.errors.txt @@ -0,0 +1,29 @@ +tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorOverloadsWithDefaultValues.ts(6,17): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + + +==== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorOverloadsWithDefaultValues.ts (1 errors) ==== + // doc 3.1 + // A parameter initializer is only allowed in a function or constructor implementation + + struct C { + foo: string; + constructor(x = 1); // error + ~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + constructor() { + } + } + + struct D { + foo: string; + constructor(); + constructor(x = 1) { // ok + } + } + + /* struct D { + foo: string; + constructor(x = 1); // error + constructor() { + } + } */ \ No newline at end of file diff --git a/tests/baselines/reference/structConstructorOverloadsWithDefaultValues.js b/tests/baselines/reference/structConstructorOverloadsWithDefaultValues.js new file mode 100644 index 0000000000000..55c4656dbbd41 --- /dev/null +++ b/tests/baselines/reference/structConstructorOverloadsWithDefaultValues.js @@ -0,0 +1,63 @@ +//// [structConstructorOverloadsWithDefaultValues.ts] +// doc 3.1 +// A parameter initializer is only allowed in a function or constructor implementation + +struct C { + foo: string; + constructor(x = 1); // error + constructor() { + } +} + +struct D { + foo: string; + constructor(); + constructor(x = 1) { // ok + } +} + +/* struct D { + foo: string; + constructor(x = 1); // error + constructor() { + } +} */ + +//// [structConstructorOverloadsWithDefaultValues.js] +// doc 3.1 +// A parameter initializer is only allowed in a function or constructor implementation +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + foo: TypedObject.string, + }); + function _ctor(x) { + if (x === void 0) { x = 1; } + } + function D(x) { + var obj = new _D(); + _ctor.call(obj ,); + return obj; + } + D._TO = _D; + return D; +})(); +/* struct D { + foo: string; + constructor(x = 1); // error + constructor() { + } +} */ diff --git a/tests/baselines/reference/structConstructorParameterNotOptional.js b/tests/baselines/reference/structConstructorParameterNotOptional.js new file mode 100644 index 0000000000000..6e7358d8b437a --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterNotOptional.js @@ -0,0 +1,62 @@ +//// [structConstructorParameterNotOptional.ts] +// doc 3.1 +// optional parameters are not allowed for struct constructor. + +struct C1 { + constructor(public x?: number) { } // error +} + +struct C2 { + constructor(private p?: number) { } // error +} + +struct C3 { + constructor(protected p?: number) { } // error +} + + +//// [structConstructorParameterNotOptional.js] +// doc 3.1 +// optional parameters are not allowed for struct constructor. +var C1 = (function () { + var _C1 = new TypedObject.StructType({ + }); + function _ctor(x) { + this.x = x; + } // error + function C1(x) { + var obj = new _C1(); + _ctor.call(obj ,); + return obj; + } + C1._TO = _C1; + return C1; +})(); +var C2 = (function () { + var _C2 = new TypedObject.StructType({ + }); + function _ctor(p) { + this.p = p; + } // error + function C2(p) { + var obj = new _C2(); + _ctor.call(obj ,); + return obj; + } + C2._TO = _C2; + return C2; +})(); +var C3 = (function () { + var _C3 = new TypedObject.StructType({ + }); + function _ctor(p) { + this.p = p; + } // error + function C3(p) { + var obj = new _C3(); + _ctor.call(obj ,); + return obj; + } + C3._TO = _C3; + return C3; +})(); diff --git a/tests/baselines/reference/structConstructorParameterNotOptional.symbols b/tests/baselines/reference/structConstructorParameterNotOptional.symbols new file mode 100644 index 0000000000000..f740ddfcd1b61 --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterNotOptional.symbols @@ -0,0 +1,25 @@ +=== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterNotOptional.ts === +// doc 3.1 +// optional parameters are not allowed for struct constructor. + +struct C1 { +>C1 : Symbol(C1, Decl(structConstructorParameterNotOptional.ts, 0, 0)) + + constructor(public x?: number) { } // error +>x : Symbol(x, Decl(structConstructorParameterNotOptional.ts, 4, 16)) +} + +struct C2 { +>C2 : Symbol(C2, Decl(structConstructorParameterNotOptional.ts, 5, 1)) + + constructor(private p?: number) { } // error +>p : Symbol(p, Decl(structConstructorParameterNotOptional.ts, 8, 16)) +} + +struct C3 { +>C3 : Symbol(C3, Decl(structConstructorParameterNotOptional.ts, 9, 1)) + + constructor(protected p?: number) { } // error +>p : Symbol(p, Decl(structConstructorParameterNotOptional.ts, 12, 16)) +} + diff --git a/tests/baselines/reference/structConstructorParameterNotOptional.types b/tests/baselines/reference/structConstructorParameterNotOptional.types new file mode 100644 index 0000000000000..5f4dae077cc02 --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterNotOptional.types @@ -0,0 +1,25 @@ +=== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterNotOptional.ts === +// doc 3.1 +// optional parameters are not allowed for struct constructor. + +struct C1 { +>C1 : C1 + + constructor(public x?: number) { } // error +>x : number +} + +struct C2 { +>C2 : C2 + + constructor(private p?: number) { } // error +>p : number +} + +struct C3 { +>C3 : C3 + + constructor(protected p?: number) { } // error +>p : number +} + diff --git a/tests/baselines/reference/structConstructorParameterProperties.errors.txt b/tests/baselines/reference/structConstructorParameterProperties.errors.txt new file mode 100644 index 0000000000000..688d09e66dab8 --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterProperties.errors.txt @@ -0,0 +1,31 @@ +tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties.ts(11,10): error TS20018: Property 'x' is private and only accessible within struct 'C'. + + +==== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties.ts (1 errors) ==== + // doc 3.1 + // modifiers in parameter property declaration define accessibility for parameters + + struct C { + y: string; + constructor(private x: string) { } + } + + var c: C; + var r = c.y; + var r2 = c.x; // error, Property 'x' is private and only accessible within struct 'C' + ~~~ +!!! error TS20018: Property 'x' is private and only accessible within struct 'C'. + + + /* struct D { + y: T; + constructor(a: T, private x: T, protected z: T) { } + } + + var d: D; + var r = d.y; + var r2 = d.x; // error + var r3 = d.a; // error, Property 'a' does not exist on type 'D' + var r4 = d.z; // error + */ + \ No newline at end of file diff --git a/tests/baselines/reference/structConstructorParameterProperties.js b/tests/baselines/reference/structConstructorParameterProperties.js new file mode 100644 index 0000000000000..be4f8e5b848f0 --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterProperties.js @@ -0,0 +1,59 @@ +//// [structConstructorParameterProperties.ts] +// doc 3.1 +// modifiers in parameter property declaration define accessibility for parameters + +struct C { + y: string; + constructor(private x: string) { } +} + +var c: C; +var r = c.y; +var r2 = c.x; // error, Property 'x' is private and only accessible within struct 'C' + + +/* struct D { + y: T; + constructor(a: T, private x: T, protected z: T) { } +} + +var d: D; +var r = d.y; +var r2 = d.x; // error +var r3 = d.a; // error, Property 'a' does not exist on type 'D' +var r4 = d.z; // error +*/ + + +//// [structConstructorParameterProperties.js] +// doc 3.1 +// modifiers in parameter property declaration define accessibility for parameters +var C = (function () { + var _C = new TypedObject.StructType({ + y: TypedObject.string, + }); + function _ctor(x) { + this.x = x; + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; +})(); +var c; +var r = c.y; +var r2 = c.x; // error, Property 'x' is private and only accessible within struct 'C' +/* struct D { + y: T; + constructor(a: T, private x: T, protected z: T) { } +} + +var d: D; +var r = d.y; +var r2 = d.x; // error +var r3 = d.a; // error, Property 'a' does not exist on type 'D' +var r4 = d.z; // error +*/ diff --git a/tests/baselines/reference/structConstructorParameterProperties2.errors.txt b/tests/baselines/reference/structConstructorParameterProperties2.errors.txt new file mode 100644 index 0000000000000..84567f30e15fd --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterProperties2.errors.txt @@ -0,0 +1,42 @@ +tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties2.ts(12,5): error TS2300: Duplicate identifier 'y'. +tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties2.ts(13,24): error TS2300: Duplicate identifier 'y'. +tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties2.ts(20,5): error TS2300: Duplicate identifier 'y'. +tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties2.ts(21,25): error TS2300: Duplicate identifier 'y'. + + +==== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterProperties2.ts (4 errors) ==== + // doc 3.1 + + struct C { + y: number; + constructor(y: number) { } // ok + } + + var c: C; + var r = c.y; + + struct D { + y: number; + ~ +!!! error TS2300: Duplicate identifier 'y'. + constructor(public y: number) { } // error, Duplicate identifier 'y' + ~ +!!! error TS2300: Duplicate identifier 'y'. + } + + var d: D; + var r2 = d.y; + + struct E { + y: number; + ~ +!!! error TS2300: Duplicate identifier 'y'. + constructor(private y: number) { } // error, Duplicate identifier 'y' + ~ +!!! error TS2300: Duplicate identifier 'y'. + } + + var e: E; + var r3 = e.y; // error + + \ No newline at end of file diff --git a/tests/baselines/reference/structConstructorParameterProperties2.js b/tests/baselines/reference/structConstructorParameterProperties2.js new file mode 100644 index 0000000000000..1a5e03c874ed4 --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterProperties2.js @@ -0,0 +1,81 @@ +//// [structConstructorParameterProperties2.ts] +// doc 3.1 + +struct C { + y: number; + constructor(y: number) { } // ok +} + +var c: C; +var r = c.y; + +struct D { + y: number; + constructor(public y: number) { } // error, Duplicate identifier 'y' +} + +var d: D; +var r2 = d.y; + +struct E { + y: number; + constructor(private y: number) { } // error, Duplicate identifier 'y' +} + +var e: E; +var r3 = e.y; // error + + + +//// [structConstructorParameterProperties2.js] +// doc 3.1 +var C = (function () { + var _C = new TypedObject.StructType({ + y: TypedObject.float64, + }); + function _ctor(y) { + } // ok + function C(y) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; +})(); +var c; +var r = c.y; +var D = (function () { + var _D = new TypedObject.StructType({ + y: TypedObject.float64, + }); + function _ctor(y) { + this.y = y; + } // error, Duplicate identifier 'y' + function D(y) { + var obj = new _D(); + _ctor.call(obj ,); + return obj; + } + D._TO = _D; + return D; +})(); +var d; +var r2 = d.y; +var E = (function () { + var _E = new TypedObject.StructType({ + y: TypedObject.float64, + }); + function _ctor(y) { + this.y = y; + } // error, Duplicate identifier 'y' + function E(y) { + var obj = new _E(); + _ctor.call(obj ,); + return obj; + } + E._TO = _E; + return E; +})(); +var e; +var r3 = e.y; // error diff --git a/tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.js b/tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.js new file mode 100644 index 0000000000000..171519791b981 --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.js @@ -0,0 +1,83 @@ +//// [structConstructorParameterPropertyDeclarationCannotOverride.ts] +// doc 3.1 +// derived struct cannot override inherited member variables from base struct + +struct Base { + constructor(public x: number) { } +} + +struct Derived extends Base { + constructor(public x: number) { // cannot overrides inherited member variables + super(x); + this.x; // OK + } +} + +struct Derived1 extends Base { + constructor(public y: number) { // ok + super(y); + this.x; // OK + this.y; // OK + } +} + +var d: Derived1; +d.x; // public, OK + +//// [structConstructorParameterPropertyDeclarationCannotOverride.js] +// doc 3.1 +// derived struct cannot override inherited member variables from base struct +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + }); + function _ctor(x) { + this.x = x; + } + function Base(x) { + var obj = new _Base(); + _ctor.call(obj ,); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor(x) { + this.x = x; + _super.call(this, x); + this.x; // OK + } + function Derived(x) { + var obj = new _Derived(); + _ctor.call(obj ,); + return obj; + } + Derived._TO = _Derived; + return Derived; +})(); +var Derived1 = (function () { + var _Derived1 = new TypedObject.StructType({ + }); + function _ctor(y) { + this.y = y; + _super.call(this, y); + this.x; // OK + this.y; // OK + } + function Derived1(y) { + var obj = new _Derived1(); + _ctor.call(obj ,); + return obj; + } + Derived1._TO = _Derived1; + return Derived1; +})(); +var d; +d.x; // public, OK diff --git a/tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.symbols b/tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.symbols new file mode 100644 index 0000000000000..f6e720d5371c4 --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.symbols @@ -0,0 +1,61 @@ +=== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterPropertyDeclarationCannotOverride.ts === +// doc 3.1 +// derived struct cannot override inherited member variables from base struct + +struct Base { +>Base : Symbol(Base, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 0, 0)) + + constructor(public x: number) { } +>x : Symbol(x, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 4, 16)) +} + +struct Derived extends Base { +>Derived : Symbol(Derived, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 5, 1)) +>Base : Symbol(Base, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 0, 0)) + + constructor(public x: number) { // cannot overrides inherited member variables +>x : Symbol(x, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 8, 13)) + + super(x); +>super : Symbol(Base, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 0, 0)) +>x : Symbol(x, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 8, 13)) + + this.x; // OK +>this.x : Symbol(x, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 8, 13)) +>this : Symbol(Derived, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 5, 1)) +>x : Symbol(x, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 8, 13)) + } +} + +struct Derived1 extends Base { +>Derived1 : Symbol(Derived1, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 12, 1)) +>Base : Symbol(Base, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 0, 0)) + + constructor(public y: number) { // ok +>y : Symbol(y, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 15, 13)) + + super(y); +>super : Symbol(Base, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 0, 0)) +>y : Symbol(y, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 15, 13)) + + this.x; // OK +>this.x : Symbol(Base.x, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 4, 16)) +>this : Symbol(Derived1, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 12, 1)) +>x : Symbol(Base.x, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 4, 16)) + + this.y; // OK +>this.y : Symbol(y, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 15, 13)) +>this : Symbol(Derived1, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 12, 1)) +>y : Symbol(y, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 15, 13)) + } +} + +var d: Derived1; +>d : Symbol(d, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 22, 3)) +>Derived1 : Symbol(Derived1, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 12, 1)) + +d.x; // public, OK +>d.x : Symbol(Base.x, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 4, 16)) +>d : Symbol(d, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 22, 3)) +>x : Symbol(Base.x, Decl(structConstructorParameterPropertyDeclarationCannotOverride.ts, 4, 16)) + diff --git a/tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.types b/tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.types new file mode 100644 index 0000000000000..784aab66bf807 --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterPropertyDeclarationCannotOverride.types @@ -0,0 +1,63 @@ +=== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParameterPropertyDeclarationCannotOverride.ts === +// doc 3.1 +// derived struct cannot override inherited member variables from base struct + +struct Base { +>Base : Base + + constructor(public x: number) { } +>x : number +} + +struct Derived extends Base { +>Derived : Derived +>Base : Base + + constructor(public x: number) { // cannot overrides inherited member variables +>x : number + + super(x); +>super(x) : void +>super : typeof Base +>x : number + + this.x; // OK +>this.x : number +>this : this +>x : number + } +} + +struct Derived1 extends Base { +>Derived1 : Derived1 +>Base : Base + + constructor(public y: number) { // ok +>y : number + + super(y); +>super(y) : void +>super : typeof Base +>y : number + + this.x; // OK +>this.x : number +>this : this +>x : number + + this.y; // OK +>this.y : number +>this : this +>y : number + } +} + +var d: Derived1; +>d : Derived1 +>Derived1 : Derived1 + +d.x; // public, OK +>d.x : number +>d : Derived1 +>x : number + diff --git a/tests/baselines/reference/structConstructorParameterShadowsOuterScopes.errors.txt b/tests/baselines/reference/structConstructorParameterShadowsOuterScopes.errors.txt new file mode 100644 index 0000000000000..6955bbcb2f189 --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterShadowsOuterScopes.errors.txt @@ -0,0 +1,35 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structConstructorParameterShadowsOuterScopes.ts(11,9): error TS2301: Initializer of instance member variable 'b' cannot reference identifier 'x' declared in the constructor. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structConstructorParameterShadowsOuterScopes.ts(13,9): error TS2322: Type 'number' is not assignable to type 'string'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structConstructorParameterShadowsOuterScopes.ts(19,9): error TS2301: Initializer of instance member variable 'b' cannot reference identifier 'y' declared in the constructor. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structConstructorParameterShadowsOuterScopes.ts (3 errors) ==== + // doc 4.1 + // Initializer expressions for instance member variables are evaluated in the scope of the struct + // constructor body, but are not permitted to reference parameters or local variables of + // the constructor. + // This effectively means that entities from outer scopes by the same name as a constructor + // parameter or local variable are inaccessible in initializer expressions for instance member + // variables. + + var x = 1; + struct C { + b = x; // error, evaluated in scope of constructor, cannot reference x + ~ +!!! error TS2301: Initializer of instance member variable 'b' cannot reference identifier 'x' declared in the constructor. + constructor(x: string) { + x = 2; // error, x is string + ~ +!!! error TS2322: Type 'number' is not assignable to type 'string'. + } + } + + var y = 1; + struct D { + b = y; // error, evaluated in scope of constructor, cannot reference y + ~ +!!! error TS2301: Initializer of instance member variable 'b' cannot reference identifier 'y' declared in the constructor. + constructor(x: string) { + var y = ""; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/structConstructorParameterShadowsOuterScopes.js b/tests/baselines/reference/structConstructorParameterShadowsOuterScopes.js new file mode 100644 index 0000000000000..5bdd17305a11e --- /dev/null +++ b/tests/baselines/reference/structConstructorParameterShadowsOuterScopes.js @@ -0,0 +1,65 @@ +//// [structConstructorParameterShadowsOuterScopes.ts] +// doc 4.1 +// Initializer expressions for instance member variables are evaluated in the scope of the struct +// constructor body, but are not permitted to reference parameters or local variables of +// the constructor. +// This effectively means that entities from outer scopes by the same name as a constructor +// parameter or local variable are inaccessible in initializer expressions for instance member +// variables. + +var x = 1; +struct C { + b = x; // error, evaluated in scope of constructor, cannot reference x + constructor(x: string) { + x = 2; // error, x is string + } +} + +var y = 1; +struct D { + b = y; // error, evaluated in scope of constructor, cannot reference y + constructor(x: string) { + var y = ""; + } +} + +//// [structConstructorParameterShadowsOuterScopes.js] +// doc 4.1 +// Initializer expressions for instance member variables are evaluated in the scope of the struct +// constructor body, but are not permitted to reference parameters or local variables of +// the constructor. +// This effectively means that entities from outer scopes by the same name as a constructor +// parameter or local variable are inaccessible in initializer expressions for instance member +// variables. +var x = 1; +var C = (function () { + var _C = new TypedObject.StructType({ + b: TypedObject.Any, + }); + function _ctor(x) { + x = 2; // error, x is string + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; +})(); +var y = 1; +var D = (function () { + var _D = new TypedObject.StructType({ + b: TypedObject.Any, + }); + function _ctor(x) { + var y = ""; + } + function D(x) { + var obj = new _D(); + _ctor.call(obj ,); + return obj; + } + D._TO = _D; + return D; +})(); diff --git a/tests/baselines/reference/structConstructorParametersAccessibility.errors.txt b/tests/baselines/reference/structConstructorParametersAccessibility.errors.txt new file mode 100644 index 0000000000000..738f74efac00e --- /dev/null +++ b/tests/baselines/reference/structConstructorParametersAccessibility.errors.txt @@ -0,0 +1,33 @@ +tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParametersAccessibility.ts(15,1): error TS20018: Property 'p' is private and only accessible within struct 'C2'. +tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParametersAccessibility.ts(22,1): error TS20019: Property 'p' is protected and only accessible within struct 'C3' and its substructs. + + +==== tests/cases/conformance/structs/constructorDeclarations/constructorParameters/structConstructorParametersAccessibility.ts (2 errors) ==== + // doc 3.1 + // Only public and private modifiers are allowed for constructor parameters, but not protected. + + struct C1 { + constructor(public x: number) { } + } + var c1: C1; + c1.x // OK + + + struct C2 { + constructor(private p: number) { } + } + var c2: C2; + c2.p // private, error + ~~~~ +!!! error TS20018: Property 'p' is private and only accessible within struct 'C2'. + + + struct C3 { + constructor(protected p: number) { } // error + } + var c3: C3; + c3.p // protected, error + ~~~~ +!!! error TS20019: Property 'p' is protected and only accessible within struct 'C3' and its substructs. + + \ No newline at end of file diff --git a/tests/baselines/reference/structConstructorParametersAccessibility.js b/tests/baselines/reference/structConstructorParametersAccessibility.js new file mode 100644 index 0000000000000..7435565dd41b6 --- /dev/null +++ b/tests/baselines/reference/structConstructorParametersAccessibility.js @@ -0,0 +1,77 @@ +//// [structConstructorParametersAccessibility.ts] +// doc 3.1 +// Only public and private modifiers are allowed for constructor parameters, but not protected. + +struct C1 { + constructor(public x: number) { } +} +var c1: C1; +c1.x // OK + + +struct C2 { + constructor(private p: number) { } +} +var c2: C2; +c2.p // private, error + + +struct C3 { + constructor(protected p: number) { } // error +} +var c3: C3; +c3.p // protected, error + + + +//// [structConstructorParametersAccessibility.js] +// doc 3.1 +// Only public and private modifiers are allowed for constructor parameters, but not protected. +var C1 = (function () { + var _C1 = new TypedObject.StructType({ + }); + function _ctor(x) { + this.x = x; + } + function C1(x) { + var obj = new _C1(); + _ctor.call(obj ,); + return obj; + } + C1._TO = _C1; + return C1; +})(); +var c1; +c1.x; // OK +var C2 = (function () { + var _C2 = new TypedObject.StructType({ + }); + function _ctor(p) { + this.p = p; + } + function C2(p) { + var obj = new _C2(); + _ctor.call(obj ,); + return obj; + } + C2._TO = _C2; + return C2; +})(); +var c2; +c2.p; // private, error +var C3 = (function () { + var _C3 = new TypedObject.StructType({ + }); + function _ctor(p) { + this.p = p; + } // error + function C3(p) { + var obj = new _C3(); + _ctor.call(obj ,); + return obj; + } + C3._TO = _C3; + return C3; +})(); +var c3; +c3.p; // protected, error diff --git a/tests/baselines/reference/structConstructorWithAssignableReturnExpression.errors.txt b/tests/baselines/reference/structConstructorWithAssignableReturnExpression.errors.txt new file mode 100644 index 0000000000000..e4ce310ab9876 --- /dev/null +++ b/tests/baselines/reference/structConstructorWithAssignableReturnExpression.errors.txt @@ -0,0 +1,48 @@ +tests/cases/conformance/structs/constructorDeclarations/structConstructorWithAssignableReturnExpression.ts(6,16): error TS20020: Cannot have return expression in struct constructor. +tests/cases/conformance/structs/constructorDeclarations/structConstructorWithAssignableReturnExpression.ts(13,16): error TS20020: Cannot have return expression in struct constructor. +tests/cases/conformance/structs/constructorDeclarations/structConstructorWithAssignableReturnExpression.ts(20,16): error TS20020: Cannot have return expression in struct constructor. + + +==== tests/cases/conformance/structs/constructorDeclarations/structConstructorWithAssignableReturnExpression.ts (3 errors) ==== + // doc 3 + // A struct constructor canot return an expression. + + struct C { + constructor() { + return 1; // error + ~ +!!! error TS20020: Cannot have return expression in struct constructor. + } + } + + struct D { + x: number; + constructor() { + return 1; // error + ~ +!!! error TS20020: Cannot have return expression in struct constructor. + } + } + + struct E { + x: number; + constructor() { + return { x: 1 }; // error + ~~~~~~~~ +!!! error TS20020: Cannot have return expression in struct constructor. + } + } + + /* struct F { + x: T; + constructor() { + return { x: 1 }; // error + } + } + + struct G { + x: T; + constructor() { + return { x: null }; // error + } + } */ \ No newline at end of file diff --git a/tests/baselines/reference/structConstructorWithAssignableReturnExpression.js b/tests/baselines/reference/structConstructorWithAssignableReturnExpression.js new file mode 100644 index 0000000000000..c3184b3e11579 --- /dev/null +++ b/tests/baselines/reference/structConstructorWithAssignableReturnExpression.js @@ -0,0 +1,98 @@ +//// [structConstructorWithAssignableReturnExpression.ts] +// doc 3 +// A struct constructor canot return an expression. + +struct C { + constructor() { + return 1; // error + } +} + +struct D { + x: number; + constructor() { + return 1; // error + } +} + +struct E { + x: number; + constructor() { + return { x: 1 }; // error + } +} + +/* struct F { + x: T; + constructor() { + return { x: 1 }; // error + } +} + +struct G { + x: T; + constructor() { + return { x: null }; // error + } +} */ + +//// [structConstructorWithAssignableReturnExpression.js] +// doc 3 +// A struct constructor canot return an expression. +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + return 1; // error + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + x: TypedObject.float64, + }); + function _ctor() { + return 1; // error + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); +var E = (function () { + var _E = new TypedObject.StructType({ + x: TypedObject.float64, + }); + function _ctor() { + return { x: 1 }; // error + } + function E() { + var obj = new _E(); + _ctor.call(obj); + return obj; + } + E._TO = _E; + return E; +})(); +/* struct F { + x: T; + constructor() { + return { x: 1 }; // error + } +} + +struct G { + x: T; + constructor() { + return { x: null }; // error + } +} */ diff --git a/tests/baselines/reference/structConstructorWithExpressionLessReturn.js b/tests/baselines/reference/structConstructorWithExpressionLessReturn.js new file mode 100644 index 0000000000000..9680c17a15832 --- /dev/null +++ b/tests/baselines/reference/structConstructorWithExpressionLessReturn.js @@ -0,0 +1,93 @@ +//// [structConstructorWithExpressionLessReturn.ts] +// doc 3 +// just return keyword is ok. +// no error + +struct C { + constructor() { + return; + } +} + +struct D { + x: number; + constructor() { + return; + } +} + +struct E { + constructor(public x: number) { + return; + } +} + +struct F { + constructor(public x: T) { + return; + } +} + +//// [structConstructorWithExpressionLessReturn.js] +// doc 3 +// just return keyword is ok. +// no error +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + return; + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + x: TypedObject.float64, + }); + function _ctor() { + return; + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); +var E = (function () { + var _E = new TypedObject.StructType({ + }); + function _ctor(x) { + this.x = x; + return; + } + function E(x) { + var obj = new _E(); + _ctor.call(obj ,); + return obj; + } + E._TO = _E; + return E; +})(); +var F = (function () { + var _F = new TypedObject.StructType({ + }); + function _ctor(x) { + this.x = x; + return; + } + function F(x) { + var obj = new _F(); + _ctor.call(obj ,); + return obj; + } + F._TO = _F; + return F; +})(); diff --git a/tests/baselines/reference/structConstructorWithExpressionLessReturn.symbols b/tests/baselines/reference/structConstructorWithExpressionLessReturn.symbols new file mode 100644 index 0000000000000..6b9209a44bfe6 --- /dev/null +++ b/tests/baselines/reference/structConstructorWithExpressionLessReturn.symbols @@ -0,0 +1,45 @@ +=== tests/cases/conformance/structs/constructorDeclarations/structConstructorWithExpressionLessReturn.ts === +// doc 3 +// just return keyword is ok. +// no error + +struct C { +>C : Symbol(C, Decl(structConstructorWithExpressionLessReturn.ts, 0, 0)) + + constructor() { + return; + } +} + +struct D { +>D : Symbol(D, Decl(structConstructorWithExpressionLessReturn.ts, 8, 1)) + + x: number; +>x : Symbol(x, Decl(structConstructorWithExpressionLessReturn.ts, 10, 10)) + + constructor() { + return; + } +} + +struct E { +>E : Symbol(E, Decl(structConstructorWithExpressionLessReturn.ts, 15, 1)) + + constructor(public x: number) { +>x : Symbol(x, Decl(structConstructorWithExpressionLessReturn.ts, 18, 16)) + + return; + } +} + +struct F { +>F : Symbol(F, Decl(structConstructorWithExpressionLessReturn.ts, 21, 1)) +>T : Symbol(T, Decl(structConstructorWithExpressionLessReturn.ts, 23, 9)) + + constructor(public x: T) { +>x : Symbol(x, Decl(structConstructorWithExpressionLessReturn.ts, 24, 16)) +>T : Symbol(T, Decl(structConstructorWithExpressionLessReturn.ts, 23, 9)) + + return; + } +} diff --git a/tests/baselines/reference/structConstructorWithExpressionLessReturn.types b/tests/baselines/reference/structConstructorWithExpressionLessReturn.types new file mode 100644 index 0000000000000..6eeeb006970f5 --- /dev/null +++ b/tests/baselines/reference/structConstructorWithExpressionLessReturn.types @@ -0,0 +1,45 @@ +=== tests/cases/conformance/structs/constructorDeclarations/structConstructorWithExpressionLessReturn.ts === +// doc 3 +// just return keyword is ok. +// no error + +struct C { +>C : C + + constructor() { + return; + } +} + +struct D { +>D : D + + x: number; +>x : number + + constructor() { + return; + } +} + +struct E { +>E : E + + constructor(public x: number) { +>x : number + + return; + } +} + +struct F { +>F : F +>T : T + + constructor(public x: T) { +>x : T +>T : T + + return; + } +} diff --git a/tests/baselines/reference/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.errors.txt b/tests/baselines/reference/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.errors.txt new file mode 100644 index 0000000000000..07f04c25da141 --- /dev/null +++ b/tests/baselines/reference/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.errors.txt @@ -0,0 +1,34 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.ts(17,21): error TS2345: Argument of type '{ a: number; }' is not assignable to parameter of type '{ a: number; b: number; }'. + Property 'b' is missing in type '{ a: number; }'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.ts(18,36): error TS2345: Argument of type '{ a: number; b: number; }' is not assignable to parameter of type '{ a: number; }'. + Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.ts (2 errors) ==== + // doc 4.2 + // A member function can access overridden base struct members using a super property access. + + struct Base { + foo(x: { a: number }): { a: number } { + return null; + } + } + + struct Derived extends Base { + foo(x: { a: number; b: number }): { a: number; b: number } { + return null; + } + + bar() { + var r = super.foo({ a: 1 }); // { a: number } + var r2 = this.foo({a: 1}); // error, Argument of type '{ a: number; }' is not assignable to parameter of type '{ a: number; b: number; }'. + ~~~~~~ +!!! error TS2345: Argument of type '{ a: number; }' is not assignable to parameter of type '{ a: number; b: number; }'. +!!! error TS2345: Property 'b' is missing in type '{ a: number; }'. + var r3 = super.foo({ a: 1, b: 2 }); // { a: number } + ~~~~ +!!! error TS2345: Argument of type '{ a: number; b: number; }' is not assignable to parameter of type '{ a: number; }'. +!!! error TS2345: Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'. + var r4 = this.foo({ a: 1, b: 2 }); // { a: number; b: number; } + } + } \ No newline at end of file diff --git a/tests/baselines/reference/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.js b/tests/baselines/reference/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.js new file mode 100644 index 0000000000000..e9d7ac15c4ee1 --- /dev/null +++ b/tests/baselines/reference/structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.js @@ -0,0 +1,69 @@ +//// [structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.ts] +// doc 4.2 +// A member function can access overridden base struct members using a super property access. + +struct Base { + foo(x: { a: number }): { a: number } { + return null; + } +} + +struct Derived extends Base { + foo(x: { a: number; b: number }): { a: number; b: number } { + return null; + } + + bar() { + var r = super.foo({ a: 1 }); // { a: number } + var r2 = this.foo({a: 1}); // error, Argument of type '{ a: number; }' is not assignable to parameter of type '{ a: number; b: number; }'. + var r3 = super.foo({ a: 1, b: 2 }); // { a: number } + var r4 = this.foo({ a: 1, b: 2 }); // { a: number; b: number; } + } +} + +//// [structDerivedTypeAccessesHiddenBaseCallViaSuperPropertyAccess.js] +// doc 4.2 +// A member function can access overridden base struct members using a super property access. +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + _Base.prototype.foo = function (x) { + return null; + }; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + _Derived.prototype.foo = function (x) { + return null; + }; + _Derived.prototype.bar = function () { + var r = _super.prototype.foo.call(this, { a: 1 }); // { a: number } + var r2 = this.foo({ a: 1 }); // error, Argument of type '{ a: number; }' is not assignable to parameter of type '{ a: number; b: number; }'. + var r3 = _super.prototype.foo.call(this, { a: 1, b: 2 }); // { a: number } + var r4 = this.foo({ a: 1, b: 2 }); // { a: number; b: number; } + }; + return Derived; +})(); diff --git a/tests/baselines/reference/structInitializerReferencingConstructorLocals.errors.txt b/tests/baselines/reference/structInitializerReferencingConstructorLocals.errors.txt new file mode 100644 index 0000000000000..52a69c33dc7c3 --- /dev/null +++ b/tests/baselines/reference/structInitializerReferencingConstructorLocals.errors.txt @@ -0,0 +1,45 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts(7,9): error TS2304: Cannot find name 'z'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts(8,15): error TS2304: Cannot find name 'z'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts(9,14): error TS2339: Property 'z' does not exist on type 'C'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts(10,15): error TS1003: Identifier expected. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts(10,20): error TS2339: Property 'z' does not exist on type 'C'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts(12,9): error TS2304: Cannot find name 'z'. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorLocals.ts (6 errors) ==== + // doc 4.1 + // Initializer expressions for instance member variables are evaluated in the scope of the + // struct constructor body but are not permitted to reference parameters or local variables + // of the constructor. + + struct C { + a = z; // error + ~ +!!! error TS2304: Cannot find name 'z'. + b: typeof z; // error + ~ +!!! error TS2304: Cannot find name 'z'. + c = this.z; // error + ~ +!!! error TS2339: Property 'z' does not exist on type 'C'. + d: typeof this.z; // error + ~~~~ +!!! error TS1003: Identifier expected. + ~ +!!! error TS2339: Property 'z' does not exist on type 'C'. + constructor(x) { + z = 1; + ~ +!!! error TS2304: Cannot find name 'z'. + } + } + + /* struct D { + a = z; // error + b: typeof z; // error + c = this.z; // error + d: typeof this.z; // error + constructor(x: T) { + z = 1; + } + } */ \ No newline at end of file diff --git a/tests/baselines/reference/structInitializerReferencingConstructorLocals.js b/tests/baselines/reference/structInitializerReferencingConstructorLocals.js new file mode 100644 index 0000000000000..1b668ee23e214 --- /dev/null +++ b/tests/baselines/reference/structInitializerReferencingConstructorLocals.js @@ -0,0 +1,58 @@ +//// [structInitializerReferencingConstructorLocals.ts] +// doc 4.1 +// Initializer expressions for instance member variables are evaluated in the scope of the +// struct constructor body but are not permitted to reference parameters or local variables +// of the constructor. + +struct C { + a = z; // error + b: typeof z; // error + c = this.z; // error + d: typeof this.z; // error + constructor(x) { + z = 1; + } +} + +/* struct D { + a = z; // error + b: typeof z; // error + c = this.z; // error + d: typeof this.z; // error + constructor(x: T) { + z = 1; + } +} */ + +//// [structInitializerReferencingConstructorLocals.js] +// doc 4.1 +// Initializer expressions for instance member variables are evaluated in the scope of the +// struct constructor body but are not permitted to reference parameters or local variables +// of the constructor. +var C = (function () { + var _C = new TypedObject.StructType({ + a: TypedObject.Any, + b: TypedObject.Any, + c: TypedObject.Any, + d: TypedObject.Any, + }); + function _ctor(x) { + z = 1; + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; +})(); +/* struct D { + a = z; // error + b: typeof z; // error + c = this.z; // error + d: typeof this.z; // error + constructor(x: T) { + z = 1; + } +} */ diff --git a/tests/baselines/reference/structInitializerReferencingConstructorParameters.errors.txt b/tests/baselines/reference/structInitializerReferencingConstructorParameters.errors.txt new file mode 100644 index 0000000000000..0cdd376128b43 --- /dev/null +++ b/tests/baselines/reference/structInitializerReferencingConstructorParameters.errors.txt @@ -0,0 +1,44 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorParameters.ts(5,9): error TS2304: Cannot find name 'x'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorParameters.ts(6,15): error TS2304: Cannot find name 'x'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorParameters.ts(11,9): error TS2304: Cannot find name 'x'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorParameters.ts(12,15): error TS2304: Cannot find name 'x'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorParameters.ts(18,15): error TS1003: Identifier expected. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInitializerReferencingConstructorParameters.ts (5 errors) ==== + // doc 4.1 + // Initializer expressions for instance member variables are evaluated in the scope of the struct constructor body but are not permitted to reference parameters or local variables of the constructor. + + struct C { + a = x; // error + ~ +!!! error TS2304: Cannot find name 'x'. + b: typeof x; // error + ~ +!!! error TS2304: Cannot find name 'x'. + constructor(x) { } + } + + struct D { + a = x; // error + ~ +!!! error TS2304: Cannot find name 'x'. + b: typeof x; // error + ~ +!!! error TS2304: Cannot find name 'x'. + constructor(public x) { } + } + + struct E { + a = this.x; // ok + b: typeof this.x; // error + ~~~~ +!!! error TS1003: Identifier expected. + constructor(public x) { } + } + + /* struct F { + a = this.x; // ok + b = x; // error + constructor(public x: T) { } + }*/ \ No newline at end of file diff --git a/tests/baselines/reference/structInitializerReferencingConstructorParameters.js b/tests/baselines/reference/structInitializerReferencingConstructorParameters.js new file mode 100644 index 0000000000000..b2a54de163c09 --- /dev/null +++ b/tests/baselines/reference/structInitializerReferencingConstructorParameters.js @@ -0,0 +1,83 @@ +//// [structInitializerReferencingConstructorParameters.ts] +// doc 4.1 +// Initializer expressions for instance member variables are evaluated in the scope of the struct constructor body but are not permitted to reference parameters or local variables of the constructor. + +struct C { + a = x; // error + b: typeof x; // error + constructor(x) { } +} + +struct D { + a = x; // error + b: typeof x; // error + constructor(public x) { } +} + +struct E { + a = this.x; // ok + b: typeof this.x; // error + constructor(public x) { } +} + +/* struct F { + a = this.x; // ok + b = x; // error + constructor(public x: T) { } +}*/ + +//// [structInitializerReferencingConstructorParameters.js] +// doc 4.1 +// Initializer expressions for instance member variables are evaluated in the scope of the struct constructor body but are not permitted to reference parameters or local variables of the constructor. +var C = (function () { + var _C = new TypedObject.StructType({ + a: TypedObject.Any, + b: TypedObject.Any, + }); + function _ctor(x) { + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + a: TypedObject.Any, + b: TypedObject.Any, + }); + function _ctor(x) { + this.x = x; + } + function D(x) { + var obj = new _D(); + _ctor.call(obj ,); + return obj; + } + D._TO = _D; + return D; +})(); +var E = (function () { + var _E = new TypedObject.StructType({ + a: TypedObject.Any, + b: TypedObject.Any, + }); + function _ctor(x) { + this.x = x; + } + function E(x) { + var obj = new _E(); + _ctor.call(obj ,); + return obj; + } + E._TO = _E; + return E; +})(); +/* struct F { + a = this.x; // ok + b = x; // error + constructor(public x: T) { } +}*/ diff --git a/tests/baselines/reference/structInstanceMemberInitialization.js b/tests/baselines/reference/structInstanceMemberInitialization.js new file mode 100644 index 0000000000000..a8f3894070589 --- /dev/null +++ b/tests/baselines/reference/structInstanceMemberInitialization.js @@ -0,0 +1,34 @@ +//// [structInstanceMemberInitialization.ts] +// 4.1 +// ok + +struct C { + x = 1; +} + +var c = new C(); +c.x = 3; +var c2 = new C(); +var r = c.x === c2.x; + +//// [structInstanceMemberInitialization.js] +// 4.1 +// ok +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.float64 + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var c = new C(); +c.x = 3; +var c2 = new C(); +var r = c.x === c2.x; diff --git a/tests/baselines/reference/structInstanceMemberInitialization.symbols b/tests/baselines/reference/structInstanceMemberInitialization.symbols new file mode 100644 index 0000000000000..6a36f782f14af --- /dev/null +++ b/tests/baselines/reference/structInstanceMemberInitialization.symbols @@ -0,0 +1,33 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInstanceMemberInitialization.ts === +// 4.1 +// ok + +struct C { +>C : Symbol(C, Decl(structInstanceMemberInitialization.ts, 0, 0)) + + x = 1; +>x : Symbol(x, Decl(structInstanceMemberInitialization.ts, 3, 10)) +} + +var c = new C(); +>c : Symbol(c, Decl(structInstanceMemberInitialization.ts, 7, 3)) +>C : Symbol(C, Decl(structInstanceMemberInitialization.ts, 0, 0)) + +c.x = 3; +>c.x : Symbol(C.x, Decl(structInstanceMemberInitialization.ts, 3, 10)) +>c : Symbol(c, Decl(structInstanceMemberInitialization.ts, 7, 3)) +>x : Symbol(C.x, Decl(structInstanceMemberInitialization.ts, 3, 10)) + +var c2 = new C(); +>c2 : Symbol(c2, Decl(structInstanceMemberInitialization.ts, 9, 3)) +>C : Symbol(C, Decl(structInstanceMemberInitialization.ts, 0, 0)) + +var r = c.x === c2.x; +>r : Symbol(r, Decl(structInstanceMemberInitialization.ts, 10, 3)) +>c.x : Symbol(C.x, Decl(structInstanceMemberInitialization.ts, 3, 10)) +>c : Symbol(c, Decl(structInstanceMemberInitialization.ts, 7, 3)) +>x : Symbol(C.x, Decl(structInstanceMemberInitialization.ts, 3, 10)) +>c2.x : Symbol(C.x, Decl(structInstanceMemberInitialization.ts, 3, 10)) +>c2 : Symbol(c2, Decl(structInstanceMemberInitialization.ts, 9, 3)) +>x : Symbol(C.x, Decl(structInstanceMemberInitialization.ts, 3, 10)) + diff --git a/tests/baselines/reference/structInstanceMemberInitialization.types b/tests/baselines/reference/structInstanceMemberInitialization.types new file mode 100644 index 0000000000000..eb127f6c4d486 --- /dev/null +++ b/tests/baselines/reference/structInstanceMemberInitialization.types @@ -0,0 +1,39 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structInstanceMemberInitialization.ts === +// 4.1 +// ok + +struct C { +>C : C + + x = 1; +>x : number +>1 : number +} + +var c = new C(); +>c : C +>new C() : C +>C : typeof C + +c.x = 3; +>c.x = 3 : number +>c.x : number +>c : C +>x : number +>3 : number + +var c2 = new C(); +>c2 : C +>new C() : C +>C : typeof C + +var r = c.x === c2.x; +>r : boolean +>c.x === c2.x : boolean +>c.x : number +>c : C +>x : number +>c2.x : number +>c2 : C +>x : number + diff --git a/tests/baselines/reference/structInstancePropertyAndFunctionWithSameName.errors.txt b/tests/baselines/reference/structInstancePropertyAndFunctionWithSameName.errors.txt new file mode 100644 index 0000000000000..e1550c94594b6 --- /dev/null +++ b/tests/baselines/reference/structInstancePropertyAndFunctionWithSameName.errors.txt @@ -0,0 +1,42 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts(7,5): error TS2300: Duplicate identifier 'x'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts(8,5): error TS2300: Duplicate identifier 'x'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts(14,5): error TS2300: Duplicate identifier 'x'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts(15,5): error TS2300: Duplicate identifier 'x'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts(19,5): error TS2300: Duplicate identifier 'x'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts(20,25): error TS2300: Duplicate identifier 'x'. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structInstancePropertyAndFunctionWithSameName.ts (6 errors) ==== + // doc 4 + // Instance property member declarations declare properties in the struct instance type, + // and must specify names that are unique among all instance property member + // and parameter property declarations in the containing struct. + + struct C { + x: number; + ~ +!!! error TS2300: Duplicate identifier 'x'. + x() { // error + ~ +!!! error TS2300: Duplicate identifier 'x'. + return 1; + } + } + + struct D { + x: number; + ~ +!!! error TS2300: Duplicate identifier 'x'. + x(v) { } // error + ~ +!!! error TS2300: Duplicate identifier 'x'. + } + + struct E { + x: number; + ~ +!!! error TS2300: Duplicate identifier 'x'. + constructor(private x: string) {} + ~ +!!! error TS2300: Duplicate identifier 'x'. + } \ No newline at end of file diff --git a/tests/baselines/reference/structInstancePropertyAndFunctionWithSameName.js b/tests/baselines/reference/structInstancePropertyAndFunctionWithSameName.js new file mode 100644 index 0000000000000..ce126318cb90a --- /dev/null +++ b/tests/baselines/reference/structInstancePropertyAndFunctionWithSameName.js @@ -0,0 +1,75 @@ +//// [structInstancePropertyAndFunctionWithSameName.ts] +// doc 4 +// Instance property member declarations declare properties in the struct instance type, +// and must specify names that are unique among all instance property member +// and parameter property declarations in the containing struct. + +struct C { + x: number; + x() { // error + return 1; + } +} + +struct D { + x: number; + x(v) { } // error +} + +struct E { + x: number; + constructor(private x: string) {} +} + +//// [structInstancePropertyAndFunctionWithSameName.js] +// doc 4 +// Instance property member declarations declare properties in the struct instance type, +// and must specify names that are unique among all instance property member +// and parameter property declarations in the containing struct. +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.float64, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.x = function () { + return 1; + }; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + x: TypedObject.float64, + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + _D.prototype.x = function (v) { }; // error + return D; +})(); +var E = (function () { + var _E = new TypedObject.StructType({ + x: TypedObject.float64, + }); + function _ctor(x) { + this.x = x; + } + function E(x) { + var obj = new _E(); + _ctor.call(obj ,); + return obj; + } + E._TO = _E; + return E; +})(); diff --git a/tests/baselines/reference/structMemberFunctionOverloadMixingStaticAndInstance.errors.txt b/tests/baselines/reference/structMemberFunctionOverloadMixingStaticAndInstance.errors.txt new file mode 100644 index 0000000000000..90c8d30cd2eda --- /dev/null +++ b/tests/baselines/reference/structMemberFunctionOverloadMixingStaticAndInstance.errors.txt @@ -0,0 +1,37 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionOverloadMixingStaticAndInstance.ts(6,12): error TS2388: Function overload must not be static. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionOverloadMixingStaticAndInstance.ts(6,12): error TS2391: Function implementation is missing or not immediately following the declaration. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionOverloadMixingStaticAndInstance.ts(11,5): error TS2387: Function overload must be static. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionOverloadMixingStaticAndInstance.ts(11,5): error TS2391: Function implementation is missing or not immediately following the declaration. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionOverloadMixingStaticAndInstance.ts (4 errors) ==== + // doc 4.2 + // All overloads of a member function must have the same accessibility (public or private) and + // kind (instance or static). + struct C { + foo(); + static foo(); // error, Function overload must not be static. + ~~~ +!!! error TS2388: Function overload must not be static. + ~~~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + + struct D { + static foo(); + foo(); // error, Function overload must be static. + ~~~ +!!! error TS2387: Function overload must be static. + ~~~ +!!! error TS2391: Function implementation is missing or not immediately following the declaration. + } + + /* struct E { + foo(x: T); + static foo(x: number); // error, Function overload must not be static. + } + + struct F { + static foo(x: number); + foo(x: T); // error, Function overload must be static. + } */ \ No newline at end of file diff --git a/tests/baselines/reference/structMemberFunctionOverloadMixingStaticAndInstance.js b/tests/baselines/reference/structMemberFunctionOverloadMixingStaticAndInstance.js new file mode 100644 index 0000000000000..943920f7fb743 --- /dev/null +++ b/tests/baselines/reference/structMemberFunctionOverloadMixingStaticAndInstance.js @@ -0,0 +1,63 @@ +//// [structMemberFunctionOverloadMixingStaticAndInstance.ts] +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +struct C { + foo(); + static foo(); // error, Function overload must not be static. +} + +struct D { + static foo(); + foo(); // error, Function overload must be static. +} + +/* struct E { + foo(x: T); + static foo(x: number); // error, Function overload must not be static. +} + +struct F { + static foo(x: number); + foo(x: T); // error, Function overload must be static. +} */ + +//// [structMemberFunctionOverloadMixingStaticAndInstance.js] +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); +/* struct E { + foo(x: T); + static foo(x: number); // error, Function overload must not be static. +} + +struct F { + static foo(x: number); + foo(x: T); // error, Function overload must be static. +} */ diff --git a/tests/baselines/reference/structMemberFunctionsWithPrivateOverloads.errors.txt b/tests/baselines/reference/structMemberFunctionsWithPrivateOverloads.errors.txt new file mode 100644 index 0000000000000..cb12414382823 --- /dev/null +++ b/tests/baselines/reference/structMemberFunctionsWithPrivateOverloads.errors.txt @@ -0,0 +1,63 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPrivateOverloads.ts(25,10): error TS20018: Property 'foo' is private and only accessible within struct 'C'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPrivateOverloads.ts(26,10): error TS20018: Property 'foo' is private and only accessible within struct 'C'. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPrivateOverloads.ts (2 errors) ==== + // doc 4.2 + // All overloads of a member function must have the same accessibility (public or private) and + // kind (instance or static). + struct C { + private foo(x: number); + private foo(x: number, y: string); + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + private bar(x: string); + private bar(x: number, y: string); + private bar(x: any, y?: any) { } + + private static foo(x: number); + private static foo(x: number, y: string); + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + private static bar(x: string); + private static bar(x: number, y: string); + private static bar(x: any, y?: any) { } + } + + var c: C; + var r1 = c.foo(1); // error + ~~~~~ +!!! error TS20018: Property 'foo' is private and only accessible within struct 'C'. + var r2 = C.foo(1); // error + ~~~~~ +!!! error TS20018: Property 'foo' is private and only accessible within struct 'C'. + + /* struct D { + private foo(x: number); + private foo(x: T, y: T); + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + private bar(x: string); + private bar(x: T, y: T); + private bar(x: any, y?: any) { } + + private static foo(x: number); + private static foo(x: number, y: number); + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + private static bar(x: string); + private static bar(x: number, y: number); + private static bar(x: any, y?: any) { } + + } + + + + var d: D; + var r2 = d.foo(2); // error + var r4 = D.bar(''); // error + */ \ No newline at end of file diff --git a/tests/baselines/reference/structMemberFunctionsWithPrivateOverloads.js b/tests/baselines/reference/structMemberFunctionsWithPrivateOverloads.js new file mode 100644 index 0000000000000..60215c734eb18 --- /dev/null +++ b/tests/baselines/reference/structMemberFunctionsWithPrivateOverloads.js @@ -0,0 +1,107 @@ +//// [structMemberFunctionsWithPrivateOverloads.ts] +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +struct C { + private foo(x: number); + private foo(x: number, y: string); + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + private bar(x: string); + private bar(x: number, y: string); + private bar(x: any, y?: any) { } + + private static foo(x: number); + private static foo(x: number, y: string); + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + private static bar(x: string); + private static bar(x: number, y: string); + private static bar(x: any, y?: any) { } +} + +var c: C; +var r1 = c.foo(1); // error +var r2 = C.foo(1); // error + +/* struct D { + private foo(x: number); + private foo(x: T, y: T); + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + private bar(x: string); + private bar(x: T, y: T); + private bar(x: any, y?: any) { } + + private static foo(x: number); + private static foo(x: number, y: number); + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + private static bar(x: string); + private static bar(x: number, y: number); + private static bar(x: any, y?: any) { } + +} + + + +var d: D; +var r2 = d.foo(2); // error +var r4 = D.bar(''); // error +*/ + +//// [structMemberFunctionsWithPrivateOverloads.js] +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.foo = function (x, y) { }; + _C.prototype.bar = function (x, y) { }; + _C.foo = function (x, y) { }; + _C.bar = function (x, y) { }; + return C; +})(); +var c; +var r1 = c.foo(1); // error +var r2 = C.foo(1); // error +/* struct D { + private foo(x: number); + private foo(x: T, y: T); + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + private bar(x: string); + private bar(x: T, y: T); + private bar(x: any, y?: any) { } + + private static foo(x: number); + private static foo(x: number, y: number); + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + private static bar(x: string); + private static bar(x: number, y: number); + private static bar(x: any, y?: any) { } + +} + + + +var d: D; +var r2 = d.foo(2); // error +var r4 = D.bar(''); // error +*/ diff --git a/tests/baselines/reference/structMemberFunctionsWithPublicOverloads.js b/tests/baselines/reference/structMemberFunctionsWithPublicOverloads.js new file mode 100644 index 0000000000000..ca7803fc5b507 --- /dev/null +++ b/tests/baselines/reference/structMemberFunctionsWithPublicOverloads.js @@ -0,0 +1,88 @@ +//// [structMemberFunctionsWithPublicOverloads.ts] +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +// ok +struct C { + public foo(x: number); + public foo(x: number, y: string); + public foo(x: any, y?: any) { } + + public bar(x: 'hi'); + public bar(x: string); + public bar(x: number, y: string); + public bar(x: any, y?: any) { } + + public static foo(x: number); + public static foo(x: number, y: string); + public static foo(x: any, y?: any) { } + + public static bar(x: 'hi'); + public static bar(x: string); + public static bar(x: number, y: string); + public static bar(x: any, y?: any) { } +} + +/* struct D { + public foo(x: number); + public foo(x: T, y: T); + public foo(x: any, y?: any) { } + + public bar(x: 'hi'); + public bar(x: string); + public bar(x: T, y: T); + public bar(x: any, y?: any) { } + + public static foo(x: number); + public static foo(x: number, y: string); + public static foo(x: any, y?: any) { } + + public static bar(x: 'hi'); + public static bar(x: string); + public static bar(x: number, y: string); + public static bar(x: any, y?: any) { } + +}*/ + +//// [structMemberFunctionsWithPublicOverloads.js] +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +// ok +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.foo = function (x, y) { }; + _C.prototype.bar = function (x, y) { }; + _C.foo = function (x, y) { }; + _C.bar = function (x, y) { }; + return C; +})(); +/* struct D { + public foo(x: number); + public foo(x: T, y: T); + public foo(x: any, y?: any) { } + + public bar(x: 'hi'); + public bar(x: string); + public bar(x: T, y: T); + public bar(x: any, y?: any) { } + + public static foo(x: number); + public static foo(x: number, y: string); + public static foo(x: any, y?: any) { } + + public static bar(x: 'hi'); + public static bar(x: string); + public static bar(x: number, y: string); + public static bar(x: any, y?: any) { } + +}*/ diff --git a/tests/baselines/reference/structMemberFunctionsWithPublicOverloads.symbols b/tests/baselines/reference/structMemberFunctionsWithPublicOverloads.symbols new file mode 100644 index 0000000000000..ba56ee3181479 --- /dev/null +++ b/tests/baselines/reference/structMemberFunctionsWithPublicOverloads.symbols @@ -0,0 +1,93 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicOverloads.ts === +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +// ok +struct C { +>C : Symbol(C, Decl(structMemberFunctionsWithPublicOverloads.ts, 0, 0)) + + public foo(x: number); +>foo : Symbol(foo, Decl(structMemberFunctionsWithPublicOverloads.ts, 4, 10), Decl(structMemberFunctionsWithPublicOverloads.ts, 5, 26), Decl(structMemberFunctionsWithPublicOverloads.ts, 6, 37)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 5, 15)) + + public foo(x: number, y: string); +>foo : Symbol(foo, Decl(structMemberFunctionsWithPublicOverloads.ts, 4, 10), Decl(structMemberFunctionsWithPublicOverloads.ts, 5, 26), Decl(structMemberFunctionsWithPublicOverloads.ts, 6, 37)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 6, 15)) +>y : Symbol(y, Decl(structMemberFunctionsWithPublicOverloads.ts, 6, 25)) + + public foo(x: any, y?: any) { } +>foo : Symbol(foo, Decl(structMemberFunctionsWithPublicOverloads.ts, 4, 10), Decl(structMemberFunctionsWithPublicOverloads.ts, 5, 26), Decl(structMemberFunctionsWithPublicOverloads.ts, 6, 37)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 7, 15)) +>y : Symbol(y, Decl(structMemberFunctionsWithPublicOverloads.ts, 7, 22)) + + public bar(x: 'hi'); +>bar : Symbol(bar, Decl(structMemberFunctionsWithPublicOverloads.ts, 7, 35), Decl(structMemberFunctionsWithPublicOverloads.ts, 9, 24), Decl(structMemberFunctionsWithPublicOverloads.ts, 10, 26), Decl(structMemberFunctionsWithPublicOverloads.ts, 11, 37)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 9, 15)) + + public bar(x: string); +>bar : Symbol(bar, Decl(structMemberFunctionsWithPublicOverloads.ts, 7, 35), Decl(structMemberFunctionsWithPublicOverloads.ts, 9, 24), Decl(structMemberFunctionsWithPublicOverloads.ts, 10, 26), Decl(structMemberFunctionsWithPublicOverloads.ts, 11, 37)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 10, 15)) + + public bar(x: number, y: string); +>bar : Symbol(bar, Decl(structMemberFunctionsWithPublicOverloads.ts, 7, 35), Decl(structMemberFunctionsWithPublicOverloads.ts, 9, 24), Decl(structMemberFunctionsWithPublicOverloads.ts, 10, 26), Decl(structMemberFunctionsWithPublicOverloads.ts, 11, 37)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 11, 15)) +>y : Symbol(y, Decl(structMemberFunctionsWithPublicOverloads.ts, 11, 25)) + + public bar(x: any, y?: any) { } +>bar : Symbol(bar, Decl(structMemberFunctionsWithPublicOverloads.ts, 7, 35), Decl(structMemberFunctionsWithPublicOverloads.ts, 9, 24), Decl(structMemberFunctionsWithPublicOverloads.ts, 10, 26), Decl(structMemberFunctionsWithPublicOverloads.ts, 11, 37)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 12, 15)) +>y : Symbol(y, Decl(structMemberFunctionsWithPublicOverloads.ts, 12, 22)) + + public static foo(x: number); +>foo : Symbol(C.foo, Decl(structMemberFunctionsWithPublicOverloads.ts, 12, 35), Decl(structMemberFunctionsWithPublicOverloads.ts, 14, 33), Decl(structMemberFunctionsWithPublicOverloads.ts, 15, 44)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 14, 22)) + + public static foo(x: number, y: string); +>foo : Symbol(C.foo, Decl(structMemberFunctionsWithPublicOverloads.ts, 12, 35), Decl(structMemberFunctionsWithPublicOverloads.ts, 14, 33), Decl(structMemberFunctionsWithPublicOverloads.ts, 15, 44)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 15, 22)) +>y : Symbol(y, Decl(structMemberFunctionsWithPublicOverloads.ts, 15, 32)) + + public static foo(x: any, y?: any) { } +>foo : Symbol(C.foo, Decl(structMemberFunctionsWithPublicOverloads.ts, 12, 35), Decl(structMemberFunctionsWithPublicOverloads.ts, 14, 33), Decl(structMemberFunctionsWithPublicOverloads.ts, 15, 44)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 16, 22)) +>y : Symbol(y, Decl(structMemberFunctionsWithPublicOverloads.ts, 16, 29)) + + public static bar(x: 'hi'); +>bar : Symbol(C.bar, Decl(structMemberFunctionsWithPublicOverloads.ts, 16, 42), Decl(structMemberFunctionsWithPublicOverloads.ts, 18, 31), Decl(structMemberFunctionsWithPublicOverloads.ts, 19, 33), Decl(structMemberFunctionsWithPublicOverloads.ts, 20, 44)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 18, 22)) + + public static bar(x: string); +>bar : Symbol(C.bar, Decl(structMemberFunctionsWithPublicOverloads.ts, 16, 42), Decl(structMemberFunctionsWithPublicOverloads.ts, 18, 31), Decl(structMemberFunctionsWithPublicOverloads.ts, 19, 33), Decl(structMemberFunctionsWithPublicOverloads.ts, 20, 44)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 19, 22)) + + public static bar(x: number, y: string); +>bar : Symbol(C.bar, Decl(structMemberFunctionsWithPublicOverloads.ts, 16, 42), Decl(structMemberFunctionsWithPublicOverloads.ts, 18, 31), Decl(structMemberFunctionsWithPublicOverloads.ts, 19, 33), Decl(structMemberFunctionsWithPublicOverloads.ts, 20, 44)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 20, 22)) +>y : Symbol(y, Decl(structMemberFunctionsWithPublicOverloads.ts, 20, 32)) + + public static bar(x: any, y?: any) { } +>bar : Symbol(C.bar, Decl(structMemberFunctionsWithPublicOverloads.ts, 16, 42), Decl(structMemberFunctionsWithPublicOverloads.ts, 18, 31), Decl(structMemberFunctionsWithPublicOverloads.ts, 19, 33), Decl(structMemberFunctionsWithPublicOverloads.ts, 20, 44)) +>x : Symbol(x, Decl(structMemberFunctionsWithPublicOverloads.ts, 21, 22)) +>y : Symbol(y, Decl(structMemberFunctionsWithPublicOverloads.ts, 21, 29)) +} + +/* struct D { + public foo(x: number); + public foo(x: T, y: T); + public foo(x: any, y?: any) { } + + public bar(x: 'hi'); + public bar(x: string); + public bar(x: T, y: T); + public bar(x: any, y?: any) { } + + public static foo(x: number); + public static foo(x: number, y: string); + public static foo(x: any, y?: any) { } + + public static bar(x: 'hi'); + public static bar(x: string); + public static bar(x: number, y: string); + public static bar(x: any, y?: any) { } + +}*/ diff --git a/tests/baselines/reference/structMemberFunctionsWithPublicOverloads.types b/tests/baselines/reference/structMemberFunctionsWithPublicOverloads.types new file mode 100644 index 0000000000000..85df0868da665 --- /dev/null +++ b/tests/baselines/reference/structMemberFunctionsWithPublicOverloads.types @@ -0,0 +1,93 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicOverloads.ts === +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +// ok +struct C { +>C : C + + public foo(x: number); +>foo : { (x: number): any; (x: number, y: string): any; } +>x : number + + public foo(x: number, y: string); +>foo : { (x: number): any; (x: number, y: string): any; } +>x : number +>y : string + + public foo(x: any, y?: any) { } +>foo : { (x: number): any; (x: number, y: string): any; } +>x : any +>y : any + + public bar(x: 'hi'); +>bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>x : 'hi' + + public bar(x: string); +>bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>x : string + + public bar(x: number, y: string); +>bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>x : number +>y : string + + public bar(x: any, y?: any) { } +>bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>x : any +>y : any + + public static foo(x: number); +>foo : { (x: number): any; (x: number, y: string): any; } +>x : number + + public static foo(x: number, y: string); +>foo : { (x: number): any; (x: number, y: string): any; } +>x : number +>y : string + + public static foo(x: any, y?: any) { } +>foo : { (x: number): any; (x: number, y: string): any; } +>x : any +>y : any + + public static bar(x: 'hi'); +>bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>x : 'hi' + + public static bar(x: string); +>bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>x : string + + public static bar(x: number, y: string); +>bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>x : number +>y : string + + public static bar(x: any, y?: any) { } +>bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>x : any +>y : any +} + +/* struct D { + public foo(x: number); + public foo(x: T, y: T); + public foo(x: any, y?: any) { } + + public bar(x: 'hi'); + public bar(x: string); + public bar(x: T, y: T); + public bar(x: any, y?: any) { } + + public static foo(x: number); + public static foo(x: number, y: string); + public static foo(x: any, y?: any) { } + + public static bar(x: 'hi'); + public static bar(x: string); + public static bar(x: number, y: string); + public static bar(x: any, y?: any) { } + +}*/ diff --git a/tests/baselines/reference/structMemberFunctionsWithPublicPrivateOverloads.errors.txt b/tests/baselines/reference/structMemberFunctionsWithPublicPrivateOverloads.errors.txt new file mode 100644 index 0000000000000..e272a4bd430c1 --- /dev/null +++ b/tests/baselines/reference/structMemberFunctionsWithPublicPrivateOverloads.errors.txt @@ -0,0 +1,75 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicPrivateOverloads.ts(6,12): error TS2385: Overload signatures must all be public, private or protected. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicPrivateOverloads.ts(10,12): error TS2385: Overload signatures must all be public, private or protected. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicPrivateOverloads.ts(15,19): error TS2385: Overload signatures must all be public, private or protected. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicPrivateOverloads.ts(19,19): error TS2385: Overload signatures must all be public, private or protected. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicPrivateOverloads.ts(25,9): error TS20018: Property 'foo' is private and only accessible within struct 'C'. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structMemberFunctionsWithPublicPrivateOverloads.ts (5 errors) ==== + // doc 4.2 + // All overloads of a member function must have the same accessibility (public or private) and + // kind (instance or static). + struct C { + private foo(x: number); + public foo(x: number, y: string); // error + ~~~ +!!! error TS2385: Overload signatures must all be public, private or protected. + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + public bar(x: string); // error + ~~~ +!!! error TS2385: Overload signatures must all be public, private or protected. + private bar(x: number, y: string); + private bar(x: any, y?: any) { } + + private static foo(x: number); + public static foo(x: number, y: string); // error + ~~~ +!!! error TS2385: Overload signatures must all be public, private or protected. + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + public static bar(x: string); // error + ~~~ +!!! error TS2385: Overload signatures must all be public, private or protected. + private static bar(x: number, y: string); + private static bar(x: any, y?: any) { } + } + + var c: C; + var r = c.foo(1); // error + ~~~~~ +!!! error TS20018: Property 'foo' is private and only accessible within struct 'C'. + + /* struct D { + private foo(x: number); + public foo(x: T, y: T); // error + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + public bar(x: string); // error + private bar(x: T, y: T); + private bar(x: any, y?: any) { } + + private baz(x: string); + protected baz(x: number, y: string); // error + private baz(x: any, y?: any) { } + + private static foo(x: number); + public static foo(x: number, y: string); // error + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + public static bar(x: string); // error + private static bar(x: number, y: string); + private static bar(x: any, y?: any) { } + + public static baz(x: string); // error + protected static baz(x: number, y: string); + protected static baz(x: any, y?: any) { } + } + + var d: D; + var r2 = d.foo(2); // error + */ \ No newline at end of file diff --git a/tests/baselines/reference/structMemberFunctionsWithPublicPrivateOverloads.js b/tests/baselines/reference/structMemberFunctionsWithPublicPrivateOverloads.js new file mode 100644 index 0000000000000..c907a595a7120 --- /dev/null +++ b/tests/baselines/reference/structMemberFunctionsWithPublicPrivateOverloads.js @@ -0,0 +1,113 @@ +//// [structMemberFunctionsWithPublicPrivateOverloads.ts] +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +struct C { + private foo(x: number); + public foo(x: number, y: string); // error + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + public bar(x: string); // error + private bar(x: number, y: string); + private bar(x: any, y?: any) { } + + private static foo(x: number); + public static foo(x: number, y: string); // error + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + public static bar(x: string); // error + private static bar(x: number, y: string); + private static bar(x: any, y?: any) { } +} + +var c: C; +var r = c.foo(1); // error + +/* struct D { + private foo(x: number); + public foo(x: T, y: T); // error + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + public bar(x: string); // error + private bar(x: T, y: T); + private bar(x: any, y?: any) { } + + private baz(x: string); + protected baz(x: number, y: string); // error + private baz(x: any, y?: any) { } + + private static foo(x: number); + public static foo(x: number, y: string); // error + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + public static bar(x: string); // error + private static bar(x: number, y: string); + private static bar(x: any, y?: any) { } + + public static baz(x: string); // error + protected static baz(x: number, y: string); + protected static baz(x: any, y?: any) { } +} + +var d: D; +var r2 = d.foo(2); // error +*/ + +//// [structMemberFunctionsWithPublicPrivateOverloads.js] +// doc 4.2 +// All overloads of a member function must have the same accessibility (public or private) and +// kind (instance or static). +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.foo = function (x, y) { }; + _C.prototype.bar = function (x, y) { }; + _C.foo = function (x, y) { }; + _C.bar = function (x, y) { }; + return C; +})(); +var c; +var r = c.foo(1); // error +/* struct D { + private foo(x: number); + public foo(x: T, y: T); // error + private foo(x: any, y?: any) { } + + private bar(x: 'hi'); + public bar(x: string); // error + private bar(x: T, y: T); + private bar(x: any, y?: any) { } + + private baz(x: string); + protected baz(x: number, y: string); // error + private baz(x: any, y?: any) { } + + private static foo(x: number); + public static foo(x: number, y: string); // error + private static foo(x: any, y?: any) { } + + private static bar(x: 'hi'); + public static bar(x: string); // error + private static bar(x: number, y: string); + private static bar(x: any, y?: any) { } + + public static baz(x: string); // error + protected static baz(x: number, y: string); + protected static baz(x: any, y?: any) { } +} + +var d: D; +var r2 = d.foo(2); // error +*/ diff --git a/tests/baselines/reference/structNotAddNonDeclaredMember.errors.txt b/tests/baselines/reference/structNotAddNonDeclaredMember.errors.txt new file mode 100644 index 0000000000000..3ab87d6080930 --- /dev/null +++ b/tests/baselines/reference/structNotAddNonDeclaredMember.errors.txt @@ -0,0 +1,12 @@ +tests/cases/conformance/structs/structNotAddNonDeclaredMember.ts(6,3): error TS2339: Property 'lastName' does not exist on type 'C'. + + +==== tests/cases/conformance/structs/structNotAddNonDeclaredMember.ts (1 errors) ==== + struct C { + constructor(public firstName: string, public age: number) {} + } + + var c = new C("John", 25); + c.lastName = c.firstName; // error, cannot add undefined members to struct + ~~~~~~~~ +!!! error TS2339: Property 'lastName' does not exist on type 'C'. \ No newline at end of file diff --git a/tests/baselines/reference/structNotAddNonDeclaredMember.js b/tests/baselines/reference/structNotAddNonDeclaredMember.js new file mode 100644 index 0000000000000..1f9c906b75e41 --- /dev/null +++ b/tests/baselines/reference/structNotAddNonDeclaredMember.js @@ -0,0 +1,26 @@ +//// [structNotAddNonDeclaredMember.ts] +struct C { + constructor(public firstName: string, public age: number) {} +} + +var c = new C("John", 25); +c.lastName = c.firstName; // error, cannot add undefined members to struct + +//// [structNotAddNonDeclaredMember.js] +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor(firstName, age) { + this.firstName = firstName; + this.age = age; + } + function C(firstName, age) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; +})(); +var c = new C("John", 25); +c.lastName = c.firstName; // error, cannot add undefined members to struct diff --git a/tests/baselines/reference/structNotHaveOptionalProperties.errors.txt b/tests/baselines/reference/structNotHaveOptionalProperties.errors.txt new file mode 100644 index 0000000000000..48bfe032f1fc0 --- /dev/null +++ b/tests/baselines/reference/structNotHaveOptionalProperties.errors.txt @@ -0,0 +1,25 @@ +tests/cases/conformance/structs/structNotHaveOptionalProperties.ts(10,3): error TS1112: A class member cannot be declared optional. +tests/cases/conformance/structs/structNotHaveOptionalProperties.ts(14,3): error TS20013: A struct member cannot be declared optional. + + +==== tests/cases/conformance/structs/structNotHaveOptionalProperties.ts (2 errors) ==== + /** + * inspired by conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties + */ + + interface I { + x?: number; // ok + } + + class C { + x?: number; // error, cannot be declared optional + ~ +!!! error TS1112: A class member cannot be declared optional. + } + + struct S { + x?: number; // error, cannot be declared optional + ~ +!!! error TS20013: A struct member cannot be declared optional. + } + \ No newline at end of file diff --git a/tests/baselines/reference/structNotHaveOptionalProperties.js b/tests/baselines/reference/structNotHaveOptionalProperties.js new file mode 100644 index 0000000000000..2066b7816b66f --- /dev/null +++ b/tests/baselines/reference/structNotHaveOptionalProperties.js @@ -0,0 +1,41 @@ +//// [structNotHaveOptionalProperties.ts] +/** + * inspired by conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties + */ + +interface I { + x?: number; // ok +} + +class C { + x?: number; // error, cannot be declared optional +} + +struct S { + x?: number; // error, cannot be declared optional +} + + +//// [structNotHaveOptionalProperties.js] +/** + * inspired by conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties + */ +var C = (function () { + function C() { + } + return C; +})(); +var S = (function () { + var _S = new TypedObject.StructType({ + x: TypedObject.float64 + }); + function _ctor() { + } + function S() { + var obj = new _S(); + _ctor.call(obj); + return obj; + } + S._TO = _S; + return S; +})(); diff --git a/tests/baselines/reference/structPropertyNamedPrototype.errors.txt b/tests/baselines/reference/structPropertyNamedPrototype.errors.txt new file mode 100644 index 0000000000000..cdef0cd34ea41 --- /dev/null +++ b/tests/baselines/reference/structPropertyNamedPrototype.errors.txt @@ -0,0 +1,13 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structPropertyNamedPrototype.ts(6,12): error TS2300: Duplicate identifier 'prototype'. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structPropertyNamedPrototype.ts (1 errors) ==== + // doc 4 + // It is an error to explicitly declare a static property member with the name ‘prototype’. + + struct C { + prototype: number; // ok + static prototype: C; // error + ~~~~~~~~~ +!!! error TS2300: Duplicate identifier 'prototype'. + } \ No newline at end of file diff --git a/tests/baselines/reference/structPropertyNamedPrototype.js b/tests/baselines/reference/structPropertyNamedPrototype.js new file mode 100644 index 0000000000000..b36d2e01e4efe --- /dev/null +++ b/tests/baselines/reference/structPropertyNamedPrototype.js @@ -0,0 +1,26 @@ +//// [structPropertyNamedPrototype.ts] +// doc 4 +// It is an error to explicitly declare a static property member with the name ‘prototype’. + +struct C { + prototype: number; // ok + static prototype: C; // error +} + +//// [structPropertyNamedPrototype.js] +// doc 4 +// It is an error to explicitly declare a static property member with the name ‘prototype’. +var C = (function () { + var _C = new TypedObject.StructType({ + prototype: TypedObject.float64, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); diff --git a/tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.js b/tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.js new file mode 100644 index 0000000000000..338c0425a32cb --- /dev/null +++ b/tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.js @@ -0,0 +1,35 @@ +//// [structStaticAndNonStaticPropertiesSameName.ts] +// doc 4 +// the declaration spaces of instance and static property members are separate. +// Thus, it is possible to have instance and static property members with the same name +// ok + +struct C { + x: number; + static x: number; + + f() { } + static f() { } +} + +//// [structStaticAndNonStaticPropertiesSameName.js] +// doc 4 +// the declaration spaces of instance and static property members are separate. +// Thus, it is possible to have instance and static property members with the same name +// ok +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.float64, + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.f = function () { }; + _C.f = function () { }; + return C; +})(); diff --git a/tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.symbols b/tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.symbols new file mode 100644 index 0000000000000..1a5c4c10c7b17 --- /dev/null +++ b/tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.symbols @@ -0,0 +1,21 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticAndNonStaticPropertiesSameName.ts === +// doc 4 +// the declaration spaces of instance and static property members are separate. +// Thus, it is possible to have instance and static property members with the same name +// ok + +struct C { +>C : Symbol(C, Decl(structStaticAndNonStaticPropertiesSameName.ts, 0, 0)) + + x: number; +>x : Symbol(x, Decl(structStaticAndNonStaticPropertiesSameName.ts, 5, 10)) + + static x: number; +>x : Symbol(C.x, Decl(structStaticAndNonStaticPropertiesSameName.ts, 6, 14)) + + f() { } +>f : Symbol(f, Decl(structStaticAndNonStaticPropertiesSameName.ts, 7, 21)) + + static f() { } +>f : Symbol(C.f, Decl(structStaticAndNonStaticPropertiesSameName.ts, 9, 11)) +} diff --git a/tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.types b/tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.types new file mode 100644 index 0000000000000..f6a5799321339 --- /dev/null +++ b/tests/baselines/reference/structStaticAndNonStaticPropertiesSameName.types @@ -0,0 +1,21 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticAndNonStaticPropertiesSameName.ts === +// doc 4 +// the declaration spaces of instance and static property members are separate. +// Thus, it is possible to have instance and static property members with the same name +// ok + +struct C { +>C : C + + x: number; +>x : number + + static x: number; +>x : number + + f() { } +>f : () => void + + static f() { } +>f : () => void +} diff --git a/tests/baselines/reference/structStaticFactory1.js b/tests/baselines/reference/structStaticFactory1.js new file mode 100644 index 0000000000000..786c8d6e1d380 --- /dev/null +++ b/tests/baselines/reference/structStaticFactory1.js @@ -0,0 +1,63 @@ +//// [structStaticFactory1.ts] +// doc 4.2 +// In a static member function, this represents the constructor function object on which +// the static member function was invoked. Thus, a call to ‘new this()’ may actually invoke +// a derived struct constructor. +// ok +struct Base { + foo() { return 1; } + static create() { + return new this(); + } +} + +struct Derived extends Base { + foo() { return 2; } +} +var d = Derived.create(); // new B() +d.foo(); // 2 + +//// [structStaticFactory1.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +// doc 4.2 +// In a static member function, this represents the constructor function object on which +// the static member function was invoked. Thus, a call to ‘new this()’ may actually invoke +// a derived struct constructor. +// ok +var Base = (function () { + var _Base = new TypedObject.StructType({ + }); + function _ctor() { + } + function Base() { + var obj = new _Base(); + _ctor.call(obj); + return obj; + } + Base._TO = _Base; + _Base.prototype.foo = function () { return 1; }; + _Base.create = function () { + return new this(); + }; + return Base; +})(); +var Derived = (function () { + var _Derived = new TypedObject.StructType({ + }); + function _ctor() { + } + function Derived() { + var obj = new _Derived(); + _ctor.call(obj); + return obj; + } + Derived._TO = _Derived; + _Derived.prototype.foo = function () { return 2; }; + return Derived; +})(); +var d = Derived.create(); // new B() +d.foo(); // 2 diff --git a/tests/baselines/reference/structStaticFactory1.symbols b/tests/baselines/reference/structStaticFactory1.symbols new file mode 100644 index 0000000000000..3120cb3821d70 --- /dev/null +++ b/tests/baselines/reference/structStaticFactory1.symbols @@ -0,0 +1,38 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structStaticFactory1.ts === +// doc 4.2 +// In a static member function, this represents the constructor function object on which +// the static member function was invoked. Thus, a call to ‘new this()’ may actually invoke +// a derived struct constructor. +// ok +struct Base { +>Base : Symbol(Base, Decl(structStaticFactory1.ts, 0, 0)) + + foo() { return 1; } +>foo : Symbol(foo, Decl(structStaticFactory1.ts, 5, 13)) + + static create() { +>create : Symbol(Base.create, Decl(structStaticFactory1.ts, 6, 23)) + + return new this(); +>this : Symbol(Base, Decl(structStaticFactory1.ts, 0, 0)) + } +} + +struct Derived extends Base { +>Derived : Symbol(Derived, Decl(structStaticFactory1.ts, 10, 1)) +>Base : Symbol(Base, Decl(structStaticFactory1.ts, 0, 0)) + + foo() { return 2; } +>foo : Symbol(foo, Decl(structStaticFactory1.ts, 12, 29)) +} +var d = Derived.create(); // new B() +>d : Symbol(d, Decl(structStaticFactory1.ts, 15, 3)) +>Derived.create : Symbol(Base.create, Decl(structStaticFactory1.ts, 6, 23)) +>Derived : Symbol(Derived, Decl(structStaticFactory1.ts, 10, 1)) +>create : Symbol(Base.create, Decl(structStaticFactory1.ts, 6, 23)) + +d.foo(); // 2 +>d.foo : Symbol(Base.foo, Decl(structStaticFactory1.ts, 5, 13)) +>d : Symbol(d, Decl(structStaticFactory1.ts, 15, 3)) +>foo : Symbol(Base.foo, Decl(structStaticFactory1.ts, 5, 13)) + diff --git a/tests/baselines/reference/structStaticFactory1.types b/tests/baselines/reference/structStaticFactory1.types new file mode 100644 index 0000000000000..6140b272a7c52 --- /dev/null +++ b/tests/baselines/reference/structStaticFactory1.types @@ -0,0 +1,43 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/structStaticFactory1.ts === +// doc 4.2 +// In a static member function, this represents the constructor function object on which +// the static member function was invoked. Thus, a call to ‘new this()’ may actually invoke +// a derived struct constructor. +// ok +struct Base { +>Base : Base + + foo() { return 1; } +>foo : () => number +>1 : number + + static create() { +>create : () => Base + + return new this(); +>new this() : Base +>this : typeof Base + } +} + +struct Derived extends Base { +>Derived : Derived +>Base : Base + + foo() { return 2; } +>foo : () => number +>2 : number +} +var d = Derived.create(); // new B() +>d : Base +>Derived.create() : Base +>Derived.create : () => Base +>Derived : typeof Derived +>create : () => Base + +d.foo(); // 2 +>d.foo() : number +>d.foo : () => number +>d : Base +>foo : () => number + diff --git a/tests/baselines/reference/structStaticMemberInitialization.js b/tests/baselines/reference/structStaticMemberInitialization.js new file mode 100644 index 0000000000000..12b9aa1d714e7 --- /dev/null +++ b/tests/baselines/reference/structStaticMemberInitialization.js @@ -0,0 +1,29 @@ +//// [structStaticMemberInitialization.ts] +// doc 4.1 +// ok + +struct C { + static x = 1; +} + +var c = new C(); +var r = C.x; + +//// [structStaticMemberInitialization.js] +// doc 4.1 +// ok +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var c = new C(); +var r = C.x; diff --git a/tests/baselines/reference/structStaticMemberInitialization.symbols b/tests/baselines/reference/structStaticMemberInitialization.symbols new file mode 100644 index 0000000000000..8db28ba4801e5 --- /dev/null +++ b/tests/baselines/reference/structStaticMemberInitialization.symbols @@ -0,0 +1,21 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structStaticMemberInitialization.ts === +// doc 4.1 +// ok + +struct C { +>C : Symbol(C, Decl(structStaticMemberInitialization.ts, 0, 0)) + + static x = 1; +>x : Symbol(C.x, Decl(structStaticMemberInitialization.ts, 3, 10)) +} + +var c = new C(); +>c : Symbol(c, Decl(structStaticMemberInitialization.ts, 7, 3)) +>C : Symbol(C, Decl(structStaticMemberInitialization.ts, 0, 0)) + +var r = C.x; +>r : Symbol(r, Decl(structStaticMemberInitialization.ts, 8, 3)) +>C.x : Symbol(C.x, Decl(structStaticMemberInitialization.ts, 3, 10)) +>C : Symbol(C, Decl(structStaticMemberInitialization.ts, 0, 0)) +>x : Symbol(C.x, Decl(structStaticMemberInitialization.ts, 3, 10)) + diff --git a/tests/baselines/reference/structStaticMemberInitialization.types b/tests/baselines/reference/structStaticMemberInitialization.types new file mode 100644 index 0000000000000..22d7a8acf204e --- /dev/null +++ b/tests/baselines/reference/structStaticMemberInitialization.types @@ -0,0 +1,23 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structStaticMemberInitialization.ts === +// doc 4.1 +// ok + +struct C { +>C : C + + static x = 1; +>x : number +>1 : number +} + +var c = new C(); +>c : C +>new C() : C +>C : typeof C + +var r = C.x; +>r : number +>C.x : number +>C : typeof C +>x : number + diff --git a/tests/baselines/reference/structStaticPropertyAndFunctionWithSameName.errors.txt b/tests/baselines/reference/structStaticPropertyAndFunctionWithSameName.errors.txt new file mode 100644 index 0000000000000..c656fac53fb36 --- /dev/null +++ b/tests/baselines/reference/structStaticPropertyAndFunctionWithSameName.errors.txt @@ -0,0 +1,23 @@ +tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticPropertyAndFunctionWithSameName.ts(7,12): error TS2300: Duplicate identifier 'f'. +tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticPropertyAndFunctionWithSameName.ts(8,12): error TS2300: Duplicate identifier 'f'. + + +==== tests/cases/conformance/structs/structPropertyMemberDeclarations/structStaticPropertyAndFunctionWithSameName.ts (2 errors) ==== + // doc 4 + // Static property member declarations declare properties in the constructor function type, + // and must specify names that are unique among all static property member declarations + // in the containing struct + + struct C { + static f: number; + ~ +!!! error TS2300: Duplicate identifier 'f'. + static f() {} // error + ~ +!!! error TS2300: Duplicate identifier 'f'. + } + + struct D { + f: number; + static f: number; + } \ No newline at end of file diff --git a/tests/baselines/reference/structStaticPropertyAndFunctionWithSameName.js b/tests/baselines/reference/structStaticPropertyAndFunctionWithSameName.js new file mode 100644 index 0000000000000..f568dc0136756 --- /dev/null +++ b/tests/baselines/reference/structStaticPropertyAndFunctionWithSameName.js @@ -0,0 +1,49 @@ +//// [structStaticPropertyAndFunctionWithSameName.ts] +// doc 4 +// Static property member declarations declare properties in the constructor function type, +// and must specify names that are unique among all static property member declarations +// in the containing struct + +struct C { + static f: number; + static f() {} // error +} + +struct D { + f: number; + static f: number; +} + +//// [structStaticPropertyAndFunctionWithSameName.js] +// doc 4 +// Static property member declarations declare properties in the constructor function type, +// and must specify names that are unique among all static property member declarations +// in the containing struct +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.f = function () { }; // error + return C; +})(); +var D = (function () { + var _D = new TypedObject.StructType({ + f: TypedObject.float64, + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); diff --git a/tests/baselines/reference/structThisInInstanceMemberInitializer.js b/tests/baselines/reference/structThisInInstanceMemberInitializer.js new file mode 100644 index 0000000000000..d646b9a2ac3a9 --- /dev/null +++ b/tests/baselines/reference/structThisInInstanceMemberInitializer.js @@ -0,0 +1,36 @@ +//// [structThisInInstanceMemberInitializer.ts] +// doc 4.1 +// In an initializer expression for an instance member variable, this is of the struct instance type. +// ok + +struct C { + x = this; +} + +/* struct D { + x = this; + y: T; +}*/ + +//// [structThisInInstanceMemberInitializer.js] +// doc 4.1 +// In an initializer expression for an instance member variable, this is of the struct instance type. +// ok +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.Object + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +/* struct D { + x = this; + y: T; +}*/ diff --git a/tests/baselines/reference/structThisInInstanceMemberInitializer.symbols b/tests/baselines/reference/structThisInInstanceMemberInitializer.symbols new file mode 100644 index 0000000000000..e6e8a4df6f59e --- /dev/null +++ b/tests/baselines/reference/structThisInInstanceMemberInitializer.symbols @@ -0,0 +1,17 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structThisInInstanceMemberInitializer.ts === +// doc 4.1 +// In an initializer expression for an instance member variable, this is of the struct instance type. +// ok + +struct C { +>C : Symbol(C, Decl(structThisInInstanceMemberInitializer.ts, 0, 0)) + + x = this; +>x : Symbol(x, Decl(structThisInInstanceMemberInitializer.ts, 4, 10)) +>this : Symbol(C, Decl(structThisInInstanceMemberInitializer.ts, 0, 0)) +} + +/* struct D { + x = this; + y: T; +}*/ diff --git a/tests/baselines/reference/structThisInInstanceMemberInitializer.types b/tests/baselines/reference/structThisInInstanceMemberInitializer.types new file mode 100644 index 0000000000000..08bfa9c3a5d75 --- /dev/null +++ b/tests/baselines/reference/structThisInInstanceMemberInitializer.types @@ -0,0 +1,17 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberVariableDeclarations/structThisInInstanceMemberInitializer.ts === +// doc 4.1 +// In an initializer expression for an instance member variable, this is of the struct instance type. +// ok + +struct C { +>C : C + + x = this; +>x : this +>this : this +} + +/* struct D { + x = this; + y: T; +}*/ diff --git a/tests/baselines/reference/structWithBaseStructButNoConstructor.errors.txt b/tests/baselines/reference/structWithBaseStructButNoConstructor.errors.txt new file mode 100644 index 0000000000000..41434702fbb7c --- /dev/null +++ b/tests/baselines/reference/structWithBaseStructButNoConstructor.errors.txt @@ -0,0 +1,48 @@ +tests/cases/conformance/structs/members/constructorFunctionTypes/structWithBaseStructButNoConstructor.ts(12,9): error TS2346: Supplied parameters do not match any signature of call target. + + +==== tests/cases/conformance/structs/members/constructorFunctionTypes/structWithBaseStructButNoConstructor.ts (1 errors) ==== + // doc 2.5 + + struct Base { + constructor(x: number) { } + } + + struct C extends Base { + foo: string; + } + + var r = C; + var c = new C(); // error + ~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + var c2 = new C(1); // ok + + /* struct Base2 { + constructor(x: T) { } + } + + struct D extends Base2 { + foo: U; + } + + var r2 = D; + var d = new D(); // error + var d2 = new D(1); // ok + + // specialized base struct + struct D2 extends Base2 { + foo: U; + } + + var r3 = D2; + var d3 = new D(); // error + var d4 = new D(1); // ok + + struct D3 extends Base2 { + foo: string; + } + + var r4 = D3; + var d5 = new D(); // error + var d6 = new D(1); // ok */ \ No newline at end of file diff --git a/tests/baselines/reference/structWithBaseStructButNoConstructor.js b/tests/baselines/reference/structWithBaseStructButNoConstructor.js new file mode 100644 index 0000000000000..ce7383a5d506c --- /dev/null +++ b/tests/baselines/reference/structWithBaseStructButNoConstructor.js @@ -0,0 +1,109 @@ +//// [structWithBaseStructButNoConstructor.ts] +// doc 2.5 + +struct Base { + constructor(x: number) { } +} + +struct C extends Base { + foo: string; +} + +var r = C; +var c = new C(); // error +var c2 = new C(1); // ok + +/* struct Base2 { + constructor(x: T) { } +} + +struct D extends Base2 { + foo: U; +} + +var r2 = D; +var d = new D(); // error +var d2 = new D(1); // ok + +// specialized base struct +struct D2 extends Base2 { + foo: U; +} + +var r3 = D2; +var d3 = new D(); // error +var d4 = new D(1); // ok + +struct D3 extends Base2 { + foo: string; +} + +var r4 = D3; +var d5 = new D(); // error +var d6 = new D(1); // ok */ + +//// [structWithBaseStructButNoConstructor.js] +// doc 2.5 +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + var _Base = new TypedObject.StructType({ + }); + function _ctor(x) { + } + function Base(x) { + var obj = new _Base(); + _ctor.call(obj ,); + return obj; + } + Base._TO = _Base; + return Base; +})(); +var C = (function () { + var _C = new TypedObject.StructType({ + foo: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var r = C; +var c = new C(); // error +var c2 = new C(1); // ok +/* struct Base2 { + constructor(x: T) { } +} + +struct D extends Base2 { + foo: U; +} + +var r2 = D; +var d = new D(); // error +var d2 = new D(1); // ok + +// specialized base struct +struct D2 extends Base2 { + foo: U; +} + +var r3 = D2; +var d3 = new D(); // error +var d4 = new D(1); // ok + +struct D3 extends Base2 { + foo: string; +} + +var r4 = D3; +var d5 = new D(); // error +var d6 = new D(1); // ok */ diff --git a/tests/baselines/reference/structWithConstructors.errors.txt b/tests/baselines/reference/structWithConstructors.errors.txt new file mode 100644 index 0000000000000..218f37ab57459 --- /dev/null +++ b/tests/baselines/reference/structWithConstructors.errors.txt @@ -0,0 +1,66 @@ +tests/cases/conformance/structs/members/constructorFunctionTypes/structWithConstructors.ts(11,13): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/structs/members/constructorFunctionTypes/structWithConstructors.ts(20,14): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/structs/members/constructorFunctionTypes/structWithConstructors.ts(26,13): error TS2346: Supplied parameters do not match any signature of call target. + + +==== tests/cases/conformance/structs/members/constructorFunctionTypes/structWithConstructors.ts (3 errors) ==== + // doc 2.5 + // If the struct contains a constructor declaration with overloads, a set of construct signatures with + // the parameter lists of the overloads, all having the same type parameters as the struct and returning + // the instance type of the struct. + + module NonGeneric { + struct C { + constructor(x: string) { } + } + + var c = new C(); // error + ~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + var c2 = new C(''); // ok + + struct C2 { + constructor(x: number); + constructor(x: string); + constructor(x: any) { } + } + + var c3 = new C2(); // error + ~~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + var c4 = new C2(''); // ok + var c5 = new C2(1); // ok + + struct D extends C2 { } + + var d = new D(); // error + ~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + var d2 = new D(1); // ok + var d3 = new D(''); // ok + } + + /* module Generics { + struct C { + constructor(x: T) { } + } + + var c = new C(); // error + var c2 = new C(''); // ok + + struct C2 { + constructor(x: T); + constructor(x: T, y: U); + constructor(x: any) { } + } + + var c3 = new C2(); // error + var c4 = new C2(''); // ok + var c5 = new C2(1, 2); // ok + + struct D extends C2 { } + + var d = new D(); // error + var d2 = new D(1); // ok + var d3 = new D(''); // ok + } */ \ No newline at end of file diff --git a/tests/baselines/reference/structWithConstructors.js b/tests/baselines/reference/structWithConstructors.js new file mode 100644 index 0000000000000..af36b3ebc67fc --- /dev/null +++ b/tests/baselines/reference/structWithConstructors.js @@ -0,0 +1,140 @@ +//// [structWithConstructors.ts] +// doc 2.5 +// If the struct contains a constructor declaration with overloads, a set of construct signatures with +// the parameter lists of the overloads, all having the same type parameters as the struct and returning +// the instance type of the struct. + +module NonGeneric { + struct C { + constructor(x: string) { } + } + + var c = new C(); // error + var c2 = new C(''); // ok + + struct C2 { + constructor(x: number); + constructor(x: string); + constructor(x: any) { } + } + + var c3 = new C2(); // error + var c4 = new C2(''); // ok + var c5 = new C2(1); // ok + + struct D extends C2 { } + + var d = new D(); // error + var d2 = new D(1); // ok + var d3 = new D(''); // ok +} + +/* module Generics { + struct C { + constructor(x: T) { } + } + + var c = new C(); // error + var c2 = new C(''); // ok + + struct C2 { + constructor(x: T); + constructor(x: T, y: U); + constructor(x: any) { } + } + + var c3 = new C2(); // error + var c4 = new C2(''); // ok + var c5 = new C2(1, 2); // ok + + struct D extends C2 { } + + var d = new D(); // error + var d2 = new D(1); // ok + var d3 = new D(''); // ok +} */ + +//// [structWithConstructors.js] +// doc 2.5 +// If the struct contains a constructor declaration with overloads, a set of construct signatures with +// the parameter lists of the overloads, all having the same type parameters as the struct and returning +// the instance type of the struct. +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var NonGeneric; +(function (NonGeneric) { + var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor(x) { + } + function C(x) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + return C; + })(); + var c = new C(); // error + var c2 = new C(''); // ok + var C2 = (function () { + var _C2 = new TypedObject.StructType({ + }); + function _ctor(x) { + } + function C2(x) { + var obj = new _C2(); + _ctor.call(obj ,); + return obj; + } + C2._TO = _C2; + return C2; + })(); + var c3 = new C2(); // error + var c4 = new C2(''); // ok + var c5 = new C2(1); // ok + var D = (function () { + var _D = new TypedObject.StructType({ + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; + })(); + var d = new D(); // error + var d2 = new D(1); // ok + var d3 = new D(''); // ok +})(NonGeneric || (NonGeneric = {})); +/* module Generics { + struct C { + constructor(x: T) { } + } + + var c = new C(); // error + var c2 = new C(''); // ok + + struct C2 { + constructor(x: T); + constructor(x: T, y: U); + constructor(x: any) { } + } + + var c3 = new C2(); // error + var c4 = new C2(''); // ok + var c5 = new C2(1, 2); // ok + + struct D extends C2 { } + + var d = new D(); // error + var d2 = new D(1); // ok + var d3 = new D(''); // ok +} */ diff --git a/tests/baselines/reference/structWithNoConstructorOrBaseStruct.js b/tests/baselines/reference/structWithNoConstructorOrBaseStruct.js new file mode 100644 index 0000000000000..4d7b1b81d33d6 --- /dev/null +++ b/tests/baselines/reference/structWithNoConstructorOrBaseStruct.js @@ -0,0 +1,54 @@ +//// [structWithNoConstructorOrBaseStruct.ts] +// doc 2.5 +// If the struct contains no constructor declaration and has no base struct, a single construct signature +// with no parameters, having the same type parameters as the struct and returning the instance type of +// the struct. +// ok + +struct C { + x: string; +} + +var c = new C(); +var r = C; + +/* struct D { + x: T; + y: U; +} + +var d = new D(); +var d2 = new D(); +var r2 = D; */ + + +//// [structWithNoConstructorOrBaseStruct.js] +// doc 2.5 +// If the struct contains no constructor declaration and has no base struct, a single construct signature +// with no parameters, having the same type parameters as the struct and returning the instance type of +// the struct. +// ok +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var c = new C(); +var r = C; +/* struct D { + x: T; + y: U; +} + +var d = new D(); +var d2 = new D(); +var r2 = D; */ diff --git a/tests/baselines/reference/structWithNoConstructorOrBaseStruct.symbols b/tests/baselines/reference/structWithNoConstructorOrBaseStruct.symbols new file mode 100644 index 0000000000000..1fab3f92ddeae --- /dev/null +++ b/tests/baselines/reference/structWithNoConstructorOrBaseStruct.symbols @@ -0,0 +1,31 @@ +=== tests/cases/conformance/structs/members/constructorFunctionTypes/structWithNoConstructorOrBaseStruct.ts === +// doc 2.5 +// If the struct contains no constructor declaration and has no base struct, a single construct signature +// with no parameters, having the same type parameters as the struct and returning the instance type of +// the struct. +// ok + +struct C { +>C : Symbol(C, Decl(structWithNoConstructorOrBaseStruct.ts, 0, 0)) + + x: string; +>x : Symbol(x, Decl(structWithNoConstructorOrBaseStruct.ts, 6, 10)) +} + +var c = new C(); +>c : Symbol(c, Decl(structWithNoConstructorOrBaseStruct.ts, 10, 3)) +>C : Symbol(C, Decl(structWithNoConstructorOrBaseStruct.ts, 0, 0)) + +var r = C; +>r : Symbol(r, Decl(structWithNoConstructorOrBaseStruct.ts, 11, 3)) +>C : Symbol(C, Decl(structWithNoConstructorOrBaseStruct.ts, 0, 0)) + +/* struct D { + x: T; + y: U; +} + +var d = new D(); +var d2 = new D(); +var r2 = D; */ + diff --git a/tests/baselines/reference/structWithNoConstructorOrBaseStruct.types b/tests/baselines/reference/structWithNoConstructorOrBaseStruct.types new file mode 100644 index 0000000000000..7afe60262747e --- /dev/null +++ b/tests/baselines/reference/structWithNoConstructorOrBaseStruct.types @@ -0,0 +1,32 @@ +=== tests/cases/conformance/structs/members/constructorFunctionTypes/structWithNoConstructorOrBaseStruct.ts === +// doc 2.5 +// If the struct contains no constructor declaration and has no base struct, a single construct signature +// with no parameters, having the same type parameters as the struct and returning the instance type of +// the struct. +// ok + +struct C { +>C : C + + x: string; +>x : string +} + +var c = new C(); +>c : C +>new C() : C +>C : typeof C + +var r = C; +>r : typeof C +>C : typeof C + +/* struct D { + x: T; + y: U; +} + +var d = new D(); +var d2 = new D(); +var r2 = D; */ + diff --git a/tests/baselines/reference/structWithStaticMembers.js b/tests/baselines/reference/structWithStaticMembers.js new file mode 100644 index 0000000000000..46e63bde06812 --- /dev/null +++ b/tests/baselines/reference/structWithStaticMembers.js @@ -0,0 +1,62 @@ +//// [structWithStaticMembers.ts] +// doc 2.5 +// ok + +struct C { + static fn() { return this; } + constructor(public a: number, private b: number) { } + static foo: string; +} + +var r = C.fn(); +var r2 = r.foo; + +struct D extends C { + bar: string; +} + +var r = D.fn(); +var r2 = r.foo; + +//// [structWithStaticMembers.js] +// doc 2.5 +// ok +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor(a, b) { + this.a = a; + this.b = b; + } + function C(a, b) { + var obj = new _C(); + _ctor.call(obj ,); + return obj; + } + C._TO = _C; + _C.fn = function () { return this; }; + return C; +})(); +var r = C.fn(); +var r2 = r.foo; +var D = (function () { + var _D = new TypedObject.StructType({ + bar: TypedObject.string + }); + function _ctor() { + } + function D() { + var obj = new _D(); + _ctor.call(obj); + return obj; + } + D._TO = _D; + return D; +})(); +var r = D.fn(); +var r2 = r.foo; diff --git a/tests/baselines/reference/structWithStaticMembers.symbols b/tests/baselines/reference/structWithStaticMembers.symbols new file mode 100644 index 0000000000000..517e6f146087b --- /dev/null +++ b/tests/baselines/reference/structWithStaticMembers.symbols @@ -0,0 +1,51 @@ +=== tests/cases/conformance/structs/members/constructorFunctionTypes/structWithStaticMembers.ts === +// doc 2.5 +// ok + +struct C { +>C : Symbol(C, Decl(structWithStaticMembers.ts, 0, 0)) + + static fn() { return this; } +>fn : Symbol(C.fn, Decl(structWithStaticMembers.ts, 3, 10)) +>this : Symbol(C, Decl(structWithStaticMembers.ts, 0, 0)) + + constructor(public a: number, private b: number) { } +>a : Symbol(a, Decl(structWithStaticMembers.ts, 5, 16)) +>b : Symbol(b, Decl(structWithStaticMembers.ts, 5, 33)) + + static foo: string; +>foo : Symbol(C.foo, Decl(structWithStaticMembers.ts, 5, 56)) +} + +var r = C.fn(); +>r : Symbol(r, Decl(structWithStaticMembers.ts, 9, 3), Decl(structWithStaticMembers.ts, 16, 3)) +>C.fn : Symbol(C.fn, Decl(structWithStaticMembers.ts, 3, 10)) +>C : Symbol(C, Decl(structWithStaticMembers.ts, 0, 0)) +>fn : Symbol(C.fn, Decl(structWithStaticMembers.ts, 3, 10)) + +var r2 = r.foo; +>r2 : Symbol(r2, Decl(structWithStaticMembers.ts, 10, 3), Decl(structWithStaticMembers.ts, 17, 3)) +>r.foo : Symbol(C.foo, Decl(structWithStaticMembers.ts, 5, 56)) +>r : Symbol(r, Decl(structWithStaticMembers.ts, 9, 3), Decl(structWithStaticMembers.ts, 16, 3)) +>foo : Symbol(C.foo, Decl(structWithStaticMembers.ts, 5, 56)) + +struct D extends C { +>D : Symbol(D, Decl(structWithStaticMembers.ts, 10, 15)) +>C : Symbol(C, Decl(structWithStaticMembers.ts, 0, 0)) + + bar: string; +>bar : Symbol(bar, Decl(structWithStaticMembers.ts, 12, 20)) +} + +var r = D.fn(); +>r : Symbol(r, Decl(structWithStaticMembers.ts, 9, 3), Decl(structWithStaticMembers.ts, 16, 3)) +>D.fn : Symbol(C.fn, Decl(structWithStaticMembers.ts, 3, 10)) +>D : Symbol(D, Decl(structWithStaticMembers.ts, 10, 15)) +>fn : Symbol(C.fn, Decl(structWithStaticMembers.ts, 3, 10)) + +var r2 = r.foo; +>r2 : Symbol(r2, Decl(structWithStaticMembers.ts, 10, 3), Decl(structWithStaticMembers.ts, 17, 3)) +>r.foo : Symbol(C.foo, Decl(structWithStaticMembers.ts, 5, 56)) +>r : Symbol(r, Decl(structWithStaticMembers.ts, 9, 3), Decl(structWithStaticMembers.ts, 16, 3)) +>foo : Symbol(C.foo, Decl(structWithStaticMembers.ts, 5, 56)) + diff --git a/tests/baselines/reference/structWithStaticMembers.types b/tests/baselines/reference/structWithStaticMembers.types new file mode 100644 index 0000000000000..9d9c71b9f6217 --- /dev/null +++ b/tests/baselines/reference/structWithStaticMembers.types @@ -0,0 +1,53 @@ +=== tests/cases/conformance/structs/members/constructorFunctionTypes/structWithStaticMembers.ts === +// doc 2.5 +// ok + +struct C { +>C : C + + static fn() { return this; } +>fn : () => typeof C +>this : typeof C + + constructor(public a: number, private b: number) { } +>a : number +>b : number + + static foo: string; +>foo : string +} + +var r = C.fn(); +>r : typeof C +>C.fn() : typeof C +>C.fn : () => typeof C +>C : typeof C +>fn : () => typeof C + +var r2 = r.foo; +>r2 : string +>r.foo : string +>r : typeof C +>foo : string + +struct D extends C { +>D : D +>C : C + + bar: string; +>bar : string +} + +var r = D.fn(); +>r : typeof C +>D.fn() : typeof C +>D.fn : () => typeof C +>D : typeof D +>fn : () => typeof C + +var r2 = r.foo; +>r2 : string +>r.foo : string +>r : typeof C +>foo : string + diff --git a/tests/baselines/reference/structWithTwoConstructorDefinitions.errors.txt b/tests/baselines/reference/structWithTwoConstructorDefinitions.errors.txt new file mode 100644 index 0000000000000..3f7e05cfa7d93 --- /dev/null +++ b/tests/baselines/reference/structWithTwoConstructorDefinitions.errors.txt @@ -0,0 +1,21 @@ +tests/cases/conformance/structs/constructorDeclarations/structWithTwoConstructorDefinitions.ts(5,5): error TS2392: Multiple constructor implementations are not allowed. +tests/cases/conformance/structs/constructorDeclarations/structWithTwoConstructorDefinitions.ts(6,5): error TS2392: Multiple constructor implementations are not allowed. + + +==== tests/cases/conformance/structs/constructorDeclarations/structWithTwoConstructorDefinitions.ts (2 errors) ==== + // doc 3 + // Multiple constructor implementations are not allowed + + struct C { + constructor() { } // error + ~~~~~~~~~~~~~~~~~ +!!! error TS2392: Multiple constructor implementations are not allowed. + constructor(x) { } // error + ~~~~~~~~~~~~~~~~~~ +!!! error TS2392: Multiple constructor implementations are not allowed. + } + + /* struct D { + constructor(x: T) { } // error + constructor(x: T, y: T) { } // error + } */ \ No newline at end of file diff --git a/tests/baselines/reference/structWithTwoConstructorDefinitions.js b/tests/baselines/reference/structWithTwoConstructorDefinitions.js new file mode 100644 index 0000000000000..9b215a1b97052 --- /dev/null +++ b/tests/baselines/reference/structWithTwoConstructorDefinitions.js @@ -0,0 +1,34 @@ +//// [structWithTwoConstructorDefinitions.ts] +// doc 3 +// Multiple constructor implementations are not allowed + +struct C { + constructor() { } // error + constructor(x) { } // error +} + +/* struct D { + constructor(x: T) { } // error + constructor(x: T, y: T) { } // error +} */ + +//// [structWithTwoConstructorDefinitions.js] +// doc 3 +// Multiple constructor implementations are not allowed +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } // error + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +/* struct D { + constructor(x: T) { } // error + constructor(x: T, y: T) { } // error +} */ diff --git a/tests/baselines/reference/structWithoutExplicitConstructor.errors.txt b/tests/baselines/reference/structWithoutExplicitConstructor.errors.txt new file mode 100644 index 0000000000000..ff9e3467c92bb --- /dev/null +++ b/tests/baselines/reference/structWithoutExplicitConstructor.errors.txt @@ -0,0 +1,25 @@ +tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/structWithoutExplicitConstructor.ts(10,10): error TS2346: Supplied parameters do not match any signature of call target. + + +==== tests/cases/conformance/structs/constructorDeclarations/automaticConstructors/structWithoutExplicitConstructor.ts (1 errors) ==== + // doc 3.3 + // If a struct omits a constructor declaration, an automatic constructor is provided. + + struct C { + x = 1 + y = 'hello'; + } + + var c = new C(); + var c2 = new C(null); // error, Supplied parameters do not match any signature of call target + ~~~~~~~~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + + /* struct D { + x = 2 + y: T = null; + } + + var d = new D(); + var d2 = new D(null); // error, Supplied parameters do not match any signature of call target + */ \ No newline at end of file diff --git a/tests/baselines/reference/structWithoutExplicitConstructor.js b/tests/baselines/reference/structWithoutExplicitConstructor.js new file mode 100644 index 0000000000000..9bf5f0542e5da --- /dev/null +++ b/tests/baselines/reference/structWithoutExplicitConstructor.js @@ -0,0 +1,49 @@ +//// [structWithoutExplicitConstructor.ts] +// doc 3.3 +// If a struct omits a constructor declaration, an automatic constructor is provided. + +struct C { + x = 1 + y = 'hello'; +} + +var c = new C(); +var c2 = new C(null); // error, Supplied parameters do not match any signature of call target + +/* struct D { + x = 2 + y: T = null; +} + +var d = new D(); +var d2 = new D(null); // error, Supplied parameters do not match any signature of call target + */ + +//// [structWithoutExplicitConstructor.js] +// doc 3.3 +// If a struct omits a constructor declaration, an automatic constructor is provided. +var C = (function () { + var _C = new TypedObject.StructType({ + x: TypedObject.float64, + y: TypedObject.string + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +var c = new C(); +var c2 = new C(null); // error, Supplied parameters do not match any signature of call target +/* struct D { + x = 2 + y: T = null; +} + +var d = new D(); +var d2 = new D(null); // error, Supplied parameters do not match any signature of call target + */ diff --git a/tests/baselines/reference/superCallInStructConstructorWithNoBaseType.errors.txt b/tests/baselines/reference/superCallInStructConstructorWithNoBaseType.errors.txt new file mode 100644 index 0000000000000..8ad7476d87033 --- /dev/null +++ b/tests/baselines/reference/superCallInStructConstructorWithNoBaseType.errors.txt @@ -0,0 +1,20 @@ +tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/superCallInStructConstructorWithNoBaseType.ts(6,9): error TS20016: 'super' can only be referenced in a derived struct. + + +==== tests/cases/conformance/structs/constructorDeclarations/structSuperCalls/superCallInStructConstructorWithNoBaseType.ts (1 errors) ==== + // doc 3.2 + // 'super' can only be referenced in a derived struct + + struct C { + constructor() { + super(); // error + ~~~~~ +!!! error TS20016: 'super' can only be referenced in a derived struct. + } + } + + /* struct D { + public constructor(public x: T) { + super(); // error + } + } */ \ No newline at end of file diff --git a/tests/baselines/reference/superCallInStructConstructorWithNoBaseType.js b/tests/baselines/reference/superCallInStructConstructorWithNoBaseType.js new file mode 100644 index 0000000000000..d7bbc2a70a084 --- /dev/null +++ b/tests/baselines/reference/superCallInStructConstructorWithNoBaseType.js @@ -0,0 +1,38 @@ +//// [superCallInStructConstructorWithNoBaseType.ts] +// doc 3.2 +// 'super' can only be referenced in a derived struct + +struct C { + constructor() { + super(); // error + } +} + +/* struct D { + public constructor(public x: T) { + super(); // error + } +} */ + +//// [superCallInStructConstructorWithNoBaseType.js] +// doc 3.2 +// 'super' can only be referenced in a derived struct +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + _super.call(this); // error + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + return C; +})(); +/* struct D { + public constructor(public x: T) { + super(); // error + } +} */ diff --git a/tests/baselines/reference/typeOfThisInStructMemberFunctions.js b/tests/baselines/reference/typeOfThisInStructMemberFunctions.js new file mode 100644 index 0000000000000..245bd20155d6e --- /dev/null +++ b/tests/baselines/reference/typeOfThisInStructMemberFunctions.js @@ -0,0 +1,82 @@ +//// [typeOfThisInStructMemberFunctions.ts] +// doc 4.2 +// In the body of a static member function declaration, the type of this is the constructor +// function type. +// ok +struct C { + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} + +/* struct D { + x: T; + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} + +struct E { + x: T; + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} */ + +//// [typeOfThisInStructMemberFunctions.js] +// doc 4.2 +// In the body of a static member function declaration, the type of this is the constructor +// function type. +// ok +var C = (function () { + var _C = new TypedObject.StructType({ + }); + function _ctor() { + } + function C() { + var obj = new _C(); + _ctor.call(obj); + return obj; + } + C._TO = _C; + _C.prototype.foo = function () { + var r = this; + }; + _C.bar = function () { + var r2 = this; + }; + return C; +})(); +/* struct D { + x: T; + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} + +struct E { + x: T; + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} */ diff --git a/tests/baselines/reference/typeOfThisInStructMemberFunctions.symbols b/tests/baselines/reference/typeOfThisInStructMemberFunctions.symbols new file mode 100644 index 0000000000000..15a80e0b68e0a --- /dev/null +++ b/tests/baselines/reference/typeOfThisInStructMemberFunctions.symbols @@ -0,0 +1,46 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/typeOfThisInStructMemberFunctions.ts === +// doc 4.2 +// In the body of a static member function declaration, the type of this is the constructor +// function type. +// ok +struct C { +>C : Symbol(C, Decl(typeOfThisInStructMemberFunctions.ts, 0, 0)) + + foo() { +>foo : Symbol(foo, Decl(typeOfThisInStructMemberFunctions.ts, 4, 10)) + + var r = this; +>r : Symbol(r, Decl(typeOfThisInStructMemberFunctions.ts, 6, 11)) +>this : Symbol(C, Decl(typeOfThisInStructMemberFunctions.ts, 0, 0)) + } + + static bar() { +>bar : Symbol(C.bar, Decl(typeOfThisInStructMemberFunctions.ts, 7, 5)) + + var r2 = this; +>r2 : Symbol(r2, Decl(typeOfThisInStructMemberFunctions.ts, 10, 11)) +>this : Symbol(C, Decl(typeOfThisInStructMemberFunctions.ts, 0, 0)) + } +} + +/* struct D { + x: T; + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} + +struct E { + x: T; + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} */ diff --git a/tests/baselines/reference/typeOfThisInStructMemberFunctions.types b/tests/baselines/reference/typeOfThisInStructMemberFunctions.types new file mode 100644 index 0000000000000..8829631e94f93 --- /dev/null +++ b/tests/baselines/reference/typeOfThisInStructMemberFunctions.types @@ -0,0 +1,46 @@ +=== tests/cases/conformance/structs/structPropertyMemberDeclarations/structMemberFunctionDeclarations/typeOfThisInStructMemberFunctions.ts === +// doc 4.2 +// In the body of a static member function declaration, the type of this is the constructor +// function type. +// ok +struct C { +>C : C + + foo() { +>foo : () => void + + var r = this; +>r : this +>this : this + } + + static bar() { +>bar : () => void + + var r2 = this; +>r2 : typeof C +>this : typeof C + } +} + +/* struct D { + x: T; + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} + +struct E { + x: T; + foo() { + var r = this; + } + + static bar() { + var r2 = this; + } +} */