-
Notifications
You must be signed in to change notification settings - Fork 885
Add 'dryRun' option to Linter #3371
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ class Linter { | |
|
||
private failures: RuleFailure[] = []; | ||
private fixes: RuleFailure[] = []; | ||
private fixedSources: Record<string, string> = {}; | ||
|
||
/** | ||
* Creates a TypeScript program object from a tsconfig.json file path and optional project directory. | ||
|
@@ -137,6 +138,7 @@ class Linter { | |
return { | ||
errorCount, | ||
failures: this.failures, | ||
fixedSources: this.fixedSources, | ||
fixes: this.fixes, | ||
format: formatterName, | ||
output, | ||
|
@@ -163,6 +165,7 @@ class Linter { | |
const fixableFailures = updatedFailures.filter((f) => f.hasFix()); | ||
this.fixes = this.fixes.concat(fixableFailures); | ||
source = this.applyFixes(sourceFileName, source, fixableFailures); | ||
this.fixedSources[sourceFileName] = source; | ||
sourceFile = this.getSourceFile(sourceFileName, source); | ||
} | ||
} | ||
|
@@ -176,15 +179,17 @@ class Linter { | |
protected applyFixes(sourceFilePath: string, source: string, fixableFailures: RuleFailure[]): string { | ||
const fixesByFile = createMultiMap(fixableFailures, (f) => [f.getFileName(), f.getFix()!]); | ||
fixesByFile.forEach((fileFixes, filePath) => { | ||
let fileNewSource: string; | ||
let fileNewSource: string | undefined; | ||
if (path.resolve(filePath) === path.resolve(sourceFilePath)) { | ||
source = Replacement.applyFixes(source, fileFixes); | ||
fileNewSource = source; | ||
} else { | ||
} else if (!this.options.dryRun) { | ||
const oldSource = fs.readFileSync(filePath, "utf-8"); | ||
fileNewSource = Replacement.applyFixes(oldSource, fileFixes); | ||
} | ||
fs.writeFileSync(filePath, fileNewSource); | ||
if (!this.options.dryRun && typeof fileNewSource === "string") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this basically reverts #2864 when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updateProgram reads the file from disk. With dryRun it is never updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, wow. That's unfortunate. Know any way around that? I think using |
||
fs.writeFileSync(filePath, fileNewSource); | ||
} | ||
this.updateProgram(filePath); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,11 @@ export interface Options { | |
*/ | ||
config?: string; | ||
|
||
/** | ||
* When running with fix: true, do not write files to disk. | ||
*/ | ||
dryRun?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only relevant for the CLI. And this doesn't make sense as CLI option |
||
|
||
/** | ||
* Exclude globs from path expansion. | ||
*/ | ||
|
@@ -211,6 +216,7 @@ async function doLinting( | |
const possibleConfigAbsolutePath = options.config !== undefined ? path.resolve(options.config) : null; | ||
const linter = new Linter( | ||
{ | ||
dryRun: !!options.dryRun, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should always be false when running from CLI |
||
fix: !!options.fix, | ||
formatter: options.format, | ||
formattersDirectory: options.formattersDirectory, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer
Map