(ts) Raise syntax error for abstract methods with a body (#12687)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Sosuke Suzuki
2021-03-02 19:57:58 +09:00
committed by GitHub
parent 0d9ad433b4
commit b416847b61
6 changed files with 147 additions and 1 deletions

View File

@@ -60,6 +60,8 @@ type ParsingContext =
| "TypeParametersOrArguments";
const TSErrors = Object.freeze({
AbstractMethodHasImplementation:
"Method '%0' cannot have an implementation because it is marked abstract.",
ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier",
ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier",
ConstructorHasTypeParameters:
@@ -2933,4 +2935,24 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.unexpected(null, tt._class);
}
}
parseMethod(...args: any[]) {
const method = super.parseMethod(...args);
if (method.abstract) {
const hasBody = this.hasPlugin("estree")
? !!method.value.body
: !!method.body;
if (hasBody) {
const { key } = method;
this.raise(
method.start,
TSErrors.AbstractMethodHasImplementation,
key.type === "Identifier"
? key.name
: `[${this.input.slice(key.start, key.end)}]`,
);
}
}
return method;
}
};