Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10583,7 +10583,7 @@ func (c *Checker) checkIdentifier(node *ast.Node, checkMode CheckMode) *Type {
return c.errorType
}
if symbol == c.argumentsSymbol {
if c.isInPropertyInitializerOrClassStaticBlock(node) {
if c.isInPropertyInitializerOrClassStaticBlock(node, true /*ignoreArrowFunctions*/) {
c.error(node, diagnostics.X_arguments_cannot_be_referenced_in_property_initializers_or_class_static_initialization_blocks)
return c.errorType
}
Expand Down Expand Up @@ -11215,7 +11215,7 @@ func (c *Checker) checkPropertyNotUsedBeforeDeclaration(prop *ast.Symbol, node *
}
var diagnostic *ast.Diagnostic
declarationName := right.Text()
if c.isInPropertyInitializerOrClassStaticBlock(node) &&
if c.isInPropertyInitializerOrClassStaticBlock(node, false /*ignoreArrowFunctions*/) &&
!c.isOptionalPropertyDeclaration(valueDeclaration) &&
!(ast.IsAccessExpression(node) && ast.IsAccessExpression(node.Expression())) &&
!c.isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right) &&
Expand Down Expand Up @@ -13151,25 +13151,19 @@ func (c *Checker) checkShorthandPropertyAssignment(node *ast.Node, inDestructuri
return expressionType
}

func (c *Checker) isInPropertyInitializerOrClassStaticBlock(node *ast.Node) bool {
func (c *Checker) isInPropertyInitializerOrClassStaticBlock(node *ast.Node, ignoreArrowFunctions bool) bool {
return ast.FindAncestorOrQuit(node, func(node *ast.Node) ast.FindAncestorResult {
switch node.Kind {
case ast.KindPropertyDeclaration:
case ast.KindPropertyDeclaration, ast.KindClassStaticBlockDeclaration:
return ast.FindAncestorTrue
case ast.KindPropertyAssignment, ast.KindMethodDeclaration, ast.KindGetAccessor, ast.KindSetAccessor, ast.KindSpreadAssignment,
ast.KindComputedPropertyName, ast.KindTemplateSpan, ast.KindJsxExpression, ast.KindJsxAttribute, ast.KindJsxAttributes,
ast.KindJsxSpreadAttribute, ast.KindJsxOpeningElement, ast.KindExpressionWithTypeArguments, ast.KindHeritageClause:
return ast.FindAncestorFalse
case ast.KindArrowFunction, ast.KindExpressionStatement:
if ast.IsBlock(node.Parent) && ast.IsClassStaticBlockDeclaration(node.Parent.Parent) {
return ast.FindAncestorTrue
}
case ast.KindTypeQuery, ast.KindJsxClosingElement:
return ast.FindAncestorQuit
case ast.KindArrowFunction:
return core.IfElse(ignoreArrowFunctions, ast.FindAncestorFalse, ast.FindAncestorQuit)
case ast.KindBlock:
return core.IfElse(ast.IsFunctionLikeDeclaration(node.Parent) && node.Parent.Kind != ast.KindArrowFunction, ast.FindAncestorQuit, ast.FindAncestorFalse)
default:
if ast.IsExpressionNode(node) {
return ast.FindAncestorFalse
}
return ast.FindAncestorQuit
return ast.FindAncestorFalse
}
}) != nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(3,10): error
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(9,10): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(15,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(21,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(33,16): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(40,7): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(42,16): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(66,6): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later.
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(77,9): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(96,26): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(102,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.


==== argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts (6 errors) ====
==== argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts (12 errors) ====
function A() {
return class T {
a = arguments
Expand Down Expand Up @@ -48,15 +54,21 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error
function D() {
return class T {
a = () => arguments // should error
~~~~~~~~~
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
}
}

function D1() {
return class T {
a = () => {
arguments; // should error
~~~~~~~~~
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
const b = () => {
return arguments; // should error
~~~~~~~~~
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
}

function f() {
Expand Down Expand Up @@ -96,6 +108,8 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
while(1) {
arguments // should error
~~~~~~~~~
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
}
}
}
Expand All @@ -115,12 +129,16 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error
function D5() {
return class T {
a = (() => { return arguments; })() // should error
~~~~~~~~~
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
}
}

function D6() {
return class T {
a = (x = arguments) => {} // should error
~~~~~~~~~
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ function D() {
>T : typeof T

a = () => arguments // should error
>a : () => IArguments
>() => arguments : () => IArguments
>arguments : IArguments
>a : () => any
>() => arguments : () => any
>arguments : any
}
}

Expand All @@ -99,14 +99,14 @@ function D1() {
>() => { arguments; // should error const b = () => { return arguments; // should error } function f() { return arguments; // ok } } : () => void

arguments; // should error
>arguments : IArguments
>arguments : any

const b = () => {
>b : () => IArguments
>() => { return arguments; // should error } : () => IArguments
>b : () => any
>() => { return arguments; // should error } : () => any

return arguments; // should error
>arguments : IArguments
>arguments : any
}

function f() {
Expand Down Expand Up @@ -175,7 +175,7 @@ function D3() {
>1 : 1

arguments // should error
>arguments : IArguments
>arguments : any
}
}
}
Expand Down Expand Up @@ -208,11 +208,11 @@ function D5() {
>T : typeof T

a = (() => { return arguments; })() // should error
>a : IArguments
>(() => { return arguments; })() : IArguments
>(() => { return arguments; }) : () => IArguments
>() => { return arguments; } : () => IArguments
>arguments : IArguments
>a : any
>(() => { return arguments; })() : any
>(() => { return arguments; }) : () => any
>() => { return arguments; } : () => any
>arguments : any
}
}

Expand All @@ -224,10 +224,10 @@ function D6() {
>T : typeof T

a = (x = arguments) => {} // should error
>a : (x?: IArguments) => void
>(x = arguments) => {} : (x?: IArguments) => void
>x : IArguments
>arguments : IArguments
>a : (x?: any) => void
>(x = arguments) => {} : (x?: any) => void
>x : any
>arguments : any
}
}

Expand Down
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has some seemingly unwanted diffs but I think this is basically desired and I opened a PR to Strada to close this gap: microsoft/TypeScript#62549

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this was just missed. Yay optional parameters

Original file line number Diff line number Diff line change
@@ -1,11 +1,65 @@
--- old.argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.types
+++ new.argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.types
@@= skipped -174, +174 lines =@@
>1 : 1
@@= skipped -80, +80 lines =@@
>T : typeof T

arguments // should error
->arguments : any
+>arguments : IArguments
a = () => arguments // should error
->a : () => IArguments
->() => arguments : () => IArguments
->arguments : IArguments
+>a : () => any
+>() => arguments : () => any
+>arguments : any
}
}

@@= skipped -18, +18 lines =@@
>() => { arguments; // should error const b = () => { return arguments; // should error } function f() { return arguments; // ok } } : () => void

arguments; // should error
->arguments : IArguments
+>arguments : any

const b = () => {
->b : () => IArguments
->() => { return arguments; // should error } : () => IArguments
+>b : () => any
+>() => { return arguments; // should error } : () => any

return arguments; // should error
->arguments : IArguments
+>arguments : any
}
}
}

function f() {
@@= skipped -109, +109 lines =@@
>T : typeof T

a = (() => { return arguments; })() // should error
->a : IArguments
->(() => { return arguments; })() : IArguments
->(() => { return arguments; }) : () => IArguments
->() => { return arguments; } : () => IArguments
->arguments : IArguments
+>a : any
+>(() => { return arguments; })() : any
+>(() => { return arguments; }) : () => any
+>() => { return arguments; } : () => any
+>arguments : any
}
}

@@= skipped -16, +16 lines =@@
>T : typeof T

a = (x = arguments) => {} // should error
->a : (x?: IArguments) => void
->(x = arguments) => {} : (x?: IArguments) => void
->x : IArguments
->arguments : IArguments
+>a : (x?: any) => void
+>(x = arguments) => {} : (x?: any) => void
+>x : any
+>arguments : any
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ decoratorUsedBeforeDeclaration.ts(1,9): error TS2450: Enum 'Enum' used before it
decoratorUsedBeforeDeclaration.ts(2,7): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(4,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
decoratorUsedBeforeDeclaration.ts(4,11): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(4,16): error TS2729: Property 'No' is used before its initialization.
decoratorUsedBeforeDeclaration.ts(5,9): error TS2450: Enum 'Enum' used before its declaration.
decoratorUsedBeforeDeclaration.ts(5,14): error TS2729: Property 'No' is used before its initialization.


==== decoratorUsedBeforeDeclaration.ts (6 errors) ====
==== decoratorUsedBeforeDeclaration.ts (8 errors) ====
@lambda(Enum.No)
~~~~~~
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
Expand All @@ -26,10 +28,16 @@ decoratorUsedBeforeDeclaration.ts(5,9): error TS2450: Enum 'Enum' used before it
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
~~
!!! error TS2729: Property 'No' is used before its initialization.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:36:3: 'No' is declared here.
@deco(Enum.No)
~~~~
!!! error TS2450: Enum 'Enum' used before its declaration.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
~~
!!! error TS2729: Property 'No' is used before its initialization.
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:36:3: 'No' is declared here.
greeting: string;

constructor(message: string) {
Expand Down
Loading