Fix: use meta parser

This commit is contained in:
eight 2018-10-01 23:14:56 +08:00
parent 918e47b1ed
commit 1f2d116aae
3 changed files with 22 additions and 7 deletions

View File

@ -1,4 +1,4 @@
/* global importScripts workerUtil CSSLint require usercssMeta */ /* global importScripts workerUtil CSSLint require metaParser */
'use strict'; 'use strict';
importScripts('/js/worker-util.js'); importScripts('/js/worker-util.js');
@ -15,12 +15,17 @@ createAPI({
return require('stylelint').lint({code, config}); return require('stylelint').lint({code, config});
}, },
metalint: code => { metalint: code => {
loadScript('/vendor/usercss-meta/usercss-meta.min.js'); loadScript(
const result = usercssMeta.parse(code, {allowErrors: true, unknownKey: 'throw'}); '/vendor/usercss-meta/usercss-meta.min.js',
'/vendor-overwrites/colorpicker/colorconverter.js',
'/js/meta-parser.js'
);
const result = metaParser.lint(code);
// extract needed info // extract needed info
result.errors = result.errors.map(err => result.errors = result.errors.map(err =>
({ ({
code: err.code, code: err.code,
args: err.args,
message: err.message, message: err.message,
index: err.index index: err.index
}) })

View File

@ -29,7 +29,7 @@ function createMetaCompiler(cm) {
({ ({
from: cm.posFromIndex((err.index || 0) + match.index), from: cm.posFromIndex((err.index || 0) + match.index),
to: cm.posFromIndex((err.index || 0) + match.index), to: cm.posFromIndex((err.index || 0) + match.index),
message: err.message, message: err.code && chrome.i18n.getMessage(`meta_${err.code}`, err.args) || err.message,
severity: err.code === 'unknownMeta' ? 'warning' : 'error' severity: err.code === 'unknownMeta' ? 'warning' : 'error'
}) })
); );

View File

@ -5,7 +5,7 @@
var metaParser = (() => { var metaParser = (() => {
const {createParser, ParseError} = usercssMeta; const {createParser, ParseError} = usercssMeta;
const PREPROCESSORS = new Set(['default', 'uso', 'stylus', 'less']); const PREPROCESSORS = new Set(['default', 'uso', 'stylus', 'less']);
const parser = createParser({ const options = {
validateKey: { validateKey: {
preprocessor: state => { preprocessor: state => {
if (!PREPROCESSORS.has(state.value)) { if (!PREPROCESSORS.has(state.value)) {
@ -38,8 +38,14 @@ var metaParser = (() => {
state.value = colorConverter.format(color, 'rgb'); state.value = colorConverter.format(color, 'rgb');
} }
} }
}); };
return {parse, nullifyInvalidVars}; const parser = createParser(options);
const looseParser = createParser(Object.assign({}, options, {allowErrors: true, unknownKey: 'throw'}));
return {
parse,
lint,
nullifyInvalidVars
};
function parse(text, indexOffset) { function parse(text, indexOffset) {
try { try {
@ -52,6 +58,10 @@ var metaParser = (() => {
} }
} }
function lint(text) {
return looseParser.parse(text);
}
function nullifyInvalidVars(vars) { function nullifyInvalidVars(vars) {
for (const va of Object.values(vars)) { for (const va of Object.values(vars)) {
if (va.value === null) { if (va.value === null) {