From 6d7bd650e9a89c2b439fc710da9bdd91af8767e1 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 28 Oct 2020 12:46:54 +0300 Subject: [PATCH] strip stylelint warnings for // comments with @preprocessor --- edit/editor-worker.js | 6 ++++-- edit/linter-engines.js | 38 +++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/edit/editor-worker.js b/edit/editor-worker.js index 43a06a9b..9dd1b368 100644 --- a/edit/editor-worker.js +++ b/edit/editor-worker.js @@ -10,9 +10,11 @@ workerUtil.createAPI({ return CSSLint.verify(code, config).messages .map(m => Object.assign(m, {rule: {id: m.rule.id}})); }, - stylelint: (code, config) => { + stylelint: async (code, config) => { loadScript('/vendor/stylelint-bundle/stylelint-bundle.min.js'); - return require('stylelint').lint({code, config}); + const {results: [res]} = await require('stylelint').lint({code, config}); + delete res._postcssResult; // huge and unused + return res; }, metalint: code => { loadScript( diff --git a/edit/linter-engines.js b/edit/linter-engines.js index 3b3ca7a1..65755434 100644 --- a/edit/linter-engines.js +++ b/edit/linter-engines.js @@ -20,19 +20,31 @@ } }); - function stylelint(text, config) { - return editorWorker.stylelint(text, config) - .then(({results}) => !results[0] ? [] : - results[0].warnings.map(({line, column: ch, text, severity}) => ({ - from: {line: line - 1, ch: ch - 1}, - to: {line: line - 1, ch}, - message: text - .replace('Unexpected ', '') - .replace(/^./, firstLetter => firstLetter.toUpperCase()) - .replace(/\s*\([^(]+\)$/, ''), // strip the rule, - rule: text.replace(/^.*?\s*\(([^(]+)\)$/, '$1'), - severity, - }))); + async function stylelint(text, config, mode) { + const raw = await editorWorker.stylelint(text, config); + if (!raw) { + return []; + } + // Hiding the errors about "//" comments as we're preprocessing only when saving/applying + // and we can't just pre-remove the comments since "//" may be inside a string token or whatever + const slashCommentAllowed = mode === 'text/x-less' || mode === 'stylus'; + const res = []; + for (const w of raw.warnings) { + const msg = w.text.match(/^(?:Unexpected\s+)?(.*?)\s*\([^()]+\)$|$/)[1] || w.text; + if (!slashCommentAllowed || !( + w.rule === 'no-invalid-double-slash-comments' || + w.rule === 'property-no-unknown' && msg.includes('"//"') + )) { + res.push({ + from: {line: w.line - 1, ch: w.column - 1}, + to: {line: w.line - 1, ch: w.column}, + message: msg.slice(0, 1).toUpperCase() + msg.slice(1), + severity: w.severity, + rule: w.rule, + }); + } + } + return res; } function csslint(text, config) {