How to use the stylelint.createPlugin function in stylelint

To help you get started, we’ve selected a few stylelint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github primer / stylelint-config-primer / plugins / no-unused-vars.js View on Github external
const stylelint = require('stylelint')
const {readFileSync} = require('fs')

const ruleName = 'primer/no-unused-vars'

const cwd = process.cwd()
const COLON = ':'
const SCSS_VARIABLE_PATTERN = /(\$[-\w]+)/g

const messages = stylelint.utils.ruleMessages(ruleName, {
  rejected: name => `The variable "${name}" is not referenced.`
})

const cache = new TapMap()

module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => {
  if (!enabled) {
    return noop
  }

  const {files = ['**/*.scss', '!node_modules'], variablePattern = SCSS_VARIABLE_PATTERN, verbose = false} = options
  // eslint-disable-next-line no-console
  const log = verbose ? (...args) => console.warn(...args) : noop
  const cacheOptions = {files, variablePattern, cwd}
  const {refs} = getCachedVariables(cacheOptions, log)

  return (root, result) => {
    root.walkDecls(decl => {
      for (const [name] of matchAll(decl.prop, variablePattern)) {
        if (!refs.has(name)) {
          stylelint.utils.report({
            message: messages.rejected(name),
github primer / stylelint-config-primer / plugins / selector-no-utility.js View on Github external
const stylelint = require('stylelint')

const ruleName = 'primer/selector-no-utility'

module.exports = stylelint.createPlugin(ruleName, (enabled, ...args) => {
  if (!enabled) {
    return noop
  }

  let selectorNoUtility
  try {
    selectorNoUtility = require('stylelint-selector-no-utility')
  } catch (error) {
    // eslint-disable-next-line no-console
    console.warn(`Unable to require('stylelint-selector-no-utility'): ${error}`)
    return noop
  }

  const deprecatedPlugin = selectorNoUtility.rule(enabled, ...args)
  return (root, result) => {
    if (enabled === false) {
github gitlabhq / gitlabhq / scripts / frontend / stylelint / stylelint-duplicate-selectors.js View on Github external
const stylelint = require('stylelint');
const utils = require('./stylelint-utils');

const ruleName = 'stylelint-gitlab/duplicate-selectors';

const messages = stylelint.utils.ruleMessages(ruleName, {
  expected: (selector1, selector2) => {
    return `"${selector1}" and "${selector2}" have the same properties.`;
  },
});

module.exports = stylelint.createPlugin(ruleName, (enabled) => {
  if (!enabled) {
    return;
  }

  // eslint-disable-next-line consistent-return
  return (root, result) => {
    const selectorGroups = {};
    utils.createPropertiesHashmap(root, result, ruleName, messages, selectorGroups, true);
  };
});

module.exports.ruleName = ruleName;
module.exports.messages = messages;
github signal-noise / stylelint-scales / lib / rules / border-width / index.js View on Github external
return;
        }
        // Report the violation to stylelint
        report({
          message,
          node: decl,
          result,
          ruleName,
          word: value
        });
      });
    }
  };
};

module.exports = createPlugin(ruleName, rule);
module.exports.ruleName = ruleName;
module.exports.messages = messages;
github barnardos / design-system / stylelint / stylelint-function-arguments-whitelist / index.js View on Github external
result,
          ruleName
        });
      });
    });
  };
};

const getArguments = node => {
  // strip first and last nodes (i.e. the parens)
  node.removeChild(node.first);
  node.removeChild(node.last);
  return node.nodes.toString();
};

module.exports = createPlugin(ruleName, rule);
module.exports.ruleName = ruleName;
module.exports.messages = messages;
github primer / stylelint-config-primer / plugins / no-override.js View on Github external
const stylelint = require('stylelint')
const {requirePrimerFile} = require('./lib/primer')

const ruleName = 'primer/no-override'
const CLASS_PATTERN = /(\.[-\w]+)/
const CLASS_PATTERN_ALL = new RegExp(CLASS_PATTERN, 'g')
const CLASS_PATTERN_ONLY = /^\.[-\w]+(:{1,2}[-\w]+)?$/

module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => {
  if (!enabled) {
    return noop
  }

  const {bundles = ['utilities'], ignoreSelectors = []} = options

  const isSelectorIgnored =
    typeof ignoreSelectors === 'function'
      ? ignoreSelectors
      : selector => {
          return ignoreSelectors.some(pattern => {
            return pattern instanceof RegExp ? pattern.test(selector) : selector.includes(pattern)
          })
        }

  const primerMeta = requirePrimerFile('dist/meta.json')
github swissquote / crafty / packages / stylelint-config-swissquote / index.js View on Github external
const noUtilityReassignment = require("./src/rules/no-utility-reassignment");
const noHackReassignment = require("./src/rules/no-hack-reassignment");
const noStateWithoutComponent = require("./src/rules/no-state-without-component");
const noTypeOutsideScope = require("./src/rules/no-type-outside-scope");
const noNegativeVar = require("./src/rules/no-negative-var");

module.exports = [
  stylelint.createPlugin(noUtilityReassignment.ruleName, noUtilityReassignment),
  stylelint.createPlugin(noHackReassignment.ruleName, noHackReassignment),
  stylelint.createPlugin(
    noStateWithoutComponent.ruleName,
    noStateWithoutComponent
  ),
  stylelint.createPlugin(noTypeOutsideScope.ruleName, noTypeOutsideScope),
  stylelint.createPlugin(noNegativeVar.ruleName, noNegativeVar)
];
github barnardos / design-system / stylelint / stylelint-color-hex-whitelist / index.js View on Github external
const message = messages.rejected(value);
        report({
          index,
          message,
          node,
          result,
          ruleName
        });
      });
    });
  };
};

rule.primaryOptionArray = true;

module.exports = createPlugin(ruleName, rule);
module.exports.ruleName = ruleName;
module.exports.messages = messages;
github swissquote / crafty / packages / stylelint-config-swissquote / index.js View on Github external
const stylelint = require("stylelint");

const noUtilityReassignment = require("./src/rules/no-utility-reassignment");
const noHackReassignment = require("./src/rules/no-hack-reassignment");
const noStateWithoutComponent = require("./src/rules/no-state-without-component");
const noTypeOutsideScope = require("./src/rules/no-type-outside-scope");
const noNegativeVar = require("./src/rules/no-negative-var");

module.exports = [
  stylelint.createPlugin(noUtilityReassignment.ruleName, noUtilityReassignment),
  stylelint.createPlugin(noHackReassignment.ruleName, noHackReassignment),
  stylelint.createPlugin(
    noStateWithoutComponent.ruleName,
    noStateWithoutComponent
  ),
  stylelint.createPlugin(noTypeOutsideScope.ruleName, noTypeOutsideScope),
  stylelint.createPlugin(noNegativeVar.ruleName, noNegativeVar)
];
github barnardos / design-system / stylelint / stylelint-property-value-rem-multiples / index.js View on Github external
if ((value * baseSize) % multiple === 0) return;
        const index = declarationValueIndex(node) + sourceIndex;
        const message = messages.expected(`${value}${unit}`, multiple);
        report({
          index,
          message,
          node,
          result,
          ruleName
        });
      });
    });
  };
};

module.exports = createPlugin(ruleName, rule);
module.exports.ruleName = ruleName;
module.exports.messages = messages;