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

Add --noglob cli option for processing exact the passed files #6492

Closed
wants to merge 4 commits 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
18 changes: 18 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ const link = <a href="example.com">http://example.com</a>;

-->

#### CLI: --noglob cli option ([#6492] by [@AzazKamaz])

Add --noglob cli option. It make cli process exactly what you passed.

<!-- prettier-ignore -->
```bash
// Input
$ echo "var x = 1;" > [code].js

// Output (Prettier stable)
$ prettier [code].js
[error] No matching files. Patterns tried: [code].js

// Output (Prettier master)
$ prettier --noglob [code].js
var x = 1;
```

#### JavaScript: More readable parentheses for new-call ([#6412] by [@bakkot])

<!-- prettier-ignore -->
Expand Down
4 changes: 4 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,7 @@ $ cat abc.css | prettier --stdin-filepath abc.css
display: none;
}
```

## `--noglob`

Use filenames exactly from parameters, without glob matching. For example if you have file `[name].js` (like in [sapper](https://sapper.svelte.dev)) with [lint-staged](https://github.com/okonet/lint-staged) (it passes list of changed files, and `[name]` by default is regex)
4 changes: 4 additions & 0 deletions src/cli/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ const options = {
default: "log",
choices: ["silent", "error", "warn", "log", "debug"]
},
noglob: {
type: "boolean",
description: "Use filenames exactly from parameters, without glob matching."
},
"only-changed": {
type: "boolean",
description: "Only format files changed since last '--write'."
Expand Down
7 changes: 7 additions & 0 deletions src/cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,13 @@ function createIgnorerFromContextOrDie(context) {
}

function eachFilename(context, patterns, callback) {
if (context.argv["noglob"]) {
patterns
.map(filePath => path.relative(process.cwd(), filePath))
.forEach(callback);
return;
}

// The '!./' globs are due to https://github.com/prettier/prettier/issues/2110
const ignoreNodeModules = context.argv["with-node-modules"] !== true;
if (ignoreNodeModules) {
Expand Down
2 changes: 2 additions & 0 deletions tests_integration/__tests__/__snapshots__/early-exit.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Other options:
--loglevel <silent|error|warn|log|debug>
What level of logs to report.
Defaults to log.
--noglob Use filenames exactly from parameters, without glob matching.
--only-changed Only format files changed since last '--write'.
--require-pragma Require either '@prettier' or '@format' to be present in the file's first docblock comment
in order for it to be formatted.
Expand Down Expand Up @@ -301,6 +302,7 @@ Other options:
--loglevel <silent|error|warn|log|debug>
What level of logs to report.
Defaults to log.
--noglob Use filenames exactly from parameters, without glob matching.
--only-changed Only format files changed since last '--write'.
--require-pragma Require either '@prettier' or '@format' to be present in the file's first docblock comment
in order for it to be formatted.
Expand Down
11 changes: 11 additions & 0 deletions tests_integration/__tests__/__snapshots__/help-options.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@ exports[`show detailed usage with --help no-semi (stdout) 1`] = `

exports[`show detailed usage with --help no-semi (write) 1`] = `Array []`;

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

exports[`show detailed usage with --help noglob (stdout) 1`] = `
"--noglob

Use filenames exactly from parameters, without glob matching.
"
`;

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

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

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

exports[`can access files with glob without --noglob (stderr) 1`] = `""`;

exports[`can access files with glob without --noglob (stdout) 1`] = `
"var x = 2;
var x = 3;
"
`;

exports[`can access files with glob without --noglob (write) 1`] = `Array []`;

exports[`cannot access exact filename without --noglob (stderr) 1`] = `
"[error] No matching files. Patterns tried: [code].js !**/node_modules/** !./node_modules/** !**/.{git,svn,hg}/** !./.{git,svn,hg}/**
"
`;

exports[`cannot access exact filename without --noglob (stdout) 1`] = `""`;

exports[`cannot access exact filename without --noglob (write) 1`] = `Array []`;

exports[`cannot access files with glob with --noglob (stderr) 1`] = `
"[error] Unable to read file: [AB].js
[error] ENOENT: no such file or directory, open '[AB].js'
"
`;

exports[`cannot access files with glob with --noglob (stdout) 1`] = `
"
"
`;

exports[`cannot access files with glob with --noglob (write) 1`] = `Array []`;

exports[`process exact passed filename with --noglob (stderr) 1`] = `""`;

exports[`process exact passed filename with --noglob (stdout) 1`] = `
"var x = 1;
"
`;

exports[`process exact passed filename with --noglob (write) 1`] = `Array []`;
27 changes: 27 additions & 0 deletions tests_integration/__tests__/noglob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

const runPrettier = require("../runPrettier");

describe("process exact passed filename with --noglob", () => {
runPrettier("cli/noglob", ["--noglob", "[code].js"]).test({
status: 0
});
});

describe("cannot access exact filename without --noglob", () => {
runPrettier("cli/noglob", ["[code].js"]).test({
status: 2
});
});

describe("can access files with glob without --noglob", () => {
runPrettier("cli/noglob", ["[AB].js"]).test({
status: 0
});
});

describe("cannot access files with glob with --noglob", () => {
runPrettier("cli/noglob", ["--noglob", "[AB].js"]).test({
status: 2
});
});
1 change: 1 addition & 0 deletions tests_integration/cli/noglob/A.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = 2;
1 change: 1 addition & 0 deletions tests_integration/cli/noglob/B.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = 3;
1 change: 1 addition & 0 deletions tests_integration/cli/noglob/[code].js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = 1;