2021-01-01 14:27:58 +00:00
|
|
|
/* global createWorkerApi */// worker-util.js
|
2018-10-01 14:03:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
(() => {
|
2022-08-06 20:19:49 +00:00
|
|
|
let sugarss = false;
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @namespace EditorWorker */
|
|
|
|
createWorkerApi({
|
|
|
|
|
|
|
|
async csslint(code, config) {
|
|
|
|
require(['/js/csslint/parserlib', '/js/csslint/csslint']); /* global CSSLint */
|
|
|
|
return CSSLint
|
|
|
|
.verify(code, config).messages
|
|
|
|
.map(m => Object.assign(m, {rule: {id: m.rule.id}}));
|
|
|
|
},
|
|
|
|
|
2021-03-14 05:33:26 +00:00
|
|
|
getCssPropsValues() {
|
|
|
|
require(['/js/csslint/parserlib']); /* global parserlib */
|
2021-08-20 10:01:56 +00:00
|
|
|
const {
|
|
|
|
css: {Colors, GlobalKeywords, Properties},
|
|
|
|
util: {describeProp},
|
|
|
|
} = parserlib;
|
2021-03-14 05:33:26 +00:00
|
|
|
const namedColors = Object.keys(Colors);
|
|
|
|
const rxNonWord = /(?:<.+?>|[^-\w<(]+\d*)+/g;
|
|
|
|
const res = {};
|
|
|
|
// moving vendor-prefixed props to the end
|
|
|
|
const cmp = (a, b) => a[0] === '-' && b[0] !== '-' ? 1 : a < b ? -1 : a > b;
|
|
|
|
for (const [k, v] of Object.entries(Properties)) {
|
2022-08-27 18:55:44 +00:00
|
|
|
res[k] = false;
|
2021-03-14 05:33:26 +00:00
|
|
|
if (typeof v === 'string') {
|
|
|
|
let last = '';
|
|
|
|
const uniq = [];
|
|
|
|
// strip definitions of function arguments
|
|
|
|
const desc = describeProp(v).replace(/([-\w]+)\(.*?\)/g, 'z-$1');
|
|
|
|
const descNoColors = desc.replace(/<named-color>/g, '');
|
|
|
|
// add a prefix to functions to group them at the end
|
|
|
|
const words = descNoColors.split(rxNonWord).sort(cmp);
|
|
|
|
for (let w of words) {
|
|
|
|
if (w.startsWith('z-')) w = w.slice(2) + '(';
|
|
|
|
if (w !== last) uniq.push(last = w);
|
|
|
|
}
|
|
|
|
if (desc !== descNoColors) uniq.push(...namedColors);
|
|
|
|
if (uniq.length) res[k] = uniq;
|
|
|
|
}
|
|
|
|
}
|
2022-08-27 18:55:44 +00:00
|
|
|
return {all: res, global: GlobalKeywords};
|
2021-03-14 05:33:26 +00:00
|
|
|
},
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
getRules(linter) {
|
|
|
|
return ruleRetriever[linter](); // eslint-disable-line no-use-before-define
|
|
|
|
},
|
|
|
|
|
|
|
|
metalint(code) {
|
|
|
|
require(['/js/meta-parser']); /* global metaParser */
|
|
|
|
const result = metaParser.lint(code);
|
|
|
|
// extract needed info
|
|
|
|
result.errors = result.errors.map(err => ({
|
2018-11-07 06:09:29 +00:00
|
|
|
code: err.code,
|
|
|
|
args: err.args,
|
|
|
|
message: err.message,
|
2020-11-18 11:17:15 +00:00
|
|
|
index: err.index,
|
2021-01-01 14:27:58 +00:00
|
|
|
}));
|
|
|
|
return result;
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-04 10:57:58 +00:00
|
|
|
async stylelint(opts) {
|
|
|
|
require(['/vendor/stylelint-bundle/stylelint-bundle.min']); /* global stylelint */
|
2022-08-06 20:19:49 +00:00
|
|
|
// Stylus-lang allows a trailing ";" but sugarss doesn't, so we monkeypatch it
|
|
|
|
stylelint.SugarSSParser.prototype.checkSemicolon = tt => {
|
|
|
|
while (tt.length && tt[tt.length - 1][0] === ';') tt.pop();
|
|
|
|
};
|
|
|
|
for (const pass of opts.mode === 'stylus' ? [sugarss, !sugarss] : [-1]) {
|
|
|
|
/* We try sugarss (for indented stylus-lang), then css mode, switching them on failure,
|
|
|
|
* so that the succeeding syntax will be used next time first. */
|
|
|
|
opts.config.customSyntax = !pass ? 'sugarss' : '';
|
|
|
|
try {
|
|
|
|
const res = await stylelint.createLinter(opts)._lintSource(opts);
|
|
|
|
if (pass !== -1) sugarss = pass;
|
|
|
|
return collectStylelintResults(res, opts);
|
|
|
|
} catch (e) {
|
|
|
|
const fatal = pass === -1 ||
|
|
|
|
!pass && !/^CssSyntaxError:.+?Unnecessary curly bracket/.test(e.stack) ||
|
|
|
|
pass && !/^CssSyntaxError:.+?Unknown word[\s\S]*?\.decl\s/.test(e.stack);
|
|
|
|
if (fatal) {
|
|
|
|
return [{
|
|
|
|
from: {line: e.line - 1, ch: e.column - 1},
|
|
|
|
to: {line: e.line - 1, ch: e.column - 1},
|
|
|
|
message: e.reason,
|
|
|
|
severity: 'error',
|
|
|
|
rule: e.name,
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 10:57:58 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
},
|
2018-10-01 14:03:17 +00:00
|
|
|
});
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
const ruleRetriever = {
|
|
|
|
|
|
|
|
csslint() {
|
|
|
|
require(['/js/csslint/csslint']);
|
2021-01-05 13:46:21 +00:00
|
|
|
return CSSLint.getRuleList().map(rule => {
|
2021-01-01 14:27:58 +00:00
|
|
|
const output = {};
|
|
|
|
for (const [key, value] of Object.entries(rule)) {
|
|
|
|
if (typeof value !== 'function') {
|
|
|
|
output[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
stylelint() {
|
|
|
|
require(['/vendor/stylelint-bundle/stylelint-bundle.min']);
|
|
|
|
const options = {};
|
|
|
|
const rxPossible = /\bpossible:("(?:[^"]*?)"|\[(?:[^\]]*?)\]|\{(?:[^}]*?)\})/g;
|
|
|
|
const rxString = /"([-\w\s]{3,}?)"/g;
|
2021-01-04 10:57:58 +00:00
|
|
|
for (const [id, rule] of Object.entries(stylelint.rules)) {
|
|
|
|
const ruleCode = `${rule()}`;
|
2021-01-01 14:27:58 +00:00
|
|
|
const sets = [];
|
|
|
|
let m, mStr;
|
|
|
|
while ((m = rxPossible.exec(ruleCode))) {
|
|
|
|
const possible = m[1];
|
|
|
|
const set = [];
|
|
|
|
while ((mStr = rxString.exec(possible))) {
|
|
|
|
const s = mStr[1];
|
|
|
|
if (s.includes(' ')) {
|
|
|
|
set.push(...s.split(/\s+/));
|
|
|
|
} else {
|
|
|
|
set.push(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (possible.includes('ignoreAtRules')) {
|
|
|
|
set.push('ignoreAtRules');
|
|
|
|
}
|
|
|
|
if (possible.includes('ignoreShorthands')) {
|
|
|
|
set.push('ignoreShorthands');
|
|
|
|
}
|
|
|
|
if (set.length) {
|
|
|
|
sets.push(set);
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 06:28:59 +00:00
|
|
|
options[id] = sets;
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
return options;
|
|
|
|
},
|
|
|
|
};
|
2022-08-06 20:19:49 +00:00
|
|
|
|
|
|
|
function collectStylelintResults({messages}, {mode}) {
|
|
|
|
/* We hide nonfatal "//" warnings since we lint with sugarss without applying @preprocessor.
|
|
|
|
* We can't easily pre-remove "//" comments which may be inside strings, comments, url(), etc.
|
|
|
|
* And even if we did, it'd be wrong to hide potential bugs in stylus-lang like #1460 */
|
2022-08-27 18:08:56 +00:00
|
|
|
const isLess = mode === 'text/x-less';
|
|
|
|
const slashCommentAllowed = isLess || mode === 'stylus';
|
2022-08-06 20:19:49 +00:00
|
|
|
const res = [];
|
|
|
|
for (const m of messages) {
|
|
|
|
if (/deprecation|invalidOption/.test(m.stylelintType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const {rule} = m;
|
|
|
|
const msg = m.text.replace(/^Unexpected\s+/, '').replace(` (${rule})`, '');
|
2022-08-28 19:47:58 +00:00
|
|
|
if (slashCommentAllowed && msg.includes('"//"') ||
|
|
|
|
isLess && /^unknown at-rule "@[-\w]+:"/.test(msg) /* LESS variables */) {
|
2022-08-06 20:19:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
res.push({
|
|
|
|
from: {line: m.line - 1, ch: m.column - 1},
|
|
|
|
to: {line: m.endLine - 1, ch: m.endColumn - 1},
|
|
|
|
message: msg[0].toUpperCase() + msg.slice(1),
|
|
|
|
severity: m.severity,
|
|
|
|
rule,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
})();
|