Skip to content

Commit

Permalink
fix: diagnose when accessing setter only property (#2800)
Browse files Browse the repository at this point in the history
Co-authored-by: CountBleck <Mr.YouKnowWhoIAm@protonmail.com>
  • Loading branch information
HerrCai0907 and CountBleck authored Nov 21, 2023
1 parent d142ac8 commit 153def6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,18 @@ export class Resolver extends DiagnosticEmitter {
}
case ElementKind.Property: { // someInstance.prop
let propertyInstance = <Property>target;
let getterInstance = assert(propertyInstance.getterInstance); // must have a getter
let getterInstance = propertyInstance.getterInstance;
if (!getterInstance) {
// In TS, getters without setters return `undefined`. Since AS doesn't have
// undefined, we instead diagnose it at compile time, but this isn't
// compatible with TS.
let setterInstance = assert(propertyInstance.setterInstance);
this.errorRelated(
DiagnosticCode.Property_0_only_has_a_setter_and_is_missing_a_getter,
targetNode.range, setterInstance.declaration.range, propertyInstance.name
);
return null;
}
let type = getterInstance.signature.returnType;
let classReference = type.getClassOrWrapper(this.program);
if (!classReference) {
Expand Down
1 change: 1 addition & 0 deletions tests/compiler/getter-setter-errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"asc_flags": [
],
"stderr": [
"AS229: Property 'm' only has a setter and is missing a getter.",
"TS2808: Get accessor 'm2' must be at least as accessible as the setter.",
"EOF"
]
Expand Down
6 changes: 5 additions & 1 deletion tests/compiler/getter-setter-errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class UseNonExistedGetter {
set m(v: string) {}
}
new UseNonExistedGetter().m.toString();

class GetSetWithoutDifferenceVisibility {
public get m1(): i32 {
return 1;
Expand All @@ -9,7 +14,6 @@ class GetSetWithoutDifferenceVisibility {
}
public set m2(v: i32) {}
}

new GetSetWithoutDifferenceVisibility().m1; // m1 is valid
new GetSetWithoutDifferenceVisibility().m2; // m2 is invalid

Expand Down

0 comments on commit 153def6

Please sign in to comment.