Type-check options.js and index.js (#490)
This commit is contained in:
parent
7627c5a2be
commit
47cade874c
15
src/index.js
15
src/index.js
@ -1,3 +1,6 @@
|
||||
// @flow
|
||||
|
||||
import type { Options } from "./options";
|
||||
import Parser, { plugins } from "./parser";
|
||||
import "./parser/util";
|
||||
import "./parser/statement";
|
||||
@ -11,6 +14,8 @@ import { types as tokTypes } from "./tokenizer/types";
|
||||
import "./tokenizer";
|
||||
import "./tokenizer/context";
|
||||
|
||||
import type { Expression, File } from "./types";
|
||||
|
||||
import estreePlugin from "./plugins/estree";
|
||||
import flowPlugin from "./plugins/flow";
|
||||
import jsxPlugin from "./plugins/jsx";
|
||||
@ -18,11 +23,11 @@ plugins.estree = estreePlugin;
|
||||
plugins.flow = flowPlugin;
|
||||
plugins.jsx = jsxPlugin;
|
||||
|
||||
export function parse(input, options) {
|
||||
export function parse(input: string, options?: Options): File {
|
||||
return getParser(options, input).parse();
|
||||
}
|
||||
|
||||
export function parseExpression(input, options) {
|
||||
export function parseExpression(input: string, options?: Options): Expression {
|
||||
const parser = getParser(options, input);
|
||||
if (parser.options.strictMode) {
|
||||
parser.state.strict = true;
|
||||
@ -33,15 +38,15 @@ export function parseExpression(input, options) {
|
||||
|
||||
export { tokTypes };
|
||||
|
||||
function getParser(options, input) {
|
||||
function getParser(options: ?Options, input: string): Parser {
|
||||
const cls = options && options.plugins ? getParserClass(options.plugins) : Parser;
|
||||
return new cls(options, input);
|
||||
}
|
||||
|
||||
const parserClassCache = {};
|
||||
const parserClassCache: { [key: string]: Class<Parser> } = {};
|
||||
|
||||
/** Get a Parser class with plugins applied. */
|
||||
function getParserClass(pluginsFromOptions) {
|
||||
function getParserClass(pluginsFromOptions: $ReadOnlyArray<string>): Class<Parser> {
|
||||
// Filter out just the plugins that have an actual mixin associated with them.
|
||||
let pluginList = pluginsFromOptions.filter((p) => p === "estree" || p === "flow" || p === "jsx");
|
||||
|
||||
|
||||
@ -1,17 +1,21 @@
|
||||
// @flow
|
||||
|
||||
// A second optional argument can be given to further configure
|
||||
// the parser process. These options are recognized:
|
||||
|
||||
export const defaultOptions: {
|
||||
sourceType: string,
|
||||
sourceFilename: any,
|
||||
startLine: number,
|
||||
allowReturnOutsideFunction: boolean,
|
||||
allowImportExportEverywhere: boolean,
|
||||
allowSuperOutsideMethod: boolean,
|
||||
plugins: Array<string>,
|
||||
strictMode: any,
|
||||
ranges: boolean,
|
||||
} = {
|
||||
export type Options = {
|
||||
sourceType: "script" | "module";
|
||||
sourceFilename?: string;
|
||||
startLine: number;
|
||||
allowReturnOutsideFunction: boolean;
|
||||
allowImportExportEverywhere: boolean;
|
||||
allowSuperOutsideMethod: boolean;
|
||||
plugins: $ReadOnlyArray<string>;
|
||||
strictMode: ?boolean;
|
||||
ranges: boolean;
|
||||
};
|
||||
|
||||
export const defaultOptions: Options = {
|
||||
// Source type ("script" or "module") for different semantics
|
||||
sourceType: "script",
|
||||
// Source filename.
|
||||
@ -44,8 +48,8 @@ export const defaultOptions: {
|
||||
|
||||
// Interpret and default an options object
|
||||
|
||||
export function getOptions(opts?: Object): Object {
|
||||
const options = {};
|
||||
export function getOptions(opts: ?Options): Options {
|
||||
const options: any = {};
|
||||
for (const key in defaultOptions) {
|
||||
options[key] = opts && key in opts ? opts[key] : defaultOptions[key];
|
||||
}
|
||||
|
||||
@ -5,10 +5,10 @@ import type { File } from "../types";
|
||||
import { getOptions } from "../options";
|
||||
import StatementParser from "./statement";
|
||||
|
||||
export const plugins = {};
|
||||
export const plugins: { [name: string]: (superClass: Class<Parser>) => Class<Parser> } = {};
|
||||
|
||||
export default class Parser extends StatementParser {
|
||||
constructor(options: Options, input: string) {
|
||||
constructor(options: ?Options, input: string) {
|
||||
options = getOptions(options);
|
||||
super(options, input);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user