add expectPlugin which throws an error with the missing plugin

This commit is contained in:
Moti Zilberman 2016-10-15 00:49:37 +03:00 committed by Henry Zhu
parent 2f76fc88be
commit cf3ebacf4f
2 changed files with 14 additions and 1 deletions

View File

@ -10,7 +10,7 @@ import CommentsParser from "./comments";
// message.
export default class LocationParser extends CommentsParser {
raise(pos: number, message: string): empty {
raise(pos: number, message: string, missingPluginName: string): empty {
const loc = getLineInfo(this.input, pos);
message += ` (${loc.line}:${loc.column})`;
// $FlowIgnore
@ -19,6 +19,9 @@ export default class LocationParser extends CommentsParser {
);
err.pos = pos;
err.loc = loc;
if (missingPluginName) {
err.missingPlugin = missingPluginName;
}
throw err;
}
}

View File

@ -109,4 +109,14 @@ export default class UtilParser extends Tokenizer {
}
throw this.raise(pos != null ? pos : this.state.start, messageOrType);
}
expectPlugin(name: string): void {
if (!this.hasPlugin(name)) {
throw this.raise(
this.state.start,
`This experimental syntax requires enabling the parser plugin: ${name}`,
name,
);
}
}
}