UNPKG

1.66 kBJavaScriptView Raw
1'use strict';
2
3const declarationValueIndex = require('../utils/declarationValueIndex');
4const isStandardSyntaxDeclaration = require('../utils/isStandardSyntaxDeclaration');
5const report = require('../utils/report');
6
7/** @typedef {(args: { source: string, index: number, lineCheckStr: string, err: (message: string) => void }) => void} LocationChecker */
8
9/**
10 * @param {{
11 * root: import('postcss').Root,
12 * locationChecker: LocationChecker,
13 * fix: ((decl: import('postcss').Declaration, index: number) => boolean) | null,
14 * result: import('stylelint').PostcssResult,
15 * checkedRuleName: string,
16 * }} opts
17 */
18module.exports = function declarationColonSpaceChecker(opts) {
19 opts.root.walkDecls((decl) => {
20 if (!isStandardSyntaxDeclaration(decl)) {
21 return;
22 }
23
24 // Get the raw prop, and only the prop
25 const endOfPropIndex = declarationValueIndex(decl) + (decl.raws.between || '').length - 1;
26
27 // The extra characters tacked onto the end ensure that there is a character to check
28 // after the colon. Otherwise, with `background:pink` the character after the
29 const propPlusColon = `${decl.toString().slice(0, endOfPropIndex)}xxx`;
30
31 for (let i = 0, l = propPlusColon.length; i < l; i++) {
32 if (propPlusColon[i] !== ':') {
33 continue;
34 }
35
36 opts.locationChecker({
37 source: propPlusColon,
38 index: i,
39 lineCheckStr: decl.value,
40 err: (message) => {
41 if (opts.fix && opts.fix(decl, i)) {
42 return;
43 }
44
45 report({
46 message,
47 node: decl,
48 index: decl.prop.toString().length + 1,
49 result: opts.result,
50 ruleName: opts.checkedRuleName,
51 });
52 },
53 });
54 break;
55 }
56 });
57};