Skip to content

Commit

Permalink
fix(api): do not report the same deprecation warning more than once (#…
Browse files Browse the repository at this point in the history
…5774)

A quick fix for suppressing duplicate deprecation warnings.
  • Loading branch information
ikatyang committed Jan 20, 2019
1 parent 3d7970a commit a9fd8e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
22 changes: 21 additions & 1 deletion src/main/options-normalizer.js
Expand Up @@ -46,6 +46,8 @@ class FlagSchema extends vnopts.ChoiceSchema {
}
}

let hasDeprecationWarned;

function normalizeOptions(
options,
optionInfos,
Expand All @@ -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 }) {
Expand Down
@@ -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\\" }.
"
Expand Down
14 changes: 9 additions & 5 deletions tests_integration/__tests__/deprecated-parser.js
Expand Up @@ -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\\" }.
"
`);
});

0 comments on commit a9fd8e2

Please sign in to comment.