How to use stylelint - 10 common examples

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 kristerkari / stylelint-scss / src / rules / at-function-pattern / index.js View on Github external
root.walkAtRules(decl => {
      if (decl.name !== "function") {
        return;
      }

      // Stripping the function of its arguments
      const funcName = decl.params.replace(/(\s*?)\((?:\s|\S)*\)/g, "");

      if (regexpPattern.test(funcName)) {
        return;
      }

      utils.report({
        message: messages.expected,
        node: decl,
        result,
        ruleName
      });
    });
  };
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 liferay / liferay-frontend-source-formatter / lib / lint_css.js View on Github external
var runLinter = (contents, file, context) => {
	var customRules = context.customRules || {};

	_.merge(stylelint.rules, customRules);

	// console.log(stylelint.rules);

	var config = context.lintConfig;

	var configs = [{}, STYLELINT_CONFIG];

	if (_.isObject(config)) {
		configs.push(config);
	}

	config = _.merge(...configs);

	return stylelint.lint(
		{
			code: contents,
github YozhikM / stylelint-a11y / jest.setup.js View on Github external
if (testCase.column !== undefined) {
                expect(_.get(warning, 'column')).toBe(testCase.column);
              }

              if (!schema.fix) {
                return;
              }

              if (!testCase.fixed) {
                throw new Error(
                  'If using { fix: true } in test schema, all reject cases must have { fixed: .. }'
                );
              }

              // Check the fix
              return stylelint.lint(Object.assign({ fix: true }, options)).then(output2 => {
                const fixedCode = getOutputCss(output2);

                expect(fixedCode).toBe(testCase.fixed);
                expect(output2.results[0].warnings.length).toBe(0); // Ensure errors are not reported on fixed code
              });
            });
          });
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);
  };
});
github csstree / stylelint-validator / index.js View on Github external
var match = syntax.matchProperty(decl.prop, value);
            var error = match.error;
            if (error) {
                var message = error.rawMessage || error.message || error;

                // ignore errors except those which make sense
                if (error.name !== 'SyntaxMatchError' &&
                    error.name !== 'SyntaxReferenceError') {
                    return;
                }

                if (message === 'Mismatch') {
                    message = messages.invalid(decl.prop);
                }

                stylelint.utils.report({
                    message: message,
                    node: decl,
                    result: result,
                    ruleName: ruleName
                });
            }
        });
    }
github gitlabhq / gitlabhq / scripts / frontend / stylelint / stylelint-utility-classes.js View on Github external
const stylelint = require('stylelint');
const utils = require('./stylelint-utils');
const utilityClasses = require('./utility-classes-map.js');

const ruleName = 'stylelint-gitlab/utility-classes';

const messages = stylelint.utils.ruleMessages(ruleName, {
  expected: (selector1, selector2) => {
    return `"${selector1}" has the same properties as our BS4 utility class "${selector2}" so please use that instead.`;
  },
});

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

  // eslint-disable-next-line consistent-return
  return (root, result) => {
    utils.createPropertiesHashmap(root, result, ruleName, messages, utilityClasses, false);
  };
});
github hudochenkov / stylelint-order / rules / order / checkOrder.js View on Github external
Boolean(d.expectedPosition)
		);

		if (
			priorSpecifiedNodeData &&
			priorSpecifiedNodeData.expectedPosition &&
			priorSpecifiedNodeData.expectedPosition > secondNodeData.expectedPosition
		) {
			if (sharedInfo.isFixEnabled) {
				sharedInfo.shouldFix = true;

				// Don't go further, fix will be applied
				return;
			}

			stylelint.utils.report({
				message: sharedInfo.messages.expected(
					secondNodeData.description,
					priorSpecifiedNodeData.description
				),
				node: secondNodeData.node,
				result: sharedInfo.result,
				ruleName: sharedInfo.ruleName,
			});

			return true; // avoid logging another warning
		}
	}

	if (firstNodeIsUnspecified && secondNodeIsUnspecified) {
		return true;
	}
github kristerkari / stylelint-scss / src / rules / dollar-variable-default / index.js View on Github external
return;
      }

      // "ignore" options
      if (
        optionsHaveIgnored(secondaryOptions, "local") &&
        decl.parent.type !== "root"
      ) {
        return;
      }

      if (decl.value.toLowerCase().includes("!default")) {
        return;
      }

      utils.report({
        message: messages.expected(decl.prop),
        node: decl,
        result,
        ruleName
      });
    });
  };