Skip to content

Commit

Permalink
Refactor getFileIgnorer() to reduce duplication (#5794)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Dec 20, 2021
1 parent 2852747 commit b37be75
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
13 changes: 13 additions & 0 deletions lib/__tests__/ignore.test.js
Expand Up @@ -202,6 +202,19 @@ test('specified `ignorePath` file ignoring one file using options.cwd', async ()
).rejects.toThrow(noFilesErrorMessage); // no files read
});

test('specified `ignorePath` directory, not a file', async () => {
process.chdir(__dirname);

await expect(
standalone({
files: [],
config: {},
ignorePath: '__snapshots__',
cwd: __dirname,
}),
).rejects.toThrow(/EISDIR/);
});

test('specified `ignorePattern` file ignoring one file', async () => {
const files = [fixtures('empty-block.css')];
const noFilesErrorMessage = new NoFilesFoundError(files);
Expand Down
38 changes: 16 additions & 22 deletions lib/standalone.js
@@ -1,25 +1,24 @@
'use strict';

const createStylelint = require('./createStylelint');
const createStylelintResult = require('./createStylelintResult');
const debug = require('debug')('stylelint:standalone');
const fastGlob = require('fast-glob');
const fs = require('fs');
const globby = require('globby');
const normalizePath = require('normalize-path');
const path = require('path');

const createStylelint = require('./createStylelint');
const createStylelintResult = require('./createStylelintResult');
const FileCache = require('./utils/FileCache');
const filterFilePaths = require('./utils/filterFilePaths');
const formatters = require('./formatters');
const fs = require('fs');
const getFileIgnorer = require('./utils/getFileIgnorer');
const getFormatterOptionsText = require('./utils/getFormatterOptionsText');
const globby = require('globby');
const hash = require('./utils/hash');
const isPathNotFoundError = require('./utils/isPathNotFoundError');
const NoFilesFoundError = require('./utils/noFilesFoundError');
const normalizePath = require('normalize-path');
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');

Expand Down Expand Up @@ -51,8 +50,8 @@ async function standalone({
formatter,
globbyOptions,
ignoreDisables,
ignorePath = DEFAULT_IGNORE_FILENAME,
ignorePattern = [],
ignorePath,
ignorePattern,
maxWarnings,
quiet,
reportDescriptionlessDisables,
Expand All @@ -74,21 +73,16 @@ async function standalone({

// The ignorer will be used to filter file paths after the glob is checked,
// before any files are actually read
const absoluteIgnoreFilePath = path.isAbsolute(ignorePath)
? ignorePath
: path.resolve(cwd, ignorePath);
let ignoreText = '';

/** @type {import('ignore').Ignore} */
let ignorer;

try {
ignoreText = fs.readFileSync(absoluteIgnoreFilePath, 'utf8');
} catch (readError) {
if (!isPathNotFoundError(readError)) {
return Promise.reject(readError);
}
ignorer = getFileIgnorer({ cwd, ignorePath, ignorePattern });
} catch (error) {
return Promise.reject(error);
}

const ignorer = ignore().add(ignoreText).add(ignorePattern);

/** @type {Formatter} */
let formatterFunction;

Expand Down
19 changes: 9 additions & 10 deletions lib/utils/getFileIgnorer.js
Expand Up @@ -9,27 +9,26 @@ const isPathNotFoundError = require('./isPathNotFoundError');

const DEFAULT_IGNORE_FILENAME = '.stylelintignore';

/** @typedef {import('stylelint').LinterOptions} StylelintOptions */

/**
* @param {StylelintOptions} options
* @param {{ cwd: string, ignorePath?: string, ignorePattern?: string[] }} options
* @return {import('ignore').Ignore}
*/
module.exports = function (options) {
module.exports = function getFileIgnorer(options) {
const ignoreFilePath = options.ignorePath || DEFAULT_IGNORE_FILENAME;
const absoluteIgnoreFilePath = path.isAbsolute(ignoreFilePath)
? ignoreFilePath
: path.resolve(options.cwd || process.cwd(), ignoreFilePath);
: path.resolve(options.cwd, ignoreFilePath);
let ignoreText = '';

try {
ignoreText = fs.readFileSync(absoluteIgnoreFilePath, 'utf8');
} catch (readError) {
if (!isPathNotFoundError(readError)) throw readError;
if (!isPathNotFoundError(readError)) {
throw readError;
}
}

const ignorePattern = options.ignorePattern || [];
const ignorer = ignore().add(ignoreText).add(ignorePattern);

return ignorer;
return ignore()
.add(ignoreText)
.add(options.ignorePattern || []);
};

0 comments on commit b37be75

Please sign in to comment.