From ef14e23bb7da8b5f4118b5002000fee0c8a2162d Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Tue, 10 Jul 2018 23:56:24 -0700 Subject: [PATCH] feat: emit warnings on missing keys Also adds strict mode which will throw errors on missing keys. --- docs/svelte.md | 10 +++ packages/svelte/README.md | 6 ++ packages/svelte/src/markup.js | 69 +++++++++++---- packages/svelte/svelte.js | 8 +- .../test/__snapshots__/svelte.test.js.snap | 86 +++++++++++++++++-- .../specimens/invalid-external-script.html | 7 ++ .../specimens/invalid-external-template.html | 4 + .../test/specimens/invalid-inline-script.html | 9 ++ .../specimens/invalid-inline-template.html | 6 ++ packages/svelte/test/specimens/invalid.css | 3 + packages/svelte/test/svelte.test.js | 56 ++++++++---- 11 files changed, 224 insertions(+), 40 deletions(-) create mode 100644 packages/svelte/test/specimens/invalid-external-script.html create mode 100644 packages/svelte/test/specimens/invalid-external-template.html create mode 100644 packages/svelte/test/specimens/invalid-inline-script.html create mode 100644 packages/svelte/test/specimens/invalid-inline-template.html create mode 100644 packages/svelte/test/specimens/invalid.css diff --git a/docs/svelte.md b/docs/svelte.md index 6a78aa8bd..6bc47f1d6 100644 --- a/docs/svelte.md +++ b/docs/svelte.md @@ -89,3 +89,13 @@ module.exports = { ] }; ``` + +## Options + +### `strict` + +If `true` whenever a missing replacement is found like `{css.doesnotexist}` an error will be throw aborting the file processing. Defaults to `false`. + +### Shared Options + +All options are passed to the underlying `Processor` instance, see [Options](https://github.com/tivac/modular-css/blob/master/docs/api.md#options). diff --git a/packages/svelte/README.md b/packages/svelte/README.md index 93c667273..322835d25 100644 --- a/packages/svelte/README.md +++ b/packages/svelte/README.md @@ -129,4 +129,10 @@ module.exports = { ## Options +### `strict` + +If `true` whenever a missing replacement is found like `{css.doesnotexist}` an error will be throw aborting the file processing. Defaults to `false`. + +### Shared Options + All options are passed to the underlying `Processor` instance, see [Options](https://github.com/tivac/modular-css/blob/master/docs/api.md#options). diff --git a/packages/svelte/src/markup.js b/packages/svelte/src/markup.js index be5710be5..05d912a88 100644 --- a/packages/svelte/src/markup.js +++ b/packages/svelte/src/markup.js @@ -9,26 +9,46 @@ const slash = require("slash"); const styleRegex = /([\S\s]*?)<\/style>/im; const scriptRegex = /([\S\s]*?)<\/script>/im; const linkRegex = /]*?\bhref=\s*(?:"([^"]+)"|'([^']+)'|([^>\s]+))[^>]*>/im; +const missedRegex = /css\.\w+/gim; -function updateCss({ content, result }) { +function updateCss({ processor, content, result, filename }) { const exported = result.files[result.file].exports; + const code = content + // Replace simple {css.} values first + .replace( + new RegExp(`{css\\.(${Object.keys(exported).join("|")})}`, "gm"), + (match, key) => exported[key].join(" ") + ) + // Then any remaining bare css. values + .replace( + new RegExp(`(\\b)css\\.(${Object.keys(exported).join("|")})(\\b)`, "gm"), + (match, prefix, key, suffix) => `${prefix}"${exported[key].join(" ")}"${suffix}` + ); + + // Check for any values in the template we couldn't convert + const missed = code.match(missedRegex); + + if(missed) { + const { strict } = processor.options; + + if(strict) { + throw new Error(`modular-css-svelte: Unable to find "${missed.join(", ")}" in ${filename}`); + } + + missed.forEach((key) => + // eslint-disable-next-line no-console + console.warn(`modular-css-svelte: Unable to find "${key}" in ${filename}`) + ); + } + return { - code : content - // Replace simple {css.} values first - .replace( - new RegExp(`{css\\.(${Object.keys(exported).join("|")})}`, "gm"), - (match, key) => exported[key].join(" ") - ) - // Then any remaining bare css. values - .replace( - new RegExp(`(\\b)css\\.(${Object.keys(exported).join("|")})(\\b)`, "gm"), - (match, prefix, key, suffix) => `${prefix}"${exported[key].join(" ")}"${suffix}` - ), + code, }; } async function extractLink({ processor, content, filename, link }) { + // This looks weird, but it's to support multiple types of quotation marks const href = link[1] || link[2] || link[3]; const external = slash(resolve(path.dirname(filename), href)); @@ -41,11 +61,13 @@ async function extractLink({ processor, content, filename, link }) { // To get rollup to watch the CSS file we need to inject an import statement // if a +" +`; + +exports[`/svelte.js should handle invalid references in " +" +`; + +exports[`/svelte.js should handle invalid references in "" +`; + +exports[`/svelte.js should handle invalid references in "template" (inline: false, strict: true) 1`] = `"modular-css-svelte: Unable to find \\"css.nope\\" in ./invalid.css"`; + +exports[`/svelte.js should handle invalid references in "template" (inline: true, strict: false) 1`] = ` +Array [ + Array [ + "modular-css-svelte: Unable to find \\"css.nope\\" in +" +`; + +exports[`/svelte.js should handle invalid references in "template" (inline: true, strict: true) 1`] = `"modular-css-svelte: Unable to find \\"css.nope\\" in " -`; +exports[`/svelte.js should throw on both + + diff --git a/packages/svelte/test/specimens/invalid-inline-template.html b/packages/svelte/test/specimens/invalid-inline-template.html new file mode 100644 index 000000000..364a0fe65 --- /dev/null +++ b/packages/svelte/test/specimens/invalid-inline-template.html @@ -0,0 +1,6 @@ +

Nope

+

Yup

+ + diff --git a/packages/svelte/test/specimens/invalid.css b/packages/svelte/test/specimens/invalid.css new file mode 100644 index 000000000..3c8db86d2 --- /dev/null +++ b/packages/svelte/test/specimens/invalid.css @@ -0,0 +1,3 @@ +.yup { + color: yellow; +} diff --git a/packages/svelte/test/svelte.test.js b/packages/svelte/test/svelte.test.js index d8f58d473..63ad07de1 100644 --- a/packages/svelte/test/svelte.test.js +++ b/packages/svelte/test/svelte.test.js @@ -72,24 +72,51 @@ describe("/svelte.js", () => { expect(output.css).toMatchSnapshot(); }); - it("should ignore invalid {css.}", async () => { + it.each` + title | inline | strict | specimen + ${"