From 6aa42c0c7f1cd05d23bb35e7f4b56e7bf4e46aa6 Mon Sep 17 00:00:00 2001 From: Laurie Date: Mon, 19 Oct 2020 12:06:19 -0400 Subject: [PATCH 01/10] Fix default --- packages/gatsby-plugin-typescript/src/gatsby-node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-typescript/src/gatsby-node.js b/packages/gatsby-plugin-typescript/src/gatsby-node.js index 51281a3d77627..29bf30ea5bc28 100644 --- a/packages/gatsby-plugin-typescript/src/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/gatsby-node.js @@ -38,7 +38,7 @@ function onCreateWebpackConfig({ actions, loaders }) { if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { exports.pluginOptionsSchema = ({ Joi }) => Joi.object({ - isTSX: Joi.boolean().description(`Enables jsx parsing.`).default(true), + isTSX: Joi.boolean().description(`Enables jsx parsing.`).default(false), jsxPragma: Joi.string() .description( `Replace the function used when compiling JSX expressions.` From 9c3c09b4b3d0c9b0fa8fcb86b69fbce2937a240e Mon Sep 17 00:00:00 2001 From: Laurie Date: Mon, 19 Oct 2020 12:32:52 -0400 Subject: [PATCH 02/10] validate matching options, add associated test --- .../src/__tests__/gatsby-node.js | 12 ++++++- .../src/gatsby-node.js | 32 ++++++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js index b3fe15e7e1053..e0005637e0264 100644 --- a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js @@ -92,12 +92,22 @@ describe(`gatsby-plugin-typescript`, () => { it(`should validate the schema`, () => { const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, { - isTSX: true, + isTSX: false, jsxPragma: `ReactFunction`, allExtensions: false, }) expect(isValid).toBe(true) }) + + it(`throws error when isTSX doesn't match allExtensions`, () => { + expect( + testPluginOptionsSchema(pluginOptionsSchema, { + isTSX: true, + jsxPragma: `ReactFunction`, + allExtensions: false, + }) + ).toThrow(`"isTSX" and "allExtensions" must match`) + }) }) }) diff --git a/packages/gatsby-plugin-typescript/src/gatsby-node.js b/packages/gatsby-plugin-typescript/src/gatsby-node.js index 29bf30ea5bc28..a40bab0ffdb3e 100644 --- a/packages/gatsby-plugin-typescript/src/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/gatsby-node.js @@ -37,17 +37,27 @@ function onCreateWebpackConfig({ actions, loaders }) { if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { exports.pluginOptionsSchema = ({ Joi }) => - Joi.object({ - isTSX: Joi.boolean().description(`Enables jsx parsing.`).default(false), - jsxPragma: Joi.string() - .description( - `Replace the function used when compiling JSX expressions.` - ) - .default(`React`), - allExtensions: Joi.boolean() - .description(`Indicates that every file should be parsed as TS or TSX.`) - .default(false), - }) + Joi.object() + .keys({ + isTSX: Joi.boolean().description(`Enables jsx parsing.`).default(false), + jsxPragma: Joi.string() + .description( + `Replace the function used when compiling JSX expressions.` + ) + .default(`React`), + allExtensions: Joi.boolean() + .description( + `Indicates that every file should be parsed as TS or TSX.` + ) + .default(false), + }) + .external(({ isTSX, allExtensions }) => { + if (isTSX !== allExtensions) { + throw new Error( + `Invalid plugin options for "gatsby-plugin-typescript": "isTSX" and "allExtensions" must match.` + ) + } + }) } exports.resolvableExtensions = resolvableExtensions From 40b40b7b50b0f29218f13eb6a9c22d3ff3d2d8c3 Mon Sep 17 00:00:00 2001 From: Laurie Date: Mon, 19 Oct 2020 13:03:09 -0400 Subject: [PATCH 03/10] fix error test --- .../src/__tests__/gatsby-node.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js index e0005637e0264..e0db3d1dc3153 100644 --- a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js @@ -100,14 +100,14 @@ describe(`gatsby-plugin-typescript`, () => { expect(isValid).toBe(true) }) - it(`throws error when isTSX doesn't match allExtensions`, () => { - expect( - testPluginOptionsSchema(pluginOptionsSchema, { - isTSX: true, - jsxPragma: `ReactFunction`, - allExtensions: false, - }) - ).toThrow(`"isTSX" and "allExtensions" must match`) + it(`should break when isTSX doesn't match allExtensions`, () => { + const { errors } = testPluginOptionsSchema(pluginOptionsSchema, { + isTSX: true, + jsxPragma: `ReactFunction`, + allExtensions: false, + }) + + expect(errors).toEqual(`"isTSX" and "allExtensions" must match`) }) }) }) From 27d8122f0d4a69b69bfb2a1653be34004fa33285 Mon Sep 17 00:00:00 2001 From: Laurie Date: Mon, 19 Oct 2020 13:14:41 -0400 Subject: [PATCH 04/10] it's only the mismatch that matters --- .../gatsby-plugin-typescript/src/__tests__/gatsby-node.js | 4 +++- packages/gatsby-plugin-typescript/src/gatsby-node.js | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js index e0db3d1dc3153..285de9d04db2d 100644 --- a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js @@ -107,7 +107,9 @@ describe(`gatsby-plugin-typescript`, () => { allExtensions: false, }) - expect(errors).toEqual(`"isTSX" and "allExtensions" must match`) + expect(errors).toEqual( + `"isTSX" is true requires "allExtensions" to be true` + ) }) }) }) diff --git a/packages/gatsby-plugin-typescript/src/gatsby-node.js b/packages/gatsby-plugin-typescript/src/gatsby-node.js index a40bab0ffdb3e..b704040437cc0 100644 --- a/packages/gatsby-plugin-typescript/src/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/gatsby-node.js @@ -52,9 +52,9 @@ if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { .default(false), }) .external(({ isTSX, allExtensions }) => { - if (isTSX !== allExtensions) { + if (isTSX && !allExtensions) { throw new Error( - `Invalid plugin options for "gatsby-plugin-typescript": "isTSX" and "allExtensions" must match.` + `Invalid plugin options for "gatsby-plugin-typescript": "isTSX" is true requires "allExtensions" to be true.` ) } }) From 05e36a1eba8b0a7022a6c171c373aec7e34ccc24 Mon Sep 17 00:00:00 2001 From: Laurie Date: Mon, 19 Oct 2020 14:02:23 -0400 Subject: [PATCH 05/10] remove test, the utility can't handle external calls yet --- .../src/__tests__/gatsby-node.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js index 285de9d04db2d..bf94356c433ed 100644 --- a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js @@ -99,17 +99,5 @@ describe(`gatsby-plugin-typescript`, () => { expect(isValid).toBe(true) }) - - it(`should break when isTSX doesn't match allExtensions`, () => { - const { errors } = testPluginOptionsSchema(pluginOptionsSchema, { - isTSX: true, - jsxPragma: `ReactFunction`, - allExtensions: false, - }) - - expect(errors).toEqual( - `"isTSX" is true requires "allExtensions" to be true` - ) - }) }) }) From cdb17ccbc551c31642c80c8ac9ffce4b33d5a434 Mon Sep 17 00:00:00 2001 From: Laurie Date: Mon, 19 Oct 2020 14:21:13 -0400 Subject: [PATCH 06/10] use when instead of async --- .../src/__tests__/gatsby-node.js | 10 ++++++ .../src/gatsby-node.js | 33 +++++++------------ 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js index bf94356c433ed..74ad0924acb61 100644 --- a/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js @@ -99,5 +99,15 @@ describe(`gatsby-plugin-typescript`, () => { expect(isValid).toBe(true) }) + + it(`should break when isTSX doesn't match allExtensions`, () => { + const { errors } = testPluginOptionsSchema(pluginOptionsSchema, { + isTSX: true, + jsxPragma: `ReactFunction`, + allExtensions: false, + }) + + expect(errors).toEqual(`"allExtensions" must be [true]`) + }) }) }) diff --git a/packages/gatsby-plugin-typescript/src/gatsby-node.js b/packages/gatsby-plugin-typescript/src/gatsby-node.js index b704040437cc0..2c807e7eb3bae 100644 --- a/packages/gatsby-plugin-typescript/src/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/gatsby-node.js @@ -37,27 +37,18 @@ function onCreateWebpackConfig({ actions, loaders }) { if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { exports.pluginOptionsSchema = ({ Joi }) => - Joi.object() - .keys({ - isTSX: Joi.boolean().description(`Enables jsx parsing.`).default(false), - jsxPragma: Joi.string() - .description( - `Replace the function used when compiling JSX expressions.` - ) - .default(`React`), - allExtensions: Joi.boolean() - .description( - `Indicates that every file should be parsed as TS or TSX.` - ) - .default(false), - }) - .external(({ isTSX, allExtensions }) => { - if (isTSX && !allExtensions) { - throw new Error( - `Invalid plugin options for "gatsby-plugin-typescript": "isTSX" is true requires "allExtensions" to be true.` - ) - } - }) + Joi.object().keys({ + isTSX: Joi.boolean().description(`Enables jsx parsing.`).default(false), + jsxPragma: Joi.string() + .description( + `Replace the function used when compiling JSX expressions.` + ) + .default(`React`), + allExtensions: Joi.boolean() + .description(`Indicates that every file should be parsed as TS or TSX.`) + .default(false) + .when(`isTSX`, { is: true, then: Joi.valid(true) }), + }) } exports.resolvableExtensions = resolvableExtensions From 002ffe2e9a67f5ff7e3b661a978c48b269a1bd96 Mon Sep 17 00:00:00 2001 From: Laurie Date: Mon, 19 Oct 2020 14:30:37 -0400 Subject: [PATCH 07/10] remove keys now that we're not using external --- packages/gatsby-plugin-typescript/src/gatsby-node.js | 2 +- packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-plugin-typescript/src/gatsby-node.js b/packages/gatsby-plugin-typescript/src/gatsby-node.js index 2c807e7eb3bae..f168344546ee4 100644 --- a/packages/gatsby-plugin-typescript/src/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/gatsby-node.js @@ -37,7 +37,7 @@ function onCreateWebpackConfig({ actions, loaders }) { if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { exports.pluginOptionsSchema = ({ Joi }) => - Joi.object().keys({ + Joi.object({ isTSX: Joi.boolean().description(`Enables jsx parsing.`).default(false), jsxPragma: Joi.string() .description( diff --git a/packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts b/packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts index 196fd89395385..2ec4be27e02ba 100644 --- a/packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts +++ b/packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts @@ -16,7 +16,7 @@ export function testPluginOptionsSchema( pluginOptionsNames.forEach(pluginOptionName => { const partialSchema = pluginSchema.extract(pluginOptionName) - + console.log(pluginOptions[pluginOptionName]) const { error } = partialSchema.validate(pluginOptions[pluginOptionName], { abortEarly: false, }) From c5dbb7c6199384a96f5715905bcb1e47e057dab1 Mon Sep 17 00:00:00 2001 From: Laurie Date: Tue, 20 Oct 2020 12:07:20 -0400 Subject: [PATCH 08/10] fix typescript --- .../gatsby-plugin-utils/src/test-plugin-options-schema.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts b/packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts index 1303659602277..8b72d6bcde628 100644 --- a/packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts +++ b/packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts @@ -1,15 +1,16 @@ import { Joi } from "./joi" import { GatsbyNode } from "gatsby" import { validateOptionsSchema } from "./validate" +import { IPluginInfoOptions } from "gatsby" interface ITestPluginOptionsSchemaReturnType { errors: Array isValid: boolean } -export async function testPluginOptionsSchema( +export async function testPluginOptionsSchema( pluginSchemaFunction: Exclude, - pluginOptions: PluginOptions + pluginOptions: IPluginInfoOptions ): Promise { const pluginSchema = pluginSchemaFunction({ Joi }) From 0c147cc00e5239cbf0bc6ab12827929af7f6490a Mon Sep 17 00:00:00 2001 From: Laurie Date: Tue, 20 Oct 2020 13:18:19 -0400 Subject: [PATCH 09/10] fix expected errors in tests --- .../gatsby-plugin-mdx/__tests__/gatsby-node.js | 3 ++- .../src/__tests__/gatsby-node.js | 18 ++++++++++++++++-- .../__tests__/test-plugin-options-schema.ts | 10 +++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/gatsby-plugin-mdx/__tests__/gatsby-node.js b/packages/gatsby-plugin-mdx/__tests__/gatsby-node.js index d69d14bf22700..db91050cd58ad 100644 --- a/packages/gatsby-plugin-mdx/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-mdx/__tests__/gatsby-node.js @@ -9,7 +9,8 @@ describe(`pluginOptionsSchema`, () => { `"gatsbyRemarkPlugins" "[0]" does not match any of the allowed types. "[1]" does not match any of the allowed types`, `"remarkPlugins" must be an array`, `"rehypePlugins" must be an array`, - `"mediaTypes" "[0]" must be a string. "[1]" must be a string`, + `"mediaTypes[0]" must be a string`, + `"mediaTypes[1]" must be a string`, `"shouldBlockNodeFromTransformation" must have an arity lesser or equal to 1`, ] diff --git a/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js index cf32b120b4925..d1403ffeccf46 100644 --- a/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js @@ -113,10 +113,24 @@ describe(`onPostBuild`, () => { describe(`pluginOptionsSchema`, () => { it(`should provide meaningful errors when fields are invalid`, async () => { const expectedErrors = [ - `"precachePages" "[0]" must be a string. "[1]" must be a string. "[2]" must be a string`, + `"precachePages[0]" must be a string`, + `"precachePages[1]" must be a string`, + `"precachePages[2]" must be a string`, `"appendScript" must be a string`, `"debug" must be a boolean`, - `"workboxConfig" "importWorkboxFrom" must be a string. "globDirectory" must be a string. "globPatterns[0]" must be a string. "globPatterns[1]" must be a string. "globPatterns[2]" must be a string. "modifyURLPrefix./" must be a string. "cacheId" must be a string. "dontCacheBustURLsMatching" must be of type object. "runtimeCaching[0].handler" must be one of [StaleWhileRevalidate, CacheFirst, NetworkFirst, NetworkOnly, CacheOnly]. "runtimeCaching[1]" must be of type object. "runtimeCaching[2]" must be of type object. "skipWaiting" must be a boolean. "clientsClaim" must be a boolean`, + `"workboxConfig.importWorkboxFrom" must be a string`, + `"workboxConfig.globDirectory" must be a string`, + `"workboxConfig.globPatterns[0]" must be a string`, + `"workboxConfig.globPatterns[1]" must be a string`, + `"workboxConfig.globPatterns[2]" must be a string`, + `"workboxConfig.modifyURLPrefix./" must be a string`, + `"workboxConfig.cacheId" must be a string`, + `"workboxConfig.dontCacheBustURLsMatching" must be of type object`, + `"workboxConfig.runtimeCaching[0].handler" must be one of [StaleWhileRevalidate, CacheFirst, NetworkFirst, NetworkOnly, CacheOnly]`, + `"workboxConfig.runtimeCaching[1]" must be of type object`, + `"workboxConfig.runtimeCaching[2]" must be of type object`, + `"workboxConfig.skipWaiting" must be a boolean`, + `"workboxConfig.clientsClaim" must be a boolean`, ] const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, { diff --git a/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts b/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts index 413072ff92464..9287220e1dbf1 100644 --- a/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts +++ b/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts @@ -38,8 +38,8 @@ describe(`testPluginOptionsSchema`, () => { expect(isValid).toBe(false) expect(errors).toMatchInlineSnapshot(` Array [ - "\\"toVerify\\" must be a boolean", "\\"nb\\" must be a number", + "\\"toVerify\\" must be a boolean", ] `) }) @@ -100,7 +100,9 @@ describe(`testPluginOptionsSchema`, () => { "\\"head\\" must be a boolean", "\\"anonymize\\" must be a boolean", "\\"respectDNT\\" must be a boolean", - "\\"exclude\\" \\"[0]\\" must be a string. \\"[1]\\" must be a string. \\"[2]\\" must be a string", + "\\"exclude\\" \\"[0]\\" must be a string", + "\\"exclude\\" \\"[1]\\" must be a string", + "\\"exclude\\" \\"[2]\\" must be a string", ] `) }) @@ -169,7 +171,9 @@ describe(`testPluginOptionsSchema`, () => { "\\"head\\" must be a boolean", "\\"anonymize\\" must be a boolean", "\\"respectDNT\\" must be a boolean", - "\\"exclude\\" \\"[0]\\" must be a string. \\"[1]\\" must be a string. \\"[2]\\" must be a string", + "\\"exclude\\" \\"[0]\\" must be a string", + "\\"exclude\\" \\"[1]\\" must be a string", + "\\"exclude\\" \\"[2]\\" must be a string", "\\"pageTransitionDelay\\" must be a number", "\\"optimizeId\\" must be a string", "\\"experimentId\\" must be a string", From 2bbc77e82c2fbc6e913012864d4d497cad4cd07c Mon Sep 17 00:00:00 2001 From: Laurie Date: Tue, 20 Oct 2020 13:41:52 -0400 Subject: [PATCH 10/10] missed some tests --- packages/gatsby-plugin-mdx/__tests__/gatsby-node.js | 7 +++++-- .../src/__tests__/test-plugin-options-schema.ts | 12 ++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/gatsby-plugin-mdx/__tests__/gatsby-node.js b/packages/gatsby-plugin-mdx/__tests__/gatsby-node.js index db91050cd58ad..18c798689f8c4 100644 --- a/packages/gatsby-plugin-mdx/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-mdx/__tests__/gatsby-node.js @@ -4,9 +4,12 @@ import { pluginOptionsSchema } from "../gatsby-node" describe(`pluginOptionsSchema`, () => { it(`should provide meaningful errors when fields are invalid`, async () => { const expectedErrors = [ - `"extensions" "[0]" must be a string. "[1]" must be a string. "[2]" must be a string`, + `"extensions[0]" must be a string`, + `"extensions[1]" must be a string`, + `"extensions[2]" must be a string`, `"defaultLayout" must be of type object`, - `"gatsbyRemarkPlugins" "[0]" does not match any of the allowed types. "[1]" does not match any of the allowed types`, + `"gatsbyRemarkPlugins[0]" does not match any of the allowed types`, + `"gatsbyRemarkPlugins[1]" does not match any of the allowed types`, `"remarkPlugins" must be an array`, `"rehypePlugins" must be an array`, `"mediaTypes[0]" must be a string`, diff --git a/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts b/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts index 9287220e1dbf1..610947ea94025 100644 --- a/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts +++ b/packages/gatsby-plugin-utils/src/__tests__/test-plugin-options-schema.ts @@ -100,9 +100,9 @@ describe(`testPluginOptionsSchema`, () => { "\\"head\\" must be a boolean", "\\"anonymize\\" must be a boolean", "\\"respectDNT\\" must be a boolean", - "\\"exclude\\" \\"[0]\\" must be a string", - "\\"exclude\\" \\"[1]\\" must be a string", - "\\"exclude\\" \\"[2]\\" must be a string", + "\\"exclude[0]\\" must be a string", + "\\"exclude[1]\\" must be a string", + "\\"exclude[2]\\" must be a string", ] `) }) @@ -171,9 +171,9 @@ describe(`testPluginOptionsSchema`, () => { "\\"head\\" must be a boolean", "\\"anonymize\\" must be a boolean", "\\"respectDNT\\" must be a boolean", - "\\"exclude\\" \\"[0]\\" must be a string", - "\\"exclude\\" \\"[1]\\" must be a string", - "\\"exclude\\" \\"[2]\\" must be a string", + "\\"exclude[0]\\" must be a string", + "\\"exclude[1]\\" must be a string", + "\\"exclude[2]\\" must be a string", "\\"pageTransitionDelay\\" must be a number", "\\"optimizeId\\" must be a string", "\\"experimentId\\" must be a string",