Skip to content

fix: diagnose when accessing setter only property #2800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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