stylelint-plugin-import
Version:
Stylelint plugin for managing imports.
49 lines (48 loc) • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fileNameStartsWithRule = void 0;
const path_1 = require("path");
const stylelint_rule_creator_1 = require("stylelint-rule-creator");
const import_path_1 = require("../../import-path");
const plugin_util_1 = require("../../plugin-util");
const messages = {
shouldStartWith(importFileName, start) {
return `"${importFileName}" import should start with "${start}"`;
},
shouldNotStartWith(importFileName, start) {
return `"${importFileName}" import should not start with "${start}"`;
},
};
const defaultOptions = {
mode: stylelint_rule_creator_1.DefaultOptionMode.REQUIRE,
startWith: '_',
};
exports.fileNameStartsWithRule = (0, stylelint_rule_creator_1.createDefaultRule)({
ruleName: `${plugin_util_1.prefix}/file-name-starts-with`,
messages,
defaultOptions,
ruleCallback: (report, messages, { ruleOptions, root, exceptionRegExps }) => {
root.walkAtRules('import', (atRule) => {
if ((0, stylelint_rule_creator_1.doesMatchLineExceptions)(atRule, exceptionRegExps)) {
return;
}
const startWith = ruleOptions.startWith || defaultOptions.startWith;
const fileName = (0, path_1.basename)((0, import_path_1.extractImportPath)(atRule));
if (ruleOptions.mode === stylelint_rule_creator_1.DefaultOptionMode.REQUIRE && !fileName.startsWith(startWith)) {
report({
message: messages.shouldStartWith(fileName, startWith),
node: atRule,
word: atRule.toString(),
});
}
else if (ruleOptions.mode === stylelint_rule_creator_1.DefaultOptionMode.BLOCK &&
fileName.startsWith(startWith)) {
report({
message: messages.shouldNotStartWith(fileName, startWith),
node: atRule,
word: atRule.toString(),
});
}
});
},
});