Skip to content
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

fix: diagnose when accessing setter only property #2800

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 11 additions & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,17 @@ 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) {
// It should compile as (undefined) for compatibility with ts.
// Since AS don't have undefined, diagnose it in compile time would be better although it does cause compatibility issues
HerrCai0907 marked this conversation as resolved.
Show resolved Hide resolved
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