diff --git a/lib/__tests__/fixtures/plugin-slashless-warn-about-foo.js b/lib/__tests__/fixtures/plugin-slashless-warn-about-foo.js index 7ad0d62104..ca2b8f6d64 100644 --- a/lib/__tests__/fixtures/plugin-slashless-warn-about-foo.js +++ b/lib/__tests__/fixtures/plugin-slashless-warn-about-foo.js @@ -11,15 +11,13 @@ const warnAboutFooMessages = stylelint.utils.ruleMessages('slashless-warn-about- module.exports = stylelint.createPlugin(ruleName, (expectation) => { return (root, result) => { root.walkRules((rule) => { - if (rule.selector === '.foo') { - if (expectation === 'always') { - stylelint.utils.report({ - result, - ruleName, - message: warnAboutFooMessages.found, - node: rule, - }); - } + if (rule.selector === '.foo' && expectation === 'always') { + stylelint.utils.report({ + result, + ruleName, + message: warnAboutFooMessages.found, + node: rule, + }); } }); }; diff --git a/lib/__tests__/fixtures/plugin-warn-about-bar.js b/lib/__tests__/fixtures/plugin-warn-about-bar.js index 1c601cfa39..b775d1231d 100644 --- a/lib/__tests__/fixtures/plugin-warn-about-bar.js +++ b/lib/__tests__/fixtures/plugin-warn-about-bar.js @@ -11,15 +11,13 @@ const warnAboutBarMessages = stylelint.utils.ruleMessages('plugin/warn-about-bar module.exports = stylelint.createPlugin(ruleName, (expectation) => { return (root, result) => { root.walkRules((rule) => { - if (rule.selector === '.bar') { - if (expectation === 'always') { - stylelint.utils.report({ - result, - ruleName, - message: warnAboutBarMessages.found, - node: rule, - }); - } + if (rule.selector === '.bar' && expectation === 'always') { + stylelint.utils.report({ + result, + ruleName, + message: warnAboutBarMessages.found, + node: rule, + }); } }); }; diff --git a/lib/__tests__/fixtures/plugin-warn-about-foo.js b/lib/__tests__/fixtures/plugin-warn-about-foo.js index a6a09ac669..e5698a8342 100644 --- a/lib/__tests__/fixtures/plugin-warn-about-foo.js +++ b/lib/__tests__/fixtures/plugin-warn-about-foo.js @@ -11,15 +11,13 @@ const warnAboutFooMessages = stylelint.utils.ruleMessages('plugin/warn-about-foo module.exports = stylelint.createPlugin(ruleName, (expectation) => { return (root, result) => { root.walkRules((rule) => { - if (rule.selector === '.foo') { - if (expectation === 'always') { - stylelint.utils.report({ - result, - ruleName, - message: warnAboutFooMessages.found, - node: rule, - }); - } + if (rule.selector === '.foo' && expectation === 'always') { + stylelint.utils.report({ + result, + ruleName, + message: warnAboutFooMessages.found, + node: rule, + }); } }); }; diff --git a/lib/augmentConfig.js b/lib/augmentConfig.js index bf958eeee1..b39ae13947 100644 --- a/lib/augmentConfig.js +++ b/lib/augmentConfig.js @@ -122,7 +122,7 @@ function augmentConfigFull(stylelint, filePath, cosmiconfigResult) { */ function absolutizePaths(config, configDir) { if (config.ignoreFiles) { - config.ignoreFiles = /** @type {string[]} */ ([]).concat(config.ignoreFiles).map((glob) => { + config.ignoreFiles = /** @type {string[]} */ [config.ignoreFiles].flat().map((glob) => { if (path.isAbsolute(glob.replace(/^!/, ''))) return glob; return globjoin(configDir, glob); @@ -130,7 +130,7 @@ function absolutizePaths(config, configDir) { } if (config.plugins) { - config.plugins = /** @type {string[]} */ ([]).concat(config.plugins).map((lookup) => { + config.plugins = /** @type {string[]} */ [config.plugins].flat().map((lookup) => { return getModulePath(configDir, lookup); }); } @@ -353,8 +353,8 @@ function addProcessorFunctions(config) { /** @type {Array} */ const resultProcessors = []; - /** @type {Array} */ ([]) - .concat(config.processors) + /** @type {Array} */ [config.processors] + .flat() .forEach((processorConfig) => { const processorKey = JSON.stringify(processorConfig); @@ -403,7 +403,7 @@ function applyOverrides(fullConfig, configDir, filePath) { } if (!Array.isArray(overrides)) { - throw Error( + throw new TypeError( 'The `overrides` configuration property should be an array, e.g. { "overrides": [{ "files": "*.css", "rules": {} }] }.', ); } @@ -412,7 +412,7 @@ function applyOverrides(fullConfig, configDir, filePath) { const { files, ...configOverrides } = override; if (!files) { - throw Error( + throw new Error( 'Every object in the `overrides` configuration property should have a `files` property with globs, e.g. { "overrides": [{ "files": "*.css", "rules": {} }] }.', ); } diff --git a/lib/formatters/tapFormatter.js b/lib/formatters/tapFormatter.js index fd05191401..f7ade9f21f 100644 --- a/lib/formatters/tapFormatter.js +++ b/lib/formatters/tapFormatter.js @@ -4,7 +4,7 @@ * @type {import('stylelint').Formatter} */ const tapFormatter = (results) => { - let lines = [`TAP version 13\n1..${results.length}`]; + const lines = [`TAP version 13\n1..${results.length}`]; results.forEach((result, index) => { lines.push( diff --git a/lib/getPostcssResult.js b/lib/getPostcssResult.js index 0bc5dfcd86..e02d26c128 100644 --- a/lib/getPostcssResult.js +++ b/lib/getPostcssResult.js @@ -46,13 +46,9 @@ module.exports = function getPostcssResult(stylelint, options = {}) { } /** @type {Syntax | null} */ - let syntax = null; - - if (options.customSyntax) { - syntax = getCustomSyntax(options.customSyntax); - } else { - syntax = cssSyntax(stylelint, options.filePath); - } + const syntax = options.customSyntax + ? getCustomSyntax(options.customSyntax) + : cssSyntax(stylelint, options.filePath); const postcssOptions = { from: options.filePath, diff --git a/lib/reference/keywordSets.js b/lib/reference/keywordSets.js index dcb52f240a..3fd9df2255 100644 --- a/lib/reference/keywordSets.js +++ b/lib/reference/keywordSets.js @@ -683,7 +683,7 @@ keywordSets.nonStandardHtmlTags = new Set([ function uniteSets(...args) { return new Set( [...args].reduce((/** @type {string[]} */ result, set) => { - return result.concat([...set]); + return [...result, ...set]; }, []), ); } diff --git a/lib/reportUnknownRuleNames.js b/lib/reportUnknownRuleNames.js index 371f4b6373..b995755bda 100644 --- a/lib/reportUnknownRuleNames.js +++ b/lib/reportUnknownRuleNames.js @@ -11,7 +11,7 @@ const MAX_SUGGESTIONS_COUNT = 3; * @return {string[]} */ function extractSuggestions(ruleName) { - const suggestions = new Array(MAX_LEVENSHTEIN_DISTANCE); + const suggestions = Array.from({ length: MAX_LEVENSHTEIN_DISTANCE }); for (let i = 0; i < suggestions.length; i++) { suggestions[i] = []; diff --git a/lib/rules/block-closing-brace-newline-after/index.js b/lib/rules/block-closing-brace-newline-after/index.js index 81472eb84f..cab53d4af2 100644 --- a/lib/rules/block-closing-brace-newline-after/index.js +++ b/lib/rules/block-closing-brace-newline-after/index.js @@ -112,11 +112,10 @@ const rule = (primary, secondaryOptions, context) => { if (primary.startsWith('always')) { const index = nodeToCheckRaws.before.search(/\r?\n/); - if (index >= 0) { - nodeToCheckRaws.before = nodeToCheckRaws.before.slice(index); - } else { - nodeToCheckRaws.before = context.newline + nodeToCheckRaws.before; - } + nodeToCheckRaws.before = + index >= 0 + ? nodeToCheckRaws.before.slice(index) + : context.newline + nodeToCheckRaws.before; return; } diff --git a/lib/rules/block-closing-brace-newline-before/index.js b/lib/rules/block-closing-brace-newline-before/index.js index 8dd6c7eeb0..ba6597c548 100644 --- a/lib/rules/block-closing-brace-newline-before/index.js +++ b/lib/rules/block-closing-brace-newline-before/index.js @@ -93,11 +93,10 @@ const rule = (primary, _secondaryOptions, context) => { firstWhitespaceIndex >= 0 ? statementRaws.after.slice(firstWhitespaceIndex) : ''; const newlineIndex = newlineAfter.search(/\r?\n/); - if (newlineIndex >= 0) { - statementRaws.after = newlineBefore + newlineAfter.slice(newlineIndex); - } else { - statementRaws.after = newlineBefore + context.newline + newlineAfter; - } + statementRaws.after = + newlineIndex >= 0 + ? newlineBefore + newlineAfter.slice(newlineIndex) + : newlineBefore + context.newline + newlineAfter; return; } diff --git a/lib/rules/block-opening-brace-newline-after/index.js b/lib/rules/block-opening-brace-newline-after/index.js index 78570e465a..d5bbc5883f 100644 --- a/lib/rules/block-opening-brace-newline-after/index.js +++ b/lib/rules/block-opening-brace-newline-after/index.js @@ -93,11 +93,10 @@ const rule = (primary, _secondaryOptions, context) => { if (primary.startsWith('always')) { const index = nodeToCheckRaws.before.search(/\r?\n/); - if (index >= 0) { - nodeToCheckRaws.before = nodeToCheckRaws.before.slice(index); - } else { - nodeToCheckRaws.before = context.newline + nodeToCheckRaws.before; - } + nodeToCheckRaws.before = + index >= 0 + ? nodeToCheckRaws.before.slice(index) + : context.newline + nodeToCheckRaws.before; backupCommentNextBefores.delete(nodeToCheck); diff --git a/lib/rules/color-named/generateColorFuncs.js b/lib/rules/color-named/generateColorFuncs.js index 6b8b3f5073..0a83f0a4ee 100644 --- a/lib/rules/color-named/generateColorFuncs.js +++ b/lib/rules/color-named/generateColorFuncs.js @@ -197,30 +197,34 @@ function generateColorFuncs(hexString) { const xyz_d50 = chromaticAdaptationD65_D50(xyz_d65); const lab = xyz2lab(xyz_d50); - func.push(`rgb(${rgbStr})`); - func.push(`rgba(${rgbStr},1)`); - func.push(`rgba(${rgbStr},100%)`); - func.push(`rgb(${rgbPercStr})`); - func.push(`rgba(${rgbPercStr},1)`); - func.push(`rgba(${rgbPercStr},100%)`); - func.push(`hsl(${hslStr})`); - func.push(`hsla(${hslStr},1)`); - func.push(`hsla(${hslStr},100%)`); - func.push(`hwb(${hwbStr})`); - func.push(`hwb(${hwbStr},1)`); - func.push(`hwb(${hwbStr},100%)`); + func.push( + `rgb(${rgbStr})`, + `rgba(${rgbStr},1)`, + `rgba(${rgbStr},100%)`, + `rgb(${rgbPercStr})`, + `rgba(${rgbPercStr},1)`, + `rgba(${rgbPercStr},100%)`, + `hsl(${hslStr})`, + `hsla(${hslStr},1)`, + `hsla(${hslStr},100%)`, + `hwb(${hwbStr})`, + `hwb(${hwbStr},1)`, + `hwb(${hwbStr},100%)`, + ); // technically, this should be 0 - but then #808080 wouldn't even be gray if (lab[1] * lab[1] < 0.01 && lab[2] * lab[2] < 0.01) { // yay! gray! const grayStr = Math.round(lab[0]); - func.push(`gray(${grayStr})`); - func.push(`gray(${grayStr},1)`); - func.push(`gray(${grayStr},100%)`); - func.push(`gray(${grayStr}%)`); - func.push(`gray(${grayStr}%,1)`); - func.push(`gray(${grayStr}%,100%)`); + func.push( + `gray(${grayStr})`, + `gray(${grayStr},1)`, + `gray(${grayStr},100%)`, + `gray(${grayStr}%)`, + `gray(${grayStr}%,1)`, + `gray(${grayStr}%,100%)`, + ); } return func; diff --git a/lib/rules/declaration-block-semicolon-newline-after/index.js b/lib/rules/declaration-block-semicolon-newline-after/index.js index a27238a3b9..b09081196d 100644 --- a/lib/rules/declaration-block-semicolon-newline-after/index.js +++ b/lib/rules/declaration-block-semicolon-newline-after/index.js @@ -67,11 +67,10 @@ const rule = (primary, _secondaryOptions, context) => { if (primary.startsWith('always')) { const index = nodeToCheck.raws.before.search(/\r?\n/); - if (index >= 0) { - nodeToCheck.raws.before = nodeToCheck.raws.before.slice(index); - } else { - nodeToCheck.raws.before = context.newline + nodeToCheck.raws.before; - } + nodeToCheck.raws.before = + index >= 0 + ? nodeToCheck.raws.before.slice(index) + : context.newline + nodeToCheck.raws.before; return; } diff --git a/lib/rules/declaration-colon-newline-after/index.js b/lib/rules/declaration-colon-newline-after/index.js index a7e555b63b..4b003e68e6 100644 --- a/lib/rules/declaration-colon-newline-after/index.js +++ b/lib/rules/declaration-colon-newline-after/index.js @@ -64,11 +64,9 @@ const rule = (primary, _secondaryOptions, context) => { const betweenBefore = between.slice(0, sliceIndex); const betweenAfter = between.slice(sliceIndex); - if (/^\s*\n/.test(betweenAfter)) { - decl.raws.between = betweenBefore + betweenAfter.replace(/^[^\S\r\n]*/, ''); - } else { - decl.raws.between = betweenBefore + context.newline + betweenAfter; - } + decl.raws.between = /^\s*\n/.test(betweenAfter) + ? betweenBefore + betweenAfter.replace(/^[^\S\r\n]*/, '') + : betweenBefore + context.newline + betweenAfter; return; } diff --git a/lib/rules/font-family-name-quotes/__tests__/index.js b/lib/rules/font-family-name-quotes/__tests__/index.js index b36413392b..0c137f9d60 100644 --- a/lib/rules/font-family-name-quotes/__tests__/index.js +++ b/lib/rules/font-family-name-quotes/__tests__/index.js @@ -29,7 +29,8 @@ testRule({ ruleName, config: ['always-unless-keyword'], - accept: variablePositiveTests.concat([ + accept: [ + ...variablePositiveTests, { code: 'a { font-family: "Lucida Grande", "Arial", sans-serif; }', }, @@ -84,7 +85,7 @@ testRule({ { code: 'a { font-family: "ሀ"; }', }, - ]), + ], reject: [ { @@ -170,7 +171,8 @@ testRule({ ruleName, config: ['always-where-recommended'], - accept: variablePositiveTests.concat([ + accept: [ + ...variablePositiveTests, { code: 'a { font: 1em "Lucida Grande", Arial, sans-serif; }', }, @@ -222,7 +224,7 @@ testRule({ { code: "a { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; }", }, - ]), + ], reject: [ { @@ -298,7 +300,8 @@ testRule({ ruleName, config: ['always-where-required'], - accept: variablePositiveTests.concat([ + accept: [ + ...variablePositiveTests, { code: 'a { font: 1em Lucida Grande, Arial, sans-serif; }', }, @@ -332,7 +335,7 @@ testRule({ { code: 'a { font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, sans-serif; }', }, - ]), + ], reject: [ { diff --git a/lib/rules/function-parentheses-newline-inside/index.js b/lib/rules/function-parentheses-newline-inside/index.js index cbcddd14e8..c1c2179251 100644 --- a/lib/rules/function-parentheses-newline-inside/index.js +++ b/lib/rules/function-parentheses-newline-inside/index.js @@ -171,7 +171,7 @@ function getCheckBefore(valueNode) { function getCheckAfter(valueNode) { let after = ''; - for (const node of valueNode.nodes.slice().reverse()) { + for (const node of [...valueNode.nodes].reverse()) { if (node.type === 'comment') { continue; } @@ -250,7 +250,7 @@ function fixAfterForAlways(valueNode, newline) { function fixAfterForNever(valueNode) { valueNode.after = ''; - for (const node of valueNode.nodes.slice().reverse()) { + for (const node of [...valueNode.nodes].reverse()) { if (node.type === 'comment') { continue; } diff --git a/lib/rules/function-whitespace-after/index.js b/lib/rules/function-whitespace-after/index.js index 44e9db2368..36219f5c44 100644 --- a/lib/rules/function-whitespace-after/index.js +++ b/lib/rules/function-whitespace-after/index.js @@ -92,22 +92,20 @@ const rule = (primary, _secondaryOptions, context) => { result, ruleName, }); - } else if (primary === 'never') { - if (isWhitespace(nextChar)) { - if (fix) { - fix(index); - - return; - } + } else if (primary === 'never' && isWhitespace(nextChar)) { + if (fix) { + fix(index); - report({ - message: messages.rejected, - node, - index: nodeIndex + index, - result, - ruleName, - }); + return; } + + report({ + message: messages.rejected, + node, + index: nodeIndex + index, + result, + ruleName, + }); } } diff --git a/lib/rules/max-line-length/__tests__/index.js b/lib/rules/max-line-length/__tests__/index.js index 7538fea016..9af2664630 100644 --- a/lib/rules/max-line-length/__tests__/index.js +++ b/lib/rules/max-line-length/__tests__/index.js @@ -3,8 +3,9 @@ const postcssScss = require('postcss-scss'); const { messages, ruleName } = require('..'); + const testUrl = 'somethingsomething something\tsomething'; -const _21whitespaces = new Array(21).fill('\u0020').join(''); +const _21whitespaces = Array.from({ length: 21 }).fill('\u0020').join(''); testRule({ ruleName, diff --git a/lib/rules/named-grid-areas-no-invalid/utils/findNotContiguousOrRectangular.js b/lib/rules/named-grid-areas-no-invalid/utils/findNotContiguousOrRectangular.js index b9911f84bd..b359c54133 100644 --- a/lib/rules/named-grid-areas-no-invalid/utils/findNotContiguousOrRectangular.js +++ b/lib/rules/named-grid-areas-no-invalid/utils/findNotContiguousOrRectangular.js @@ -46,7 +46,7 @@ function namedAreas(areas) { names.delete('.'); - return Array.from(names); + return [...names]; } /** diff --git a/lib/rules/no-duplicate-selectors/__tests__/index.js b/lib/rules/no-duplicate-selectors/__tests__/index.js index 119135dce9..8250a6aab4 100644 --- a/lib/rules/no-duplicate-selectors/__tests__/index.js +++ b/lib/rules/no-duplicate-selectors/__tests__/index.js @@ -5,6 +5,7 @@ const postcss = require('postcss'); const postcssImport = require('postcss-import'); const rule = require('..'); + const { messages, ruleName } = rule; testRule({ diff --git a/lib/rules/no-duplicate-selectors/index.js b/lib/rules/no-duplicate-selectors/index.js index b6e51a341c..6aacf596b7 100644 --- a/lib/rules/no-duplicate-selectors/index.js +++ b/lib/rules/no-duplicate-selectors/index.js @@ -67,7 +67,7 @@ function rule(actual, options) { // Sort the selectors list so that the order of the constituents // doesn't matter - const sortedSelectorList = normalizedSelectorList.slice().sort().join(','); + const sortedSelectorList = [...normalizedSelectorList].sort().join(','); const selectorLine = ruleNode.source.start.line; diff --git a/lib/rules/no-extra-semicolons/index.js b/lib/rules/no-extra-semicolons/index.js index 3bff6eb47e..52de368c18 100644 --- a/lib/rules/no-extra-semicolons/index.js +++ b/lib/rules/no-extra-semicolons/index.js @@ -82,12 +82,12 @@ function rule(actual, options, context) { return; } - let rawBeforeNode = node.raws.before; + const rawBeforeNode = node.raws.before; if (rawBeforeNode && rawBeforeNode.trim().length !== 0) { - let allowedSemi = 0; + const allowedSemi = 0; - let rawBeforeIndexStart = 0; + const rawBeforeIndexStart = 0; const fixSemiIndices = []; @@ -151,7 +151,7 @@ function rule(actual, options, context) { const rawOwnSemicolon = node.raws.ownSemicolon; if (rawOwnSemicolon) { - let allowedSemi = 0; + const allowedSemi = 0; const fixSemiIndices = []; diff --git a/lib/rules/no-invalid-position-at-import-rule/index.js b/lib/rules/no-invalid-position-at-import-rule/index.js index df4d84acb2..819e252326 100644 --- a/lib/rules/no-invalid-position-at-import-rule/index.js +++ b/lib/rules/no-invalid-position-at-import-rule/index.js @@ -53,15 +53,13 @@ function rule(actual, options) { return; } - if (node.type === 'atrule' && nodeName === 'import') { - if (invalidPosition) { - report({ - message: messages.rejected, - node, - result, - ruleName, - }); - } + if (node.type === 'atrule' && nodeName === 'import' && invalidPosition) { + report({ + message: messages.rejected, + node, + result, + ruleName, + }); } }); }; diff --git a/lib/rules/selector-attribute-name-disallowed-list/index.js b/lib/rules/selector-attribute-name-disallowed-list/index.js index c89ac0fd3a..09ba062071 100644 --- a/lib/rules/selector-attribute-name-disallowed-list/index.js +++ b/lib/rules/selector-attribute-name-disallowed-list/index.js @@ -17,7 +17,7 @@ const messages = ruleMessages(ruleName, { }); function rule(listInput) { - const list = [].concat(listInput); + const list = [listInput].flat(); return (root, result) => { const validOptions = validateOptions(result, ruleName, { diff --git a/lib/rules/selector-attribute-operator-allowed-list/index.js b/lib/rules/selector-attribute-operator-allowed-list/index.js index 8818dd2f85..0437cba1a3 100644 --- a/lib/rules/selector-attribute-operator-allowed-list/index.js +++ b/lib/rules/selector-attribute-operator-allowed-list/index.js @@ -16,7 +16,7 @@ const messages = ruleMessages(ruleName, { }); function rule(listInput) { - const list = [].concat(listInput); + const list = [listInput].flat(); return (root, result) => { const validOptions = validateOptions(result, ruleName, { diff --git a/lib/rules/selector-attribute-operator-disallowed-list/index.js b/lib/rules/selector-attribute-operator-disallowed-list/index.js index c8c14182ec..b7ecc88e20 100644 --- a/lib/rules/selector-attribute-operator-disallowed-list/index.js +++ b/lib/rules/selector-attribute-operator-disallowed-list/index.js @@ -16,7 +16,7 @@ const messages = ruleMessages(ruleName, { }); function rule(listInput) { - const list = [].concat(listInput); + const list = [listInput].flat(); return (root, result) => { const validOptions = validateOptions(result, ruleName, { diff --git a/lib/rules/selector-disallowed-list/index.js b/lib/rules/selector-disallowed-list/index.js index afbc09fb87..b95769b10b 100644 --- a/lib/rules/selector-disallowed-list/index.js +++ b/lib/rules/selector-disallowed-list/index.js @@ -16,7 +16,7 @@ const messages = ruleMessages(ruleName, { }); function rule(listInput) { - const list = [].concat(listInput); + const list = [listInput].flat(); return (root, result) => { const validOptions = validateOptions(result, ruleName, { diff --git a/lib/rules/selector-list-comma-newline-after/__tests__/index.js b/lib/rules/selector-list-comma-newline-after/__tests__/index.js index ee5681a89c..3691e9c975 100644 --- a/lib/rules/selector-list-comma-newline-after/__tests__/index.js +++ b/lib/rules/selector-list-comma-newline-after/__tests__/index.js @@ -187,11 +187,13 @@ testRule({ code: 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z {\n}', fixed: 'a,\nb,\nc,\nd,\ne,\nf,\ng,\nh,\ni,\nj,\nk,\nl,\nm,\nn,\no,\np,\nq,\nr,\ns,\nt,\nu,\nv,\nw,\nx,\ny,\nz {\n}', - warnings: new Array(25).fill(0).map((_, i) => ({ - message: messages.expectedAfter(), - line: 1, - column: 2 * (i + 1), - })), + warnings: Array.from({ length: 25 }) + .fill(0) + .map((_, i) => ({ + message: messages.expectedAfter(), + line: 1, + column: 2 * (i + 1), + })), }, ], }); @@ -329,11 +331,13 @@ testRule({ { code: 'a,\nb,\nc,\nd,\ne,\nf,\ng,\nh,\ni,\nj,\nk,\nl,\nm,\nn,\no,\np,\nq,\nr,\ns,\nt,\nu,\nv,\nw,\nx,\ny,\nz {\n}', fixed: 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z {\n}', - warnings: new Array(25).fill(0).map((_, i) => ({ - message: messages.rejectedAfterMultiLine(), - line: 1 + i, - column: 2, - })), + warnings: Array.from({ length: 25 }) + .fill(0) + .map((_, i) => ({ + message: messages.rejectedAfterMultiLine(), + line: 1 + i, + column: 2, + })), }, ], }); diff --git a/lib/rules/string-quotes/index.js b/lib/rules/string-quotes/index.js index 9fafe76463..77dc710766 100644 --- a/lib/rules/string-quotes/index.js +++ b/lib/rules/string-quotes/index.js @@ -82,28 +82,26 @@ function rule(expectation, secondary, context) { return; } - if (attributeNode.quoteMark === correctQuote) { - if (avoidEscape) { - const needsCorrectEscape = attributeNode.value.includes(correctQuote); - const needsOtherEscape = attributeNode.value.includes(erroneousQuote); + if (attributeNode.quoteMark === correctQuote && avoidEscape) { + const needsCorrectEscape = attributeNode.value.includes(correctQuote); + const needsOtherEscape = attributeNode.value.includes(erroneousQuote); - if (needsOtherEscape) { - return; - } + if (needsOtherEscape) { + return; + } - if (needsCorrectEscape) { - if (context.fix) { - selectorFixed = true; - attributeNode.quoteMark = erroneousQuote; - } else { - report({ - message: messages.expected(expectation === 'single' ? 'double' : expectation), - node: ruleNode, - index: attributeNode.sourceIndex + attributeNode.offsetOf('value'), - result, - ruleName, - }); - } + if (needsCorrectEscape) { + if (context.fix) { + selectorFixed = true; + attributeNode.quoteMark = erroneousQuote; + } else { + report({ + message: messages.expected(expectation === 'single' ? 'double' : expectation), + node: ruleNode, + index: attributeNode.sourceIndex + attributeNode.offsetOf('value'), + result, + ruleName, + }); } } } diff --git a/lib/rules/unit-allowed-list/index.js b/lib/rules/unit-allowed-list/index.js index 2a3b53e4e3..a71d4fda75 100644 --- a/lib/rules/unit-allowed-list/index.js +++ b/lib/rules/unit-allowed-list/index.js @@ -20,7 +20,7 @@ const messages = ruleMessages(ruleName, { }); function rule(listInput, options) { - const list = [].concat(listInput); + const list = [listInput].flat(); return (root, result) => { const validOptions = validateOptions( diff --git a/lib/rules/unit-disallowed-list/index.js b/lib/rules/unit-disallowed-list/index.js index 6bfec7142b..4c09e01230 100644 --- a/lib/rules/unit-disallowed-list/index.js +++ b/lib/rules/unit-disallowed-list/index.js @@ -29,7 +29,7 @@ const getMediaFeatureName = (mediaFeatureNode) => { }; function rule(listInput, options) { - const list = [].concat(listInput); + const list = [listInput].flat(); return (root, result) => { const validOptions = validateOptions( diff --git a/lib/standalone.js b/lib/standalone.js index 0e9e01950c..d1963305cd 100644 --- a/lib/standalone.js +++ b/lib/standalone.js @@ -18,6 +18,7 @@ const path = require('path'); const pkg = require('../package.json'); const prepareReturnValue = require('./prepareReturnValue'); const { default: ignore } = require('ignore'); + const DEFAULT_IGNORE_FILENAME = '.stylelintignore'; const ALWAYS_IGNORED_GLOBS = ['**/node_modules/**']; const writeFileAtomic = require('write-file-atomic'); @@ -157,13 +158,9 @@ module.exports = function (options) { !postcssResult.stylelint.ignored && !postcssResult.stylelint.ruleDisableFix ) { - if (!postcssResult.stylelint.disableWritingFix) { - // If we're fixing, the output should be the fixed code - returnValue.output = postcssResult.root.toString(postcssResult.opts.syntax); - } else { - // If the writing of the fix is disabled, the input code is returned as-is - returnValue.output = code; - } + returnValue.output = !postcssResult.stylelint.disableWritingFix + ? postcssResult.root.toString(postcssResult.opts.syntax) + : code; } return returnValue; diff --git a/lib/utils/__tests__/isNonNegativeInteger.test.js b/lib/utils/__tests__/isNonNegativeInteger.test.js index 23a2cfc761..933e0a0dd1 100644 --- a/lib/utils/__tests__/isNonNegativeInteger.test.js +++ b/lib/utils/__tests__/isNonNegativeInteger.test.js @@ -12,7 +12,7 @@ describe('isNonNegativeInteger', () => { expect(isNonNegativeInteger(-1)).toBe(false); expect(isNonNegativeInteger(-0.1)).toBe(false); expect(isNonNegativeInteger(0.1)).toBe(false); - expect(isNonNegativeInteger(NaN)).toBe(false); + expect(isNonNegativeInteger(Number.NaN)).toBe(false); }); it('returns false for a non-number', () => { diff --git a/lib/utils/addEmptyLineBefore.js b/lib/utils/addEmptyLineBefore.js index a2d3db4f46..68a53891f4 100644 --- a/lib/utils/addEmptyLineBefore.js +++ b/lib/utils/addEmptyLineBefore.js @@ -15,11 +15,9 @@ module.exports = function addEmptyLineBefore(node, newline) { return node; } - if (!/\r?\n/.test(raws.before)) { - raws.before = newline.repeat(2) + raws.before; - } else { - raws.before = raws.before.replace(/(\r?\n)/, `${newline}$1`); - } + raws.before = !/\r?\n/.test(raws.before) + ? newline.repeat(2) + raws.before + : raws.before.replace(/(\r?\n)/, `${newline}$1`); return node; }; diff --git a/lib/utils/whitespaceChecker.js b/lib/utils/whitespaceChecker.js index b8bd464c4e..47edff9b77 100644 --- a/lib/utils/whitespaceChecker.js +++ b/lib/utils/whitespaceChecker.js @@ -219,10 +219,12 @@ module.exports = function whitespaceChecker(targetWhitespace, expectation, messa return; } - if (targetWhitespace === 'space' && oneCharBefore === ' ') { - if (activeArgs.onlyOneChar || !isWhitespace(twoCharsBefore)) { - return; - } + if ( + targetWhitespace === 'space' && + oneCharBefore === ' ' && + (activeArgs.onlyOneChar || !isWhitespace(twoCharsBefore)) + ) { + return; } assertFunction(messageFunc); @@ -289,24 +291,26 @@ module.exports = function whitespaceChecker(targetWhitespace, expectation, messa if (targetWhitespace === 'newline') { // If index is followed by a Windows CR-LF ... - if (oneCharAfter === '\r' && twoCharsAfter === '\n') { - if (activeArgs.onlyOneChar || !isWhitespace(source[index + 3])) { - return; - } + if ( + oneCharAfter === '\r' && + twoCharsAfter === '\n' && + (activeArgs.onlyOneChar || !isWhitespace(source[index + 3])) + ) { + return; } // If index is followed by a Unix LF ... - if (oneCharAfter === '\n') { - if (activeArgs.onlyOneChar || !isWhitespace(twoCharsAfter)) { - return; - } + if (oneCharAfter === '\n' && (activeArgs.onlyOneChar || !isWhitespace(twoCharsAfter))) { + return; } } - if (targetWhitespace === 'space' && oneCharAfter === ' ') { - if (activeArgs.onlyOneChar || !isWhitespace(twoCharsAfter)) { - return; - } + if ( + targetWhitespace === 'space' && + oneCharAfter === ' ' && + (activeArgs.onlyOneChar || !isWhitespace(twoCharsAfter)) + ) { + return; } assertFunction(messageFunc); @@ -347,6 +351,6 @@ function isValue(x) { */ function assertFunction(x) { if (typeof x !== 'function') { - throw new Error(`\`${x}\` must be a function`); + throw new TypeError(`\`${x}\` must be a function`); } }