Skip to content

Commit

Permalink
Refactor to slightly simplify the code (#7675)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mouvedia committed May 8, 2024
1 parent 87ed665 commit 1ec9b62
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 79 deletions.
25 changes: 11 additions & 14 deletions lib/augmentConfig.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,21 @@ function absolutizePaths(config, configDir, cwd) {
config.ignoreFiles = [config.ignoreFiles].flat().map((glob) => absolutizeGlob(glob, configDir));
}

if (config.plugins) {
config.plugins = [config.plugins].flat().map((lookup) => {
if (typeof lookup === 'string') {
return getModulePath(configDir, lookup, cwd);
}
/** @type {<T>(lookup: T) => (string | T)} */
const toAbsolutePath = (lookup) => {
if (typeof lookup === 'string') {
return getModulePath(configDir, lookup, cwd);
}

return lookup;
};

return lookup;
});
if (config.plugins) {
config.plugins = [config.plugins].flat().map(toAbsolutePath);
}

if (config.processors) {
config.processors = config.processors.map((lookup) => {
if (typeof lookup === 'string') {
return getModulePath(configDir, lookup, cwd);
}

return lookup;
});
config.processors = config.processors.map(toAbsolutePath);
}

return config;
Expand Down
25 changes: 11 additions & 14 deletions lib/augmentConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,21 @@ function absolutizePaths(config, configDir, cwd) {
config.ignoreFiles = [config.ignoreFiles].flat().map((glob) => absolutizeGlob(glob, configDir));
}

if (config.plugins) {
config.plugins = [config.plugins].flat().map((lookup) => {
if (typeof lookup === 'string') {
return getModulePath(configDir, lookup, cwd);
}
/** @type {<T>(lookup: T) => (string | T)} */
const toAbsolutePath = (lookup) => {
if (typeof lookup === 'string') {
return getModulePath(configDir, lookup, cwd);
}

return lookup;
};

return lookup;
});
if (config.plugins) {
config.plugins = [config.plugins].flat().map(toAbsolutePath);
}

if (config.processors) {
config.processors = config.processors.map((lookup) => {
if (typeof lookup === 'string') {
return getModulePath(configDir, lookup, cwd);
}

return lookup;
});
config.processors = config.processors.map(toAbsolutePath);
}

return config;
Expand Down
1 change: 0 additions & 1 deletion lib/reference/properties.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ const longhandSubPropertiesOfShorthandProperties = new Map([
* reset explicitly: normal | small-caps
* reset implicitly: all-small-caps | petite-caps | all-petite-caps | unicase | titling-caps
* i.e. either way it will be reset
* @todo add font-variant shorthand to longhandSubPropertiesOfShorthandProperties
* {@link https://www.w3.org/TR/css-fonts-4/#font-variant-prop World Wide Web Consortium}
*/
'font-variant',
Expand Down
1 change: 0 additions & 1 deletion lib/reference/properties.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ export const longhandSubPropertiesOfShorthandProperties = new Map([
* reset explicitly: normal | small-caps
* reset implicitly: all-small-caps | petite-caps | all-petite-caps | unicase | titling-caps
* i.e. either way it will be reset
* @todo add font-variant shorthand to longhandSubPropertiesOfShorthandProperties
* {@link https://www.w3.org/TR/css-fonts-4/#font-variant-prop World Wide Web Consortium}
*/
'font-variant',
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/alpha-value-notation/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const validateTypes = require('../../utils/validateTypes.cjs');
const declarationValueIndex = require('../../utils/declarationValueIndex.cjs');
const getDeclarationValue = require('../../utils/getDeclarationValue.cjs');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue.cjs');
const typeGuards = require('../../utils/typeGuards.cjs');
const optionsMatches = require('../../utils/optionsMatches.cjs');
const report = require('../../utils/report.cjs');
const ruleMessages = require('../../utils/ruleMessages.cjs');
Expand Down Expand Up @@ -196,7 +197,7 @@ function findAlphaInFunction(node) {
if (slashNodeIndex !== -1) {
const nodesAfterSlash = node.nodes.slice(slashNodeIndex + 1, node.nodes.length);

return nodesAfterSlash.find(({ type }) => type === 'word');
return nodesAfterSlash.find(typeGuards.isValueWord);
}

return undefined;
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/alpha-value-notation/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { assert, isRegExp, isString } from '../../utils/validateTypes.mjs';
import declarationValueIndex from '../../utils/declarationValueIndex.mjs';
import getDeclarationValue from '../../utils/getDeclarationValue.mjs';
import isStandardSyntaxValue from '../../utils/isStandardSyntaxValue.mjs';
import { isValueWord } from '../../utils/typeGuards.mjs';
import optionsMatches from '../../utils/optionsMatches.mjs';
import report from '../../utils/report.mjs';
import ruleMessages from '../../utils/ruleMessages.mjs';
Expand Down Expand Up @@ -193,7 +194,7 @@ function findAlphaInFunction(node) {
if (slashNodeIndex !== -1) {
const nodesAfterSlash = node.nodes.slice(slashNodeIndex + 1, node.nodes.length);

return nodesAfterSlash.find(({ type }) => type === 'word');
return nodesAfterSlash.find(isValueWord);
}

return undefined;
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/number-max-precision/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ const rule = (primary, secondaryOptions) => {
*/
function check(node, getIndex, value) {
// Get out quickly if there are no periods
if (!value.includes('.')) {
return;
}
if (!value.includes('.')) return;

const prop = 'prop' in node ? node.prop : undefined;

Expand Down
4 changes: 1 addition & 3 deletions lib/rules/number-max-precision/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ const rule = (primary, secondaryOptions) => {
*/
function check(node, getIndex, value) {
// Get out quickly if there are no periods
if (!value.includes('.')) {
return;
}
if (!value.includes('.')) return;

const prop = 'prop' in node ? node.prop : undefined;

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/value-no-vendor-prefix/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const rule = (primary, secondaryOptions, context) => {

/**
* @todo consolidate in the next major
* @see stylelint/stylelin#7542
* @see stylelint/stylelint#7542
*/
if (optionsMatches(groups, 'unprefixed', vendor.unprefixed(value))) {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/value-no-vendor-prefix/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const rule = (primary, secondaryOptions, context) => {

/**
* @todo consolidate in the next major
* @see stylelint/stylelin#7542
* @see stylelint/stylelint#7542
*/
if (optionsMatches(groups, 'unprefixed', vendor.unprefixed(value))) {
return;
Expand Down
13 changes: 4 additions & 9 deletions lib/utils/getCacheFile.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@ function getCacheFile(cacheFile, cwd) {
return node_path.join(resolvedCacheFile, `.stylelintcache_${hash(cwd)}`);
}

let fileStats;

try {
fileStats = node_fs.lstatSync(resolvedCacheFile);
} catch {
fileStats = null;
}

if (looksLikeADirectory || (fileStats && fileStats.isDirectory())) {
if (
looksLikeADirectory ||
node_fs.lstatSync(resolvedCacheFile, { throwIfNoEntry: false })?.isDirectory()
) {
// Return path to provided directory with generated file name.
return getCacheFileForDirectory();
}
Expand Down
13 changes: 4 additions & 9 deletions lib/utils/getCacheFile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,10 @@ export default function getCacheFile(cacheFile, cwd) {
return join(resolvedCacheFile, `.stylelintcache_${hash(cwd)}`);
}

let fileStats;

try {
fileStats = lstatSync(resolvedCacheFile);
} catch {
fileStats = null;
}

if (looksLikeADirectory || (fileStats && fileStats.isDirectory())) {
if (
looksLikeADirectory ||
lstatSync(resolvedCacheFile, { throwIfNoEntry: false })?.isDirectory()
) {
// Return path to provided directory with generated file name.
return getCacheFileForDirectory();
}
Expand Down
20 changes: 10 additions & 10 deletions lib/utils/getFileIgnorer.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const node_path = require('node:path');
const node_fs = require('node:fs');
const node_path = require('node:path');
const ignore = require('ignore');
const constants = require('../constants.cjs');
const isPathNotFoundError = require('./isPathNotFoundError.cjs');

/**
* @typedef {import('stylelint').LinterOptions} LinterOptions
Expand All @@ -27,15 +26,16 @@ function getFileIgnorer({ ignorePath, ignorePattern, cwd }) {
? ignoreFilePath
: node_path.resolve(cwd, ignoreFilePath);

try {
const ignoreText = node_fs.readFileSync(absoluteIgnoreFilePath, 'utf8');
if (!node_fs.existsSync(absoluteIgnoreFilePath)) continue;

const ignoreText = node_fs.readFileSync(absoluteIgnoreFilePath, {
// utf must remain lowercased to hit the fast path
// see nodejs/node#49888
encoding: 'utf8',
flag: 'r',
});

ignorer.add(ignoreText);
} catch (readError) {
if (!isPathNotFoundError(readError)) {
throw readError;
}
}
ignorer.add(ignoreText);
}

if (ignorePattern) ignorer.add(ignorePattern);
Expand Down
21 changes: 10 additions & 11 deletions lib/utils/getFileIgnorer.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { existsSync, readFileSync } from 'node:fs';
import { isAbsolute, resolve } from 'node:path';
import { readFileSync } from 'node:fs';

import ignore from 'ignore';

import { DEFAULT_IGNORE_FILENAME } from '../constants.mjs';
import isPathNotFoundError from './isPathNotFoundError.mjs';

/**
* @typedef {import('stylelint').LinterOptions} LinterOptions
Expand All @@ -25,15 +23,16 @@ export default function getFileIgnorer({ ignorePath, ignorePattern, cwd }) {
? ignoreFilePath
: resolve(cwd, ignoreFilePath);

try {
const ignoreText = readFileSync(absoluteIgnoreFilePath, 'utf8');
if (!existsSync(absoluteIgnoreFilePath)) continue;

const ignoreText = readFileSync(absoluteIgnoreFilePath, {
// utf must remain lowercased to hit the fast path
// see nodejs/node#49888
encoding: 'utf8',
flag: 'r',
});

ignorer.add(ignoreText);
} catch (readError) {
if (!isPathNotFoundError(readError)) {
throw readError;
}
}
ignorer.add(ignoreText);
}

if (ignorePattern) ignorer.add(ignorePattern);
Expand Down

0 comments on commit 1ec9b62

Please sign in to comment.