Skip to content

Commit a05e479

Browse files
authored
Re-port isUsedInFunctionOrInstanceProperty (#1872)
1 parent cf3e0c2 commit a05e479

File tree

5 files changed

+117
-28
lines changed

5 files changed

+117
-28
lines changed

internal/checker/checker.go

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,46 +1905,64 @@ func (c *Checker) isBlockScopedNameDeclaredBeforeUse(declaration *ast.Node, usag
19051905
}
19061906

19071907
func (c *Checker) isUsedInFunctionOrInstanceProperty(usage *ast.Node, declaration *ast.Node, declContainer *ast.Node) bool {
1908-
for current := usage; current != nil && current != declContainer; current = current.Parent {
1908+
return ast.FindAncestorOrQuit(usage, func(current *ast.Node) ast.FindAncestorResult {
1909+
if current == declContainer {
1910+
return ast.FindAncestorQuit
1911+
}
19091912
if ast.IsFunctionLike(current) {
1910-
return ast.GetImmediatelyInvokedFunctionExpression(current) == nil
1913+
return ast.ToFindAncestorResult(ast.GetImmediatelyInvokedFunctionExpression(current) == nil)
19111914
}
19121915
if ast.IsClassStaticBlockDeclaration(current) {
1913-
return declaration.Pos() < usage.Pos()
1916+
return ast.ToFindAncestorResult(declaration.Pos() < usage.Pos())
19141917
}
1915-
if current.Parent != nil && ast.IsPropertyDeclaration(current.Parent) && current.Parent.Initializer() == current {
1916-
if ast.IsStatic(current.Parent) {
1917-
if ast.IsMethodDeclaration(declaration) {
1918-
return true
1919-
}
1920-
if ast.IsPropertyDeclaration(declaration) && ast.GetContainingClass(usage) == ast.GetContainingClass(declaration) {
1921-
propName := declaration.Name()
1922-
if ast.IsIdentifier(propName) || ast.IsPrivateIdentifier(propName) {
1923-
t := c.getTypeOfSymbol(c.getSymbolOfDeclaration(declaration))
1924-
staticBlocks := core.Filter(declaration.Parent.Members(), ast.IsClassStaticBlockDeclaration)
1925-
if c.isPropertyInitializedInStaticBlocks(propName, t, staticBlocks, declaration.Parent.Pos(), current.Pos()) {
1926-
return true
1918+
1919+
if current.Parent != nil && ast.IsPropertyDeclaration(current.Parent) {
1920+
propertyDeclaration := current.Parent
1921+
initializerOfProperty := propertyDeclaration.Initializer() == current
1922+
if initializerOfProperty {
1923+
if ast.IsStatic(current.Parent) {
1924+
if ast.IsMethodDeclaration(declaration) {
1925+
return ast.FindAncestorTrue
1926+
}
1927+
if ast.IsPropertyDeclaration(declaration) && ast.GetContainingClass(usage) == ast.GetContainingClass(declaration) {
1928+
propName := declaration.Name()
1929+
if ast.IsIdentifier(propName) || ast.IsPrivateIdentifier(propName) {
1930+
t := c.getTypeOfSymbol(c.getSymbolOfDeclaration(declaration))
1931+
staticBlocks := core.Filter(declaration.Parent.Members(), ast.IsClassStaticBlockDeclaration)
1932+
if c.isPropertyInitializedInStaticBlocks(propName, t, staticBlocks, declaration.Parent.Pos(), current.Pos()) {
1933+
return ast.FindAncestorTrue
1934+
}
19271935
}
19281936
}
1929-
}
1930-
} else {
1931-
isDeclarationInstanceProperty := ast.IsPropertyDeclaration(declaration) && !ast.IsStatic(declaration)
1932-
if !isDeclarationInstanceProperty || ast.GetContainingClass(usage) != ast.GetContainingClass(declaration) {
1933-
return true
1937+
} else {
1938+
isDeclarationInstanceProperty := ast.IsPropertyDeclaration(declaration) && !ast.IsStatic(declaration)
1939+
if !isDeclarationInstanceProperty || ast.GetContainingClass(usage) != ast.GetContainingClass(declaration) {
1940+
return ast.FindAncestorTrue
1941+
}
19341942
}
19351943
}
19361944
}
1937-
if current.Parent != nil && ast.IsDecorator(current.Parent) && current.Parent.AsDecorator().Expression == current {
1945+
1946+
if current.Parent != nil && ast.IsDecorator(current.Parent) {
19381947
decorator := current.Parent.AsDecorator()
1939-
if ast.IsParameter(decorator.Parent) {
1940-
return c.isUsedInFunctionOrInstanceProperty(decorator.Parent.Parent.Parent, declaration, declContainer)
1941-
}
1942-
if ast.IsMethodDeclaration(decorator.Parent) {
1943-
return c.isUsedInFunctionOrInstanceProperty(decorator.Parent.Parent, declaration, declContainer)
1948+
if decorator.Expression == current {
1949+
if ast.IsParameter(decorator.Parent) {
1950+
if c.isUsedInFunctionOrInstanceProperty(decorator.Parent.Parent.Parent, declaration, declContainer) {
1951+
return ast.FindAncestorTrue
1952+
}
1953+
return ast.FindAncestorQuit
1954+
}
1955+
if ast.IsMethodDeclaration(decorator.Parent) {
1956+
if c.isUsedInFunctionOrInstanceProperty(decorator.Parent.Parent, declaration, declContainer) {
1957+
return ast.FindAncestorTrue
1958+
}
1959+
return ast.FindAncestorQuit
1960+
}
19441961
}
19451962
}
1946-
}
1947-
return false
1963+
1964+
return ast.FindAncestorFalse
1965+
}) != nil
19481966
}
19491967

19501968
func isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration *ast.Node, usage *ast.Node, declContainer *ast.Node) bool {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/blockedScopeVariableNotUnused1.ts] ////
2+
3+
//// [blockedScopeVariableNotUnused1.ts]
4+
export function foo() {
5+
const _fn = () => {
6+
;(() => numFilesSelected)()
7+
}
8+
9+
const numFilesSelected = 1
10+
}
11+
12+
13+
//// [blockedScopeVariableNotUnused1.js]
14+
"use strict";
15+
Object.defineProperty(exports, "__esModule", { value: true });
16+
exports.foo = foo;
17+
function foo() {
18+
const _fn = () => {
19+
;
20+
(() => numFilesSelected)();
21+
};
22+
const numFilesSelected = 1;
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/blockedScopeVariableNotUnused1.ts] ////
2+
3+
=== blockedScopeVariableNotUnused1.ts ===
4+
export function foo() {
5+
>foo : Symbol(foo, Decl(blockedScopeVariableNotUnused1.ts, 0, 0))
6+
7+
const _fn = () => {
8+
>_fn : Symbol(_fn, Decl(blockedScopeVariableNotUnused1.ts, 1, 7))
9+
10+
;(() => numFilesSelected)()
11+
>numFilesSelected : Symbol(numFilesSelected, Decl(blockedScopeVariableNotUnused1.ts, 5, 7))
12+
}
13+
14+
const numFilesSelected = 1
15+
>numFilesSelected : Symbol(numFilesSelected, Decl(blockedScopeVariableNotUnused1.ts, 5, 7))
16+
}
17+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/blockedScopeVariableNotUnused1.ts] ////
2+
3+
=== blockedScopeVariableNotUnused1.ts ===
4+
export function foo() {
5+
>foo : () => void
6+
7+
const _fn = () => {
8+
>_fn : () => void
9+
>() => { ;(() => numFilesSelected)() } : () => void
10+
11+
;(() => numFilesSelected)()
12+
>(() => numFilesSelected)() : number
13+
>(() => numFilesSelected) : () => number
14+
>() => numFilesSelected : () => number
15+
>numFilesSelected : 1
16+
}
17+
18+
const numFilesSelected = 1
19+
>numFilesSelected : 1
20+
>1 : 1
21+
}
22+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @strict: true
2+
3+
export function foo() {
4+
const _fn = () => {
5+
;(() => numFilesSelected)()
6+
}
7+
8+
const numFilesSelected = 1
9+
}

0 commit comments

Comments
 (0)