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

Support --ignore-pattern as a command line option #14223

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ If you don’t have a configuration file, or want to ignore it if it does exist,

Path to a file containing patterns that describe files to ignore. By default, Prettier looks for `./.prettierignore`.

## `--ignore-pattern`

This allows you to specify patterns of files to ignore (in addition to those in .prettierignore).

```bash
prettier --ignore-pattern "./ignore**.js" --check .
```

## `--list-different`

Another useful flag is `--list-different` (or `-l`) which prints the filenames of files that are different from Prettier formatting. If there are differences the script errors out, which is useful in a CI scenario.
Expand Down
3 changes: 2 additions & 1 deletion src/cli/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ async function createIgnorerFromContextOrDie(context) {
try {
return await createIgnorer(
context.argv.ignorePath,
context.argv.withNodeModules
context.argv.withNodeModules,
context.argv.ignorePatterns
);
} catch (e) {
context.logger.error(e.message);
Expand Down
18 changes: 13 additions & 5 deletions src/common/create-ignorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,40 @@ const getFileContentOrNull = require("../utils/get-file-content-or-null.js");
/**
* @param {string?} ignorePath
* @param {boolean?} withNodeModules
* @param {Array<string>?} ignorePatterns
*/
async function createIgnorer(ignorePath, withNodeModules) {
async function createIgnorer(ignorePath, withNodeModules, ignorePatterns = []) {
const ignoreContent = ignorePath
? await getFileContentOrNull(path.resolve(ignorePath))
: null;

return _createIgnorer(ignoreContent, withNodeModules);
return _createIgnorer(ignoreContent, withNodeModules, ignorePatterns);
}

/**
* @param {string?} ignorePath
* @param {boolean?} withNodeModules
*/
createIgnorer.sync = function (ignorePath, withNodeModules) {
createIgnorer.sync = function (
ignorePath,
withNodeModules,
ignorePatterns = []
) {
const ignoreContent = !ignorePath
? null
: getFileContentOrNull.sync(path.resolve(ignorePath));
return _createIgnorer(ignoreContent, withNodeModules);
return _createIgnorer(ignoreContent, withNodeModules, ignorePatterns);
};

/**
* @param {null | string} ignoreContent
* @param {boolean?} withNodeModules
* @param {Array<string>} ignorePatterns
*/
function _createIgnorer(ignoreContent, withNodeModules) {
function _createIgnorer(ignoreContent, withNodeModules, ignorePatterns) {
const ignorer = ignore({ allowRelativePaths: true }).add(ignoreContent || "");
ignorer.add(ignorePatterns);

if (!withNodeModules) {
ignorer.add("node_modules");
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/core-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ const options = {
cliCategory: CATEGORY_OTHER,
cliDescription: "Path to the file to pretend that stdin comes from.",
},
ignorePatterns: {
since: "2.9.0",
type: "path",
array: true,
default: [{ value: [] }],
category: CATEGORY_GLOBAL,
description: outdent`
Specify the patterns of files to ignore, in addition to those in the
configuration.
`,
exception: (value) =>
typeof value === "string" || typeof value === "object",
cliName: "ignore-pattern",
cliCategory: CATEGORY_CONFIG,
},
insertPragma: {
since: "1.8.0",
category: CATEGORY_SPECIAL,
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/__tests__/__snapshots__/early-exit.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ Config options:
Find and print the path to a configuration file for the given input file.
--ignore-path <path> Path to a file with patterns describing files to ignore.
Defaults to .prettierignore.
--ignore-pattern <path> Specify the patterns of files to ignore, in addition to those in the
configuration.
Defaults to [].
--plugin <path> Add a plugin. Multiple plugins can be passed as separate \`--plugin\`s.
Defaults to [].
--plugin-search-dir <path>
Expand Down Expand Up @@ -289,6 +292,9 @@ Config options:
Find and print the path to a configuration file for the given input file.
--ignore-path <path> Path to a file with patterns describing files to ignore.
Defaults to .prettierignore.
--ignore-pattern <path> Specify the patterns of files to ignore, in addition to those in the
configuration.
Defaults to [].
--plugin <path> Add a plugin. Multiple plugins can be passed as separate \`--plugin\`s.
Defaults to [].
--plugin-search-dir <path>
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/__tests__/__snapshots__/help-options.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ Default: .prettierignore

exports[`show detailed usage with --help ignore-path (write) 1`] = `[]`;

exports[`show detailed usage with --help ignore-pattern (stderr) 1`] = `""`;

exports[`show detailed usage with --help ignore-pattern (stdout) 1`] = `
"--ignore-pattern <path>

Specify the patterns of files to ignore, in addition to those in the
configuration.

Default: []
"
`;

exports[`show detailed usage with --help ignore-pattern (write) 1`] = `[]`;

exports[`show detailed usage with --help ignore-unknown (stderr) 1`] = `""`;

exports[`show detailed usage with --help ignore-unknown (stdout) 1`] = `
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/__tests__/__snapshots__/ignore-patterns.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ignore patterns (stderr) 1`] = `""`;

exports[`ignore patterns (stdout) 1`] = `
"regular-module.js
"
`;

exports[`ignore patterns (write) 1`] = `[]`;

exports[`ignore patterns with multiple arguments (stderr) 1`] = `""`;

exports[`ignore patterns with multiple arguments (stdout) 1`] = `""`;

exports[`ignore patterns with multiple arguments (write) 1`] = `[]`;

exports[`outputs files as-is if no --write (stderr) 1`] = `""`;

exports[`outputs files as-is if no --write (stdout) 1`] = `
"'use strict';
"
`;

exports[`outputs files as-is if no --write (write) 1`] = `[]`;
9 changes: 9 additions & 0 deletions tests/integration/__tests__/__snapshots__/schema.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ This option cannot be used with --range-start and --range-end.",
},
],
},
"ignorePatterns": {
"default": [],
"description": "Specify the patterns of files to ignore, in addition to those in the
configuration.",
"items": {
"type": "string",
},
"type": "array",
},
"insertPragma": {
"default": false,
"description": "Insert @format pragma into file's first docblock comment.",
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/__tests__/__snapshots__/support-info.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ exports[`API getSupportInfo() 1`] = `
"default": "css",
"type": "choice",
},
"ignorePatterns": {
"default": [],
"type": "path",
},
"insertPragma": {
"default": false,
"type": "boolean",
Expand Down Expand Up @@ -912,6 +916,16 @@ exports[`CLI --support-info (stdout) 1`] = `
"since": "1.15.0",
"type": "choice"
},
{
"array": true,
"category": "Global",
"default": [],
"description": "Specify the patterns of files to ignore, in addition to those in the\\nconfiguration.",
"name": "ignorePatterns",
"pluginDefaults": {},
"since": "2.9.0",
"type": "path"
},
{
"category": "Special",
"default": false,
Expand Down
39 changes: 39 additions & 0 deletions tests/integration/__tests__/ignore-patterns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

const runPrettier = require("../run-prettier.js");

describe("ignore patterns", () => {
runPrettier("cli/ignore-patterns", [
"**/*.js",
"--ignore-pattern",
"**/other-regular-module.js",
"-l",
]).test({
status: 1,
});
});

describe("ignore patterns with multiple arguments", () => {
runPrettier("cli/ignore-patterns", [
"**/*.js",
"--ignore-pattern",
"**/other-regular-module.js",
"--ignore-pattern",
"**/regular-module.js",
"-l",
]).test({
status: 0,
});
});

describe("outputs files as-is if no --write", () => {
runPrettier(
"cli/ignore-patterns",
["--ignore-pattern", "**/regular-module.js", "regular-module.js"],
{
ignoreLineEndings: true,
}
).test({
status: 0,
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict';
1 change: 1 addition & 0 deletions tests/integration/cli/ignore-patterns/regular-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict';