-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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(gatsby): fix subplugin validation #27616
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8224991
Move plugin validation before plugin processing
mxstbr 0852ac8
Fix typescript plugin defaults, TypeScript error
mxstbr 4a45a4b
Clean up code, no mutation, proper types
mxstbr a64cbc1
Cleanup now that TS is properly fixed
mxstbr 606bb0a
Remove case that cant be hit
mxstbr 8c35764
Update packages/gatsby/src/bootstrap/load-plugins/index.ts
mxstbr 8fc5b65
Update packages/gatsby/src/bootstrap/load-plugins/validate.ts
mxstbr 096be01
Clean up subplugin normalization code to make ts happy
mxstbr 038e161
Dont short-circuit early on failing subplugin validation
mxstbr e29330d
Cleanup code
mxstbr cb0c38b
Add pluginOptionsSchema to gatsby-remark-autolink-headers
mxstbr 077eff5
Add unit test for subplugin validation
mxstbr e8b2595
Remove unnecessary devDeps
mxstbr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { | ||
exports.pluginOptionsSchema = function (_ref) { | ||
var Joi = _ref.Joi; | ||
return Joi.object({ | ||
offsetY: Joi.number().integer().description("Signed integer. Vertical offset value in pixels.").default(0), | ||
icon: Joi.alternatives().try(Joi.string(), Joi.boolean()).description("SVG shape inside a template literal or boolean 'false'. Set your own svg or disable icon.").default(true), | ||
className: Joi.string().description("Set your own class for the anchor.").default("anchor"), | ||
maintainCase: Joi.boolean().description("Maintains the case for markdown header."), | ||
removeAccents: Joi.boolean().description("Remove accents from generated headings IDs."), | ||
enableCustomId: Joi.boolean().description("Enable custom header IDs with `{#id}`"), | ||
isIconAfterHeader: Joi.boolean().description("Enable the anchor icon to be inline at the end of the header text."), | ||
elements: Joi.array().items(Joi.string()).description("Specify which type of header tags to link.") | ||
}); | ||
}; | ||
} |
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
66 changes: 66 additions & 0 deletions
66
packages/gatsby-remark-autolink-headers/src/__tests__/gatsby-node.js
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,66 @@ | ||
const { testPluginOptionsSchema } = require(`gatsby-plugin-utils`) | ||
const { pluginOptionsSchema } = require(`../gatsby-node`) | ||
|
||
describe(`pluginOptionsSchema`, () => { | ||
it(`should validate a valid config`, async () => { | ||
// Only the "toVerify" key of the schema will be verified in this test | ||
const { isValid, errors } = await testPluginOptionsSchema( | ||
pluginOptionsSchema, | ||
{ | ||
offsetY: 100, | ||
icon: `<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>`, | ||
className: `custom-class`, | ||
maintainCase: true, | ||
removeAccents: true, | ||
isIconAfterHeader: true, | ||
elements: [`h1`, `h4`], | ||
} | ||
) | ||
|
||
expect(isValid).toBe(true) | ||
expect(errors).toEqual([]) | ||
}) | ||
|
||
it(`should validate a boolean icon`, async () => { | ||
// Only the "toVerify" key of the schema will be verified in this test | ||
const { isValid, errors } = await testPluginOptionsSchema( | ||
pluginOptionsSchema, | ||
{ | ||
icon: false, | ||
} | ||
) | ||
|
||
expect(isValid).toBe(true) | ||
expect(errors).toEqual([]) | ||
}) | ||
|
||
it(`should invalidate an invalid config`, async () => { | ||
// Only the "toVerify" key of the schema will be verified in this test | ||
const { isValid, errors } = await testPluginOptionsSchema( | ||
pluginOptionsSchema, | ||
{ | ||
offsetY: `string`, | ||
icon: 1000, | ||
className: true, | ||
maintainCase: `bla`, | ||
removeAccents: `yes`, | ||
isIconAfterHeader: `yes`, | ||
elements: [1, 2], | ||
} | ||
) | ||
|
||
expect(isValid).toBe(false) | ||
expect(errors).toMatchInlineSnapshot(` | ||
Array [ | ||
"\\"offsetY\\" must be a number", | ||
"\\"icon\\" must be one of [string, boolean]", | ||
"\\"className\\" must be a string", | ||
"\\"maintainCase\\" must be a boolean", | ||
"\\"removeAccents\\" must be a boolean", | ||
"\\"isIconAfterHeader\\" must be a boolean", | ||
"\\"elements[0]\\" must be a string", | ||
"\\"elements[1]\\" must be a string", | ||
] | ||
`) | ||
}) | ||
}) |
33 changes: 33 additions & 0 deletions
33
packages/gatsby-remark-autolink-headers/src/gatsby-node.js
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,33 @@ | ||
if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { | ||
exports.pluginOptionsSchema = ({ Joi }) => | ||
Joi.object({ | ||
offsetY: Joi.number() | ||
.integer() | ||
.description(`Signed integer. Vertical offset value in pixels.`) | ||
.default(0), | ||
icon: Joi.alternatives() | ||
.try(Joi.string(), Joi.boolean()) | ||
.description( | ||
`SVG shape inside a template literal or boolean 'false'. Set your own svg or disable icon.` | ||
) | ||
.default(true), | ||
className: Joi.string() | ||
.description(`Set your own class for the anchor.`) | ||
.default(`anchor`), | ||
maintainCase: Joi.boolean().description( | ||
`Maintains the case for markdown header.` | ||
), | ||
removeAccents: Joi.boolean().description( | ||
`Remove accents from generated headings IDs.` | ||
), | ||
enableCustomId: Joi.boolean().description( | ||
`Enable custom header IDs with \`{#id}\`` | ||
), | ||
isIconAfterHeader: Joi.boolean().description( | ||
`Enable the anchor icon to be inline at the end of the header text.` | ||
), | ||
elements: Joi.array() | ||
.items(Joi.string()) | ||
.description(`Specify which type of header tags to link.`), | ||
}) | ||
} |
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 |
---|---|---|
|
@@ -233,15 +233,20 @@ export function loadPlugins( | |
|
||
// TypeScript support by default! use the user-provided one if it exists | ||
const typescriptPlugin = (config.plugins || []).find( | ||
plugin => | ||
(plugin as IPluginRefObject).resolve === `gatsby-plugin-typescript` || | ||
plugin === `gatsby-plugin-typescript` | ||
plugin => plugin.resolve === `gatsby-plugin-typescript` | ||
) | ||
|
||
if (typescriptPlugin === undefined) { | ||
plugins.push( | ||
processPlugin({ | ||
resolve: require.resolve(`gatsby-plugin-typescript`), | ||
options: { | ||
// TODO(@mxstbr): Do not hard-code these defaults but infer them from the | ||
// pluginOptionsSchema of gatsby-plugin-typescript | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't have to happen for now, but would be a small cleanup in the future should we ever change the typescript plugins' defaults! |
||
allExtensions: false, | ||
isTSX: false, | ||
jsxPragma: `React`, | ||
}, | ||
}) | ||
) | ||
} | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: removed the string case because
plugin
cannot be a string at this point anymore now that we normalize them ahead of time. TypeScript helpfully pointed that out to me!