Sha256: 826944ddc52cb67acbac70c522fc6c3317340ac5c9ce0c3deb7ab8006f3da1bf

Contents?: true

Size: 1.46 KB

Versions: 43

Compression:

Stored size: 1.46 KB

Contents

'use strict';

var helpers = require('../helpers'),
    yaml = require('js-yaml'),
    fs = require('fs'),
    path = require('path');

var cssColors = yaml.safeLoad(fs.readFileSync(path.join(__dirname, '../../data', 'literals.yml'), 'utf8')).split(' ');

/**
 * Checks if a node's parent is a valid type as we dont want to apply
 * this rule to function names or variable names
 *
 * @param {Object} node - The parent node to test
 * @returns {boolean} Whether the node is a valid type or not
 */
var checkValidParentType = function (node) {
  if (node && (node.is('function') || node.is('variable') || node.is('customProperty'))) {
    return false;
  }

  return true;
};

module.exports = {
  'name': 'no-color-keywords',
  'defaults': {},
  'detect': function (ast, parser) {
    var result = [];

    ast.traverseByType('value', function (node) {
      node.traverse(function (elem, i, parent) {
        if (elem.type === 'ident' && checkValidParentType(parent)) {
          var index = cssColors.indexOf(elem.content.toLowerCase());

          if (index !== -1) {
            result = helpers.addUnique(result, {
              'ruleId': parser.rule.name,
              'line': elem.start.line,
              'column': elem.start.column,
              'message': 'Color \'' + elem.content + '\' should be written in its hexadecimal form #' + cssColors[index + 1],
              'severity': parser.severity
            });
          }
        }
      });

    });
    return result;
  }
};

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
govuk_publishing_components-18.0.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.21.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.20.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.19.1 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.19.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.18.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.17.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.16.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.15.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.14.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.13.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.12.2 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.12.1 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.12.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.11.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.10.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.9.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.8.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.7.0 node_modules/sass-lint/lib/rules/no-color-keywords.js
govuk_publishing_components-17.6.1 node_modules/sass-lint/lib/rules/no-color-keywords.js