From 47cade874c1add7b3f35a0939dbf26f7abd77f6b Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 26 Apr 2017 14:18:17 -0700 Subject: [PATCH] Type-check options.js and index.js (#490) --- src/index.js | 15 ++++++++++----- src/options.js | 30 +++++++++++++++++------------- src/parser/index.js | 4 ++-- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/index.js b/src/index.js index a797b55f15..09a5c40cbe 100755 --- a/src/index.js +++ b/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 } = {}; /** Get a Parser class with plugins applied. */ -function getParserClass(pluginsFromOptions) { +function getParserClass(pluginsFromOptions: $ReadOnlyArray): Class { // Filter out just the plugins that have an actual mixin associated with them. let pluginList = pluginsFromOptions.filter((p) => p === "estree" || p === "flow" || p === "jsx"); diff --git a/src/options.js b/src/options.js index ad75996fce..72d65ed0df 100755 --- a/src/options.js +++ b/src/options.js @@ -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, - strictMode: any, - ranges: boolean, -} = { +export type Options = { + sourceType: "script" | "module"; + sourceFilename?: string; + startLine: number; + allowReturnOutsideFunction: boolean; + allowImportExportEverywhere: boolean; + allowSuperOutsideMethod: boolean; + plugins: $ReadOnlyArray; + 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]; } diff --git a/src/parser/index.js b/src/parser/index.js index 41ce90fde2..7a66b89a54 100644 --- a/src/parser/index.js +++ b/src/parser/index.js @@ -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) => Class } = {}; export default class Parser extends StatementParser { - constructor(options: Options, input: string) { + constructor(options: ?Options, input: string) { options = getOptions(options); super(options, input);