diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 950b9a8e30fb8..25590c6b4bfa8 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -1930,8 +1930,9 @@ func (c *Checker) onSuccessfullyResolvedSymbol(errorLocation *ast.Node, result * if associatedDeclarationForContainingInitializerOrBindingName != nil && !withinDeferredContext && (meaning&ast.SymbolFlagsValue) == ast.SymbolFlagsValue { candidate := c.getMergedSymbol(c.getLateBoundSymbol(result)) root := ast.GetRootDeclaration(associatedDeclarationForContainingInitializerOrBindingName) - // A parameter initializer or binding pattern initializer within a parameter cannot refer to itself - if candidate == c.getSymbolOfDeclaration(associatedDeclarationForContainingInitializerOrBindingName) { + // A parameter initializer or binding pattern initializer within a parameter cannot refer to itself. + // Compare declarations because the symbol of a parameter property declaration is its class property. + if candidate.ValueDeclaration() == associatedDeclarationForContainingInitializerOrBindingName { c.error(errorLocation, diagnostics.Parameter_0_cannot_reference_itself, scanner.DeclarationNameToString(associatedDeclarationForContainingInitializerOrBindingName.Name())) } else if candidate.ValueDeclaration() != nil && candidate.ValueDeclaration().Pos() > associatedDeclarationForContainingInitializerOrBindingName.Pos() && root.Parent.Locals() != nil && c.getSymbol(root.Parent.Locals(), candidate.Name(), meaning) == candidate { c.error(errorLocation, diagnostics.Parameter_0_cannot_reference_identifier_1_declared_after_it, scanner.DeclarationNameToString(associatedDeclarationForContainingInitializerOrBindingName.Name()), scanner.DeclarationNameToString(errorLocation)) diff --git a/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.errors.txt b/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.errors.txt new file mode 100644 index 0000000000000..ac59fed613699 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.errors.txt @@ -0,0 +1,33 @@ +selfReferencesInParameterProperties.ts(2,37): error TS2372: Parameter 'a' cannot reference itself. +selfReferencesInParameterProperties.ts(7,36): error TS2372: Parameter 'b' cannot reference itself. +selfReferencesInParameterProperties.ts(11,38): error TS2373: Parameter 'c' cannot reference identifier 'd' declared after it. + + +==== selfReferencesInParameterProperties.ts (3 errors) ==== + class DirectSelfReference { + constructor(private a: number = a) {} + ~ +!!! error TS2372: Parameter 'a' cannot reference itself. + } + + const b = 1; + class ShadowedOuterVariable { + constructor(public b: number = b) {} + ~ +!!! error TS2372: Parameter 'b' cannot reference itself. + } + + class ReferenceToLaterParameter { + constructor(readonly c: number = d, readonly d: number = 1) {} + ~ +!!! error TS2373: Parameter 'c' cannot reference identifier 'd' declared after it. + } + + class ReferenceToEarlierParameter { + constructor(private e: number, private f: number = e) {} + } + + class DeferredSelfReference { + constructor(private g: () => number = () => g()) {} + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.js b/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.js new file mode 100644 index 0000000000000..a92a3a130ee72 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.js @@ -0,0 +1,55 @@ +//// [tests/cases/compiler/selfReferencesInParameterProperties.ts] //// + +//// [selfReferencesInParameterProperties.ts] +class DirectSelfReference { + constructor(private a: number = a) {} +} + +const b = 1; +class ShadowedOuterVariable { + constructor(public b: number = b) {} +} + +class ReferenceToLaterParameter { + constructor(readonly c: number = d, readonly d: number = 1) {} +} + +class ReferenceToEarlierParameter { + constructor(private e: number, private f: number = e) {} +} + +class DeferredSelfReference { + constructor(private g: () => number = () => g()) {} +} + + +//// [selfReferencesInParameterProperties.js] +"use strict"; +class DirectSelfReference { + constructor(a = a) { + this.a = a; + } +} +const b = 1; +class ShadowedOuterVariable { + constructor(b = b) { + this.b = b; + } +} +class ReferenceToLaterParameter { + constructor(c = d, d = 1) { + this.c = c; + this.d = d; + } +} +class ReferenceToEarlierParameter { + constructor(e, f = e) { + this.e = e; + this.f = f; + } +} +class DeferredSelfReference { + constructor(g = () => g()) { + this.g = g; + } +} diff --git a/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.symbols b/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.symbols new file mode 100644 index 0000000000000..5ef0d4045db75 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.symbols @@ -0,0 +1,48 @@ +//// [tests/cases/compiler/selfReferencesInParameterProperties.ts] //// + +=== selfReferencesInParameterProperties.ts === +class DirectSelfReference { +>DirectSelfReference : Symbol(DirectSelfReference, Decl(selfReferencesInParameterProperties.ts, 0, 0)) + + constructor(private a: number = a) {} +>a : Symbol(DirectSelfReference.a, Decl(selfReferencesInParameterProperties.ts, 1, 16)) +>a : Symbol(a, Decl(selfReferencesInParameterProperties.ts, 1, 16)) +} + +const b = 1; +>b : Symbol(b, Decl(selfReferencesInParameterProperties.ts, 4, 5)) + +class ShadowedOuterVariable { +>ShadowedOuterVariable : Symbol(ShadowedOuterVariable, Decl(selfReferencesInParameterProperties.ts, 4, 12)) + + constructor(public b: number = b) {} +>b : Symbol(ShadowedOuterVariable.b, Decl(selfReferencesInParameterProperties.ts, 6, 16)) +>b : Symbol(b, Decl(selfReferencesInParameterProperties.ts, 6, 16)) +} + +class ReferenceToLaterParameter { +>ReferenceToLaterParameter : Symbol(ReferenceToLaterParameter, Decl(selfReferencesInParameterProperties.ts, 7, 1)) + + constructor(readonly c: number = d, readonly d: number = 1) {} +>c : Symbol(ReferenceToLaterParameter.c, Decl(selfReferencesInParameterProperties.ts, 10, 16)) +>d : Symbol(d, Decl(selfReferencesInParameterProperties.ts, 10, 39)) +>d : Symbol(ReferenceToLaterParameter.d, Decl(selfReferencesInParameterProperties.ts, 10, 39)) +} + +class ReferenceToEarlierParameter { +>ReferenceToEarlierParameter : Symbol(ReferenceToEarlierParameter, Decl(selfReferencesInParameterProperties.ts, 11, 1)) + + constructor(private e: number, private f: number = e) {} +>e : Symbol(ReferenceToEarlierParameter.e, Decl(selfReferencesInParameterProperties.ts, 14, 16)) +>f : Symbol(ReferenceToEarlierParameter.f, Decl(selfReferencesInParameterProperties.ts, 14, 34)) +>e : Symbol(e, Decl(selfReferencesInParameterProperties.ts, 14, 16)) +} + +class DeferredSelfReference { +>DeferredSelfReference : Symbol(DeferredSelfReference, Decl(selfReferencesInParameterProperties.ts, 15, 1)) + + constructor(private g: () => number = () => g()) {} +>g : Symbol(DeferredSelfReference.g, Decl(selfReferencesInParameterProperties.ts, 18, 16)) +>g : Symbol(g, Decl(selfReferencesInParameterProperties.ts, 18, 16)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.types b/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.types new file mode 100644 index 0000000000000..798d7b5be1dd1 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/selfReferencesInParameterProperties.types @@ -0,0 +1,52 @@ +//// [tests/cases/compiler/selfReferencesInParameterProperties.ts] //// + +=== selfReferencesInParameterProperties.ts === +class DirectSelfReference { +>DirectSelfReference : DirectSelfReference + + constructor(private a: number = a) {} +>a : number +>a : number +} + +const b = 1; +>b : 1 +>1 : 1 + +class ShadowedOuterVariable { +>ShadowedOuterVariable : ShadowedOuterVariable + + constructor(public b: number = b) {} +>b : number +>b : number +} + +class ReferenceToLaterParameter { +>ReferenceToLaterParameter : ReferenceToLaterParameter + + constructor(readonly c: number = d, readonly d: number = 1) {} +>c : number +>d : number +>d : number +>1 : 1 +} + +class ReferenceToEarlierParameter { +>ReferenceToEarlierParameter : ReferenceToEarlierParameter + + constructor(private e: number, private f: number = e) {} +>e : number +>f : number +>e : number +} + +class DeferredSelfReference { +>DeferredSelfReference : DeferredSelfReference + + constructor(private g: () => number = () => g()) {} +>g : () => number +>() => g() : () => number +>g() : number +>g : () => number +} + diff --git a/tsc/testdata/tests/cases/compiler/selfReferencesInParameterProperties.ts b/tsc/testdata/tests/cases/compiler/selfReferencesInParameterProperties.ts new file mode 100644 index 0000000000000..2e9f8a5cd4453 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/selfReferencesInParameterProperties.ts @@ -0,0 +1,22 @@ +// @target: es2015 + +class DirectSelfReference { + constructor(private a: number = a) {} +} + +const b = 1; +class ShadowedOuterVariable { + constructor(public b: number = b) {} +} + +class ReferenceToLaterParameter { + constructor(readonly c: number = d, readonly d: number = 1) {} +} + +class ReferenceToEarlierParameter { + constructor(private e: number, private f: number = e) {} +} + +class DeferredSelfReference { + constructor(private g: () => number = () => g()) {} +}