Skip to content

Commit 153def6

Browse files
HerrCai0907CountBleck
andauthoredNov 21, 2023
fix: diagnose when accessing setter only property (#2800)
Co-authored-by: CountBleck <Mr.YouKnowWhoIAm@protonmail.com>
1 parent d142ac8 commit 153def6

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed
 

‎src/resolver.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,18 @@ export class Resolver extends DiagnosticEmitter {
13631363
}
13641364
case ElementKind.Property: { // someInstance.prop
13651365
let propertyInstance = <Property>target;
1366-
let getterInstance = assert(propertyInstance.getterInstance); // must have a getter
1366+
let getterInstance = propertyInstance.getterInstance;
1367+
if (!getterInstance) {
1368+
// In TS, getters without setters return `undefined`. Since AS doesn't have
1369+
// undefined, we instead diagnose it at compile time, but this isn't
1370+
// compatible with TS.
1371+
let setterInstance = assert(propertyInstance.setterInstance);
1372+
this.errorRelated(
1373+
DiagnosticCode.Property_0_only_has_a_setter_and_is_missing_a_getter,
1374+
targetNode.range, setterInstance.declaration.range, propertyInstance.name
1375+
);
1376+
return null;
1377+
}
13671378
let type = getterInstance.signature.returnType;
13681379
let classReference = type.getClassOrWrapper(this.program);
13691380
if (!classReference) {

‎tests/compiler/getter-setter-errors.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"asc_flags": [
33
],
44
"stderr": [
5+
"AS229: Property 'm' only has a setter and is missing a getter.",
56
"TS2808: Get accessor 'm2' must be at least as accessible as the setter.",
67
"EOF"
78
]

‎tests/compiler/getter-setter-errors.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
class UseNonExistedGetter {
2+
set m(v: string) {}
3+
}
4+
new UseNonExistedGetter().m.toString();
5+
16
class GetSetWithoutDifferenceVisibility {
27
public get m1(): i32 {
38
return 1;
@@ -9,7 +14,6 @@ class GetSetWithoutDifferenceVisibility {
914
}
1015
public set m2(v: i32) {}
1116
}
12-
1317
new GetSetWithoutDifferenceVisibility().m1; // m1 is valid
1418
new GetSetWithoutDifferenceVisibility().m2; // m2 is invalid
1519

0 commit comments

Comments
 (0)
Please sign in to comment.