Skip to content

Commit 9ebe8bf

Browse files
committed
fix: #294
1 parent 8c1671e commit 9ebe8bf

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

lizard_languages/typescript.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,18 @@ def _state_global(self, token):
111111
self.next(self._state_simple_type, token)
112112

113113
def _state_simple_type(self, token):
114-
if token in '{=;':
114+
print(token)
115+
if token == '<':
116+
print(token)
117+
self.next(self._state_generic_type, token)
118+
elif token in '{=;':
115119
self.saved_token = token
116120
self.statemachine_return()
117121

118122
@CodeStateMachine.read_inside_brackets_then("{}")
119123
def _inline_type_annotation(self, _):
120124
self.statemachine_return()
125+
126+
@CodeStateMachine.read_inside_brackets_then("<>")
127+
def _state_generic_type(self, token):
128+
self.statemachine_return()

test/test_languages/testTypeScript.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,60 @@ def test_multiple_functions(self):
157157
self.assertEqual(["helper1", "method1", "method2"], [f.name for f in functions])
158158
self.assertEqual(2, functions[2].cyclomatic_complexity)
159159

160+
def test_type_annotation_with_generic(self):
161+
code = '''
162+
class BaseType {
163+
required(): RequiredType<this> {
164+
return result;
165+
}
166+
}
167+
'''
168+
functions = get_ts_function_list(code)
169+
self.assertEqual(
170+
["required"],
171+
[f.name for f in functions]
172+
)
173+
for f in functions:
174+
self.assertEqual(1, f.cyclomatic_complexity)
175+
176+
177+
def test_abstract_class_methods(self):
178+
code = '''
179+
export abstract class BaseType {
180+
required(): RequiredType<this> {
181+
return result;
182+
}
183+
184+
forbidden(): never {
185+
return this as any as never;
186+
}
187+
188+
options(options: Joi.ValidationOptions) {
189+
return this;
190+
}
191+
192+
strict(isStrict?: boolean) {
193+
return this;
194+
}
195+
196+
default(value: any) {
197+
return this;
198+
}
199+
200+
error(err: Error | Joi.ValidationErrorFunction) {
201+
return this;
202+
}
203+
204+
nullable(): UnionType<this, ConstType<null>> {
205+
return this as any;
206+
}
207+
}
208+
'''
209+
functions = get_ts_function_list(code)
210+
self.assertEqual(
211+
["required", "forbidden", "options", "strict", "default", "error", "nullable"],
212+
[f.name for f in functions]
213+
)
214+
for f in functions:
215+
self.assertEqual(1, f.cyclomatic_complexity)
216+

0 commit comments

Comments
 (0)