lint-codemirror-helper.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* global CodeMirror CSSLint parserlib stylelint linterConfig */
  2. 'use strict';
  3. CodeMirror.registerHelper('lint', 'csslint', code => {
  4. if (!CSSLint.suppressUsoVarError) {
  5. CSSLint.suppressUsoVarError = true;
  6. parserlib.css.Tokens[parserlib.css.Tokens.COMMENT].hide = false;
  7. const isUsoVar = ({value}) => value.startsWith('/*[[') && value.endsWith(']]*/');
  8. CSSLint.addRule({
  9. id: 'uso-vars',
  10. init(parser, reporter) {
  11. parser.addListener('error', function ({message, line, col}) {
  12. if (!isUsoVar(this._tokenStream._token)) {
  13. const {_lt, _ltIndex: i} = this._tokenStream;
  14. if (i < 2 || !_lt.slice(0, i - 1).reverse().some(isUsoVar)) {
  15. reporter.error(message, line, col);
  16. }
  17. }
  18. });
  19. },
  20. });
  21. }
  22. const rules = deepCopy(linterConfig.getCurrent('csslint'));
  23. Object.defineProperty(rules, 'errors', {get: () => 0, set: () => 0});
  24. rules['uso-vars'] = 1;
  25. return CSSLint.verify(code, rules).messages
  26. .map(({line, col, message, rule, type}) => line && {
  27. message,
  28. from: {line: line - 1, ch: col - 1},
  29. to: {line: line - 1, ch: col},
  30. rule: rule.id,
  31. severity: type
  32. })
  33. .filter(Boolean);
  34. });
  35. CodeMirror.registerHelper('lint', 'stylelint', code =>
  36. stylelint.lint({
  37. code,
  38. config: deepCopy(linterConfig.getCurrent('stylelint')),
  39. }).then(({results}) => {
  40. if (!results[0]) {
  41. return [];
  42. }
  43. return results[0].warnings.map(warning => ({
  44. from: CodeMirror.Pos(warning.line - 1, warning.column - 1),
  45. to: CodeMirror.Pos(warning.line - 1, warning.column),
  46. message: warning.text
  47. .replace('Unexpected ', '')
  48. .replace(/^./, firstLetter => firstLetter.toUpperCase())
  49. .replace(/\s*\([^(]+\)$/, ''), // strip the rule,
  50. rule: warning.text.replace(/^.*?\s*\(([^(]+)\)$/, '$1'),
  51. severity : warning.severity
  52. }));
  53. })
  54. );