Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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\\" }.
"
`);
});