stylelint-no-mismatching-module-file
Version:
Warns or errors when using a top-level module name that does not match the file name.
48 lines (40 loc) • 1.54 kB
JavaScript
var stylelint = require("stylelint");
var _ = require("lodash");
var ruleName = "tmi/no-mismatching-module-file"
module.exports = stylelint.createPlugin(ruleName, function (enabled) {
return function (root, result) {
var validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual: enabled,
possible: [
true,
false
]
})
if (!validOptions) { return }
/**
* SUPER lazy. This checks to see if we're in the "modules" directory because these
* rules shouldn't apply to any other directory. I might change this someday.
* ...maybe.
*/
var path = result.opts.from.split('/');
if (path[path.length - 2] !== 'modules') { return }
var fileName = result.opts.from.match(/_(.*)/g)[0].replace(/_/g, '.').replace(/\.[^/.]+$/g, '');
var fileExt = result.opts.from.split('.').pop();
var selectorFileName = fileName.slice(1);
root.walkRules(function (statement) {
if (
statement.parent.type === 'root' &&
_.values(statement.parent.indexes)[0] === 0 &&
fileName !== statement.selector
) {
stylelint.utils.report({
ruleName: ruleName,
result: result,
node: statement,
message: `Your selector does not match this module's file name. Change the "${statement.selector}" selector to "${fileName}" or rename _${selectorFileName}.${fileExt} to _${statement.selector.replace(/\./g, '')}.${fileExt}.`
});
}
})
}
})
module.exports.ruleName = ruleName