Skip to content

Commit

Permalink
Assorted linting tweaks found with xo (#5048)
Browse files Browse the repository at this point in the history
* Assorted linting tweaks found with xo

* More
  • Loading branch information
XhmikosR committed Nov 22, 2020
1 parent c2ebdc0 commit 0a2e727
Show file tree
Hide file tree
Showing 35 changed files with 74 additions and 102 deletions.
2 changes: 1 addition & 1 deletion lib/__tests__/syntaxes.test.js
@@ -1,6 +1,6 @@
'use strict';

const syntaxes = require('../syntaxes/index');
const syntaxes = require('../syntaxes');

const inputs = Object.keys(syntaxes).map((name) => [
name,
Expand Down
1 change: 1 addition & 0 deletions lib/assignDisabledRanges.js
Expand Up @@ -116,6 +116,7 @@ module.exports = function (root, result) {
current = next;
lastLine = currentLine;
}

checkComment(fullComment);
});

Expand Down
4 changes: 2 additions & 2 deletions lib/getPostcssResult.js
Expand Up @@ -120,7 +120,7 @@ function getCustomSyntax(customSyntax) {
if (typeof customSyntax === 'string') {
try {
resolved = require(customSyntax);
} catch (error) {
} catch {
throw new Error(
`Cannot resolve custom syntax module ${customSyntax}. Check that module ${customSyntax} is available and spelled correctly.`,
);
Expand All @@ -144,7 +144,7 @@ function getCustomSyntax(customSyntax) {
if (typeof customSyntax.parse === 'function') {
resolved = { ...customSyntax };
} else {
throw new Error(
throw new TypeError(
`An object provided to the "customSyntax" option must have a "parse" property. Ensure the "parse" property exists and its value is a function.`,
);
}
Expand Down
15 changes: 5 additions & 10 deletions lib/lintSource.js
Expand Up @@ -42,17 +42,12 @@ module.exports = function lintSource(stylelint, options = {}) {
return getIsIgnored.then((isIgnored) => {
if (isIgnored) {
/** @type {PostcssResult} */
let postcssResult;

if (options.existingPostcssResult) {
postcssResult = Object.assign(options.existingPostcssResult, {
stylelint: createEmptyStylelintPostcssResult(),
});
} else {
postcssResult = createEmptyPostcssResult(inputFilePath);
}

return postcssResult;
return options.existingPostcssResult
? Object.assign(options.existingPostcssResult, {
stylelint: createEmptyStylelintPostcssResult(),
})
: createEmptyPostcssResult(inputFilePath);
}

const configSearchPath = stylelint._options.configFile || inputFilePath;
Expand Down
4 changes: 2 additions & 2 deletions lib/reference/keywordSets.js
Expand Up @@ -670,8 +670,8 @@ keywordSets.nonStandardHtmlTags = new Set([
*/
function uniteSets(...args) {
return new Set(
Array.from(args).reduce((/** @type {string[]} */ result, set) => {
return result.concat(Array.from(set));
[...args].reduce((/** @type {string[]} */ result, set) => {
return result.concat([...set]);
}, []),
);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/alpha-value-notation/index.js
Expand Up @@ -18,8 +18,8 @@ const messages = ruleMessages(ruleName, {
expected: (unfixed, fixed) => `Expected "${unfixed}" to be "${fixed}"`,
});

const ALPHA_PROPS = ['opacity', 'shape-image-threshold'];
const ALPHA_FUNCS = ['hsl', 'hsla', 'hwb', 'lab', 'lch', 'rgb', 'rgba'];
const ALPHA_PROPS = new Set(['opacity', 'shape-image-threshold']);
const ALPHA_FUNCS = new Set(['hsl', 'hsla', 'hwb', 'lab', 'lch', 'rgb', 'rgba']);

function rule(primary, options, context) {
return (root, result) => {
Expand Down Expand Up @@ -48,12 +48,12 @@ function rule(primary, options, context) {
parsedValue.walk((node) => {
let alpha;

if (ALPHA_PROPS.includes(decl.prop.toLowerCase())) {
if (ALPHA_PROPS.has(decl.prop.toLowerCase())) {
alpha = findAlphaInValue(node);
} else {
if (node.type !== 'function') return;

if (!ALPHA_FUNCS.includes(node.value.toLowerCase())) return;
if (!ALPHA_FUNCS.has(node.value.toLowerCase())) return;

alpha = findAlphaInFunction(node);
}
Expand Down
1 change: 1 addition & 0 deletions lib/rules/block-opening-brace-newline-after/index.js
Expand Up @@ -115,6 +115,7 @@ function rule(expectation, options, context) {

fixTarget = fixTarget.next();
}

nodeToCheck.raws.before = '';

return;
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/color-function-notation/index.js
Expand Up @@ -15,8 +15,8 @@ const messages = ruleMessages(ruleName, {
expected: (primary) => `Expected ${primary} color-function notation`,
});

const LEGACY_FUNCS = ['rgba', 'hsla'];
const LEGACY_NOTATION_FUNCS = ['rgb', 'rgba', 'hsl', 'hsla'];
const LEGACY_FUNCS = new Set(['rgba', 'hsla']);
const LEGACY_NOTATION_FUNCS = new Set(['rgb', 'rgba', 'hsl', 'hsla']);

function rule(primary, secondary, context) {
return (root, result) => {
Expand All @@ -36,7 +36,7 @@ function rule(primary, secondary, context) {

if (type !== 'function') return;

if (!LEGACY_NOTATION_FUNCS.includes(value.toLowerCase())) return;
if (!LEGACY_NOTATION_FUNCS.has(value.toLowerCase())) return;

if (primary === 'modern' && !hasCommas(node)) return;

Expand Down Expand Up @@ -64,7 +64,7 @@ function rule(primary, secondary, context) {
});

// Remove trailing 'a' from legacy function name
if (LEGACY_FUNCS.includes(node.value.toLowerCase())) {
if (LEGACY_FUNCS.has(node.value.toLowerCase())) {
node.value = node.value.slice(0, -1);
}

Expand Down
10 changes: 3 additions & 7 deletions lib/rules/color-named/generateColorFuncs.js
Expand Up @@ -13,7 +13,7 @@ function lin_sRGB(RGB) {
return val / 12.92;
}

return Math.pow((val + 0.055) / 1.055, 2.4);
return ((val + 0.055) / 1.055) ** 2.4;
});
}

Expand Down Expand Up @@ -102,11 +102,7 @@ function rgb2hsl(r, g, b) {

l = (M + m) / 2;

if (d === 0) {
s = 0;
} else {
s = d / (1 - Math.abs(2 * l - 1));
}
s = d === 0 ? 0 : d / (1 - Math.abs(2 * l - 1));

s *= 100;
l *= 100;
Expand Down Expand Up @@ -152,7 +148,7 @@ function generateColorFuncs(hexString) {
const rgb = [0, 0, 0];

for (let i = 0; i < 3; i += 1) {
rgb[i] = parseInt(hexString.substr(2 * i + 1, 2), 16);
rgb[i] = Number.parseInt(hexString.substr(2 * i + 1, 2), 16);
}

const hsl = rgb2hsl(rgb[0], rgb[1], rgb[2]);
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/color-named/index.js
Expand Up @@ -25,7 +25,7 @@ const messages = ruleMessages(ruleName, {
});

// Todo tested on case insensivity
const NODE_TYPES = ['word', 'function'];
const NODE_TYPES = new Set(['word', 'function']);

function rule(expectation, options) {
return (root, result) => {
Expand Down Expand Up @@ -90,7 +90,7 @@ function rule(expectation, options) {
}

// Return early if neither a word nor a function
if (!NODE_TYPES.includes(type)) {
if (!NODE_TYPES.has(type)) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/font-weight-notation/index.js
Expand Up @@ -23,7 +23,7 @@ const messages = ruleMessages(ruleName, {
const INHERIT_KEYWORD = 'inherit';
const INITIAL_KEYWORD = 'initial';
const NORMAL_KEYWORD = 'normal';
const WEIGHTS_WITH_KEYWORD_EQUIVALENTS = ['400', '700'];
const WEIGHTS_WITH_KEYWORD_EQUIVALENTS = new Set(['400', '700']);

function rule(expectation, options) {
return (root, result) => {
Expand Down Expand Up @@ -121,7 +121,7 @@ function rule(expectation, options) {

if (expectation === 'named-where-possible') {
if (isNumbery(weightValue)) {
if (WEIGHTS_WITH_KEYWORD_EQUIVALENTS.includes(weightValue)) {
if (WEIGHTS_WITH_KEYWORD_EQUIVALENTS.has(weightValue)) {
complain(messages.expected('named'));
}

Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-whitespace-after/index.js
Expand Up @@ -116,6 +116,7 @@ function rule(expectation, options, context) {
while (whitespaceEndIndex < value.length && isWhitespace(value[whitespaceEndIndex])) {
whitespaceEndIndex++;
}

fixed += value.slice(lastIndex, index);
lastIndex = whitespaceEndIndex;
};
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/hue-degree-notation/index.js
Expand Up @@ -18,7 +18,7 @@ const messages = ruleMessages(ruleName, {

const HUE_FIRST_ARG_FUNCS = ['hsl', 'hsla', 'hwb'];
const HUE_THIRD_ARG_FUNCS = ['lch'];
const HUE_FUNCS = [...HUE_FIRST_ARG_FUNCS, ...HUE_THIRD_ARG_FUNCS];
const HUE_FUNCS = new Set([...HUE_FIRST_ARG_FUNCS, ...HUE_THIRD_ARG_FUNCS]);

function rule(primary, secondary, context) {
return (root, result) => {
Expand All @@ -36,7 +36,7 @@ function rule(primary, secondary, context) {
parsedValue.walk((node) => {
if (node.type !== 'function') return;

if (!HUE_FUNCS.includes(node.value.toLowerCase())) return;
if (!HUE_FUNCS.has(node.value.toLowerCase())) return;

const hue = findHue(node);

Expand Down
12 changes: 2 additions & 10 deletions lib/rules/indentation/index.js
Expand Up @@ -573,22 +573,14 @@ function inferRootIndentLevel(root, baseIndentLevel, indentSize) {
if (document) {
const nextRoot = document.nodes[document.nodes.indexOf(root) + 1];

if (nextRoot) {
afterEnd = nextRoot.raws.beforeStart;
} else {
afterEnd = document.raws.afterEnd;
}
afterEnd = nextRoot ? nextRoot.raws.beforeStart : document.raws.afterEnd;
} else {
// Nested root node in css-in-js lang
const parent = root.parent;

const nextRoot = parent.nodes[parent.nodes.indexOf(root) + 1];

if (nextRoot) {
afterEnd = nextRoot.raws.beforeStart;
} else {
afterEnd = root.raws.afterEnd;
}
afterEnd = nextRoot ? nextRoot.raws.beforeStart : root.raws.afterEnd;
}
} else {
afterEnd = after;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/length-zero-no-unit/index.js
Expand Up @@ -147,7 +147,7 @@ function rule(actual, secondary, context) {
// Only pay attention if the value parses to 0
// and units with lengths
if (
parseFloat(valueWithZero) !== 0 ||
Number.parseFloat(valueWithZero) !== 0 ||
!keywordSets.lengthUnits.has(parsedValue.unit.toLowerCase())
) {
return;
Expand Down
38 changes: 15 additions & 23 deletions lib/rules/max-empty-lines/index.js
Expand Up @@ -163,29 +163,21 @@ function rule(max, options, context) {
const emptyLFLines = '\n'.repeat(repeatTimes);
const emptyCRLFLines = '\r\n'.repeat(repeatTimes);

// TODO: Issue #4985
// eslint-disable-next-line no-shadow
let result;

if (/(\r\n)+/g.test(str)) {
result = str.replace(/(\r\n)+/g, ($1) => {
if ($1.length / 2 > repeatTimes) {
return emptyCRLFLines;
}

return $1;
});
} else {
result = str.replace(/(\n)+/g, ($1) => {
if ($1.length > repeatTimes) {
return emptyLFLines;
}

return $1;
});
}

return result;
return /(\r\n)+/g.test(str)
? str.replace(/(\r\n)+/g, ($1) => {
if ($1.length / 2 > repeatTimes) {
return emptyCRLFLines;
}

return $1;
})
: str.replace(/(\n)+/g, ($1) => {
if ($1.length > repeatTimes) {
return emptyLFLines;
}

return $1;
});
}
};
}
Expand Down
8 changes: 3 additions & 5 deletions lib/rules/media-query-list-comma-newline-after/index.js
Expand Up @@ -65,11 +65,9 @@ function rule(expectation, options, context) {
const afterComma = params.slice(index + 1);

if (expectation.startsWith('always')) {
if (/^\s*\r?\n/.test(afterComma)) {
params = beforeComma + afterComma.replace(/^[^\S\r\n]*/, '');
} else {
params = beforeComma + context.newline + afterComma;
}
params = /^\s*\r?\n/.test(afterComma)
? beforeComma + afterComma.replace(/^[^\S\r\n]*/, '')
: beforeComma + context.newline + afterComma;
} else if (expectation.startsWith('never')) {
params = beforeComma + afterComma.replace(/^\s*/, '');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-duplicate-selectors/index.js
Expand Up @@ -74,7 +74,7 @@ function rule(actual, options) {
let previousDuplicatePosition;
// When `disallowInList` is true, we must parse `sortedSelectorList` into
// list items.
let selectorListParsed = [];
const selectorListParsed = [];

if (shouldDisallowDuplicateInList) {
parseSelector(sortedSelectorList, result, rule, (selectors) => {
Expand Down Expand Up @@ -131,7 +131,7 @@ function rule(actual, options) {
});

if (shouldDisallowDuplicateInList) {
for (let selector of selectorListParsed) {
for (const selector of selectorListParsed) {
// [selectorLine] will not really be accurate for multi-line
// selectors, such as "bar" in "foo,\nbar {}".
contextSelectorSet.set(selector, selectorLine);
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/number-max-precision/index.js
Expand Up @@ -89,7 +89,7 @@ function rule(precision, options) {
ruleName,
node,
index: getIndex(node) + valueNode.sourceIndex + match.index,
message: messages.expected(parseFloat(match[0]), precision),
message: messages.expected(Number.parseFloat(match[0]), precision),
});
});
}
Expand Down
6 changes: 1 addition & 5 deletions lib/rules/selector-attribute-brackets-space-inside/index.js
Expand Up @@ -151,11 +151,7 @@ function rule(expectation, options, context) {
let key;

if (attributeNode.operator) {
if (attributeNode.insensitive) {
key = 'insensitive';
} else {
key = 'value';
}
key = attributeNode.insensitive ? 'insensitive' : 'value';
} else {
key = 'attribute';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/selector-max-specificity/index.js
Expand Up @@ -176,7 +176,7 @@ function rule(max, options) {
});
}
});
} catch (e) {
} catch {
result.warn('Cannot parse selector', {
node: rule,
stylelintType: 'parseError',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/selector-pseudo-element-colon-notation/index.js
Expand Up @@ -43,7 +43,7 @@ function rule(expectation, options, context) {
const fixPositions = [];

// match only level 1 and 2 pseudo elements
const pseudoElementsWithColons = Array.from(keywordSets.levelOneAndTwoPseudoElements).map(
const pseudoElementsWithColons = [...keywordSets.levelOneAndTwoPseudoElements].map(
(x) => `:${x}`,
);

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/time-min-milliseconds/index.js
Expand Up @@ -19,7 +19,7 @@ const messages = ruleMessages(ruleName, {
expected: (time) => `Expected a minimum of ${time} milliseconds`,
});

const DELAY_PROPERTIES = ['animation-delay', 'transition-delay'];
const DELAY_PROPERTIES = new Set(['animation-delay', 'transition-delay']);

function rule(minimum, options) {
return (root, result) => {
Expand Down Expand Up @@ -98,7 +98,7 @@ function rule(minimum, options) {
}

function isIgnoredProperty(propertyName) {
if (optionsMatches(options, 'ignore', 'delay') && DELAY_PROPERTIES.includes(propertyName)) {
if (optionsMatches(options, 'ignore', 'delay') && DELAY_PROPERTIES.has(propertyName)) {
return true;
}

Expand Down

0 comments on commit 0a2e727

Please sign in to comment.