Skip to content

Commit 6adbbd9

Browse files
authored
Warn on unused generic exports (#1305)
1 parent 451dd4d commit 6adbbd9

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

src/compiler.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,9 @@ export class Compiler extends DiagnosticEmitter {
695695

696696
// traverse instances
697697
case ElementKind.FUNCTION_PROTOTYPE: {
698-
let functionInstances = (<FunctionPrototype>element).instances;
699-
if (functionInstances) {
698+
let functionPrototype = <FunctionPrototype>element;
699+
let functionInstances = functionPrototype.instances;
700+
if (functionInstances !== null && functionInstances.size > 0) {
700701
// TODO: for (let instance of instances.values()) {
701702
for (let _values = Map_values(functionInstances), i = 0, k = _values.length; i < k; ++i) {
702703
let instance = unchecked(_values[i]);
@@ -707,12 +708,18 @@ export class Compiler extends DiagnosticEmitter {
707708
}
708709
this.ensureModuleExport(instanceName, instance, prefix);
709710
}
711+
} else if (functionPrototype.is(CommonFlags.GENERIC)) {
712+
this.warning(
713+
DiagnosticCode.Exported_generic_function_or_class_has_no_concrete_instances,
714+
functionPrototype.identifierNode.range
715+
);
710716
}
711717
break;
712718
}
713719
case ElementKind.CLASS_PROTOTYPE: {
714-
let classInstances = (<ClassPrototype>element).instances;
715-
if (classInstances) {
720+
let classPrototype = <ClassPrototype>element;
721+
let classInstances = classPrototype.instances;
722+
if (classInstances !== null && classInstances.size > 0) {
716723
// TODO: for (let instance of instances.values()) {
717724
for (let _values = Map_values(classInstances), i = 0, k = _values.length; i < k; ++i) {
718725
let instance = unchecked(_values[i]);
@@ -723,6 +730,11 @@ export class Compiler extends DiagnosticEmitter {
723730
}
724731
this.ensureModuleExport(instanceName, instance, prefix);
725732
}
733+
} else if (classPrototype.is(CommonFlags.GENERIC)) {
734+
this.warning(
735+
DiagnosticCode.Exported_generic_function_or_class_has_no_concrete_instances,
736+
classPrototype.identifierNode.range
737+
);
726738
}
727739
break;
728740
}

src/diagnosticMessages.generated.ts

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export enum DiagnosticCode {
4848
Property_0_only_has_a_setter_and_is_missing_a_getter = 229,
4949
_0_keyword_cannot_be_used_here = 230,
5050
A_class_with_a_constructor_explicitly_returning_something_else_than_this_must_be_final = 231,
51+
Exported_generic_function_or_class_has_no_concrete_instances = 232,
5152
Type_0_is_cyclic_Module_will_include_deferred_garbage_collection = 900,
5253
Importing_the_table_disables_some_indirect_call_optimizations = 901,
5354
Exporting_the_table_disables_some_indirect_call_optimizations = 902,
@@ -221,6 +222,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
221222
case 229: return "Property '{0}' only has a setter and is missing a getter.";
222223
case 230: return "'{0}' keyword cannot be used here.";
223224
case 231: return "A class with a constructor explicitly returning something else than 'this' must be '@final'.";
225+
case 232: return "Exported generic function or class has no concrete instances.";
224226
case 900: return "Type '{0}' is cyclic. Module will include deferred garbage collection.";
225227
case 901: return "Importing the table disables some indirect call optimizations.";
226228
case 902: return "Exporting the table disables some indirect call optimizations.";

src/diagnosticMessages.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"Property '{0}' only has a setter and is missing a getter.": 229,
4242
"'{0}' keyword cannot be used here.": 230,
4343
"A class with a constructor explicitly returning something else than 'this' must be '@final'.": 231,
44+
"Exported generic function or class has no concrete instances.": 232,
4445

4546
"Type '{0}' is cyclic. Module will include deferred garbage collection.": 900,
4647
"Importing the table disables some indirect call optimizations.": 901,

tests/compiler/export-generic.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"asc_flags": [
3+
"--runtime none"
4+
],
5+
"stderr": [
6+
"AS232: Exported generic function or class has no concrete instances.",
7+
"export function testFunction<T>",
8+
"AS232: Exported generic function or class has no concrete instances.",
9+
"export class TestClass<T>",
10+
"AS232: Exported generic function or class has no concrete instances.",
11+
"public testMethod<T>()",
12+
"AS232: Exported generic function or class has no concrete instances.",
13+
"export function testNamespacedFunction<T>",
14+
"AS232: Exported generic function or class has no concrete instances.",
15+
"export class TestNamespacedClass<T>"
16+
]
17+
}

tests/compiler/export-generic.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function testFunction<T>(): void {}
2+
3+
export class TestClass<T> {}
4+
5+
export class Foo {
6+
public testMethod<T>(): void {}
7+
}
8+
9+
export namespace test {
10+
export function testNamespacedFunction<T>(): void {}
11+
export class TestNamespacedClass<T> {}
12+
}
13+
14+
ERROR("EOF");

0 commit comments

Comments
 (0)