npm-groovy-lint
Advanced tools
Comparing version 2.2.0-beta.4 to 2.2.0-beta.5
@@ -9,2 +9,3 @@ # Changelog | ||
- New option "source", allowing to call NpmGroovyLint with the groovy code as a string , not only path & files pattern | ||
- Run lint again after fix to get updated error log | ||
@@ -11,0 +12,0 @@ ## [2.0.1] - 2020-02-21 |
@@ -25,3 +25,3 @@ #! /usr/bin/env node | ||
constructor(lintResult, optionsIn) { | ||
this.updatedLintResult = lintResult; | ||
this.updatedLintResult = JSON.parse(JSON.stringify(lintResult)); // Clone object to not compare the initial one | ||
this.options = optionsIn; | ||
@@ -31,3 +31,3 @@ this.verbose = optionsIn.verbose || false; | ||
this.npmGroovyLintRules = this.options.groovyLintRulesOverride ? require(this.options.groovyLintRulesOverride) : npmGroovyLintRules; | ||
if (this.options.fixrules !== "all" && this.options.fixrules !== null) { | ||
if (this.options.fixrules && this.options.fixrules !== "all") { | ||
this.fixRules = this.options.fixrules.split(","); | ||
@@ -57,2 +57,4 @@ } | ||
this.updateResultCounters(); | ||
// Clear progress bar | ||
@@ -268,4 +270,15 @@ this.bar.stop(); | ||
} | ||
// Update result counters | ||
updateResultCounters() { | ||
// Build remaining errors number if a fix has been performed | ||
this.updatedLintResult.summary.totalRemainingErrorNumber = | ||
this.updatedLintResult.summary.totalErrorNumber - this.updatedLintResult.summary.totalFixedErrorNumber; | ||
this.updatedLintResult.summary.totalRemainingWarningNumber = | ||
this.updatedLintResult.summary.totalWarningNumber - this.updatedLintResult.summary.totalFixedWarningNumber; | ||
this.updatedLintResult.summary.totalRemainingInfoNumber = | ||
this.updatedLintResult.summary.totalInfoNumber - this.updatedLintResult.summary.totalFixedInfoNumber; | ||
} | ||
} | ||
module.exports = NpmGroovyLintFix; |
@@ -23,2 +23,3 @@ #! /usr/bin/env node | ||
jdeployRootPath; | ||
parseOptions; | ||
tmpXmlFileName; | ||
@@ -46,3 +47,3 @@ tmpGroovyFileName; | ||
// Construction: initialize options & args | ||
constructor(argsIn, internalOpts = {}) { | ||
constructor(argsIn, internalOpts = { parseOptions: true }) { | ||
if (argsIn) { | ||
@@ -53,2 +54,3 @@ this.args = argsIn; | ||
this.jdeployRootPath = internalOpts.jdeployRootPath || process.env.JDEPLOY_ROOT_PATH || __dirname; | ||
this.parseOptions = internalOpts.parseOptions !== false; | ||
} | ||
@@ -75,11 +77,15 @@ | ||
// Parse options | ||
try { | ||
this.options = optionsDefinition.parse(this.args); | ||
} catch (error) { | ||
this.status = 2; | ||
throw new Error(error.message); | ||
// Parse options ( or force them if coming from lint re-run after fix) | ||
if (this.parseOptions) { | ||
try { | ||
this.options = optionsDefinition.parse(this.args); | ||
} catch (error) { | ||
this.status = 2; | ||
throw new Error(error.message); | ||
} | ||
} else { | ||
this.options = this.args; | ||
} | ||
// Show version (to do more clean) | ||
// Show version (TODO: more clean) | ||
if (this.options.version) { | ||
@@ -244,3 +250,3 @@ console.info("v2.0.0"); | ||
// Parse XML result as js object | ||
await this.parseCodeNarcResult(); | ||
this.lintResult = await this.parseCodeNarcResult(); | ||
// Fix when possible | ||
@@ -256,2 +262,6 @@ if (this.options.fix) { | ||
this.lintResult = this.fixer.updatedLintResult; | ||
// If there has been fixes, call CodeNarc again to get updated error list | ||
if (this.fixer.fixedErrorsNumber > 0) { | ||
await this.lintAgainAfterFix(); | ||
} | ||
} | ||
@@ -267,2 +277,3 @@ // Output result | ||
} | ||
// Parse XML result file as js object | ||
@@ -329,6 +340,58 @@ async parseCodeNarcResult() { | ||
result.files = files; | ||
this.lintResult = result; | ||
fse.removeSync(this.tmpXmlFileName); // Remove temporary file | ||
await fse.remove(this.tmpXmlFileName); // Remove temporary file | ||
return result; | ||
} | ||
// Lint again after fixes and merge in existing results | ||
async lintAgainAfterFix() { | ||
// same Options except fix = false & output = none | ||
const lintAgainOptions = JSON.parse(JSON.stringify(this.options)); | ||
if (this.options.source) { | ||
lintAgainOptions.source = this.lintResult.files[0].updatedSource; | ||
} | ||
lintAgainOptions.fix = false; | ||
lintAgainOptions.output = "none"; | ||
const newLinter = new NpmGroovyLint(lintAgainOptions, { | ||
parseOptions: false, | ||
jdeployFile: this.jdeployFile, | ||
jdeployRootPath: this.jdeployRootPath | ||
}); | ||
// Run linter | ||
await newLinter.run(); | ||
const newLintResult = newLinter.lintResult; | ||
// Merge new linter results in existing results | ||
this.lintResult = this.mergeResults(this.lintResult, newLintResult); | ||
} | ||
// Merge --fix results and following lint results | ||
mergeResults(initialResults, afterFixResults) { | ||
const updatedResults = JSON.parse(JSON.stringify(initialResults)); | ||
// Reset properties and update counters | ||
updatedResults.files = {}; | ||
updatedResults.summary.totalErrorNumber = afterFixResults.summary.totalErrorNumber; | ||
updatedResults.summary.totalWarningNumber = afterFixResults.summary.totalWarningNumber; | ||
updatedResults.summary.totalInfoNumber = afterFixResults.summary.totalInfoNumber; | ||
updatedResults.summary.totalFixedErrorNumber = initialResults.summary.totalFixedErrorNumber; | ||
updatedResults.summary.totalFixedWarningNumber = initialResults.summary.totalFixedWarningNumber; | ||
updatedResults.summary.totalFixedInfoNumber = initialResults.summary.totalFixedInfoNumber; | ||
// Remove not fixed errors from initial result and add remaining errors of afterfixResults | ||
for (const fileNm of Object.keys(initialResults.files)) { | ||
const initialResfileErrors = initialResults.files[fileNm].errors; | ||
const afterFixResfileErrors = afterFixResults.files[fileNm].errors; | ||
const fileDtl = { | ||
errors: afterFixResfileErrors, | ||
updatedSource: initialResults.files[fileNm].updatedSource | ||
}; | ||
for (const initialFileError of initialResfileErrors) { | ||
if (initialFileError.fixed) { | ||
fileDtl.errors.push(initialFileError); | ||
} | ||
} | ||
updatedResults.files[fileNm] = fileDtl; | ||
} | ||
return updatedResults; | ||
} | ||
// Reformat output if requested in command line | ||
@@ -385,3 +448,3 @@ async processNglOutput() { | ||
"Total fixed": this.lintResult.summary.totalFixedErrorNumber, | ||
"Total remaining": this.lintResult.summary.totalErrorNumber - this.lintResult.summary.totalFixedErrorNumber | ||
"Total remaining": this.lintResult.summary.totalRemainingErrorNumber | ||
}; | ||
@@ -392,3 +455,3 @@ const warningTableLine = { | ||
"Total fixed": this.lintResult.summary.totalFixedWarningNumber, | ||
"Total remaining": this.lintResult.summary.totalWarningNumber - this.lintResult.summary.totalFixedWarningNumber | ||
"Total remaining": this.lintResult.summary.totalRemainingWarningNumber | ||
}; | ||
@@ -399,3 +462,3 @@ const infoTableLine = { | ||
"Total fixed": this.lintResult.summary.totalFixedInfoNumber, | ||
"Total remaining": this.lintResult.summary.totalInfoNumber - this.lintResult.summary.totalFixedInfoNumber | ||
"Total remaining": this.lintResult.summary.totalRemainingInfoNumber | ||
}; | ||
@@ -402,0 +465,0 @@ const summaryTable = [errorTableLine, warningTableLine, infoTableLine]; |
@@ -95,2 +95,3 @@ /** | ||
default: "all", | ||
dependsOn: ["fix"], | ||
description: "List of rule identifiers to fix (if not specified, all available fixes will be applied)" | ||
@@ -102,3 +103,3 @@ }, | ||
{ | ||
option: "ignore-pattern", | ||
option: "ignorepattern", | ||
alias: "i", | ||
@@ -145,4 +146,5 @@ type: "String", | ||
[["path", "files"], "source"], | ||
["failonerror", "failonwarning", "failoninfo"] | ||
["failonerror", "failonwarning", "failoninfo"], | ||
["codenarcargs", ["failonerror", "failonwarning", "failoninfo", "path", "files", "source", "fix", "fixrules"]] | ||
] | ||
}); |
{ | ||
"name": "npm-groovy-lint", | ||
"version": "2.2.0-beta.4", | ||
"version": "2.2.0-beta.5", | ||
"description": "NPM CodeNarc wrapper to easily lint Groovy files", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -45,3 +45,3 @@ # NPM GROOVY LINT (and FIX !) | ||
| --fix | Boolean | (Experimental) Automatically fix problems when possible<br/> See [Autofixable rules](#Autofixable-rules) | | ||
| -i<br/> --ignore-pattern | String | Comma-separated list of Ant-style file patterns specifying files that must be ignored<br/> Default: none<br/> Example: `"**/test/*""` | | ||
| -i<br/> --ignorepattern | String | Comma-separated list of Ant-style file patterns specifying files that must be ignored<br/> Default: none<br/> Example: `"**/test/*""` | | ||
| --failonerror | Boolean | Fails if at least one error is found | | ||
@@ -48,0 +48,0 @@ | --failonwarning | Boolean | Fails if at least one warning is found | |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15906270
1428