forked from openstyles/stylus
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite linter system (openstyles#487)
* Add: implement new linter system * Refactor: pull out editor worker * Switch to new linter and worker * Enable eslint cache * Fix: undefined error * Windows compatibility * Fix: refresh linter if the editor.linter changes * Add: stylelint * Add: getStylelintRules, getCsslintRules * Fix: logic to get correct linter * WIP: linter-report * Fix: toggle hidden state * Add: matain the order of lint report for section editor * Add: unhook event * Add: gotoLintIssue * Fix: shouldn't delete rule.init * Add: linter-help-dialog * Drop linterConfig * Add: linter-config-dialog, cacheFn * Add: use cacheFn * Drop lint.js * Add: refresh. Fix report order * Fix: hide empty table * Add: updateCount. Fix table caption * Switch to new linter/worker * Fix: remove unneeded comment * Fix: cacheFn -> cacheFirstCall * Fix: use cacheFirstCall * Fix: cache metaIndex * Fix: i < trs.length * Fix: drop isEmpty * Fix: expose some simple states to global * Fix: return object code style * Fix: use proxy to reflect API * Fix: eslint-disable-line -> eslint-disable-next-line * Fix: requestId -> id * Fix: one-liner * Fix: one-liner * Fix: move dom event block to top * Fix: pending -> pendingResponse * Fix: onSuccess -> onUpdated * Fix: optimize row removing when i === 0 * Fix: hook/unhook -> enableForEditor/disableForEditor * Fix: linter.refresh -> linter.run * Fix: some shadowing * Fix: simplify getAnnotations * Fix: cacheFirstCall -> memoize * Fix: table.update -> table.updateCaption * Fix: unneeded reassign * Fix: callbacks -> listeners * Fix: don't compose but extend * Refactor: replace linter modules with linter-defaults and linter-engines * Fix: implement linter fallbacks * Fix: linter.onChange -> linter.onLintingUpdated * Fix: cms -> tables * Fix: parseMozFormat is not called correctly * Move csslint-loader to background * Fix: watch config changes * Fix: switch to LINTER_DEFAULTS * Fix: csslint-loader -> parserlib-loader
- Loading branch information
Showing
27 changed files
with
1,090 additions
and
968 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ node_modules/ | |
package-lock.json | ||
yarn.lock | ||
*.zip | ||
.eslintcache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* global importScripts parserlib CSSLint parseMozFormat */ | ||
'use strict'; | ||
|
||
importScripts('/vendor-overwrites/csslint/parserlib.js', '/js/moz-parser.js'); | ||
parserlib.css.Tokens[parserlib.css.Tokens.COMMENT].hide = false; | ||
|
||
self.onmessage = ({data}) => { | ||
self.postMessage(parseMozFormat(data)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* global importScripts parseMozFormat parserlib CSSLint require */ | ||
'use strict'; | ||
|
||
createAPI({ | ||
csslint: (code, config) => { | ||
loadParserLib(); | ||
loadScript(['/vendor-overwrites/csslint/csslint.js']); | ||
return CSSLint.verify(code, config).messages | ||
.map(m => Object.assign(m, {rule: {id: m.rule.id}})); | ||
}, | ||
stylelint: (code, config) => { | ||
loadScript(['/vendor/stylelint-bundle/stylelint-bundle.min.js']); | ||
return require('stylelint').lint({code, config}); | ||
}, | ||
parseMozFormat: data => { | ||
loadParserLib(); | ||
loadScript(['/js/moz-parser.js']); | ||
return parseMozFormat(data); | ||
}, | ||
getStylelintRules, | ||
getCsslintRules | ||
}); | ||
|
||
function getCsslintRules() { | ||
loadScript(['/vendor-overwrites/csslint/csslint.js']); | ||
return CSSLint.getRules().map(rule => { | ||
const output = {}; | ||
for (const [key, value] of Object.entries(rule)) { | ||
if (typeof value !== 'function') { | ||
output[key] = value; | ||
} | ||
} | ||
return output; | ||
}); | ||
} | ||
|
||
function getStylelintRules() { | ||
loadScript(['/vendor/stylelint-bundle/stylelint-bundle.min.js']); | ||
const stylelint = require('stylelint'); | ||
const options = {}; | ||
const rxPossible = /\bpossible:("(?:[^"]*?)"|\[(?:[^\]]*?)\]|\{(?:[^}]*?)\})/g; | ||
const rxString = /"([-\w\s]{3,}?)"/g; | ||
for (const id of Object.keys(stylelint.rules)) { | ||
const ruleCode = String(stylelint.rules[id]); | ||
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); | ||
} | ||
} | ||
if (sets.length) { | ||
options[id] = sets; | ||
} | ||
} | ||
return options; | ||
} | ||
|
||
function loadParserLib() { | ||
if (typeof parserlib !== 'undefined') { | ||
return; | ||
} | ||
importScripts('/vendor-overwrites/csslint/parserlib.js'); | ||
parserlib.css.Tokens[parserlib.css.Tokens.COMMENT].hide = false; | ||
} | ||
|
||
const loadedUrls = new Set(); | ||
function loadScript(urls) { | ||
urls = urls.filter(u => !loadedUrls.has(u)); | ||
importScripts(...urls); | ||
urls.forEach(u => loadedUrls.add(u)); | ||
} | ||
|
||
function createAPI(methods) { | ||
self.onmessage = e => { | ||
const message = e.data; | ||
Promise.resolve() | ||
.then(() => methods[message.action](...message.args)) | ||
.then(result => ({ | ||
id: message.id, | ||
error: false, | ||
data: result | ||
})) | ||
.catch(err => ({ | ||
id: message.id, | ||
error: true, | ||
data: cloneError(err) | ||
})) | ||
.then(data => self.postMessage(data)); | ||
}; | ||
} | ||
|
||
function cloneError(err) { | ||
return Object.assign({ | ||
name: err.name, | ||
stack: err.stack, | ||
message: err.message, | ||
lineNumber: err.lineNumber, | ||
columnNumber: err.columnNumber, | ||
fileName: err.fileName | ||
}, err); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
// eslint-disable-next-line no-var | ||
var editorWorker = (() => { | ||
let worker; | ||
return new Proxy({}, { | ||
get: (target, prop) => | ||
(...args) => { | ||
if (!worker) { | ||
worker = createWorker(); | ||
} | ||
return worker.invoke(prop, args); | ||
} | ||
}); | ||
|
||
function createWorker() { | ||
let id = 0; | ||
const pendingResponse = new Map(); | ||
const worker = new Worker('/edit/editor-worker-body.js'); | ||
worker.onmessage = e => { | ||
const message = e.data; | ||
pendingResponse.get(message.id)[message.error ? 'reject' : 'resolve'](message.data); | ||
pendingResponse.delete(message.id); | ||
}; | ||
return {invoke}; | ||
|
||
function invoke(action, args) { | ||
return new Promise((resolve, reject) => { | ||
pendingResponse.set(id, {resolve, reject}); | ||
worker.postMessage({ | ||
id, | ||
action, | ||
args | ||
}); | ||
id++; | ||
}); | ||
} | ||
} | ||
})(); |
Oops, something went wrong.