Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor getFileIgnorer() to reduce duplication #5794

Merged
merged 1 commit into from Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] Separated by a blank line to distinguish external and internal importing.

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 || []);
};