Make htmlBuilder and mathmlBuilder params type-safe. (#1312)

* Make htmlBuilder and mathmlBuilder params type-safe.

Also correct and refine some of the ParseNode types.
This commit is contained in:
Ashish Myles
2018-05-17 10:38:48 -04:00
committed by GitHub
parent 7eed150c48
commit 431434258d
17 changed files with 210 additions and 161 deletions

View File

@@ -7,6 +7,7 @@ import ParseNode from "./ParseNode";
import type Parser from "./Parser";
import type {ArgType, Mode} from "./types";
import type {NodeType} from "./ParseNode";
/**
* The context contains the following properties:
@@ -25,11 +26,11 @@ type EnvContext = {|
* - args: an array of arguments passed to \begin{name}
* - optArgs: an array of optional arguments passed to \begin{name}
*/
type EnvHandler = (
type EnvHandler<NODETYPE: NodeType> = (
context: EnvContext,
args: ParseNode<*>[],
optArgs: (?ParseNode<*>)[],
) => ParseNode<*>;
) => ParseNode<NODETYPE>;
/**
* - numArgs: (default 0) The number of arguments after the \begin{name} function.
@@ -49,13 +50,16 @@ type EnvProps = {
* 2. requires all arguments except argType
* It is generated by `defineEnvironment()` below.
*/
export type EnvSpec = {|
export type EnvSpec<NODETYPE: NodeType> = {|
type: NODETYPE, // Need to use the type to avoid error. See NOTES below.
numArgs: number,
argTypes?: ArgType[],
greediness: number,
allowedInText: boolean,
numOptionalArgs: number,
handler: EnvHandler,
// FLOW TYPE NOTES: Same issue as the notes on the handler of FunctionSpec
// in defineFunction.
handler: EnvHandler<*>,
|};
/**
@@ -63,11 +67,11 @@ export type EnvSpec = {|
* `environments.js` exports this same dictionary again and makes it public.
* `Parser.js` requires this dictionary via `environments.js`.
*/
export const _environments: {[string]: EnvSpec} = {};
export const _environments: {[string]: EnvSpec<*>} = {};
type EnvDefSpec = {|
type EnvDefSpec<NODETYPE: NodeType> = {|
// Unique string to differentiate parse nodes.
type: string,
type: NODETYPE,
// List of functions which use the give handler, htmlBuilder,
// and mathmlBuilder.
@@ -76,34 +80,35 @@ type EnvDefSpec = {|
// Properties that control how the environments are parsed.
props: EnvProps,
handler: EnvHandler,
handler: EnvHandler<NODETYPE>,
// This function returns an object representing the DOM structure to be
// created when rendering the defined LaTeX function.
// TODO: Port buildHTML to flow and make the group and return types explicit.
htmlBuilder: (group: *, options: Options) => *,
// TODO: Port buildHTML to flow and make the return type explicit.
htmlBuilder: (group: ParseNode<NODETYPE>, options: Options) => *,
// This function returns an object representing the MathML structure to be
// created when rendering the defined LaTeX function.
// TODO: Port buildMathML to flow and make the group and return types explicit.
mathmlBuilder: (group: *, options: Options) => *,
// TODO: Port buildMathML to flow and make the return type explicit.
mathmlBuilder: (group: ParseNode<NODETYPE>, options: Options) => *,
|};
export default function defineEnvironment({
export default function defineEnvironment<NODETYPE: NodeType>({
type,
names,
props,
handler,
htmlBuilder,
mathmlBuilder,
}: EnvDefSpec) {
// Set default values of environments
}: EnvDefSpec<NODETYPE>) {
// Set default values of environments.
const data = {
type,
numArgs: props.numArgs || 0,
greediness: 1,
allowedInText: false,
numOptionalArgs: 0,
handler: handler,
handler,
};
for (let i = 0; i < names.length; ++i) {
_environments[names[i]] = data;