123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 'use strict';
- const assignDisabledRanges = require('./assignDisabledRanges');
- const getOsEol = require('./utils/getOsEol');
- const reportUnknownRuleNames = require('./reportUnknownRuleNames');
- const rules = require('./rules');
- function lintPostcssResult(stylelintOptions, postcssResult, config) {
- postcssResult.stylelint.ruleSeverities = {};
- postcssResult.stylelint.customMessages = {};
- postcssResult.stylelint.ruleMetadata = {};
- postcssResult.stylelint.stylelintError = false;
- postcssResult.stylelint.quiet = config.quiet;
- postcssResult.stylelint.config = config;
-
- let newline;
- const postcssDoc = postcssResult.root;
- if (postcssDoc) {
- if (!('type' in postcssDoc)) {
- throw new Error('Unexpected Postcss root object!');
- }
- const newlineMatch = postcssDoc.source && postcssDoc.source.input.css.match(/\r?\n/);
- newline = newlineMatch ? newlineMatch[0] : getOsEol();
- assignDisabledRanges(postcssDoc, postcssResult);
- }
- const isFileFixCompatible = isFixCompatible(postcssResult);
- if (!isFileFixCompatible) {
- postcssResult.stylelint.disableWritingFix = true;
- }
- const postcssRoots = (
- postcssDoc && postcssDoc.constructor.name === 'Document' ? postcssDoc.nodes : [postcssDoc]
- );
-
-
-
-
- const performRules = [];
- const rulesOrder = Object.keys(rules);
- const ruleNames = config.rules
- ? Object.keys(config.rules).sort((a, b) => rulesOrder.indexOf(a) - rulesOrder.indexOf(b))
- : [];
- for (const ruleName of ruleNames) {
- const ruleFunction =
- rules[ruleName] || (config.pluginFunctions && config.pluginFunctions[ruleName]);
- if (ruleFunction === undefined) {
- performRules.push(
- Promise.all(
- postcssRoots.map((postcssRoot) =>
- reportUnknownRuleNames(ruleName, postcssRoot, postcssResult),
- ),
- ),
- );
- continue;
- }
- const ruleSettings = config.rules && config.rules[ruleName];
- if (ruleSettings === null || ruleSettings[0] === null) {
- continue;
- }
- const primaryOption = ruleSettings[0];
- const secondaryOptions = ruleSettings[1];
-
- const defaultSeverity = config.defaultSeverity || 'error';
-
- const disableFix = (secondaryOptions && secondaryOptions.disableFix === true) || false;
- if (disableFix) {
- postcssResult.stylelint.ruleDisableFix = true;
- }
- postcssResult.stylelint.ruleSeverities[ruleName] =
- (secondaryOptions && secondaryOptions.severity) || defaultSeverity;
- postcssResult.stylelint.customMessages[ruleName] = secondaryOptions && secondaryOptions.message;
- postcssResult.stylelint.ruleMetadata[ruleName] = ruleFunction.meta || {};
- performRules.push(
- Promise.all(
- postcssRoots.map((postcssRoot) =>
- ruleFunction(primaryOption, secondaryOptions, {
- fix:
- !disableFix &&
- stylelintOptions.fix &&
-
- isFileFixCompatible &&
- !postcssResult.stylelint.disabledRanges[ruleName],
- newline,
- })(postcssRoot, postcssResult),
- ),
- ),
- );
- }
- return Promise.all(performRules);
- }
- function isFixCompatible({ stylelint }) {
-
- if (stylelint.disabledRanges.all && stylelint.disabledRanges.all.length) return false;
- return true;
- }
- module.exports = lintPostcssResult;
|