Skip to content

Commit bd86719

Browse files
committed
Move to Prettier
1 parent 8cd683d commit bd86719

28 files changed

+503
-359
lines changed

.eslintrc

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@
44
"es6": true,
55
"jest": true
66
},
7-
"extends": "airbnb",
8-
"rules": {
9-
"block-spacing": ["error", "never"],
10-
"comma-dangle": ["error", "always-multiline"],
11-
"max-len": "off",
12-
"no-continue": "off",
13-
"no-param-reassign": "off",
14-
"no-plusplus": "off",
15-
"no-restricted-syntax": "off",
16-
"no-shadow": "off",
17-
"no-use-before-define": ["error", "nofunc"],
18-
"object-curly-spacing": ["error", "never"],
19-
"quotes": ["error", "single"],
20-
"strict": "off",
21-
"function-paren-newline": "off",
22-
"prefer-destructuring": "off"
7+
"extends": ["airbnb", "plugin:prettier/recommended"],
8+
"plugins": ["prettier"],
9+
"rules": {
10+
"no-continue": "off",
11+
"no-param-reassign": "off",
12+
"no-plusplus": "off",
13+
"no-restricted-syntax": "off",
14+
"no-shadow": "off",
15+
"no-use-before-define": ["error", "nofunc"],
16+
"no-useless-concat": "off",
17+
"prefer-destructuring": "off",
18+
"strict": "off"
2319
}
2420
}

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
singleQuote: true,
3+
trailingComma: 'es5',
4+
bracketSpacing: false
5+
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### HEAD
22

3+
* Move to Prettier
34
* Switch to Yarn
45

56
### 3.1.0 (October 11, 2017)

index.js

Lines changed: 126 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ const DEFINE_DIRECTIVE = new RegExp(
1414
`(?:\\*?\\s*@define ${DEFINE_VALUE})|(?:\\s*postcss-bem-linter: define ${DEFINE_VALUE})\\s*`
1515
);
1616
const END_DIRECTIVE = new RegExp(
17-
'(?:\\*\\s*@end\\s*)|' +
18-
'(?:\\s*postcss-bem-linter: end)\\s*'
17+
'(?:\\*\\s*@end\\s*)|' + '(?:\\s*postcss-bem-linter: end)\\s*'
1918
);
2019
const UTILITIES_IDENT = 'utilities';
2120
const WEAK_IDENT = 'weak';
@@ -34,128 +33,150 @@ function stripUnderscore(str) {
3433
* @param {Object|String} primaryOptions
3534
* @param {Object} [secondaryOptions]
3635
*/
37-
module.exports = postcss.plugin('postcss-bem-linter', (primaryOptions, secondaryOptions) => {
38-
const config = generateConfig(primaryOptions, secondaryOptions);
39-
const patterns = config.patterns;
40-
41-
return (root, result) => {
42-
const ranges = findRanges(root);
43-
44-
root.walkRules((rule) => {
45-
if (rule.parent && rule.parent.name === 'keyframes') return;
46-
if (!rule.source) return;
47-
48-
const ruleStartLine = rule.source.start.line;
49-
ranges.forEach((range) => {
50-
if (ruleStartLine < range.start) return;
51-
if (range.end && ruleStartLine > range.end) return;
52-
checkRule(rule, range);
36+
module.exports = postcss.plugin(
37+
'postcss-bem-linter',
38+
(primaryOptions, secondaryOptions) => {
39+
const config = generateConfig(primaryOptions, secondaryOptions);
40+
const patterns = config.patterns;
41+
42+
return (root, result) => {
43+
const ranges = findRanges(root);
44+
45+
root.walkRules(rule => {
46+
if (rule.parent && rule.parent.name === 'keyframes') return;
47+
if (!rule.source) return;
48+
49+
const ruleStartLine = rule.source.start.line;
50+
ranges.forEach(range => {
51+
if (ruleStartLine < range.start) return;
52+
if (range.end && ruleStartLine > range.end) return;
53+
checkRule(rule, range);
54+
});
5355
});
54-
});
5556

56-
function checkRule(rule, range) {
57-
if (range.defined === UTILITIES_IDENT) {
58-
if (!patterns.utilitySelectors) {
57+
function checkRule(rule, range) {
58+
if (range.defined === UTILITIES_IDENT) {
59+
if (!patterns.utilitySelectors) {
60+
throw new Error(
61+
'You tried to `@define utilities` but have not provided ' +
62+
'a `utilitySelectors` pattern'
63+
);
64+
}
65+
validateUtilities({
66+
rule,
67+
utilityPattern: toRegexp(patterns.utilitySelectors),
68+
ignorePattern: toRegexp(patterns.ignoreSelectors),
69+
result,
70+
});
71+
return;
72+
}
73+
74+
if (!patterns.componentSelectors) {
5975
throw new Error(
60-
'You tried to `@define utilities` but have not provided ' +
61-
'a `utilitySelectors` pattern'
76+
'You tried to `@define` a component but have not provided ' +
77+
'a `componentSelectors` pattern'
6278
);
6379
}
64-
validateUtilities({
80+
validateCustomProperties({
6581
rule,
66-
utilityPattern: toRegexp(patterns.utilitySelectors),
82+
componentName: range.defined,
83+
result,
84+
ignorePattern: toRegexp(patterns.ignoreCustomProperties),
85+
});
86+
validateSelectors({
87+
rule,
88+
componentName: range.defined,
89+
weakMode: range.weakMode,
90+
selectorPattern: patterns.componentSelectors,
91+
selectorPatternOptions: config.presetOptions,
6792
ignorePattern: toRegexp(patterns.ignoreSelectors),
6893
result,
6994
});
70-
return;
7195
}
7296

73-
if (!patterns.componentSelectors) {
74-
throw new Error(
75-
'You tried to `@define` a component but have not provided ' +
76-
'a `componentSelectors` pattern'
77-
);
78-
}
79-
validateCustomProperties({
80-
rule,
81-
componentName: range.defined,
82-
result,
83-
ignorePattern: toRegexp(patterns.ignoreCustomProperties),
84-
});
85-
validateSelectors({
86-
rule,
87-
componentName: range.defined,
88-
weakMode: range.weakMode,
89-
selectorPattern: patterns.componentSelectors,
90-
selectorPatternOptions: config.presetOptions,
91-
ignorePattern: toRegexp(patterns.ignoreSelectors),
92-
result,
93-
});
94-
}
97+
function findRanges(root) {
98+
const ranges = [];
99+
100+
if (root.source && root.source.input && root.source.input.file) {
101+
const filename = root.source.input.file;
102+
if (
103+
checkImplicit.isImplicitUtilities(
104+
config.implicitUtilities,
105+
filename
106+
)
107+
) {
108+
ranges.push({
109+
defined: 'utilities',
110+
start: 0,
111+
weakMode: false,
112+
});
113+
} else if (
114+
checkImplicit.isImplicitComponent(
115+
config.implicitComponents,
116+
filename
117+
)
118+
) {
119+
let defined = stripUnderscore(
120+
path.basename(filename).split('.')[0]
121+
);
122+
if (defined === 'index') {
123+
defined = path.basename(path.join(filename, '..'));
124+
}
125+
126+
if (
127+
defined !== UTILITIES_IDENT &&
128+
!toRegexp(config.componentNamePattern).test(defined)
129+
) {
130+
result.warn(
131+
`Invalid component name from implicit conversion from filename ${filename}`
132+
);
133+
}
134+
ranges.push({
135+
defined,
136+
start: 0,
137+
weakMode: false,
138+
});
139+
}
140+
}
95141

96-
function findRanges(root) {
97-
const ranges = [];
142+
root.walkComments(comment => {
143+
const commentStartLine = comment.source
144+
? comment.source.start.line
145+
: null;
146+
if (!commentStartLine) return;
98147

99-
if (root.source && root.source.input && root.source.input.file) {
100-
const filename = root.source.input.file;
101-
if (checkImplicit.isImplicitUtilities(config.implicitUtilities, filename)) {
102-
ranges.push({
103-
defined: 'utilities',
104-
start: 0,
105-
weakMode: false,
106-
});
107-
} else if (checkImplicit.isImplicitComponent(config.implicitComponents, filename)) {
108-
let defined = stripUnderscore(path.basename(filename).split('.')[0]);
109-
if (defined === 'index') {
110-
defined = path.basename(path.join(filename, '..'));
148+
if (END_DIRECTIVE.test(comment.text)) {
149+
endCurrentRange(commentStartLine);
150+
return;
111151
}
112152

113-
if (defined !== UTILITIES_IDENT && !toRegexp(config.componentNamePattern).test(defined)) {
114-
result.warn(
115-
`Invalid component name from implicit conversion from filename ${filename}`
116-
);
153+
const directiveMatch = comment.text.match(DEFINE_DIRECTIVE);
154+
if (!directiveMatch) return;
155+
const defined = (directiveMatch[1] || directiveMatch[3]).trim();
156+
if (
157+
defined !== UTILITIES_IDENT &&
158+
!toRegexp(config.componentNamePattern).test(defined)
159+
) {
160+
result.warn(`Invalid component name in definition /*${comment}*/`, {
161+
node: comment,
162+
});
117163
}
164+
endCurrentRange(commentStartLine);
118165
ranges.push({
119166
defined,
120-
start: 0,
121-
weakMode: false,
167+
start: commentStartLine,
168+
weakMode: directiveMatch[2] === WEAK_IDENT,
122169
});
123-
}
124-
}
125-
126-
root.walkComments((comment) => {
127-
const commentStartLine = (comment.source) ? comment.source.start.line : null;
128-
if (!commentStartLine) return;
129-
130-
if (END_DIRECTIVE.test(comment.text)) {
131-
endCurrentRange(commentStartLine);
132-
return;
133-
}
134-
135-
const directiveMatch = comment.text.match(DEFINE_DIRECTIVE);
136-
if (!directiveMatch) return;
137-
const defined = (directiveMatch[1] || directiveMatch[3]).trim();
138-
if (defined !== UTILITIES_IDENT && !toRegexp(config.componentNamePattern).test(defined)) {
139-
result.warn(
140-
`Invalid component name in definition /*${comment}*/`,
141-
{node: comment}
142-
);
143-
}
144-
endCurrentRange(commentStartLine);
145-
ranges.push({
146-
defined,
147-
start: commentStartLine,
148-
weakMode: directiveMatch[2] === WEAK_IDENT,
149170
});
150-
});
151-
return ranges;
171+
return ranges;
152172

153-
function endCurrentRange(line) {
154-
if (!ranges.length) return;
155-
const lastRange = ranges[ranges.length - 1];
156-
if (lastRange.end) return;
157-
lastRange.end = line;
173+
function endCurrentRange(line) {
174+
if (!ranges.length) return;
175+
const lastRange = ranges[ranges.length - 1];
176+
if (lastRange.end) return;
177+
lastRange.end = line;
178+
}
158179
}
159-
}
160-
};
161-
});
180+
};
181+
}
182+
);

lib/generate-config.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ module.exports = (primaryOptions, secondaryOptions) => {
4848
presetOptions = primaryOptions.presetOptions;
4949
}
5050

51-
const implicitComponents =
52-
getImplicitDefineValue('implicitComponents', primaryOptions, secondaryOptions);
53-
const implicitUtilities =
54-
getImplicitDefineValue('implicitUtilities', primaryOptions, secondaryOptions);
51+
const implicitComponents = getImplicitDefineValue(
52+
'implicitComponents',
53+
primaryOptions,
54+
secondaryOptions
55+
);
56+
const implicitUtilities = getImplicitDefineValue(
57+
'implicitUtilities',
58+
primaryOptions,
59+
secondaryOptions
60+
);
5561

5662
return {
5763
patterns,

lib/get-selectors.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ function hasNoDeclarations(node) {
1515
}
1616

1717
function hasOnlyExtends(node) {
18-
return hasChildNodes(node) && node.every(child => child.type === 'atrule' && child.name === 'extend');
18+
return (
19+
hasChildNodes(node) &&
20+
node.every(child => child.type === 'atrule' && child.name === 'extend')
21+
);
1922
}
2023

2124
function getComponentRootRule(node) {
@@ -27,10 +30,12 @@ function getComponentRootRule(node) {
2730

2831
function unWrapSelectors(parent, rule) {
2932
let selectors = [];
30-
parent.walkRules((node) => {
33+
parent.walkRules(node => {
3134
// Only unwrap as far as the current rule being linted
32-
if (node.selector !== rule.selector) {return;}
33-
node.selectors.forEach((selector) => {
35+
if (node.selector !== rule.selector) {
36+
return;
37+
}
38+
node.selectors.forEach(selector => {
3439
selectors = selectors.concat(resolveNestedSelector(selector, node));
3540
});
3641
});

lib/list-sequences.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
* @param {String} selector
1414
* @returns {String[]}
1515
*/
16-
module.exports = (selector) => {
16+
module.exports = selector => {
1717
const withoutPseudos = selector.split(':')[0];
18-
return withoutPseudos
19-
.split(/[\s>+~]/)
20-
.filter(s => s !== '');
18+
return withoutPseudos.split(/[\s>+~]/).filter(s => s !== '');
2119
};

0 commit comments

Comments
 (0)