Skip to content

Commit

Permalink
Refactor to use severity constants (#7882)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous authored Jul 28, 2024
1 parent cfe2bcf commit 8fefd14
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 35 deletions.
4 changes: 3 additions & 1 deletion lib/createPartialStylelintResult.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const constants = require('./constants.cjs');

/** @import {LintResult as StylelintResult, PostcssResult} from 'stylelint' */

/**
Expand Down Expand Up @@ -96,7 +98,7 @@ function createPartialStylelintResult(postcssResult, cssSyntaxError) {
endLine: cssSyntaxError.endLine,
endColumn: cssSyntaxError.endColumn,
rule: cssSyntaxError.name,
severity: 'error',
severity: constants.SEVERITY_ERROR,
text: `${cssSyntaxError.reason} (${cssSyntaxError.name})`,
},
],
Expand Down
4 changes: 3 additions & 1 deletion lib/createPartialStylelintResult.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SEVERITY_ERROR } from './constants.mjs';

/** @import {LintResult as StylelintResult, PostcssResult} from 'stylelint' */

/**
Expand Down Expand Up @@ -92,7 +94,7 @@ export default function createPartialStylelintResult(postcssResult, cssSyntaxErr
endLine: cssSyntaxError.endLine,
endColumn: cssSyntaxError.endColumn,
rule: cssSyntaxError.name,
severity: 'error',
severity: SEVERITY_ERROR,
text: `${cssSyntaxError.reason} (${cssSyntaxError.name})`,
},
],
Expand Down
6 changes: 4 additions & 2 deletions lib/formatters/calcSeverityCounts.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const constants = require('../constants.cjs');

/**
* @import {Severity} from 'stylelint'
*
Expand All @@ -11,10 +13,10 @@
*/
function calcSeverityCounts(severity, counts) {
switch (severity) {
case 'error':
case constants.SEVERITY_ERROR:
counts.error += 1;
break;
case 'warning':
case constants.SEVERITY_WARNING:
counts.warning += 1;
break;
default:
Expand Down
6 changes: 4 additions & 2 deletions lib/formatters/calcSeverityCounts.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SEVERITY_ERROR, SEVERITY_WARNING } from '../constants.mjs';

/**
* @import {Severity} from 'stylelint'
*
Expand All @@ -7,10 +9,10 @@
*/
export default function calcSeverityCounts(severity, counts) {
switch (severity) {
case 'error':
case SEVERITY_ERROR:
counts.error += 1;
break;
case 'warning':
case SEVERITY_WARNING:
counts.warning += 1;
break;
default:
Expand Down
6 changes: 4 additions & 2 deletions lib/formatters/preprocessWarnings.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const constants = require('../constants.cjs');

/** @import {LintResult} from 'stylelint' */
/** @typedef {LintResult['parseErrors'][0]} ParseError */
/** @typedef {LintResult['warnings'][0]} Warning */
Expand Down Expand Up @@ -37,7 +39,7 @@ function parseErrorToWarning(error) {
line: error.line,
column: error.column,
rule: error.stylelintType,
severity: 'error',
severity: constants.SEVERITY_ERROR,
text: `${error.text} (${error.stylelintType})`,
};
}
Expand All @@ -49,7 +51,7 @@ function parseErrorToWarning(error) {
function normalizeSeverity(warning) {
// NOTE: Plugins may add a warning without severity, for example,
// by directly using the PostCSS `Result#warn()` API.
return warning.severity || 'error';
return warning.severity || constants.DEFAULT_SEVERITY;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/formatters/preprocessWarnings.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DEFAULT_SEVERITY, SEVERITY_ERROR } from '../constants.mjs';

/** @import {LintResult} from 'stylelint' */
/** @typedef {LintResult['parseErrors'][0]} ParseError */
/** @typedef {LintResult['warnings'][0]} Warning */
Expand Down Expand Up @@ -33,7 +35,7 @@ function parseErrorToWarning(error) {
line: error.line,
column: error.column,
rule: error.stylelintType,
severity: 'error',
severity: SEVERITY_ERROR,
text: `${error.text} (${error.stylelintType})`,
};
}
Expand All @@ -45,7 +47,7 @@ function parseErrorToWarning(error) {
function normalizeSeverity(warning) {
// NOTE: Plugins may add a warning without severity, for example,
// by directly using the PostCSS `Result#warn()` API.
return warning.severity || 'error';
return warning.severity || DEFAULT_SEVERITY;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/lintPostcssResult.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
'use strict';

const node_os = require('node:os');
const configurationComment = require('./utils/configurationComment.cjs');
const constants = require('./constants.cjs');
const configurationComment = require('./utils/configurationComment.cjs');
const assignDisabledRanges = require('./assignDisabledRanges.cjs');
const getStylelintRule = require('./utils/getStylelintRule.cjs');
const reportUnknownRuleNames = require('./reportUnknownRuleNames.cjs');
Expand Down Expand Up @@ -90,7 +90,7 @@ async function lintPostcssResult(stylelintOptions, postcssResult, config) {
const secondaryOptions = ruleSettings[1];

// Log the rule's severity in the PostCSS result
const defaultSeverity = config.defaultSeverity || 'error';
const defaultSeverity = config.defaultSeverity || constants.DEFAULT_SEVERITY;
// disableFix in secondary option
const disableFix = (secondaryOptions && secondaryOptions.disableFix === true) || false;

Expand Down
4 changes: 2 additions & 2 deletions lib/lintPostcssResult.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EOL } from 'node:os';

import { DEFAULT_SEVERITY, RULE_NAME_ALL } from './constants.mjs';
import { DEFAULT_CONFIGURATION_COMMENT } from './utils/configurationComment.mjs';
import { RULE_NAME_ALL } from './constants.mjs';
import assignDisabledRanges from './assignDisabledRanges.mjs';
import getStylelintRule from './utils/getStylelintRule.mjs';
import reportUnknownRuleNames from './reportUnknownRuleNames.mjs';
Expand Down Expand Up @@ -87,7 +87,7 @@ export default async function lintPostcssResult(stylelintOptions, postcssResult,
const secondaryOptions = ruleSettings[1];

// Log the rule's severity in the PostCSS result
const defaultSeverity = config.defaultSeverity || 'error';
const defaultSeverity = config.defaultSeverity || DEFAULT_SEVERITY;
// disableFix in secondary option
const disableFix = (secondaryOptions && secondaryOptions.disableFix === true) || false;

Expand Down
4 changes: 2 additions & 2 deletions lib/prepareReturnValue.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const constants = require('./constants.cjs');
const emitDeprecationWarning = require('./utils/emitDeprecationWarning.cjs');

/** @import { Formatter, LinterOptions, LinterResult, LintResult } from 'stylelint' */


/**
* @param {object} args
* @param {LintResult[]} args.results
Expand All @@ -29,7 +29,7 @@ function prepareReturnValue({
if (
result.errored ||
result.parseErrors.length > 0 ||
result.warnings.some((warning) => warning.severity === 'error')
result.warnings.some((warning) => warning.severity === constants.SEVERITY_ERROR)
) {
errored = true;
result.errored = true;
Expand Down
7 changes: 4 additions & 3 deletions lib/prepareReturnValue.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @import { Formatter, LinterOptions, LinterResult, LintResult } from 'stylelint' */

import { SEVERITY_ERROR } from './constants.mjs';
import emitDeprecationWarning from './utils/emitDeprecationWarning.mjs';

/** @import { Formatter, LinterOptions, LinterResult, LintResult } from 'stylelint' */

/**
* @param {object} args
* @param {LintResult[]} args.results
Expand All @@ -24,7 +25,7 @@ export default function prepareReturnValue({
if (
result.errored ||
result.parseErrors.length > 0 ||
result.warnings.some((warning) => warning.severity === 'error')
result.warnings.some((warning) => warning.severity === SEVERITY_ERROR)
) {
errored = true;
result.errored = true;
Expand Down
3 changes: 2 additions & 1 deletion lib/reportDisables.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const constants = require('./constants.cjs');
const reportCommentProblem = require('./utils/reportCommentProblem.cjs');

/**
Expand Down Expand Up @@ -32,7 +33,7 @@ function reportDisables(postcssResult) {
reportCommentProblem({
rule: 'reportDisables',
message: `Rule "${rule}" may not be disabled`,
severity: 'error',
severity: constants.SEVERITY_ERROR,
node: range.node,
postcssResult,
});
Expand Down
3 changes: 2 additions & 1 deletion lib/reportDisables.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SEVERITY_ERROR } from './constants.mjs';
import reportCommentProblem from './utils/reportCommentProblem.mjs';

/**
Expand Down Expand Up @@ -28,7 +29,7 @@ export default function reportDisables(postcssResult) {
reportCommentProblem({
rule: 'reportDisables',
message: `Rule "${rule}" may not be disabled`,
severity: 'error',
severity: SEVERITY_ERROR,
node: range.node,
postcssResult,
});
Expand Down
3 changes: 2 additions & 1 deletion lib/reportUnknownRuleNames.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict';

const fastestLevenshtein = require('fastest-levenshtein');
const constants = require('./constants.cjs');
const index = require('./rules/index.cjs');

const MAX_LEVENSHTEIN_DISTANCE = 6;
Expand Down Expand Up @@ -70,7 +71,7 @@ function reportUnknownRuleNames(unknownRuleName, postcssRoot, postcssResult) {

cache.set(unknownRuleName, suggestions);
postcssResult.warn(rejectMessage(unknownRuleName, suggestions), {
severity: 'error',
severity: constants.SEVERITY_ERROR,
rule: unknownRuleName,
node: postcssRoot,
index: 0,
Expand Down
3 changes: 2 additions & 1 deletion lib/reportUnknownRuleNames.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { distance } from 'fastest-levenshtein';

import { SEVERITY_ERROR } from './constants.mjs';
import rules from './rules/index.mjs';

const MAX_LEVENSHTEIN_DISTANCE = 6;
Expand Down Expand Up @@ -67,7 +68,7 @@ export default function reportUnknownRuleNames(unknownRuleName, postcssRoot, pos

cache.set(unknownRuleName, suggestions);
postcssResult.warn(rejectMessage(unknownRuleName, suggestions), {
severity: 'error',
severity: SEVERITY_ERROR,
rule: unknownRuleName,
node: postcssRoot,
index: 0,
Expand Down
6 changes: 4 additions & 2 deletions lib/utils/reportCommentProblem.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const constants = require('../constants.cjs');

/**
* @param {{
* rule: string;
Expand All @@ -28,10 +30,10 @@ function reportCommentProblem({ rule, message, severity, node, postcssResult })
});

switch (severity) {
case 'error':
case constants.SEVERITY_ERROR:
postcssResult.stylelint.stylelintError = true;
break;
case 'warning':
case constants.SEVERITY_WARNING:
postcssResult.stylelint.stylelintWarning = true;
break;
default:
Expand Down
6 changes: 4 additions & 2 deletions lib/utils/reportCommentProblem.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SEVERITY_ERROR, SEVERITY_WARNING } from '../constants.mjs';

/**
* @param {{
* rule: string;
Expand All @@ -24,10 +26,10 @@ export default function reportCommentProblem({ rule, message, severity, node, po
});

switch (severity) {
case 'error':
case SEVERITY_ERROR:
postcssResult.stylelint.stylelintError = true;
break;
case 'warning':
case SEVERITY_WARNING:
postcssResult.stylelint.stylelintWarning = true;
break;
default:
Expand Down
9 changes: 5 additions & 4 deletions lib/validateDisableSettings.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
'use strict';

const validateTypes = require('./utils/validateTypes.cjs');
const constants = require('./constants.cjs');
const validateOptions = require('./utils/validateOptions.cjs');

/** @import {DisableOptions} from 'stylelint' */
/** @import {DisablePropertyName, DisableOptions, PostcssResult} from 'stylelint' */

/**
* Validates that the stylelint config for `result` has a valid disable field
Expand All @@ -14,8 +15,8 @@ const validateOptions = require('./utils/validateOptions.cjs');
* Returns `[]` if no disables should be reported, and automatically reports
* an invalid configuration.
*
* @param {import('stylelint').PostcssResult} result
* @param {import('stylelint').DisablePropertyName} field
* @param {PostcssResult} result
* @param {DisablePropertyName} field
* @returns {[boolean, Required<DisableOptions>] | []}
*/
function validateDisableSettings(result, field) {
Expand Down Expand Up @@ -64,7 +65,7 @@ function validateDisableSettings(result, field) {
enabled,
{
except: options.except || [],
severity: options.severity || stylelintResult.config.defaultSeverity || 'error',
severity: options.severity || stylelintResult.config.defaultSeverity || constants.DEFAULT_SEVERITY,
},
];
}
Expand Down
9 changes: 5 additions & 4 deletions lib/validateDisableSettings.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { isRegExp, isString } from './utils/validateTypes.mjs';
import { DEFAULT_SEVERITY } from './constants.mjs';
import validateOptions from './utils/validateOptions.mjs';

/** @import {DisableOptions} from 'stylelint' */
/** @import {DisablePropertyName, DisableOptions, PostcssResult} from 'stylelint' */

/**
* Validates that the stylelint config for `result` has a valid disable field
Expand All @@ -10,8 +11,8 @@ import validateOptions from './utils/validateOptions.mjs';
* Returns `[]` if no disables should be reported, and automatically reports
* an invalid configuration.
*
* @param {import('stylelint').PostcssResult} result
* @param {import('stylelint').DisablePropertyName} field
* @param {PostcssResult} result
* @param {DisablePropertyName} field
* @returns {[boolean, Required<DisableOptions>] | []}
*/
export default function validateDisableSettings(result, field) {
Expand Down Expand Up @@ -60,7 +61,7 @@ export default function validateDisableSettings(result, field) {
enabled,
{
except: options.except || [],
severity: options.severity || stylelintResult.config.defaultSeverity || 'error',
severity: options.severity || stylelintResult.config.defaultSeverity || DEFAULT_SEVERITY,
},
];
}

0 comments on commit 8fefd14

Please sign in to comment.