From f736ed02efd83d276dea96fbcd8602373ac21680 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 30 Nov 2018 16:59:25 +0300 Subject: [PATCH] feat: emit warning on invalid url --- lib/plugins/postcss-url-parser.js | 40 +++++++++++++--------- test/__snapshots__/url-option.test.js.snap | 26 +++++++++++++- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/lib/plugins/postcss-url-parser.js b/lib/plugins/postcss-url-parser.js index be137eaf5..1cf6b5f5b 100644 --- a/lib/plugins/postcss-url-parser.js +++ b/lib/plugins/postcss-url-parser.js @@ -20,9 +20,7 @@ function walkUrls(parsed, callback) { node.after = ''; /* eslint-enable */ - if (url.trim().replace(/\\[\r\n]/g, '').length !== 0) { - callback(node, url); - } + callback(node, url); // Do not traverse inside url // eslint-disable-next-line consistent-return @@ -30,22 +28,30 @@ function walkUrls(parsed, callback) { }); } -function filterUrls(parsed, filter) { - const result = []; +function filterUrls(parsed, result, decl, filter) { + const urls = []; - walkUrls(parsed, (node, content) => { - if (!filter(content)) { + walkUrls(parsed, (node, url) => { + if (url.trim().replace(/\\[\r\n]/g, '').length === 0) { + result.warn(`Unable to find uri in '${decl.toString()}'`, { + node: decl, + }); + + return; + } + + if (!filter(url)) { return; } - result.push(content); + urls.push(url); }); - return result; + return urls; } -function walkDeclsWithUrl(css, filter) { - const result = []; +function walkDeclsWithUrl(css, result, filter) { + const items = []; css.walkDecls((decl) => { if (!/url\(/i.test(decl.value)) { @@ -53,16 +59,16 @@ function walkDeclsWithUrl(css, filter) { } const parsed = valueParser(decl.value); - const values = filterUrls(parsed, filter); + const values = filterUrls(parsed, result, decl, filter); if (values.length === 0) { return; } - result.push({ decl, parsed, values }); + items.push({ decl, parsed, values }); }); - return result; + return items; } function flatten(array) { @@ -92,9 +98,11 @@ function uniq(array) { module.exports = postcss.plugin( pluginName, (options) => - function process(css) { + function process(css, result) { const urlItems = []; - const traversed = walkDeclsWithUrl(css, (value) => isUrlRequest(value)); + const traversed = walkDeclsWithUrl(css, result, (value) => + isUrlRequest(value) + ); const paths = uniq(flatten(traversed.map((item) => item.values))); if (paths.length === 0) { diff --git a/test/__snapshots__/url-option.test.js.snap b/test/__snapshots__/url-option.test.js.snap index 16f7bba58..819f341b1 100644 --- a/test/__snapshots__/url-option.test.js.snap +++ b/test/__snapshots__/url-option.test.js.snap @@ -421,4 +421,28 @@ exports.push([module.id, \\".class {\\\\n background: url(\\" + escape(require( " `; -exports[`url option true: warnings 1`] = `Array []`; +exports[`url option true: warnings 1`] = ` +Array [ + [ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js): +Warning + +(120:3) Unable to find uri in 'background: green url() xyz'], + [ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js): +Warning + +(124:3) Unable to find uri in 'background: green url('') xyz'], + [ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js): +Warning + +(128:3) Unable to find uri in 'background: green url("") xyz'], + [ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js): +Warning + +(132:3) Unable to find uri in 'background: green url('') xyz'], + [ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js): +Warning + +(136:3) Unable to find uri in 'background: green url( + ) xyz'], +] +`;