Skip to content

Commit

Permalink
Mostly autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Sep 16, 2021
1 parent 8f4e870 commit 310ef51
Show file tree
Hide file tree
Showing 35 changed files with 180 additions and 190 deletions.
16 changes: 7 additions & 9 deletions lib/__tests__/fixtures/plugin-slashless-warn-about-foo.js
Expand Up @@ -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,
});
}
});
};
Expand Down
16 changes: 7 additions & 9 deletions lib/__tests__/fixtures/plugin-warn-about-bar.js
Expand Up @@ -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,
});
}
});
};
Expand Down
16 changes: 7 additions & 9 deletions lib/__tests__/fixtures/plugin-warn-about-foo.js
Expand Up @@ -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,
});
}
});
};
Expand Down
12 changes: 6 additions & 6 deletions lib/augmentConfig.js
Expand Up @@ -122,15 +122,15 @@ 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);
});
}

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);
});
}
Expand Down Expand Up @@ -353,8 +353,8 @@ function addProcessorFunctions(config) {
/** @type {Array<Function>} */
const resultProcessors = [];

/** @type {Array<StylelintConfigProcessor>} */ ([])
.concat(config.processors)
/** @type {Array<StylelintConfigProcessor>} */ [config.processors]
.flat()
.forEach((processorConfig) => {
const processorKey = JSON.stringify(processorConfig);

Expand Down Expand Up @@ -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": {} }] }.',
);
}
Expand All @@ -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": {} }] }.',
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/formatters/tapFormatter.js
Expand Up @@ -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(
Expand Down
10 changes: 3 additions & 7 deletions lib/getPostcssResult.js
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/reference/keywordSets.js
Expand Up @@ -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];
}, []),
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/reportUnknownRuleNames.js
Expand Up @@ -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] = [];
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/block-closing-brace-newline-after/index.js
Expand Up @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/block-closing-brace-newline-before/index.js
Expand Up @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/block-opening-brace-newline-after/index.js
Expand Up @@ -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);

Expand Down
40 changes: 22 additions & 18 deletions lib/rules/color-named/generateColorFuncs.js
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/declaration-block-semicolon-newline-after/index.js
Expand Up @@ -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;
}
Expand Down
8 changes: 3 additions & 5 deletions lib/rules/declaration-colon-newline-after/index.js
Expand Up @@ -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;
}
Expand Down
15 changes: 9 additions & 6 deletions lib/rules/font-family-name-quotes/__tests__/index.js
Expand Up @@ -29,7 +29,8 @@ testRule({
ruleName,
config: ['always-unless-keyword'],

accept: variablePositiveTests.concat([
accept: [
...variablePositiveTests,
{
code: 'a { font-family: "Lucida Grande", "Arial", sans-serif; }',
},
Expand Down Expand Up @@ -84,7 +85,7 @@ testRule({
{
code: 'a { font-family: "ሀ"; }',
},
]),
],

reject: [
{
Expand Down Expand Up @@ -170,7 +171,8 @@ testRule({
ruleName,
config: ['always-where-recommended'],

accept: variablePositiveTests.concat([
accept: [
...variablePositiveTests,
{
code: 'a { font: 1em "Lucida Grande", Arial, sans-serif; }',
},
Expand Down Expand Up @@ -222,7 +224,7 @@ testRule({
{
code: "a { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; }",
},
]),
],

reject: [
{
Expand Down Expand Up @@ -298,7 +300,8 @@ testRule({
ruleName,
config: ['always-where-required'],

accept: variablePositiveTests.concat([
accept: [
...variablePositiveTests,
{
code: 'a { font: 1em Lucida Grande, Arial, sans-serif; }',
},
Expand Down Expand Up @@ -332,7 +335,7 @@ testRule({
{
code: 'a { font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, sans-serif; }',
},
]),
],

reject: [
{
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/function-parentheses-newline-inside/index.js
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down

5 comments on commit 310ef51

@XhmikosR
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeddy3 should I open a PR with this patch? I still cannot make the xo branch work well with the prettier config, so this is the best I can get with autofix only :)

https://github.com/stylelint/stylelint/compare/v14...xo?w=1

@jeddy3
Copy link
Member

@jeddy3 jeddy3 commented on 310ef51 Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XhmikosR Yes, please. These refactors make the code easier to understand.

@jeddy3
Copy link
Member

@jeddy3 jeddy3 commented on 310ef51 Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's done with autofix, best wait for #5542 to be merged first before opening a pull request so that we avoid conflicts.

@XhmikosR
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeddy3 can do, just if you remember, please ping me in case I forget :)

@jeddy3
Copy link
Member

@jeddy3 jeddy3 commented on 310ef51 Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XhmikosR Just merged #5542 now.

Please sign in to comment.