Skip to content

Commit

Permalink
fix(deno-lint): Support directories to scan on the command line
Browse files Browse the repository at this point in the history
Allow limiting the scan to selected directories. If no directory
is provided, the current directory will be scanned, as it was.

BREAKING CHANGE: If you passed a single directory to denolint to
specify the project root, it will not work any more. You can pass
the root directory using the argument -r,--root. However, it was
used only if neither .denolintignore nor .eslintignore were found
and was passed instead of either of them to the walker. Because
the root directory was no file path, I think that it was useless.
Nevertheless, I implemented the previous functionality using the
argument -r,--root, which might be worth for other purposes later.
  • Loading branch information
prantlf committed Sep 25, 2022
1 parent d6cf2b6 commit 9fc221b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
8 changes: 7 additions & 1 deletion packages/deno-lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ Emit nothing even if there were errors happened.

### usage

`npx denolint`
`npx denolint [options] [dirs...]`

The current directory will be considered the project root and scanned by default.

### `--root`, `-r`

Root directory of the project, which contains files `.denolintignore` or `.eslintignore`. Defaults to the current directory.

### `--config`, `-c`

Expand Down
8 changes: 5 additions & 3 deletions packages/deno-lint/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import { Cli, Command, Option } from 'clipanion'

class LintCommand extends Command {
static usage = {
description: 'deno lint [options] [path]',
description: 'denolint [options] [dirs...]',
}

private readonly cwd = Option.String({ required: false })
private readonly dirs: string[] = Option.Rest()

private readonly rootDir = Option.String('-r,--root', { required: false })

private readonly configPath = Option.String('-c,--config', { required: false })

private readonly checkOnly = Option.Boolean('--check-only', { required: false })

async execute() {
const hasError = denolint(this.cwd ?? __dirname, this.configPath ?? '.denolint.json')
const hasError = denolint(this.rootDir ?? __dirname, this.configPath ?? '.denolint.json', this.dirs)
return Promise.resolve(hasError && !this.checkOnly ? 1 : 0)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/deno-lint/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export function lint(
sourceCode: string | Buffer,
allRules?: boolean | undefined | null,
): Array<string>
export function denolint(dirname: string, configPath: string): boolean
export function denolint(dirname: string, configPath: string, scanDirs?: Array<string> | undefined | null): boolean
17 changes: 15 additions & 2 deletions packages/deno-lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ fn lint(
}

#[napi]
fn denolint(__dirname: String, config_path: String) -> Result<bool> {
fn denolint(
__dirname: String,
config_path: String,
scan_dirs: Option<Vec<String>>,
) -> Result<bool> {
let mut has_error = false;
let cwd = env::current_dir().map_err(|e| {
Error::new(
Expand Down Expand Up @@ -133,11 +137,20 @@ fn denolint(__dirname: String, config_path: String) -> Result<bool> {
Err(_) => __dirname.as_str(),
},
};
let mut dir_walker = WalkBuilder::new(cwd);
let dirs = scan_dirs.unwrap_or_default();
let root = if dirs.is_empty() {
cwd.as_path()
} else {
Path::new(&dirs[0])
};
let mut dir_walker = WalkBuilder::new(root);
dir_walker
.add_custom_ignore_filename(ignore_file_path)
.types(types)
.follow_links(true);
for i in dirs.into_iter().skip(1) {
dir_walker.add(i);
}
for i in cfg_ignore_files {
dir_walker.add_ignore(i);
}
Expand Down

0 comments on commit 9fc221b

Please sign in to comment.