fix(api): do not report the same deprecation warning more than once (#5774)

A quick fix for suppressing duplicate deprecation warnings.
master
Ika 2019-01-20 15:38:19 +08:00 committed by GitHub
parent 3d7970a673
commit a9fd8e2cf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 11 deletions

View File

@ -46,6 +46,8 @@ class FlagSchema extends vnopts.ChoiceSchema {
}
}
let hasDeprecationWarned;
function normalizeOptions(
options,
optionInfos,
@ -60,7 +62,25 @@ function normalizeOptions(
const descriptor = isCLI ? cliDescriptor : vnopts.apiDescriptor;
const schemas = optionInfosToSchemas(optionInfos, { isCLI });
return vnopts.normalize(options, schemas, { logger, unknown, descriptor });
const normalizer = new vnopts.Normalizer(schemas, {
logger,
unknown,
descriptor
});
const shouldSuppressDuplicateDeprecationWarnings = logger !== false;
if (shouldSuppressDuplicateDeprecationWarnings && hasDeprecationWarned) {
normalizer._hasDeprecationWarned = hasDeprecationWarned;
}
const normalized = normalizer.normalize(options);
if (shouldSuppressDuplicateDeprecationWarnings) {
hasDeprecationWarned = normalizer._hasDeprecationWarned;
}
return normalized;
}
function optionInfosToSchemas(optionInfos, { isCLI }) {

View File

@ -1,10 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`API format with deprecated parser (babylon) should work 1`] = `
"{ parser: \\"babylon\\" } is deprecated; we now treat it as { parser: \\"babel\\" }.
"
`;
exports[`API format with deprecated parser (postcss) should work 1`] = `
"{ parser: \\"postcss\\" } is deprecated; we now treat it as { parser: \\"css\\" }.
"

View File

@ -25,9 +25,13 @@ test("API format with deprecated parser (postcss) should work", () => {
expect(warnings).toMatchSnapshot();
});
test("API format with deprecated parser (babylon) should work", () => {
expect(() =>
prettier.format("hello_world( )", { parser: "babylon" })
).not.toThrowError();
expect(warnings).toMatchSnapshot();
test("API format with deprecated parser (babylon) should work and do not report the same deprecation warning more than once", () => {
expect(() => {
prettier.format("hello_world( )", { parser: "babylon" });
prettier.format("hello_world( )", { parser: "babylon" });
}).not.toThrowError();
expect(warnings).toMatchInlineSnapshot(`
"{ parser: \\"babylon\\" } is deprecated; we now treat it as { parser: \\"babel\\" }.
"
`);
});