Clean up modified css-lint

This commit is contained in:
Rob Garrison 2017-08-20 14:08:32 -05:00
parent 5bfbab62d7
commit 91825d11fd

View File

@ -3,35 +3,47 @@
// Depends on csslint.js from https://github.com/stubbornella/csslint // Depends on csslint.js from https://github.com/stubbornella/csslint
// declare global: CSSLint /* global CodeMirror require define */
/* global CSSLint stylelint stylelintDefaultConfig */
'use strict';
(function(mod) { (mod => {
if (typeof exports == "object" && typeof module == "object") // CommonJS if (typeof exports === 'object' && typeof module === 'object') {
mod(require("../../lib/codemirror")); // CommonJS
else if (typeof define == "function" && define.amd) // AMD mod(require('../../lib/codemirror'));
define(["../../lib/codemirror"], mod); } else if (typeof define === 'function' && define.amd) {
else // Plain browser env // AMD
define(['../../lib/codemirror'], mod);
} else {
// Plain browser env
mod(CodeMirror); mod(CodeMirror);
})(function(CodeMirror) { }
"use strict"; })(CodeMirror => {
CodeMirror.registerHelper('lint', 'csslint', text => {
CodeMirror.registerHelper("lint", "csslint", function(text) { const found = [];
let found = [];
if (window.CSSLint) { if (window.CSSLint) {
/* STYLISH: hack start (part 1) */ /* STYLISH: hack start (part 1) */
var rules = CSSLint.getRules(); const rules = CSSLint.getRules();
var allowedRules = ["display-property-grouping", "duplicate-properties", "empty-rules", "errors", "known-properties"]; const allowedRules = [
'display-property-grouping',
'duplicate-properties',
'empty-rules',
'errors',
'known-properties'
];
CSSLint.clearRules(); CSSLint.clearRules();
rules.forEach(function(rule) { rules.forEach(rule => {
if (allowedRules.indexOf(rule.id) >= 0) { if (allowedRules.indexOf(rule.id) >= 0) {
CSSLint.addRule(rule); CSSLint.addRule(rule);
} }
}); });
/* STYLISH: hack end */ /* STYLISH: hack end */
var results = CSSLint.verify(text), messages = results.messages, message = null; const results = CSSLint.verify(text);
for ( var i = 0; i < messages.length; i++) { 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;
for (let i = 0; i < messages.length; i++) {
message = messages[i]; message = messages[i];
/* STYLISH: hack start (part 2) */ /* STYLISH: hack start (part 2) */
@ -39,18 +51,19 @@ CodeMirror.registerHelper("lint", "csslint", function(text) {
// @font-face {font-family: 'Ampersand'; unicode-range: U+26;} // @font-face {font-family: 'Ampersand'; unicode-range: U+26;}
if (message.message.indexOf('unicode-range') !== -1) { if (message.message.indexOf('unicode-range') !== -1) {
continue; continue;
} } else if (
else if ( // color: hsl(210, 100%, 2.2%); or color: hsla(210, 100%, 2.2%, 0.3); // color: hsl(210, 100%, 2.2%); or color: hsla(210, 100%, 2.2%, 0.3);
message.message.startsWith('Expected (<color>) but found \'hsl') && message.message.startsWith('Expected (<color>) but found \'hsl') &&
/hsla?\(\s*(-?\d+)%?\s*,\s*(-?\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%(\s*,\s*(-?\d+|-?\d*.\d+))?\s*\)/.test(message.message) hslRegex.test(message.message)
) { ) {
continue; continue;
} }
//
} }
/* STYLISH: hack end */ /* STYLISH: hack end */
const startLine = message.line - 1;
var startLine = message.line -1, endLine = message.line -1, startCol = message.col -1, endCol = message.col; const endLine = message.line - 1;
const startCol = message.col - 1;
const endCol = message.col;
found.push({ found.push({
from: CodeMirror.Pos(startLine, startCol), from: CodeMirror.Pos(startLine, startCol),
to: CodeMirror.Pos(endLine, endCol), to: CodeMirror.Pos(endLine, endCol),
@ -62,8 +75,8 @@ CodeMirror.registerHelper("lint", "csslint", function(text) {
return found; return found;
}); });
CodeMirror.registerHelper("lint", "stylelint", function(text) { CodeMirror.registerHelper('lint', 'stylelint', text => {
let found = []; const found = [];
window.stylelint = require('stylelint').lint; window.stylelint = require('stylelint').lint;
if (window.stylelint) { if (window.stylelint) {
return BG.chromeLocal.getValue('editorStylelintRules').then((rules = stylelintDefaultConfig.rules) => { return BG.chromeLocal.getValue('editorStylelintRules').then((rules = stylelintDefaultConfig.rules) => {
@ -78,11 +91,11 @@ CodeMirror.registerHelper("lint", "stylelint", function(text) {
rules: rules rules: rules
} }
}).then(output => { }).then(output => {
const warnings = output.results.length ? output.results[0].warnings : [], const warnings = output.results.length ? output.results[0].warnings : [];
len = warnings.length; const len = warnings.length;
let i, warning; let warning;
if (len) { if (len) {
for (i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
warning = warnings[i]; warning = warnings[i];
found.push({ found.push({
from: CodeMirror.Pos(warning.line - 1, warning.column - 1), from: CodeMirror.Pos(warning.line - 1, warning.column - 1),
@ -98,5 +111,4 @@ CodeMirror.registerHelper("lint", "stylelint", function(text) {
} }
return found; return found;
}); });
}); });