linter-bundle
Version:
Ready-to use bundle of linting tools, containing configurations for ESLint, stylelint and markdownlint.
97 lines (77 loc) • 2.65 kB
JavaScript
// @ts-nocheck
import stylelint from 'stylelint';
import blockString from 'stylelint/lib/utils/blockString.mjs';
import rawNodeString from 'stylelint/lib/utils/rawNodeString.mjs';
import report from 'stylelint/lib/utils/report.mjs';
import ruleMessages from 'stylelint/lib/utils/ruleMessages.mjs';
import { isAtRule, isRule } from 'stylelint/lib/utils/typeGuards.mjs';
import validateOptions from 'stylelint/lib/utils/validateOptions.mjs';
import whitespaceChecker from '../../utils/whitespaceChecker.mjs';
const ruleName = 'plugin/declaration-block-semicolon-space-after';
const messages = ruleMessages(ruleName, {
expectedAfter: () => 'Expected single space after ";"',
rejectedAfter: () => 'Unexpected whitespace after ";"',
expectedAfterSingleLine: () => 'Expected single space after ";" in a single-line declaration block',
rejectedAfterSingleLine: () => 'Unexpected whitespace after ";" in a single-line declaration block'
});
const meta = {
url: 'https://github.com/jens-duttke/linter-bundle/blob/main/stylelint/plugins/stylelint-15.11.0-stylistic/rules/declaration-block-semicolon-space-after/README.md',
fixable: true
};
/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions) => {
const checker = whitespaceChecker('space', primary, messages);
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: ['always', 'never', 'always-single-line', 'never-single-line']
});
if (!validOptions) {
return;
}
root.walkDecls((decl) => {
// Ignore last declaration if there's no trailing semicolon
const parentRule = decl.parent;
if (!parentRule) { throw new Error('A parent node must be present'); }
if (!isAtRule(parentRule) && !isRule(parentRule)) {
return;
}
if (!parentRule.raws.semicolon && parentRule.last === decl) {
return;
}
const nextDecl = decl.next();
if (!nextDecl) {
return;
}
checker.after({
source: rawNodeString(nextDecl),
index: -1,
lineCheckStr: blockString(parentRule),
err: (m) => {
report({
message: m,
node: decl,
index: decl.toString().length + 1,
endIndex: decl.toString().length + 1,
result,
ruleName,
fix: () => {
if (primary.startsWith('always')) {
nextDecl.raws.before = ' ';
return;
}
if (primary.startsWith('never')) {
nextDecl.raws.before = '';
return;
}
}
});
}
});
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
export default stylelint.createPlugin(ruleName, rule);