1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 17:49:08 -05:00
denoland-deno/tools/lint.js
Bartek Iwańczuk 5874fc3d0a
feat: add support for globs in the config file and CLI arguments for files (#19102)
Follow up to https://github.com/denoland/deno/pull/19084.

This commit adds support for globs in the configuration file as well 
as CLI arguments for files. 

With this change users can now use glob syntax for "include" and 
"exclude" fields, like so:

```json
{
  "lint": {
    "include": [
      "directory/test*.ts",
      "other_dir/"
    ],
    "exclude": [
      "other_dir/foo*.ts",
      "nested/nested2/*"
    ]
  },
  "test": {
    "include": [
      "data/test*.ts",
      "nested/",
      "tests/test[1-9].ts"
    ],
    "exclude": [
      "nested/foo?.ts",
      "nested/nested2/*"
    ]
  }
}
```

Or in CLI args like so:
```
// notice quotes here; these values will be passed to Deno verbatim
// and deno will perform glob expansion

$ deno fmt --ignore="data/*.ts"
$ deno lint "data/**/*.ts"
```

Closes https://github.com/denoland/deno/issues/17971
Closes https://github.com/denoland/deno/issues/6365
2023-05-23 03:39:59 +02:00

168 lines
4.2 KiB
JavaScript
Executable file

#!/usr/bin/env -S deno run --unstable --allow-write --allow-read --allow-run
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import {
buildMode,
getPrebuiltToolPath,
getSources,
join,
ROOT_PATH,
} from "./util.js";
import { checkCopyright } from "./copyright_checker.js";
let didLint = false;
if (Deno.args.includes("--js")) {
await dlint();
await dlintPreferPrimordials();
didLint = true;
}
if (Deno.args.includes("--rs")) {
await clippy();
didLint = true;
}
if (!didLint) {
await Promise.all([
dlint(),
dlintPreferPrimordials(),
checkCopyright(),
clippy(),
]);
}
async function dlint() {
const configFile = join(ROOT_PATH, ".dlint.json");
const execPath = getPrebuiltToolPath("dlint");
const sourceFiles = await getSources(ROOT_PATH, [
"*.js",
"*.ts",
":!:.github/mtime_cache/action.js",
":!:cli/tests/testdata/swc_syntax_error.ts",
":!:cli/tests/testdata/error_008_checkjs.js",
":!:cli/bench/http/node*.js",
":!:cli/bench/testdata/npm/*",
":!:cli/bench/testdata/express-router.js",
":!:cli/bench/testdata/react-dom.js",
":!:cli/compilers/wasm_wrap.js",
":!:cli/tsc/dts/**",
":!:cli/tests/testdata/encoding/**",
":!:cli/tests/testdata/error_syntax.js",
":!:cli/tests/testdata/file_extensions/ts_with_js_extension.js",
":!:cli/tests/testdata/fmt/**",
":!:cli/tests/testdata/npm/**",
":!:cli/tests/testdata/lint/**",
":!:cli/tests/testdata/run/**",
":!:cli/tests/testdata/tsc/**",
":!:cli/tests/testdata/test/glob/**",
":!:cli/tsc/*typescript.js",
":!:cli/tsc/compiler.d.ts",
":!:test_util/wpt/**",
]);
if (!sourceFiles.length) {
return;
}
const chunks = splitToChunks(sourceFiles, `${execPath} run`.length);
const pending = [];
for (const chunk of chunks) {
const cmd = new Deno.Command(execPath, {
cwd: ROOT_PATH,
args: ["run", "--config=" + configFile, ...chunk],
// capture to not conflict with clippy output
stderr: "piped",
});
pending.push(
cmd.output().then(({ stderr, code }) => {
if (code > 0) {
const decoder = new TextDecoder();
console.log("\n------ dlint ------");
console.log(decoder.decode(stderr));
throw new Error("dlint failed");
}
}),
);
}
await Promise.all(pending);
}
// `prefer-primordials` has to apply only to files related to bootstrapping,
// which is different from other lint rules. This is why this dedicated function
// is needed.
async function dlintPreferPrimordials() {
const execPath = getPrebuiltToolPath("dlint");
const sourceFiles = await getSources(ROOT_PATH, [
"runtime/**/*.js",
"ext/**/*.js",
// TODO(petamoriken): enable for node polyfills
// "ext/node/polyfills/*.mjs",
// "ext/node/polyfills/*.ts",
// ":!:ext/node/polyfills/*.d.ts",
"core/*.js",
":!:core/*_test.js",
":!:core/examples/**",
]);
if (!sourceFiles.length) {
return;
}
const chunks = splitToChunks(sourceFiles, `${execPath} run`.length);
for (const chunk of chunks) {
const cmd = new Deno.Command(execPath, {
cwd: ROOT_PATH,
args: ["run", "--rule", "prefer-primordials", ...chunk],
stdout: "inherit",
stderr: "inherit",
});
const { code } = await cmd.output();
if (code > 0) {
throw new Error("prefer-primordials failed");
}
}
}
function splitToChunks(paths, initCmdLen) {
let cmdLen = initCmdLen;
const MAX_COMMAND_LEN = 30000;
const chunks = [[]];
for (const p of paths) {
if (cmdLen + p.length > MAX_COMMAND_LEN) {
chunks.push([p]);
cmdLen = initCmdLen;
} else {
chunks[chunks.length - 1].push(p);
cmdLen += p.length;
}
}
return chunks;
}
async function clippy() {
const currentBuildMode = buildMode();
const cmd = ["clippy", "--all-targets", "--all-features", "--locked"];
if (currentBuildMode != "debug") {
cmd.push("--release");
}
const cargoCmd = new Deno.Command("cargo", {
cwd: ROOT_PATH,
args: [
...cmd,
"--",
"-D",
"warnings",
],
stdout: "inherit",
stderr: "inherit",
});
const { code } = await cargoCmd.output();
if (code > 0) {
throw new Error("clippy failed");
}
}