stylus/vendor-overwrites/codemirror/addon/lint/css-lint.js

115 lines
3.8 KiB
JavaScript
Raw Normal View History

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-27 17:36:36 +00:00
/* global CSSLint stylelint stylelintDefaultConfig csslintDefaultConfig */
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) */
2017-08-27 17:36:36 +00:00
return BG.chromeSync.getValue('editorCSSLintConfig').then(config => {
// csslintDefaultConfig stored in csslint-config.js & loaded by edit/lint.js
if (Object.keys(config || []).length === 0) {
config = Object.assign({}, csslintDefaultConfig);
2017-08-23 22:13:55 +00:00
}
2017-08-27 15:23:35 +00:00
const results = CSSLint.verify(text, config);
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 = [];
window.stylelint = require('stylelint');
2017-08-20 19:08:32 +00:00
if (window.stylelint) {
2017-08-27 17:36:36 +00:00
return BG.chromeSync.getValue('editorStylelintConfig').then(rules => {
2017-08-23 22:13:55 +00:00
// stylelintDefaultConfig stored in stylelint-config.js & loaded by edit/lint.js
2017-08-27 17:36:36 +00:00
if (Object.keys(rules || []).length === 0) {
2017-08-20 19:08:32 +00:00
rules = stylelintDefaultConfig.rules;
2017-08-17 19:08: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;
let message;
2017-08-20 19:08:32 +00:00
if (len) {
for (let i = 0; i < len; i++) {
warning = warnings[i];
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),
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
});