2017-08-15 19:13:58 +00:00
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
|
|
// Distributed under an MIT license: http://codemirror.net/LICENSE
|
|
|
|
|
|
|
|
// Depends on csslint.js from https://github.com/stubbornella/csslint
|
|
|
|
|
2017-08-20 19:08:32 +00:00
|
|
|
/* global CodeMirror require define */
|
2017-08-23 22:13:55 +00:00
|
|
|
/* global CSSLint stylelint stylelintDefaultConfig csslintDefaultRuleset */
|
2017-08-20 19:08:32 +00:00
|
|
|
'use strict';
|
2017-08-15 19:13:58 +00:00
|
|
|
|
2017-08-20 19:08:32 +00:00
|
|
|
(mod => {
|
|
|
|
if (typeof exports === 'object' && typeof module === 'object') {
|
|
|
|
// CommonJS
|
|
|
|
mod(require('../../lib/codemirror'));
|
|
|
|
} else if (typeof define === 'function' && define.amd) {
|
|
|
|
// AMD
|
|
|
|
define(['../../lib/codemirror'], mod);
|
|
|
|
} else {
|
|
|
|
// Plain browser env
|
2017-08-15 19:13:58 +00:00
|
|
|
mod(CodeMirror);
|
2017-08-20 19:08:32 +00:00
|
|
|
}
|
|
|
|
})(CodeMirror => {
|
|
|
|
CodeMirror.registerHelper('lint', 'csslint', text => {
|
|
|
|
const found = [];
|
2017-08-23 22:13:55 +00:00
|
|
|
if (!window.CSSLint) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
/* STYLUS: hack start (part 1) */
|
|
|
|
return BG.chromeLocal.getValue('editorCSSLintRules').then((ruleset = csslintDefaultRuleset) => {
|
|
|
|
// csslintDefaultRuleset stored in csslint-ruleset.js & loaded by edit/lint.js
|
|
|
|
if (Object.keys(ruleset).length === 0) {
|
|
|
|
ruleset = Object.assign({}, csslintDefaultRuleset);
|
|
|
|
}
|
|
|
|
const results = CSSLint.verify(text, ruleset);
|
2017-08-20 19:08:32 +00:00
|
|
|
const messages = results.messages;
|
|
|
|
const hslRegex = /hsla?\(\s*(-?\d+)%?\s*,\s*(-?\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%(\s*,\s*(-?\d+|-?\d*.\d+))?\s*\)/;
|
|
|
|
let message = null;
|
2017-08-23 22:13:55 +00:00
|
|
|
/* STYLUS: hack end */
|
|
|
|
|
2017-08-20 19:08:32 +00:00
|
|
|
for (let i = 0; i < messages.length; i++) {
|
|
|
|
message = messages[i];
|
2017-08-15 19:13:58 +00:00
|
|
|
|
2017-08-20 20:14:12 +00:00
|
|
|
/* STYLUS: hack start (part 2) */
|
2017-08-20 19:08:32 +00:00
|
|
|
if (message.type === 'warning') {
|
|
|
|
// @font-face {font-family: 'Ampersand'; unicode-range: U+26;}
|
|
|
|
if (message.message.indexOf('unicode-range') !== -1) {
|
|
|
|
continue;
|
|
|
|
} else if (
|
|
|
|
// color: hsl(210, 100%, 2.2%); or color: hsla(210, 100%, 2.2%, 0.3);
|
|
|
|
message.message.startsWith('Expected (<color>) but found \'hsl') &&
|
|
|
|
hslRegex.test(message.message)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-15 19:13:58 +00:00
|
|
|
}
|
2017-08-20 19:08:32 +00:00
|
|
|
const startLine = message.line - 1;
|
|
|
|
const endLine = message.line - 1;
|
|
|
|
const startCol = message.col - 1;
|
|
|
|
const endCol = message.col;
|
2017-08-23 22:13:55 +00:00
|
|
|
/* STYLUS: hack end */
|
|
|
|
|
2017-08-20 19:08:32 +00:00
|
|
|
found.push({
|
|
|
|
from: CodeMirror.Pos(startLine, startCol),
|
|
|
|
to: CodeMirror.Pos(endLine, endCol),
|
2017-08-23 22:13:55 +00:00
|
|
|
message: message.message + ` (${message.rule.id})`,
|
2017-08-20 19:08:32 +00:00
|
|
|
severity : message.type
|
|
|
|
});
|
2017-08-15 19:13:58 +00:00
|
|
|
}
|
2017-08-23 22:13:55 +00:00
|
|
|
return found;
|
|
|
|
});
|
2017-08-20 19:08:32 +00:00
|
|
|
});
|
2017-08-16 21:01:45 +00:00
|
|
|
|
2017-08-20 19:08:32 +00:00
|
|
|
CodeMirror.registerHelper('lint', 'stylelint', text => {
|
|
|
|
const found = [];
|
2017-08-25 19:09:48 +00:00
|
|
|
window.stylelint = require('stylelint');
|
2017-08-20 19:08:32 +00:00
|
|
|
if (window.stylelint) {
|
|
|
|
return BG.chromeLocal.getValue('editorStylelintRules').then((rules = stylelintDefaultConfig.rules) => {
|
2017-08-23 22:13:55 +00:00
|
|
|
// stylelintDefaultConfig stored in stylelint-config.js & loaded by edit/lint.js
|
2017-08-20 19:08:32 +00:00
|
|
|
if (Object.keys(rules).length === 0) {
|
|
|
|
rules = stylelintDefaultConfig.rules;
|
2017-08-17 19:08:48 +00:00
|
|
|
}
|
2017-08-25 19:09:48 +00:00
|
|
|
return stylelint.lint({
|
2017-08-20 19:08:32 +00:00
|
|
|
code: text,
|
|
|
|
config: {
|
|
|
|
syntax: stylelintDefaultConfig.syntax,
|
|
|
|
rules: rules
|
2017-08-17 19:08:48 +00:00
|
|
|
}
|
2017-08-20 19:08:32 +00:00
|
|
|
}).then(output => {
|
|
|
|
const warnings = output.results.length ? output.results[0].warnings : [];
|
|
|
|
const len = warnings.length;
|
|
|
|
let warning;
|
2017-08-20 20:41:07 +00:00
|
|
|
let message;
|
2017-08-20 19:08:32 +00:00
|
|
|
if (len) {
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
warning = warnings[i];
|
2017-08-20 20:41:07 +00:00
|
|
|
message = warning.text
|
|
|
|
.replace('Unexpected ', '')
|
|
|
|
.replace(/^./, function (firstLetter) {
|
|
|
|
return firstLetter.toUpperCase();
|
|
|
|
});
|
2017-08-20 19:08:32 +00:00
|
|
|
found.push({
|
|
|
|
from: CodeMirror.Pos(warning.line - 1, warning.column - 1),
|
|
|
|
to: CodeMirror.Pos(warning.line - 1, warning.column),
|
2017-08-20 20:41:07 +00:00
|
|
|
message,
|
2017-08-20 19:08:32 +00:00
|
|
|
severity : warning.severity
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
});
|
2017-08-17 19:08:48 +00:00
|
|
|
});
|
2017-08-20 19:08:32 +00:00
|
|
|
}
|
|
|
|
return found;
|
|
|
|
});
|
2017-08-15 19:13:58 +00:00
|
|
|
});
|