From 98b6db81d9fade9f06eb903d591766dfd8ed2025 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 9 Aug 2019 16:11:25 -0700 Subject: [PATCH] Allow accessors in ambient class declarations (#32787) * Allow accessors in ambient class declarations * Update src/compiler/transformers/declarations.ts Co-Authored-By: Wesley Wigham --- src/compiler/checker.ts | 57 ++++++------- src/compiler/diagnosticMessages.json | 4 - src/compiler/transformers/declarations.ts | 79 +++++++++++++++++-- src/compiler/utilities.ts | 4 +- src/services/codefixes/helpers.ts | 52 +++++++++--- .../accessorsInAmbientContext.errors.txt | 48 +++++------ .../reference/ambientAccessors(target=es3).js | 30 +++++++ .../ambientAccessors(target=es3).symbols | 33 ++++++++ .../ambientAccessors(target=es3).types | 33 ++++++++ .../reference/ambientAccessors(target=es5).js | 30 +++++++ .../ambientAccessors(target=es5).symbols | 33 ++++++++ .../ambientAccessors(target=es5).types | 33 ++++++++ .../reference/ambientGetters.errors.txt | 11 +-- tests/baselines/reference/giant.errors.txt | 56 +------------ tests/baselines/reference/giant.js | 24 +++--- .../reference/parserAccessors5.errors.txt | 6 +- .../reference/parserAccessors6.errors.txt | 6 +- .../parserComputedPropertyName23.errors.txt | 5 +- .../ambientAccessors.ts | 16 ++++ ...codeFixAmbientClassExtendAbstractMethod.ts | 31 ++++++++ ...FixAmbientClassExtendAbstractMethod_all.ts | 26 ++++++ ...ImplementClassAbstractGettersAndSetters.ts | 32 ++++++++ ...entClassImplementClassMethodViaHeritage.ts | 16 ++++ .../codeFixClassExtendAbstractGetterSetter.ts | 31 ++++++-- ...ImplementClassAbstractGettersAndSetters.ts | 15 +++- 25 files changed, 537 insertions(+), 174 deletions(-) create mode 100644 tests/baselines/reference/ambientAccessors(target=es3).js create mode 100644 tests/baselines/reference/ambientAccessors(target=es3).symbols create mode 100644 tests/baselines/reference/ambientAccessors(target=es3).types create mode 100644 tests/baselines/reference/ambientAccessors(target=es5).js create mode 100644 tests/baselines/reference/ambientAccessors(target=es5).symbols create mode 100644 tests/baselines/reference/ambientAccessors(target=es5).types create mode 100644 tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/ambientAccessors.ts create mode 100644 tests/cases/fourslash/codeFixAmbientClassExtendAbstractMethod.ts create mode 100644 tests/cases/fourslash/codeFixAmbientClassExtendAbstractMethod_all.ts create mode 100644 tests/cases/fourslash/codeFixAmbientClassImplementClassAbstractGettersAndSetters.ts create mode 100644 tests/cases/fourslash/codeFixAmbientClassImplementClassMethodViaHeritage.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 464c97d551..65f550cb66 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5938,7 +5938,9 @@ namespace ts { // Otherwise, fall back to 'any'. else { if (setter) { - errorOrSuggestion(noImplicitAny, setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol)); + if (!isPrivateWithinAmbient(setter)) { + errorOrSuggestion(noImplicitAny, setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol)); + } } else { Debug.assert(!!getter, "there must existed getter as we are current checking either setter or getter in this function"); @@ -33059,43 +33061,39 @@ namespace ts { } function checkGrammarAccessor(accessor: AccessorDeclaration): boolean { - const kind = accessor.kind; - if (languageVersion < ScriptTarget.ES5) { - return grammarErrorOnNode(accessor.name, Diagnostics.Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher); + if (!(accessor.flags & NodeFlags.Ambient)) { + if (languageVersion < ScriptTarget.ES5) { + return grammarErrorOnNode(accessor.name, Diagnostics.Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher); + } + if (accessor.body === undefined && !hasModifier(accessor, ModifierFlags.Abstract)) { + return grammarErrorAtPos(accessor, accessor.end - 1, ";".length, Diagnostics._0_expected, "{"); + } } - else if (accessor.flags & NodeFlags.Ambient) { - return grammarErrorOnNode(accessor.name, Diagnostics.An_accessor_cannot_be_declared_in_an_ambient_context); - } - else if (accessor.body === undefined && !hasModifier(accessor, ModifierFlags.Abstract)) { - return grammarErrorAtPos(accessor, accessor.end - 1, ";".length, Diagnostics._0_expected, "{"); - } - else if (accessor.body && hasModifier(accessor, ModifierFlags.Abstract)) { + if (accessor.body && hasModifier(accessor, ModifierFlags.Abstract)) { return grammarErrorOnNode(accessor, Diagnostics.An_abstract_accessor_cannot_have_an_implementation); } - else if (accessor.typeParameters) { + if (accessor.typeParameters) { return grammarErrorOnNode(accessor.name, Diagnostics.An_accessor_cannot_have_type_parameters); } - else if (!doesAccessorHaveCorrectParameterCount(accessor)) { + if (!doesAccessorHaveCorrectParameterCount(accessor)) { return grammarErrorOnNode(accessor.name, - kind === SyntaxKind.GetAccessor ? + accessor.kind === SyntaxKind.GetAccessor ? Diagnostics.A_get_accessor_cannot_have_parameters : Diagnostics.A_set_accessor_must_have_exactly_one_parameter); } - else if (kind === SyntaxKind.SetAccessor) { + if (accessor.kind === SyntaxKind.SetAccessor) { if (accessor.type) { return grammarErrorOnNode(accessor.name, Diagnostics.A_set_accessor_cannot_have_a_return_type_annotation); } - else { - const parameter = accessor.parameters[0]; - if (parameter.dotDotDotToken) { - return grammarErrorOnNode(parameter.dotDotDotToken, Diagnostics.A_set_accessor_cannot_have_rest_parameter); - } - else if (parameter.questionToken) { - return grammarErrorOnNode(parameter.questionToken, Diagnostics.A_set_accessor_cannot_have_an_optional_parameter); - } - else if (parameter.initializer) { - return grammarErrorOnNode(accessor.name, Diagnostics.A_set_accessor_parameter_cannot_have_an_initializer); - } + const parameter = Debug.assertDefined(getSetAccessorValueParameter(accessor), "Return value does not match parameter count assertion."); + if (parameter.dotDotDotToken) { + return grammarErrorOnNode(parameter.dotDotDotToken, Diagnostics.A_set_accessor_cannot_have_rest_parameter); + } + if (parameter.questionToken) { + return grammarErrorOnNode(parameter.questionToken, Diagnostics.A_set_accessor_cannot_have_an_optional_parameter); + } + if (parameter.initializer) { + return grammarErrorOnNode(accessor.name, Diagnostics.A_set_accessor_parameter_cannot_have_an_initializer); } } return false; @@ -33583,14 +33581,9 @@ namespace ts { function checkGrammarStatementInAmbientContext(node: Node): boolean { if (node.flags & NodeFlags.Ambient) { - // An accessors is already reported about the ambient context - if (isAccessor(node.parent)) { - return getNodeLinks(node).hasReportedStatementInAmbientContext = true; - } - // Find containing block which is either Block, ModuleBlock, SourceFile const links = getNodeLinks(node); - if (!links.hasReportedStatementInAmbientContext && isFunctionLike(node.parent)) { + if (!links.hasReportedStatementInAmbientContext && (isFunctionLike(node.parent) || isAccessor(node.parent))) { return getNodeLinks(node).hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, Diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts); } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 4528d504d0..5cca456dd4 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -243,10 +243,6 @@ "category": "Error", "code": 1085 }, - "An accessor cannot be declared in an ambient context.": { - "category": "Error", - "code": 1086 - }, "'{0}' modifier cannot appear on a constructor declaration.": { "category": "Error", "code": 1089 diff --git a/src/compiler/transformers/declarations.ts b/src/compiler/transformers/declarations.ts index 6dfc909b93..2eaab8c069 100644 --- a/src/compiler/transformers/declarations.ts +++ b/src/compiler/transformers/declarations.ts @@ -393,7 +393,7 @@ namespace ts { } } - function ensureParameter(p: ParameterDeclaration, modifierMask?: ModifierFlags): ParameterDeclaration { + function ensureParameter(p: ParameterDeclaration, modifierMask?: ModifierFlags, type?: TypeNode): ParameterDeclaration { let oldDiag: typeof getSymbolAccessibilityDiagnostic | undefined; if (!suppressNewDiagnosticContexts) { oldDiag = getSymbolAccessibilityDiagnostic; @@ -406,7 +406,7 @@ namespace ts { p.dotDotDotToken, filterBindingPatternInitializers(p.name), resolver.isOptionalParameter(p) ? (p.questionToken || createToken(SyntaxKind.QuestionToken)) : undefined, - ensureType(p, p.type, /*ignorePrivate*/ true), // Ignore private param props, since this type is going straight back into a param + ensureType(p, type || p.type, /*ignorePrivate*/ true), // Ignore private param props, since this type is going straight back into a param ensureNoInitializer(p) ); if (!suppressNewDiagnosticContexts) { @@ -535,6 +535,36 @@ namespace ts { return createNodeArray(newParams, params.hasTrailingComma); } + function updateAccessorParamsList(input: AccessorDeclaration, isPrivate: boolean) { + let newParams: ParameterDeclaration[] | undefined; + if (!isPrivate) { + const thisParameter = getThisParameter(input); + if (thisParameter) { + newParams = [ensureParameter(thisParameter)]; + } + } + if (isSetAccessorDeclaration(input)) { + let newValueParameter: ParameterDeclaration | undefined; + if (!isPrivate) { + const valueParameter = getSetAccessorValueParameter(input); + if (valueParameter) { + const accessorType = getTypeAnnotationFromAllAccessorDeclarations(input, resolver.getAllAccessorDeclarations(input)); + newValueParameter = ensureParameter(valueParameter, /*modifierMask*/ undefined, accessorType); + } + } + if (!newValueParameter) { + newValueParameter = createParameter( + /*decorators*/ undefined, + /*modifiers*/ undefined, + /*dotDotDotToken*/ undefined, + "value" + ); + } + newParams = append(newParams, newValueParameter); + } + return createNodeArray(newParams || emptyArray) as NodeArray; + } + function ensureTypeParams(node: Node, params: NodeArray | undefined) { return hasModifier(node, ModifierFlags.Private) ? undefined : visitNodes(params, visitDeclarationSubtree); } @@ -811,10 +841,33 @@ namespace ts { return cleanup(sig); } case SyntaxKind.GetAccessor: { + // For now, only emit class accessors as accessors if they were already declared in an ambient context. + if (input.flags & NodeFlags.Ambient) { + const isPrivate = hasModifier(input, ModifierFlags.Private); + const accessorType = getTypeAnnotationFromAllAccessorDeclarations(input, resolver.getAllAccessorDeclarations(input)); + return cleanup(updateGetAccessor( + input, + /*decorators*/ undefined, + ensureModifiers(input), + input.name, + updateAccessorParamsList(input, isPrivate), + !isPrivate ? ensureType(input, accessorType) : undefined, + /*body*/ undefined)); + } const newNode = ensureAccessor(input); return cleanup(newNode); } case SyntaxKind.SetAccessor: { + // For now, only emit class accessors as accessors if they were already declared in an ambient context. + if (input.flags & NodeFlags.Ambient) { + return cleanup(updateSetAccessor( + input, + /*decorators*/ undefined, + ensureModifiers(input), + input.name, + updateAccessorParamsList(input, hasModifier(input, ModifierFlags.Private)), + /*body*/ undefined)); + } const newNode = ensureAccessor(input); return cleanup(newNode); } @@ -1374,17 +1427,27 @@ namespace ts { return maskModifierFlags(node, mask, additions); } + function getTypeAnnotationFromAllAccessorDeclarations(node: AccessorDeclaration, accessors: AllAccessorDeclarations) { + let accessorType = getTypeAnnotationFromAccessor(node); + if (!accessorType && node !== accessors.firstAccessor) { + accessorType = getTypeAnnotationFromAccessor(accessors.firstAccessor); + // If we end up pulling the type from the second accessor, we also need to change the diagnostic context to get the expected error message + getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(accessors.firstAccessor); + } + if (!accessorType && accessors.secondAccessor && node !== accessors.secondAccessor) { + accessorType = getTypeAnnotationFromAccessor(accessors.secondAccessor); + // If we end up pulling the type from the second accessor, we also need to change the diagnostic context to get the expected error message + getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(accessors.secondAccessor); + } + return accessorType; + } + function ensureAccessor(node: AccessorDeclaration): PropertyDeclaration | undefined { const accessors = resolver.getAllAccessorDeclarations(node); if (node.kind !== accessors.firstAccessor.kind) { return; } - let accessorType = getTypeAnnotationFromAccessor(node); - if (!accessorType && accessors.secondAccessor) { - accessorType = getTypeAnnotationFromAccessor(accessors.secondAccessor); - // If we end up pulling the type from the second accessor, we also need to change the diagnostic context to get the expected error message - getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(accessors.secondAccessor); - } + const accessorType = getTypeAnnotationFromAllAccessorDeclarations(node, accessors); const prop = createProperty(/*decorators*/ undefined, maskModifiers(node, /*mask*/ undefined, (!accessors.setAccessor) ? ModifierFlags.Readonly : ModifierFlags.None), node.name, node.questionToken, ensureType(node, accessorType), /*initializer*/ undefined); const leadingsSyntheticCommentRanges = accessors.secondAccessor && getLeadingCommentRangesOfNode(accessors.secondAccessor, currentSourceFile); if (leadingsSyntheticCommentRanges) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 4ca13e663a..02ca57919c 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3518,7 +3518,7 @@ namespace ts { return find(node.members, (member): member is ConstructorDeclaration & { body: FunctionBody } => isConstructorDeclaration(member) && nodeIsPresent(member.body)); } - function getSetAccessorValueParameter(accessor: SetAccessorDeclaration): ParameterDeclaration | undefined { + export function getSetAccessorValueParameter(accessor: SetAccessorDeclaration): ParameterDeclaration | undefined { if (accessor && accessor.parameters.length > 0) { const hasThis = accessor.parameters.length === 2 && parameterIsThisKeyword(accessor.parameters[0]); return accessor.parameters[hasThis ? 1 : 0]; @@ -3553,7 +3553,7 @@ namespace ts { return id.originalKeywordKind === SyntaxKind.ThisKeyword; } - export function getAllAccessorDeclarations(declarations: NodeArray, accessor: AccessorDeclaration): AllAccessorDeclarations { + export function getAllAccessorDeclarations(declarations: readonly Declaration[], accessor: AccessorDeclaration): AllAccessorDeclarations { // TODO: GH#18217 let firstAccessor!: AccessorDeclaration; let secondAccessor!: AccessorDeclaration; diff --git a/src/services/codefixes/helpers.ts b/src/services/codefixes/helpers.ts index 45a79613db..e4c56e3ea5 100644 --- a/src/services/codefixes/helpers.ts +++ b/src/services/codefixes/helpers.ts @@ -55,10 +55,9 @@ namespace ts.codefix { const modifiers = visibilityModifier ? createNodeArray([visibilityModifier]) : undefined; const type = checker.getWidenedType(checker.getTypeOfSymbolAtLocation(symbol, enclosingDeclaration)); const optional = !!(symbol.flags & SymbolFlags.Optional); + const ambient = !!(enclosingDeclaration.flags & NodeFlags.Ambient); switch (declaration.kind) { - case SyntaxKind.GetAccessor: - case SyntaxKind.SetAccessor: case SyntaxKind.PropertySignature: case SyntaxKind.PropertyDeclaration: const typeNode = checker.typeToTypeNode(type, enclosingDeclaration, /*flags*/ undefined, getNoopSymbolTrackerWithResolver(context)); @@ -70,6 +69,37 @@ namespace ts.codefix { typeNode, /*initializer*/ undefined)); break; + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: { + const allAccessors = getAllAccessorDeclarations(declarations, declaration as AccessorDeclaration); + const typeNode = checker.typeToTypeNode(type, enclosingDeclaration, /*flags*/ undefined, getNoopSymbolTrackerWithResolver(context)); + const orderedAccessors = allAccessors.secondAccessor + ? [allAccessors.firstAccessor, allAccessors.secondAccessor] + : [allAccessors.firstAccessor]; + for (const accessor of orderedAccessors) { + if (isGetAccessorDeclaration(accessor)) { + out(createGetAccessor( + /*decorators*/ undefined, + modifiers, + name, + emptyArray, + typeNode, + ambient ? undefined : createStubbedMethodBody(preferences))); + } + else { + Debug.assertNode(accessor, isSetAccessorDeclaration); + const parameter = getSetAccessorValueParameter(accessor); + const parameterName = parameter && isIdentifier(parameter.name) ? idText(parameter.name) : undefined; + out(createSetAccessor( + /*decorators*/ undefined, + modifiers, + name, + createDummyParameters(1, [parameterName], [typeNode], 1, /*inJs*/ false), + ambient ? undefined : createStubbedMethodBody(preferences))); + } + } + break; + } case SyntaxKind.MethodSignature: case SyntaxKind.MethodDeclaration: // The signature for the implementation appears as an entry in `signatures` iff @@ -87,7 +117,7 @@ namespace ts.codefix { if (declarations.length === 1) { Debug.assert(signatures.length === 1); const signature = signatures[0]; - outputMethod(signature, modifiers, name, createStubbedMethodBody(preferences)); + outputMethod(signature, modifiers, name, ambient ? undefined : createStubbedMethodBody(preferences)); break; } @@ -96,13 +126,15 @@ namespace ts.codefix { outputMethod(signature, getSynthesizedDeepClones(modifiers, /*includeTrivia*/ false), getSynthesizedDeepClone(name, /*includeTrivia*/ false)); } - if (declarations.length > signatures.length) { - const signature = checker.getSignatureFromDeclaration(declarations[declarations.length - 1] as SignatureDeclaration)!; - outputMethod(signature, modifiers, name, createStubbedMethodBody(preferences)); - } - else { - Debug.assert(declarations.length === signatures.length); - out(createMethodImplementingSignatures(signatures, name, optional, modifiers, preferences)); + if (!ambient) { + if (declarations.length > signatures.length) { + const signature = checker.getSignatureFromDeclaration(declarations[declarations.length - 1] as SignatureDeclaration)!; + outputMethod(signature, modifiers, name, createStubbedMethodBody(preferences)); + } + else { + Debug.assert(declarations.length === signatures.length); + out(createMethodImplementingSignatures(signatures, name, optional, modifiers, preferences)); + } } break; } diff --git a/tests/baselines/reference/accessorsInAmbientContext.errors.txt b/tests/baselines/reference/accessorsInAmbientContext.errors.txt index 44b100bd58..9a625f84c1 100644 --- a/tests/baselines/reference/accessorsInAmbientContext.errors.txt +++ b/tests/baselines/reference/accessorsInAmbientContext.errors.txt @@ -1,44 +1,44 @@ -tests/cases/compiler/accessorsInAmbientContext.ts(3,13): error TS1086: An accessor cannot be declared in an ambient context. -tests/cases/compiler/accessorsInAmbientContext.ts(4,13): error TS1086: An accessor cannot be declared in an ambient context. -tests/cases/compiler/accessorsInAmbientContext.ts(6,20): error TS1086: An accessor cannot be declared in an ambient context. -tests/cases/compiler/accessorsInAmbientContext.ts(7,20): error TS1086: An accessor cannot be declared in an ambient context. -tests/cases/compiler/accessorsInAmbientContext.ts(12,9): error TS1086: An accessor cannot be declared in an ambient context. -tests/cases/compiler/accessorsInAmbientContext.ts(13,9): error TS1086: An accessor cannot be declared in an ambient context. -tests/cases/compiler/accessorsInAmbientContext.ts(15,16): error TS1086: An accessor cannot be declared in an ambient context. -tests/cases/compiler/accessorsInAmbientContext.ts(16,16): error TS1086: An accessor cannot be declared in an ambient context. +tests/cases/compiler/accessorsInAmbientContext.ts(3,17): error TS1183: An implementation cannot be declared in ambient contexts. +tests/cases/compiler/accessorsInAmbientContext.ts(4,18): error TS1183: An implementation cannot be declared in ambient contexts. +tests/cases/compiler/accessorsInAmbientContext.ts(6,24): error TS1183: An implementation cannot be declared in ambient contexts. +tests/cases/compiler/accessorsInAmbientContext.ts(7,25): error TS1183: An implementation cannot be declared in ambient contexts. +tests/cases/compiler/accessorsInAmbientContext.ts(12,13): error TS1183: An implementation cannot be declared in ambient contexts. +tests/cases/compiler/accessorsInAmbientContext.ts(13,14): error TS1183: An implementation cannot be declared in ambient contexts. +tests/cases/compiler/accessorsInAmbientContext.ts(15,20): error TS1183: An implementation cannot be declared in ambient contexts. +tests/cases/compiler/accessorsInAmbientContext.ts(16,21): error TS1183: An implementation cannot be declared in ambient contexts. ==== tests/cases/compiler/accessorsInAmbientContext.ts (8 errors) ==== declare module M { class C { get X() { return 1; } - ~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. set X(v) { } - ~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. static get Y() { return 1; } - ~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. static set Y(v) { } - ~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. } } declare class C { get X() { return 1; } - ~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. set X(v) { } - ~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. static get Y() { return 1; } - ~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. static set Y(v) { } - ~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. } \ No newline at end of file diff --git a/tests/baselines/reference/ambientAccessors(target=es3).js b/tests/baselines/reference/ambientAccessors(target=es3).js new file mode 100644 index 0000000000..4ddebd5e0e --- /dev/null +++ b/tests/baselines/reference/ambientAccessors(target=es3).js @@ -0,0 +1,30 @@ +//// [ambientAccessors.ts] +// ok to use accessors in ambient class in ES3 +declare class C { + static get a(): string; + static set a(value: string); + + private static get b(): string; + private static set b(foo: string); + + get x(): string; + set x(value: string); + + private get y(): string; + private set y(foo: string); +} + +//// [ambientAccessors.js] + + +//// [ambientAccessors.d.ts] +declare class C { + static get a(): string; + static set a(value: string); + private static get b(); + private static set b(value); + get x(): string; + set x(value: string); + private get y(); + private set y(value); +} diff --git a/tests/baselines/reference/ambientAccessors(target=es3).symbols b/tests/baselines/reference/ambientAccessors(target=es3).symbols new file mode 100644 index 0000000000..a73d3dcdab --- /dev/null +++ b/tests/baselines/reference/ambientAccessors(target=es3).symbols @@ -0,0 +1,33 @@ +=== tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/ambientAccessors.ts === +// ok to use accessors in ambient class in ES3 +declare class C { +>C : Symbol(C, Decl(ambientAccessors.ts, 0, 0)) + + static get a(): string; +>a : Symbol(C.a, Decl(ambientAccessors.ts, 1, 17), Decl(ambientAccessors.ts, 2, 27)) + + static set a(value: string); +>a : Symbol(C.a, Decl(ambientAccessors.ts, 1, 17), Decl(ambientAccessors.ts, 2, 27)) +>value : Symbol(value, Decl(ambientAccessors.ts, 3, 17)) + + private static get b(): string; +>b : Symbol(C.b, Decl(ambientAccessors.ts, 3, 32), Decl(ambientAccessors.ts, 5, 35)) + + private static set b(foo: string); +>b : Symbol(C.b, Decl(ambientAccessors.ts, 3, 32), Decl(ambientAccessors.ts, 5, 35)) +>foo : Symbol(foo, Decl(ambientAccessors.ts, 6, 25)) + + get x(): string; +>x : Symbol(C.x, Decl(ambientAccessors.ts, 6, 38), Decl(ambientAccessors.ts, 8, 20)) + + set x(value: string); +>x : Symbol(C.x, Decl(ambientAccessors.ts, 6, 38), Decl(ambientAccessors.ts, 8, 20)) +>value : Symbol(value, Decl(ambientAccessors.ts, 9, 10)) + + private get y(): string; +>y : Symbol(C.y, Decl(ambientAccessors.ts, 9, 25), Decl(ambientAccessors.ts, 11, 28)) + + private set y(foo: string); +>y : Symbol(C.y, Decl(ambientAccessors.ts, 9, 25), Decl(ambientAccessors.ts, 11, 28)) +>foo : Symbol(foo, Decl(ambientAccessors.ts, 12, 18)) +} diff --git a/tests/baselines/reference/ambientAccessors(target=es3).types b/tests/baselines/reference/ambientAccessors(target=es3).types new file mode 100644 index 0000000000..48b4f03823 --- /dev/null +++ b/tests/baselines/reference/ambientAccessors(target=es3).types @@ -0,0 +1,33 @@ +=== tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/ambientAccessors.ts === +// ok to use accessors in ambient class in ES3 +declare class C { +>C : C + + static get a(): string; +>a : string + + static set a(value: string); +>a : string +>value : string + + private static get b(): string; +>b : string + + private static set b(foo: string); +>b : string +>foo : string + + get x(): string; +>x : string + + set x(value: string); +>x : string +>value : string + + private get y(): string; +>y : string + + private set y(foo: string); +>y : string +>foo : string +} diff --git a/tests/baselines/reference/ambientAccessors(target=es5).js b/tests/baselines/reference/ambientAccessors(target=es5).js new file mode 100644 index 0000000000..4ddebd5e0e --- /dev/null +++ b/tests/baselines/reference/ambientAccessors(target=es5).js @@ -0,0 +1,30 @@ +//// [ambientAccessors.ts] +// ok to use accessors in ambient class in ES3 +declare class C { + static get a(): string; + static set a(value: string); + + private static get b(): string; + private static set b(foo: string); + + get x(): string; + set x(value: string); + + private get y(): string; + private set y(foo: string); +} + +//// [ambientAccessors.js] + + +//// [ambientAccessors.d.ts] +declare class C { + static get a(): string; + static set a(value: string); + private static get b(); + private static set b(value); + get x(): string; + set x(value: string); + private get y(); + private set y(value); +} diff --git a/tests/baselines/reference/ambientAccessors(target=es5).symbols b/tests/baselines/reference/ambientAccessors(target=es5).symbols new file mode 100644 index 0000000000..a73d3dcdab --- /dev/null +++ b/tests/baselines/reference/ambientAccessors(target=es5).symbols @@ -0,0 +1,33 @@ +=== tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/ambientAccessors.ts === +// ok to use accessors in ambient class in ES3 +declare class C { +>C : Symbol(C, Decl(ambientAccessors.ts, 0, 0)) + + static get a(): string; +>a : Symbol(C.a, Decl(ambientAccessors.ts, 1, 17), Decl(ambientAccessors.ts, 2, 27)) + + static set a(value: string); +>a : Symbol(C.a, Decl(ambientAccessors.ts, 1, 17), Decl(ambientAccessors.ts, 2, 27)) +>value : Symbol(value, Decl(ambientAccessors.ts, 3, 17)) + + private static get b(): string; +>b : Symbol(C.b, Decl(ambientAccessors.ts, 3, 32), Decl(ambientAccessors.ts, 5, 35)) + + private static set b(foo: string); +>b : Symbol(C.b, Decl(ambientAccessors.ts, 3, 32), Decl(ambientAccessors.ts, 5, 35)) +>foo : Symbol(foo, Decl(ambientAccessors.ts, 6, 25)) + + get x(): string; +>x : Symbol(C.x, Decl(ambientAccessors.ts, 6, 38), Decl(ambientAccessors.ts, 8, 20)) + + set x(value: string); +>x : Symbol(C.x, Decl(ambientAccessors.ts, 6, 38), Decl(ambientAccessors.ts, 8, 20)) +>value : Symbol(value, Decl(ambientAccessors.ts, 9, 10)) + + private get y(): string; +>y : Symbol(C.y, Decl(ambientAccessors.ts, 9, 25), Decl(ambientAccessors.ts, 11, 28)) + + private set y(foo: string); +>y : Symbol(C.y, Decl(ambientAccessors.ts, 9, 25), Decl(ambientAccessors.ts, 11, 28)) +>foo : Symbol(foo, Decl(ambientAccessors.ts, 12, 18)) +} diff --git a/tests/baselines/reference/ambientAccessors(target=es5).types b/tests/baselines/reference/ambientAccessors(target=es5).types new file mode 100644 index 0000000000..48b4f03823 --- /dev/null +++ b/tests/baselines/reference/ambientAccessors(target=es5).types @@ -0,0 +1,33 @@ +=== tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/ambientAccessors.ts === +// ok to use accessors in ambient class in ES3 +declare class C { +>C : C + + static get a(): string; +>a : string + + static set a(value: string); +>a : string +>value : string + + private static get b(): string; +>b : string + + private static set b(foo: string); +>b : string +>foo : string + + get x(): string; +>x : string + + set x(value: string); +>x : string +>value : string + + private get y(): string; +>y : string + + private set y(foo: string); +>y : string +>foo : string +} diff --git a/tests/baselines/reference/ambientGetters.errors.txt b/tests/baselines/reference/ambientGetters.errors.txt index b245f56181..b061f4b04f 100644 --- a/tests/baselines/reference/ambientGetters.errors.txt +++ b/tests/baselines/reference/ambientGetters.errors.txt @@ -1,16 +1,13 @@ -tests/cases/compiler/ambientGetters.ts(2,9): error TS1086: An accessor cannot be declared in an ambient context. -tests/cases/compiler/ambientGetters.ts(6,9): error TS1086: An accessor cannot be declared in an ambient context. +tests/cases/compiler/ambientGetters.ts(6,18): error TS1183: An implementation cannot be declared in ambient contexts. -==== tests/cases/compiler/ambientGetters.ts (2 errors) ==== +==== tests/cases/compiler/ambientGetters.ts (1 errors) ==== declare class A { get length() : number; - ~~~~~~ -!!! error TS1086: An accessor cannot be declared in an ambient context. } declare class B { get length() { return 0; } - ~~~~~~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. } \ No newline at end of file diff --git a/tests/baselines/reference/giant.errors.txt b/tests/baselines/reference/giant.errors.txt index eb899ff132..3c81c601f5 100644 --- a/tests/baselines/reference/giant.errors.txt +++ b/tests/baselines/reference/giant.errors.txt @@ -75,28 +75,22 @@ tests/cases/compiler/giant.ts(242,21): error TS1183: An implementation cannot be tests/cases/compiler/giant.ts(243,22): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/compiler/giant.ts(244,16): error TS2300: Duplicate identifier 'pgF'. tests/cases/compiler/giant.ts(244,22): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(245,20): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(245,20): error TS2300: Duplicate identifier 'pgF'. tests/cases/compiler/giant.ts(246,16): error TS2300: Duplicate identifier 'psF'. tests/cases/compiler/giant.ts(246,31): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(247,20): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(247,20): error TS2300: Duplicate identifier 'psF'. tests/cases/compiler/giant.ts(248,17): error TS2300: Duplicate identifier 'rgF'. tests/cases/compiler/giant.ts(248,23): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(249,21): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(249,21): error TS2300: Duplicate identifier 'rgF'. tests/cases/compiler/giant.ts(250,17): error TS2300: Duplicate identifier 'rsF'. tests/cases/compiler/giant.ts(250,32): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(251,21): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(251,21): error TS2300: Duplicate identifier 'rsF'. tests/cases/compiler/giant.ts(253,21): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/compiler/giant.ts(254,16): error TS2300: Duplicate identifier 'tsF'. tests/cases/compiler/giant.ts(254,31): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(255,20): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(255,20): error TS2300: Duplicate identifier 'tsF'. tests/cases/compiler/giant.ts(256,16): error TS2300: Duplicate identifier 'tgF'. tests/cases/compiler/giant.ts(256,22): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(257,20): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(257,20): error TS2300: Duplicate identifier 'tgF'. tests/cases/compiler/giant.ts(261,22): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/compiler/giant.ts(261,25): error TS1036: Statements are not allowed in ambient contexts. @@ -178,28 +172,22 @@ tests/cases/compiler/giant.ts(500,21): error TS1183: An implementation cannot be tests/cases/compiler/giant.ts(501,22): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/compiler/giant.ts(502,16): error TS2300: Duplicate identifier 'pgF'. tests/cases/compiler/giant.ts(502,22): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(503,20): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(503,20): error TS2300: Duplicate identifier 'pgF'. tests/cases/compiler/giant.ts(504,16): error TS2300: Duplicate identifier 'psF'. tests/cases/compiler/giant.ts(504,31): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(505,20): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(505,20): error TS2300: Duplicate identifier 'psF'. tests/cases/compiler/giant.ts(506,17): error TS2300: Duplicate identifier 'rgF'. tests/cases/compiler/giant.ts(506,23): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(507,21): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(507,21): error TS2300: Duplicate identifier 'rgF'. tests/cases/compiler/giant.ts(508,17): error TS2300: Duplicate identifier 'rsF'. tests/cases/compiler/giant.ts(508,32): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(509,21): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(509,21): error TS2300: Duplicate identifier 'rsF'. tests/cases/compiler/giant.ts(511,21): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/compiler/giant.ts(512,16): error TS2300: Duplicate identifier 'tsF'. tests/cases/compiler/giant.ts(512,31): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(513,20): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(513,20): error TS2300: Duplicate identifier 'tsF'. tests/cases/compiler/giant.ts(514,16): error TS2300: Duplicate identifier 'tgF'. tests/cases/compiler/giant.ts(514,22): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(515,20): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(515,20): error TS2300: Duplicate identifier 'tgF'. tests/cases/compiler/giant.ts(519,22): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/compiler/giant.ts(519,25): error TS1036: Statements are not allowed in ambient contexts. @@ -210,28 +198,22 @@ tests/cases/compiler/giant.ts(536,17): error TS1183: An implementation cannot be tests/cases/compiler/giant.ts(537,18): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/compiler/giant.ts(538,12): error TS2300: Duplicate identifier 'pgF'. tests/cases/compiler/giant.ts(538,18): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(539,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(539,16): error TS2300: Duplicate identifier 'pgF'. tests/cases/compiler/giant.ts(540,12): error TS2300: Duplicate identifier 'psF'. tests/cases/compiler/giant.ts(540,27): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(541,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(541,16): error TS2300: Duplicate identifier 'psF'. tests/cases/compiler/giant.ts(542,13): error TS2300: Duplicate identifier 'rgF'. tests/cases/compiler/giant.ts(542,19): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(543,17): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(543,17): error TS2300: Duplicate identifier 'rgF'. tests/cases/compiler/giant.ts(544,13): error TS2300: Duplicate identifier 'rsF'. tests/cases/compiler/giant.ts(544,28): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(545,17): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(545,17): error TS2300: Duplicate identifier 'rsF'. tests/cases/compiler/giant.ts(547,17): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/compiler/giant.ts(548,12): error TS2300: Duplicate identifier 'tsF'. tests/cases/compiler/giant.ts(548,27): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(549,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(549,16): error TS2300: Duplicate identifier 'tsF'. tests/cases/compiler/giant.ts(550,12): error TS2300: Duplicate identifier 'tgF'. tests/cases/compiler/giant.ts(550,18): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/compiler/giant.ts(551,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/giant.ts(551,16): error TS2300: Duplicate identifier 'tgF'. tests/cases/compiler/giant.ts(555,18): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/compiler/giant.ts(555,21): error TS1036: Statements are not allowed in ambient contexts. @@ -265,7 +247,7 @@ tests/cases/compiler/giant.ts(671,25): error TS1036: Statements are not allowed tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be declared in ambient contexts. -==== tests/cases/compiler/giant.ts (265 errors) ==== +==== tests/cases/compiler/giant.ts (247 errors) ==== /* Prefixes p -> public @@ -666,8 +648,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. public get pgF() ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'pgF'. public psF(param:any) { } ~~~ @@ -676,8 +656,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. public set psF(param:any) ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'psF'. private rgF() { } ~~~ @@ -686,8 +664,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. private get rgF() ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'rgF'. private rsF(param:any) { } ~~~ @@ -696,8 +672,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. private set rsF(param:any) ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'rsF'. static tV; static tF() { } @@ -710,8 +684,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. static set tsF(param:any) ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'tsF'. static tgF() { } ~~~ @@ -720,8 +692,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. static get tgF() ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'tgF'. } export declare module eaM { @@ -1130,8 +1100,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. public get pgF() ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'pgF'. public psF(param:any) { } ~~~ @@ -1140,8 +1108,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. public set psF(param:any) ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'psF'. private rgF() { } ~~~ @@ -1150,8 +1116,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. private get rgF() ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'rgF'. private rsF(param:any) { } ~~~ @@ -1160,8 +1124,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. private set rsF(param:any) ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'rsF'. static tV; static tF() { } @@ -1174,8 +1136,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. static set tsF(param:any) ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'tsF'. static tgF() { } ~~~ @@ -1184,8 +1144,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. static get tgF() ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'tgF'. } export declare module eaM { @@ -1230,8 +1188,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. public get pgF() ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'pgF'. public psF(param:any) { } ~~~ @@ -1240,8 +1196,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. public set psF(param:any) ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'psF'. private rgF() { } ~~~ @@ -1250,8 +1204,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. private get rgF() ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'rgF'. private rsF(param:any) { } ~~~ @@ -1260,8 +1212,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. private set rsF(param:any) ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'rsF'. static tV; static tF() { } @@ -1274,8 +1224,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. static set tsF(param:any) ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'tsF'. static tgF() { } ~~~ @@ -1284,8 +1232,6 @@ tests/cases/compiler/giant.ts(675,30): error TS1183: An implementation cannot be !!! error TS1183: An implementation cannot be declared in ambient contexts. static get tgF() ~~~ -!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~~~ !!! error TS2300: Duplicate identifier 'tgF'. } export declare module eaM { diff --git a/tests/baselines/reference/giant.js b/tests/baselines/reference/giant.js index e76937b00a..85686faa01 100644 --- a/tests/baselines/reference/giant.js +++ b/tests/baselines/reference/giant.js @@ -1237,19 +1237,19 @@ export declare module eM { pF(): void; private rF; pgF(): void; - readonly pgF: any; + get pgF(): any; psF(param: any): void; - psF: any; + set psF(param: any); private rgF; - private readonly rgF; - private rsF; + private get rgF(); private rsF; + private set rsF(value); static tV: any; static tF(): void; static tsF(param: any): void; - static tsF: any; + static set tsF(param: any); static tgF(): void; - static readonly tgF: any; + static get tgF(): any; } module eaM { var V: any; @@ -1277,19 +1277,19 @@ export declare class eaC { pF(): void; private rF; pgF(): void; - readonly pgF: any; + get pgF(): any; psF(param: any): void; - psF: any; + set psF(param: any); private rgF; - private readonly rgF; - private rsF; + private get rgF(); private rsF; + private set rsF(value); static tV: any; static tF(): void; static tsF(param: any): void; - static tsF: any; + static set tsF(param: any); static tgF(): void; - static readonly tgF: any; + static get tgF(): any; } export declare module eaM { var V: any; diff --git a/tests/baselines/reference/parserAccessors5.errors.txt b/tests/baselines/reference/parserAccessors5.errors.txt index b7dcd8ef92..66f346e5ef 100644 --- a/tests/baselines/reference/parserAccessors5.errors.txt +++ b/tests/baselines/reference/parserAccessors5.errors.txt @@ -1,9 +1,9 @@ -tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors5.ts(2,7): error TS1086: An accessor cannot be declared in an ambient context. +tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors5.ts(2,13): error TS1183: An implementation cannot be declared in ambient contexts. ==== tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors5.ts (1 errors) ==== declare class C { get foo() { return 0; } - ~~~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. } \ No newline at end of file diff --git a/tests/baselines/reference/parserAccessors6.errors.txt b/tests/baselines/reference/parserAccessors6.errors.txt index 023ec6d79c..13512269df 100644 --- a/tests/baselines/reference/parserAccessors6.errors.txt +++ b/tests/baselines/reference/parserAccessors6.errors.txt @@ -1,9 +1,9 @@ -tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors6.ts(2,7): error TS1086: An accessor cannot be declared in an ambient context. +tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors6.ts(2,14): error TS1183: An implementation cannot be declared in ambient contexts. ==== tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors6.ts (1 errors) ==== declare class C { set foo(v) { } - ~~~ -!!! error TS1086: An accessor cannot be declared in an ambient context. + ~ +!!! error TS1183: An implementation cannot be declared in ambient contexts. } \ No newline at end of file diff --git a/tests/baselines/reference/parserComputedPropertyName23.errors.txt b/tests/baselines/reference/parserComputedPropertyName23.errors.txt index 8c95992253..2ecf45eb69 100644 --- a/tests/baselines/reference/parserComputedPropertyName23.errors.txt +++ b/tests/baselines/reference/parserComputedPropertyName23.errors.txt @@ -1,12 +1,9 @@ -tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName23.ts(2,9): error TS1086: An accessor cannot be declared in an ambient context. tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName23.ts(2,10): error TS2304: Cannot find name 'e'. -==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName23.ts (2 errors) ==== +==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName23.ts (1 errors) ==== declare class C { get [e](): number - ~~~ -!!! error TS1086: An accessor cannot be declared in an ambient context. ~ !!! error TS2304: Cannot find name 'e'. } \ No newline at end of file diff --git a/tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/ambientAccessors.ts b/tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/ambientAccessors.ts new file mode 100644 index 0000000000..44861363ad --- /dev/null +++ b/tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/ambientAccessors.ts @@ -0,0 +1,16 @@ +// @target: es3, es5 +// @declaration: true +// ok to use accessors in ambient class in ES3 +declare class C { + static get a(): string; + static set a(value: string); + + private static get b(): string; + private static set b(foo: string); + + get x(): string; + set x(value: string); + + private get y(): string; + private set y(foo: string); +} \ No newline at end of file diff --git a/tests/cases/fourslash/codeFixAmbientClassExtendAbstractMethod.ts b/tests/cases/fourslash/codeFixAmbientClassExtendAbstractMethod.ts new file mode 100644 index 0000000000..35da0b24fa --- /dev/null +++ b/tests/cases/fourslash/codeFixAmbientClassExtendAbstractMethod.ts @@ -0,0 +1,31 @@ +/// + +////abstract class A { +//// abstract f(a: number, b: string): boolean; +//// abstract f(a: number, b: string): this; +//// abstract f(a: string, b: number): Function; +//// abstract f(a: string): Function; +//// abstract foo(): number; +////} +//// +////declare class C extends A {} + +verify.codeFix({ + description: "Implement inherited abstract class", + newFileContent: +`abstract class A { + abstract f(a: number, b: string): boolean; + abstract f(a: number, b: string): this; + abstract f(a: string, b: number): Function; + abstract f(a: string): Function; + abstract foo(): number; +} + +declare class C extends A { + f(a: number, b: string): boolean; + f(a: number, b: string): this; + f(a: string, b: number): Function; + f(a: string): Function; + foo(): number; +}` +}); diff --git a/tests/cases/fourslash/codeFixAmbientClassExtendAbstractMethod_all.ts b/tests/cases/fourslash/codeFixAmbientClassExtendAbstractMethod_all.ts new file mode 100644 index 0000000000..699f5e8b49 --- /dev/null +++ b/tests/cases/fourslash/codeFixAmbientClassExtendAbstractMethod_all.ts @@ -0,0 +1,26 @@ +/// + +////abstract class A { +//// abstract m(): void; +//// abstract n(): void; +////} +////declare class B extends A {} +////declare class C extends A {} + +verify.codeFixAll({ + fixId: "fixClassDoesntImplementInheritedAbstractMember", + fixAllDescription: "Implement all inherited abstract classes", + newFileContent: +`abstract class A { + abstract m(): void; + abstract n(): void; +} +declare class B extends A { + m(): void; + n(): void; +} +declare class C extends A { + m(): void; + n(): void; +}`, +}); diff --git a/tests/cases/fourslash/codeFixAmbientClassImplementClassAbstractGettersAndSetters.ts b/tests/cases/fourslash/codeFixAmbientClassImplementClassAbstractGettersAndSetters.ts new file mode 100644 index 0000000000..1defadb5c7 --- /dev/null +++ b/tests/cases/fourslash/codeFixAmbientClassImplementClassAbstractGettersAndSetters.ts @@ -0,0 +1,32 @@ +/// + +////abstract class A { +//// abstract get a(): string; +//// abstract set a(newName: string); +//// +//// abstract get b(): number; +//// +//// abstract set c(arg: number | string); +////} +//// +////declare class C implements A {} + +verify.codeFix({ + description: "Implement interface 'A'", + newFileContent: +`abstract class A { + abstract get a(): string; + abstract set a(newName: string); + + abstract get b(): number; + + abstract set c(arg: number | string); +} + +declare class C implements A { + get a(): string; + set a(newName: string); + get b(): number; + set c(arg: string | number); +}`, +}); diff --git a/tests/cases/fourslash/codeFixAmbientClassImplementClassMethodViaHeritage.ts b/tests/cases/fourslash/codeFixAmbientClassImplementClassMethodViaHeritage.ts new file mode 100644 index 0000000000..20bdd4a4a1 --- /dev/null +++ b/tests/cases/fourslash/codeFixAmbientClassImplementClassMethodViaHeritage.ts @@ -0,0 +1,16 @@ +/// + +//// class C1 { +//// f1() {} +//// } +//// +//// class C2 extends C1 { +//// +//// } +//// +//// declare class C3 implements C2 {[| +//// |]f2(); +//// } + +verify.rangeAfterCodeFix(`f1(): void; +`); diff --git a/tests/cases/fourslash/codeFixClassExtendAbstractGetterSetter.ts b/tests/cases/fourslash/codeFixClassExtendAbstractGetterSetter.ts index 0a66f445cc..00c140dfcc 100644 --- a/tests/cases/fourslash/codeFixClassExtendAbstractGetterSetter.ts +++ b/tests/cases/fourslash/codeFixClassExtendAbstractGetterSetter.ts @@ -38,12 +38,29 @@ verify.codeFix({ abstract class B extends A {} class C extends A { - a: string | number; - b: this; - c: A; - d: string | number; - e: this; - f: A; - g: string; + get a(): string | number { + throw new Error("Method not implemented."); + } + get b(): this { + throw new Error("Method not implemented."); + } + get c(): A { + throw new Error("Method not implemented."); + } + set d(arg: string | number) { + throw new Error("Method not implemented."); + } + set e(arg: this) { + throw new Error("Method not implemented."); + } + set f(arg: A) { + throw new Error("Method not implemented."); + } + get g(): string { + throw new Error("Method not implemented."); + } + set g(newName: string) { + throw new Error("Method not implemented."); + } }` }); diff --git a/tests/cases/fourslash/codeFixClassImplementClassAbstractGettersAndSetters.ts b/tests/cases/fourslash/codeFixClassImplementClassAbstractGettersAndSetters.ts index 71e0aba73b..dad3c42964 100644 --- a/tests/cases/fourslash/codeFixClassImplementClassAbstractGettersAndSetters.ts +++ b/tests/cases/fourslash/codeFixClassImplementClassAbstractGettersAndSetters.ts @@ -24,8 +24,17 @@ verify.codeFix({ } class C implements A { - a: string; - b: number; - c: string | number; + get a(): string { + throw new Error("Method not implemented."); + } + set a(newName: string) { + throw new Error("Method not implemented."); + } + get b(): number { + throw new Error("Method not implemented."); + } + set c(arg: string | number) { + throw new Error("Method not implemented."); + } }`, });