summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--std/prettier/ignore.ts15
-rw-r--r--std/prettier/ignore_test.ts81
-rwxr-xr-xstd/prettier/main.ts17
3 files changed, 103 insertions, 10 deletions
diff --git a/std/prettier/ignore.ts b/std/prettier/ignore.ts
new file mode 100644
index 000000000..ffada98de
--- /dev/null
+++ b/std/prettier/ignore.ts
@@ -0,0 +1,15 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+
+/**
+ * Parse the contents of the ignore file and return patterns.
+ * It can parse files like .gitignore/.npmignore/.prettierignore
+ * @param ignoreString
+ * @returns patterns
+ */
+export function parse(ignoreString: string): Set<string> {
+ const partterns = ignoreString
+ .split(/\r?\n/)
+ .filter(line => line.trim() !== "" && line.charAt(0) !== "#");
+
+ return new Set(partterns);
+}
diff --git a/std/prettier/ignore_test.ts b/std/prettier/ignore_test.ts
new file mode 100644
index 000000000..05f67edf4
--- /dev/null
+++ b/std/prettier/ignore_test.ts
@@ -0,0 +1,81 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { test, runIfMain } from "../testing/mod.ts";
+import { assertEquals } from "../testing/asserts.ts";
+import { parse } from "./ignore.ts";
+
+const testCases = [
+ {
+ input: `# this is a comment
+node_modules
+`,
+ output: new Set(["node_modules"])
+ },
+ {
+ input: ` # invalid comment
+`,
+ output: new Set([" # invalid comment"])
+ },
+ {
+ input: `
+node_modules
+package.json
+`,
+ output: new Set(["node_modules", "package.json"])
+ },
+ {
+ input: `
+ node_modules
+ package.json
+`,
+ output: new Set([" node_modules", " package.json"])
+ },
+ {
+ input: `*.orig
+*.pyc
+*.swp
+
+/.idea/
+/.vscode/
+gclient_config.py_entries
+/gh-pages/
+/target/
+
+# Files that help ensure VSCode can work but we don't want checked into the
+# repo
+/node_modules
+/tsconfig.json
+
+# We use something stronger than lockfiles, we have all NPM modules stored in a
+# git. We do not download from NPM during build.
+# https://github.com/denoland/deno_third_party
+yarn.lock
+# yarn creates this in error.
+tools/node_modules/
+ `,
+ output: new Set([
+ "*.orig",
+ "*.pyc",
+ "*.swp",
+ "/.idea/",
+ "/.vscode/",
+ "gclient_config.py_entries",
+ "/gh-pages/",
+ "/target/",
+ "/node_modules",
+ "/tsconfig.json",
+ "yarn.lock",
+ "tools/node_modules/"
+ ])
+ }
+];
+
+test({
+ name: "[encoding.ignore] basic",
+ fn(): void {
+ for (const { input, output } of testCases) {
+ assertEquals(parse(input), output);
+ }
+ }
+});
+
+runIfMain(import.meta);
diff --git a/std/prettier/main.ts b/std/prettier/main.ts
index e3f2270c3..b3c469659 100755
--- a/std/prettier/main.ts
+++ b/std/prettier/main.ts
@@ -27,6 +27,7 @@ import { parse } from "../flags/mod.ts";
import * as path from "../path/mod.ts";
import * as toml from "../encoding/toml.ts";
import * as yaml from "../encoding/yaml.ts";
+import * as ignore from "./ignore.ts";
import { ExpandGlobOptions, WalkInfo, expandGlob } from "../fs/mod.ts";
import { prettier, prettierPlugins } from "./prettier.ts";
const { args, cwd, exit, readAll, readFile, stdin, stdout, writeFile } = Deno;
@@ -460,7 +461,7 @@ async function resolveConfig(
/**
* auto detect .prettierignore and return pattern if file exist.
*/
-async function autoResolveIgnoreFile(): Promise<string[]> {
+async function autoResolveIgnoreFile(): Promise<Set<string>> {
const files = await Deno.readDir(".");
for (const f of files) {
@@ -469,20 +470,16 @@ async function autoResolveIgnoreFile(): Promise<string[]> {
}
}
- return [];
+ return new Set([]);
}
/**
* parse prettier ignore file.
* @param filepath the ignore file path.
*/
-async function resolveIgnoreFile(filepath: string): Promise<string[]> {
+async function resolveIgnoreFile(filepath: string): Promise<Set<string>> {
const raw = new TextDecoder().decode(await Deno.readFile(filepath));
-
- return raw
- .split("\n")
- .filter((v: string) => !!v.trim() && v.trim().indexOf("#") !== 0)
- .map(v => v);
+ return ignore.parse(raw);
}
async function main(opts): Promise<void> {
@@ -532,12 +529,12 @@ async function main(opts): Promise<void> {
const ignoreFilepath = opts["ignore-path"];
if (ignoreFilepath && ignoreFilepath !== "disable") {
- const ignorePatterns: string[] =
+ const ignorePatterns =
ignoreFilepath === "auto"
? await autoResolveIgnoreFile()
: await resolveIgnoreFile(ignoreFilepath);
- ignore = ignore.concat(ignorePatterns);
+ ignore = ignore.concat(Array.from(ignorePatterns));
}
const files = getTargetFiles(args.length ? args : ["."], ignore);