-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[babel 8] type checking preset-flow options (#12751)
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
- Loading branch information
1 parent
b0d83da
commit 16d8300
Showing
5 changed files
with
86 additions
and
2 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,25 @@ | ||
import { OptionValidator } from "@babel/helper-validator-option"; | ||
const v = new OptionValidator("@babel/preset-flow"); | ||
|
||
export default function normalizeOptions(options = {}) { | ||
let { all } = options; | ||
const { allowDeclareFields } = options; | ||
|
||
if (process.env.BABEL_8_BREAKING) { | ||
v.invariant( | ||
!("allowDeclareFields" in options), | ||
`Since Babel 8, \`declare property: A\` is always supported, and the "allowDeclareFields" option is no longer available. Please remove it from your config.`, | ||
); | ||
const TopLevelOptions = { | ||
all: "all", | ||
}; | ||
v.validateTopLevelOptions(options, TopLevelOptions); | ||
all = v.validateBooleanOption(TopLevelOptions.all, options.all); | ||
return { all }; | ||
} else { | ||
return { | ||
all, | ||
allowDeclareFields, | ||
}; | ||
} | ||
} |
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,55 @@ | ||
import normalizeOptions from "../src/normalize-options"; | ||
describe("normalize options", () => { | ||
(process.env.BABEL_8_BREAKING ? describe : describe.skip)("Babel 8", () => { | ||
it("should throw on unknown options", () => { | ||
expect(() => normalizeOptions({ al: true })) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"@babel/preset-flow: 'al' is not a valid top-level option. | ||
- Did you mean 'all'?" | ||
`); | ||
}); | ||
it("should throw on Babel 7 `allowDeclareFields` option", () => { | ||
expect(() => | ||
normalizeOptions({ allowDeclareFields: true }), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"@babel/preset-flow: Since Babel 8, \`declare property: A\` is always supported, and the \\"allowDeclareFields\\" option is no longer available. Please remove it from your config."`, | ||
); | ||
}); | ||
it.each(["all"])("should throw when `%p` is not a boolean", optionName => { | ||
expect(() => normalizeOptions({ [optionName]: 0 })).toThrow( | ||
`@babel/preset-flow: '${optionName}' option must be a boolean.`, | ||
); | ||
}); | ||
it("should not throw when options is not defined", () => { | ||
expect(() => normalizeOptions()).not.toThrowError(); | ||
}); | ||
it("default values", () => { | ||
expect(normalizeOptions({})).toMatchInlineSnapshot(` | ||
Object { | ||
"all": undefined, | ||
} | ||
`); | ||
}); | ||
}); | ||
(process.env.BABEL_8_BREAKING ? describe.skip : describe)("Babel 7", () => { | ||
it("should not throw on unknown options", () => { | ||
expect(() => | ||
normalizeOptions({ allDeclareField: true }), | ||
).not.toThrowError(); | ||
}); | ||
it.each(["all", "allowDeclareFields"])( | ||
"should not throw when `%p` is not a boolean", | ||
optionName => { | ||
expect(() => normalizeOptions({ [optionName]: 0 })).not.toThrowError(); | ||
}, | ||
); | ||
it("default values", () => { | ||
expect(normalizeOptions({})).toMatchInlineSnapshot(` | ||
Object { | ||
"all": undefined, | ||
"allowDeclareFields": undefined, | ||
} | ||
`); | ||
}); | ||
}); | ||
}); |
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