Sha256: 3ae50cfc06573e6b1831b290fecd501053677373e5791796e735352c2061ac03

Contents?: true

Size: 1.74 KB

Versions: 45

Compression:

Stored size: 1.74 KB

Contents

'use strict';

var helpers = require('../helpers'),
    capitalize = require('lodash.capitalize'),
    kebabcase = require('lodash.kebabcase');

// Our nestable selector types, separated by type for ease of use with rules
// we replace ident with 'selector' for readability'
var nestableElements = ['selector', 'class', 'id', 'typeSelector', 'parentSelectorExtension'],
    nestableAttributes = ['attributeSelector'],
    nestablePseudo = ['pseudoClass', 'pseudoElement', 'nth', 'nthSelector'];

/**
 * Formats a string from camelCase to hyphens and capitalizes
 * @param {string} str - The string to be formatted
 * @returns {string} A hyphenated and capitalized string
 */
var formatOutput = function (str) {
  return capitalize(kebabcase(str));
};

module.exports = {
  'name': 'force-element-nesting',
  'defaults': {},
  'detect': function (ast, parser) {
    var result = [],
        elements = nestableElements.concat(nestableAttributes, nestablePseudo);

    ast.traverseByType('ruleset', function (ruleset) {
      ruleset.forEach(function (selector) {

        var previousVal;
        selector.forEach(function (item) {
          if (previousVal) {
            if (helpers.isNestable(item.type, previousVal.type, elements, nestableElements)) {
              helpers.addUnique(result, {
                'ruleId': parser.rule.name,
                'line': selector.start.line,
                'column': selector.start.column,
                'message': formatOutput(item.type) + ' should be nested within its parent ' + formatOutput(previousVal.type),
                'severity': parser.severity
              });
            }
          }
          if (!item.is('space')) {
            previousVal = item;
          }
        });
      });
    });

    return result;
  }
};

Version data entries

45 entries across 45 versions & 2 rubygems

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