-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add runtime validation for options (#468)
Fixes #467 This uses the libraries ts-interface-builder and ts-interface-checker to codegen a data structure of the options TS type and then check it on every `transform` invocation. I tried a few approaches here, including generating JSON schema and then validating that, but that this seemed to be pretty smooth from a build system standpoint. I timed this on 1000 compilations of a large file and any difference was well within the time variability, so it seems fine to run this type validation on every `transform` invocation. It could potentially be moved out into a separate routine if perf is ever an issue, though.
- Loading branch information
1 parent
5ae23c8
commit fff991e
Showing
6 changed files
with
141 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* This module was automatically generated by `ts-interface-builder` | ||
*/ | ||
import * as t from "ts-interface-checker"; | ||
// tslint:disable:object-literal-key-quotes | ||
|
||
export const Transform = t.union( | ||
t.lit("jsx"), | ||
t.lit("typescript"), | ||
t.lit("flow"), | ||
t.lit("imports"), | ||
t.lit("react-hot-loader"), | ||
); | ||
|
||
export const SourceMapOptions = t.iface([], { | ||
compiledFilename: "string", | ||
}); | ||
|
||
export const Options = t.iface([], { | ||
transforms: t.array("Transform"), | ||
jsxPragma: t.opt("string"), | ||
jsxFragmentPragma: t.opt("string"), | ||
enableLegacyTypeScriptModuleInterop: t.opt("boolean"), | ||
enableLegacyBabel5ModuleInterop: t.opt("boolean"), | ||
sourceMapOptions: t.opt("SourceMapOptions"), | ||
filePath: t.opt("string"), | ||
production: t.opt("boolean"), | ||
}); | ||
|
||
const exportedTypeSuite: t.ITypeSuite = { | ||
Transform, | ||
SourceMapOptions, | ||
Options, | ||
}; | ||
export default exportedTypeSuite; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {createCheckers} from "ts-interface-checker"; | ||
import OptionsGenTypes from "./Options-gen-types"; | ||
|
||
const {Options: OptionsChecker} = createCheckers(OptionsGenTypes); | ||
|
||
export type Transform = "jsx" | "typescript" | "flow" | "imports" | "react-hot-loader"; | ||
|
||
export interface SourceMapOptions { | ||
/** | ||
* The name to use in the "file" field of the source map. This should be the name of the compiled | ||
* file. | ||
*/ | ||
compiledFilename: string; | ||
} | ||
|
||
export interface Options { | ||
transforms: Array<Transform>; | ||
/** | ||
* If specified, function name to use in place of React.createClass when compiling JSX. | ||
*/ | ||
jsxPragma?: string; | ||
/** | ||
* If specified, function name to use in place of React.Fragment when compiling JSX. | ||
*/ | ||
jsxFragmentPragma?: string; | ||
/** | ||
* If true, replicate the import behavior of TypeScript's esModuleInterop: false. | ||
*/ | ||
enableLegacyTypeScriptModuleInterop?: boolean; | ||
/** | ||
* If true, replicate the import behavior Babel 5 and babel-plugin-add-module-exports. | ||
*/ | ||
enableLegacyBabel5ModuleInterop?: boolean; | ||
/** | ||
* If specified, we also return a RawSourceMap object alongside the code. Currently, source maps | ||
* simply map each line to the original line without any mappings within lines, since Sucrase | ||
* preserves line numbers. filePath must be specified if this option is enabled. | ||
*/ | ||
sourceMapOptions?: SourceMapOptions; | ||
/** | ||
* File path to use in error messages, React display names, and source maps. | ||
*/ | ||
filePath?: string; | ||
/** | ||
* If specified, omit any development-specific code in the output. | ||
*/ | ||
production?: boolean; | ||
} | ||
|
||
export function validateOptions(options: Options): void { | ||
OptionsChecker.strictCheck(options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters