From 1f2d116aae15b61212fa059eee356d94951ca6aa Mon Sep 17 00:00:00 2001 From: eight Date: Mon, 1 Oct 2018 23:14:56 +0800 Subject: [PATCH] Fix: use meta parser --- edit/editor-worker.js | 11 ++++++++--- edit/linter-meta.js | 2 +- js/meta-parser.js | 16 +++++++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/edit/editor-worker.js b/edit/editor-worker.js index 03ad6c97..62ef380c 100644 --- a/edit/editor-worker.js +++ b/edit/editor-worker.js @@ -1,4 +1,4 @@ -/* global importScripts workerUtil CSSLint require usercssMeta */ +/* global importScripts workerUtil CSSLint require metaParser */ 'use strict'; importScripts('/js/worker-util.js'); @@ -15,12 +15,17 @@ createAPI({ return require('stylelint').lint({code, config}); }, metalint: code => { - loadScript('/vendor/usercss-meta/usercss-meta.min.js'); - const result = usercssMeta.parse(code, {allowErrors: true, unknownKey: 'throw'}); + loadScript( + '/vendor/usercss-meta/usercss-meta.min.js', + '/vendor-overwrites/colorpicker/colorconverter.js', + '/js/meta-parser.js' + ); + const result = metaParser.lint(code); // extract needed info result.errors = result.errors.map(err => ({ code: err.code, + args: err.args, message: err.message, index: err.index }) diff --git a/edit/linter-meta.js b/edit/linter-meta.js index 8bd494f2..779f564b 100644 --- a/edit/linter-meta.js +++ b/edit/linter-meta.js @@ -29,7 +29,7 @@ function createMetaCompiler(cm) { ({ from: 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' }) ); diff --git a/js/meta-parser.js b/js/meta-parser.js index b25540d6..e660effb 100644 --- a/js/meta-parser.js +++ b/js/meta-parser.js @@ -5,7 +5,7 @@ var metaParser = (() => { const {createParser, ParseError} = usercssMeta; const PREPROCESSORS = new Set(['default', 'uso', 'stylus', 'less']); - const parser = createParser({ + const options = { validateKey: { preprocessor: state => { if (!PREPROCESSORS.has(state.value)) { @@ -38,8 +38,14 @@ var metaParser = (() => { 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) { try { @@ -52,6 +58,10 @@ var metaParser = (() => { } } + function lint(text) { + return looseParser.parse(text); + } + function nullifyInvalidVars(vars) { for (const va of Object.values(vars)) { if (va.value === null) {