mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 21:50:00 -05:00
feat: add ignore parser for std/prettier (#3399)
This commit is contained in:
parent
c016684653
commit
2a348144c6
3 changed files with 103 additions and 10 deletions
15
std/prettier/ignore.ts
Normal file
15
std/prettier/ignore.ts
Normal file
|
@ -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);
|
||||
}
|
81
std/prettier/ignore_test.ts
Normal file
81
std/prettier/ignore_test.ts
Normal file
|
@ -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);
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue