Add ability to ignore pattern when formatting (#435)

This commit is contained in:
Timothee Guerin 2022-04-11 13:29:06 -07:00 коммит произвёл GitHub
Родитель 57bb55be38
Коммит 19e203dcca
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 63 добавлений и 42 удалений

Просмотреть файл

@ -151,12 +151,17 @@ equivalent to passing `main.cadl` in that directory.
### Formatting Cadl files
Cadl provides an auto-formatter to keep your specs clean and organized.
`node_modules` folders are automatically excluded by default
```bash
cadl format <patterns...>
# Format all the files in the current directory with the cadl extension.
cadl format **/*.cadl
# Exclude certain patterns. Either use `!` prefix or pass it via the `--exclude` or `-x` option.
cadl format **/*.cadl "!mytestfolder/**/*"
cadl format **/*.cadl --exclude "mytestfolder/**/*"
```
### Installing VS Code Extension

Просмотреть файл

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cadl-lang/compiler",
"comment": "Formatter has ability to ignore patterns",
"type": "minor"
}
],
"packageName": "@cadl-lang/compiler"
}

27
common/config/rush/pnpm-lock.yaml сгенерированный
Просмотреть файл

@ -19,7 +19,6 @@ dependencies:
'@rush-temp/versioning': file:projects/versioning.tgz
'@rushstack/eslint-patch': 1.1.0
'@types/debounce': 1.2.1
'@types/glob': 7.1.4
'@types/js-yaml': 4.0.5
'@types/lz-string': 1.3.34
'@types/mkdirp': 1.0.2
@ -43,7 +42,7 @@ dependencies:
eslint: 8.13.0
eslint-config-prettier: 8.5.0_eslint@8.13.0
eslint-plugin-prettier: 4.0.0_1815ac95b7fb26c13c7d48a8eef62d0f
glob: 7.1.7
globby: 13.1.1
grammarkdown: 3.1.2
js-yaml: 4.1.0
lzutf8: 0.6.1
@ -2880,6 +2879,18 @@ packages:
node: '>=10'
resolution:
integrity: sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==
/globby/13.1.1:
dependencies:
dir-glob: 3.0.1
fast-glob: 3.2.11
ignore: 5.2.0
merge2: 1.4.1
slash: 4.0.0
dev: false
engines:
node: ^12.20.0 || ^14.13.1 || >=16.0.0
resolution:
integrity: sha512-XMzoDZbGZ37tufiv7g0N4F/zp3zkwdFtVbV3EHsVl1KQr4RPLfNoT068/97RPshz2J5xYNEjLKKBKaGHifBd3Q==
/graceful-fs/4.2.10:
dev: false
resolution:
@ -4453,6 +4464,12 @@ packages:
node: '>=8'
resolution:
integrity: sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==
/slash/4.0.0:
dev: false
engines:
node: '>=12'
resolution:
integrity: sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew==
/slice-ansi/4.0.0:
dependencies:
ansi-styles: 4.3.0
@ -5285,6 +5302,7 @@ packages:
change-case: 4.1.2
eslint: 8.13.0
glob: 7.1.7
globby: 13.1.1
grammarkdown: 3.1.2
js-yaml: 4.1.0
mkdirp: 1.0.4
@ -5304,7 +5322,7 @@ packages:
dev: false
name: '@rush-temp/compiler'
resolution:
integrity: sha512-5x9UYejMVYUH4jAb0ZJSG3wcmXnFsGw3f+wmsdj932tlMnqd5u71RycVU81TkmIiPcDcMPTjSKj32KX2c7Jz5A==
integrity: sha512-aHffENEXBx7ax7e9VjM3Qa1VmblOahXjhpM/aMBTNiB6FTkT6E+7s5QH/aGzPt5I35ao6WXUdw3wSktCxLNNNA==
tarball: file:projects/compiler.tgz
version: 0.0.0
file:projects/eslint-config-cadl.tgz_prettier@2.6.2:
@ -5486,7 +5504,6 @@ specifiers:
'@rush-temp/versioning': file:./projects/versioning.tgz
'@rushstack/eslint-patch': '1.1.0 '
'@types/debounce': ~1.2.1
'@types/glob': ~7.1.3
'@types/js-yaml': ~4.0.1
'@types/lz-string': ~1.3.34
'@types/mkdirp': ~1.0.1
@ -5510,7 +5527,7 @@ specifiers:
eslint: ^8.12.0
eslint-config-prettier: ^8.5.0
eslint-plugin-prettier: ^4.0.0
glob: ~7.1.6
globby: ~13.1.1
grammarkdown: ~3.1.2
js-yaml: ~4.1.0
lzutf8: ~0.6.1

Просмотреть файл

@ -147,6 +147,12 @@ async function main() {
array: true,
demandOption: true,
})
.option("exclude", {
alias: "x",
type: "string",
array: true,
describe: "Pattern to exclude",
})
.option("check", {
alias: "c",
type: "boolean",
@ -156,6 +162,7 @@ async function main() {
async (args) => {
if (args["check"]) {
const unformatted = await findUnformattedCadlFiles(args["include"], {
exclude: args["exclude"],
debug: args.debug,
});
if (unformatted.length > 0) {
@ -166,7 +173,7 @@ async function main() {
process.exit(1);
}
} else {
await formatCadlFiles(args["include"], { debug: args.debug });
await formatCadlFiles(args["include"], { exclude: args["exclude"], debug: args.debug });
}
}
)

Просмотреть файл

@ -1,5 +1,5 @@
import { readFile, writeFile } from "fs/promises";
import glob from "glob";
import { globby } from "globby";
import prettier from "prettier";
import * as cadlPrettierPlugin from "../formatter/index.js";
import { PrettierParserError } from "../formatter/parser.js";
@ -29,12 +29,17 @@ export async function checkFormatCadl(
});
}
export interface CadlFormatOptions {
exclude?: string[];
debug?: boolean;
}
/**
* Format all the Cadl files.
* @param patterns List of wildcard pattern searching for Cadl files.
*/
export async function formatCadlFiles(patterns: string[], { debug }: { debug?: boolean }) {
const files = await findFiles(patterns);
export async function formatCadlFiles(patterns: string[], { exclude, debug }: CadlFormatOptions) {
const files = await findFiles(patterns, exclude);
for (const file of files) {
try {
await formatCadlFile(file);
@ -56,9 +61,9 @@ export async function formatCadlFiles(patterns: string[], { debug }: { debug?: b
*/
export async function findUnformattedCadlFiles(
patterns: string[],
{ debug }: { debug?: boolean }
{ exclude, debug }: CadlFormatOptions
): Promise<string[]> {
const files = await findFiles(patterns);
const files = await findFiles(patterns, exclude);
const unformatted = [];
for (const file of files) {
try {
@ -96,18 +101,7 @@ export async function checkFormatCadlFile(filename: string): Promise<boolean> {
return await checkFormatCadl(content.toString(), prettierConfig ?? {});
}
async function findFilesFromPattern(pattern: string): Promise<string[]> {
return new Promise((resolve, reject) => {
glob(pattern, (err, matches) => {
if (err) {
reject(err);
}
resolve(matches);
});
});
}
async function findFiles(include: string[]): Promise<string[]> {
const result = await Promise.all(include.map((path) => findFilesFromPattern(path)));
return result.flat();
async function findFiles(include: string[], ignore: string[] = []): Promise<string[]> {
const patterns = [...include, "!**/node_modules", ...ignore.map((x) => `!${x}`)];
return globby(patterns);
}

Просмотреть файл

@ -54,7 +54,7 @@
},
"dependencies": {
"ajv": "~8.9.0",
"glob": "~7.1.6",
"globby": "~13.1.1",
"js-yaml": "~4.1.0",
"mkdirp": "~1.0.4",
"mustache": "~4.2.0",
@ -68,7 +68,6 @@
"change-case": "~4.1.2"
},
"devDependencies": {
"@types/glob": "~7.1.3",
"@types/js-yaml": "~4.0.1",
"@types/mkdirp": "~1.0.1",
"@types/mocha": "~9.1.0",

Просмотреть файл

@ -1,6 +1,6 @@
import assert from "assert";
import { readFile } from "fs/promises";
import glob from "glob";
import { globby } from "globby";
import { fileURLToPath, pathToFileURL } from "url";
import { createSourceFile, logDiagnostics, logVerboseTestOutput } from "../core/diagnostics.js";
import { NodeHost } from "../core/node-host.js";
@ -287,19 +287,8 @@ async function createTestHostInternal(): Promise<TestHost> {
}
export async function findFilesFromPattern(directory: string, pattern: string): Promise<string[]> {
return new Promise((resolve, reject) => {
glob(
pattern,
{
cwd: directory,
nodir: true,
},
(err, matches) => {
if (err) {
reject(err);
}
resolve(matches);
}
);
return globby(pattern, {
cwd: directory,
onlyFiles: true,
});
}