npm-groovy-lint
Advanced tools
Comparing version 2.0.0-beta.2 to 2.0.0-beta3
#! /usr/bin/env node | ||
// Imports | ||
//const util = require("util"); | ||
const fse = require("fs-extra"); | ||
const cliProgress = require("cli-progress"); | ||
//const os = require("os"); | ||
//const xml2js = require("xml2js"); | ||
const { npmGroovyLintRules, npmGroovyLintGlobalReplacements } = require("./groovy-lint-rules.js"); | ||
const decodeHtml = require("decode-html"); | ||
const { npmGroovyLintRules } = require("./groovy-lint-rules.js"); | ||
@@ -69,3 +67,3 @@ class NpmGroovyLintFix { | ||
for (const err of fileErrors) { | ||
if (this.npmGroovyLintRules[err.rule] != null && this.npmGroovyLintRules[err.rule].fixes != null) { | ||
if (this.npmGroovyLintRules[err.rule] != null && this.npmGroovyLintRules[err.rule].fix != null) { | ||
const fixableError = { | ||
@@ -81,10 +79,13 @@ id: err.id, | ||
} | ||
// Sort errors putting scope = file at last | ||
// Sort errors putting file scope at last, and sorter file scopes by ascending priority | ||
this.fixableErrors[fileNm].sort((a, b) => { | ||
if (a.rule.scope && a.rule.scope === "file" && b.rule.scope == null) { | ||
return -1; | ||
} else if (b.rule.scope && b.rule.scope === "file" && a.rule.scope == null) { | ||
return 1; | ||
} | ||
return 0; | ||
return a.rule.scope && a.rule.scope === "file" && b.rule.scope == null | ||
? 1 | ||
: b.rule.scope && b.rule.scope === "file" && a.rule.scope == null | ||
? -1 | ||
: a.rule.scope && a.rule.scope === "file" && b.rule.scope === "file" && a.rule.priority > b.rule.priority | ||
? 1 | ||
: a.rule.scope && a.rule.scope === "file" && b.rule.scope === "file" && a.rule.priority < b.rule.priority | ||
? -1 | ||
: 0; | ||
}); | ||
@@ -106,6 +107,7 @@ } | ||
for (const fileFixableError of this.fixableErrors[fileNm]) { | ||
const lineNb = parseInt(fileFixableError.lineNb, 10) - 1; | ||
// File scope violation | ||
if (fileFixableError.rule.scope === "file") { | ||
const fileLinesNew = this.applyFixRule(fileLines, fileFixableError); | ||
if (fileLinesNew.toString() !== fileLines.toString()) { | ||
const fileLinesNew = this.applyFixRule(fileLines, lineNb, fileFixableError).slice(); // copy result lines | ||
if (JSON.stringify(fileLinesNew) !== JSON.stringify(fileLines.toString)) { | ||
fileLines = fileLinesNew; | ||
@@ -119,5 +121,4 @@ fixedInFileNb = fixedInFileNb + 1; | ||
else { | ||
const lineNb = parseInt(fileFixableError.lineNb, 10) - 1; | ||
const line = fileLines[lineNb]; | ||
const fixedLine = this.applyFixRule(line, fileFixableError); | ||
const fixedLine = this.applyFixRule(line, lineNb, fileFixableError); | ||
if (fixedLine !== line) { | ||
@@ -140,3 +141,3 @@ fileLines[lineNb] = fixedLine; | ||
// Evaluate variables and apply rule defined fixes | ||
applyFixRule(line, fixableError) { | ||
applyFixRule(line, lineNb, fixableError) { | ||
// Evaluate variables from message | ||
@@ -150,3 +151,3 @@ const evaluatedVars = []; | ||
const regexPos = varDef.regexPos || 1; | ||
evaluatedVars.push({ name: varDef.name, value: regexRes[regexPos] }); | ||
evaluatedVars.push({ name: varDef.name, value: decodeHtml(regexRes[regexPos]) }); | ||
} | ||
@@ -157,39 +158,29 @@ } else if (varDef.value) { | ||
} | ||
evaluatedVars.push({ name: "lineNb", value: lineNb }); | ||
// Apply replacement | ||
// Apply fix : replacement or custom function | ||
let newLine = line; | ||
for (const fix of fixableError.rule.fixes) { | ||
// Replace String | ||
if (fix.type === "replaceString") { | ||
// Replace {{VARNAME}} by real variables | ||
const strBefore = this.setVariablesValues(fix.before, evaluatedVars.concat(npmGroovyLintGlobalReplacements)); | ||
const strAfter = this.setVariablesValues(fix.after, evaluatedVars.concat(npmGroovyLintGlobalReplacements)); | ||
// Process replacement with evualuated expressions (except if issue in evaluated expression) | ||
if (!strBefore.includes("{{") && !strAfter.includes("{{")) { | ||
newLine = newLine.replace(strBefore, strAfter); | ||
} else if (this.options.verbose) { | ||
console.debug("NGL: missing replacement variable(s):\n" + strBefore + "\n" + strAfter + "\n"); | ||
} | ||
const fix = fixableError.rule.fix; | ||
// Replace String | ||
if (fix.type === "replaceString") { | ||
// Replace {{VARNAME}} by real variables | ||
const strBefore = this.setVariablesValues(fix.before, evaluatedVars); | ||
const strAfter = this.setVariablesValues(fix.after, evaluatedVars); | ||
// Process replacement with evualuated expressions (except if issue in evaluated expression) | ||
if (!strBefore.includes("{{") && !strAfter.includes("{{")) { | ||
newLine = newLine.replace(strBefore, strAfter); | ||
} else if (this.options.verbose) { | ||
console.debug("NGL: missing replacement variable(s):\n" + strBefore + "\n" + strAfter + "\n"); | ||
} | ||
// Replace regex | ||
else if (fix.type === "replaceRegex") { | ||
// Replace {{VARNAME}} by real variables | ||
const regexBefore = this.setVariablesValues(fix.before, evaluatedVars.concat(npmGroovyLintGlobalReplacements)); | ||
const strAfter = this.setVariablesValues(fix.after, evaluatedVars.concat(npmGroovyLintGlobalReplacements)); | ||
if (!regexBefore.includes("{{") && !strAfter.includes("{{")) { | ||
newLine = newLine.replace(new RegExp(regexBefore, "g"), strAfter); | ||
} | ||
// Function defined in rule | ||
else if (fix.type === "function") { | ||
try { | ||
newLine = fix.func(newLine, evaluatedVars); | ||
} catch (e) { | ||
if (this.options.verbose) { | ||
console.error("NGL: Function error: " + e.message + " / " + JSON.stringify(fixableError)); | ||
} | ||
} | ||
// Function defined in rule | ||
else if (fix.type === "function") { | ||
try { | ||
newLine = fix.func(newLine, evaluatedVars); | ||
} catch (e) { | ||
if (this.options.verbose) { | ||
console.error("NGL: Function error: " + e.message + " / " + JSON.stringify(fixableError)); | ||
} | ||
} | ||
} | ||
} | ||
return newLine; | ||
@@ -196,0 +187,0 @@ } |
// List fixable CodeNarc rules | ||
"use strict"; | ||
const decodeHtml = require("decode-html"); | ||
const npmGroovyLintRules = { | ||
@@ -8,23 +10,40 @@ // Consecutive blank lines | ||
scope: "file", | ||
fixes: [ | ||
{ | ||
type: "function", | ||
func: fileLines => { | ||
const newFileLines = []; | ||
for (const line of fileLines) { | ||
if (line.trim() === "") { | ||
// Check if previous line is empty: if not, add empty line | ||
if (!(newFileLines.length > 0 && newFileLines[newFileLines.length - 1].trim() === "")) { | ||
newFileLines.push(""); | ||
} | ||
} else { | ||
newFileLines.push(line); | ||
priority: 999, | ||
fix: { | ||
type: "function", | ||
func: fileLines => { | ||
const newFileLines = []; | ||
for (const line of fileLines) { | ||
if (line.trim() === "") { | ||
// Check if previous line is empty: if not, add empty line | ||
if (!(newFileLines.length > 0 && newFileLines[newFileLines.length - 1].trim() === "")) { | ||
newFileLines.push(""); | ||
} | ||
} else { | ||
newFileLines.push(line); | ||
} | ||
return newFileLines; | ||
} | ||
return newFileLines; | ||
} | ||
] | ||
} | ||
}, | ||
/* nvuillam: Not working, especially when embedded missing If statements ... | ||
let's let people correct that manually for now :) | ||
// Missing if braces | ||
IfStatementBraces: { | ||
scope: "file", | ||
priority: 1, | ||
fix: { | ||
type: "function", | ||
func: (fileLines, variables) => { | ||
const lineNumber = getVariable(variables, 'lineNb', { mandatory: true }); | ||
fileLines[lineNumber - 1] = fileLines[lineNumber - 1] + ' {'; | ||
fileLines[lineNumber] = fileLines[lineNumber] + ' }'; | ||
return fileLines; | ||
} | ||
} | ||
}, | ||
*/ | ||
// Indentation | ||
@@ -44,29 +63,41 @@ Indentation: { | ||
], | ||
fixes: [ | ||
{ | ||
type: "function", | ||
func: (newLine, evaluatedVars) => { | ||
const expectedIndent = parseInt(getVariable(evaluatedVars, "EXPECTED", { mandatory: true }), 10); | ||
const foundIndent = parseInt(getVariable(evaluatedVars, "FOUND", { mandatory: true })); | ||
if (newLine.trim() === "}") { | ||
// Manage Wrong info frrom codeNarc :/ { | ||
newLine = newLine.replace(" ".repeat(foundIndent - 1), " ".repeat((expectedIndent - 1) * 2)); | ||
} else { | ||
newLine = newLine.replace(" ".repeat(foundIndent - 1), " ".repeat(expectedIndent - 1)); | ||
} | ||
return newLine.trimEnd(); | ||
fix: { | ||
type: "function", | ||
func: (line, evaluatedVars) => { | ||
const expectedIndent = parseInt(getVariable(evaluatedVars, "EXPECTED", { mandatory: true }), 10); | ||
const foundIndent = parseInt(getVariable(evaluatedVars, "FOUND", { mandatory: true })); | ||
if (line.trim() === "}") { | ||
// Manage Wrong info from codeNarc :/ { | ||
line = line.replace(" ".repeat(foundIndent - 1), " ".repeat((expectedIndent - 1) * 2)); | ||
} else { | ||
line = line.replace(" ".repeat(foundIndent - 1), " ".repeat(expectedIndent - 1)); | ||
} | ||
return line; | ||
} | ||
] | ||
} | ||
}, | ||
// No tab character | ||
NoTabCharacter: { | ||
scope: "file", | ||
priority: 2, | ||
fix: { | ||
type: "function", | ||
func: fileLines => { | ||
const newFileLines = []; | ||
for (const line of fileLines) { | ||
newFileLines.push(line.replace("\t", "")); | ||
} | ||
return newFileLines; | ||
} | ||
} | ||
}, | ||
// Space after catch | ||
SpaceAfterCatch: { | ||
fixes: [ | ||
{ | ||
type: "replaceString", | ||
before: "){", | ||
after: ") {" | ||
} | ||
] | ||
fix: { | ||
type: "replaceString", | ||
before: "){", | ||
after: ") {" | ||
} | ||
}, | ||
@@ -76,9 +107,12 @@ | ||
SpaceAfterOpeningBrace: { | ||
fixes: [ | ||
{ | ||
type: "replaceString", | ||
before: "{}", | ||
after: "{ }" | ||
fix: { | ||
type: "function", | ||
func: line => { | ||
const regexMatch = line.match(new RegExp(/{[^ ]/, "g")); | ||
if (regexMatch && regexMatch[0]) { | ||
line = line.replace(regexMatch[0], "{ " + regexMatch[0][1]); | ||
} | ||
return line; | ||
} | ||
] | ||
} | ||
}, | ||
@@ -91,23 +125,36 @@ | ||
name: "OPERATOR", | ||
regex: /The operator "(.*)" within class (.*) is not preceded by a space or whitespace/ | ||
regex: /The operator "(.*)" within class (.*) is not (.*) by a space or whitespace/ | ||
} | ||
], | ||
fixes: [ | ||
{ | ||
type: "function", | ||
func: (newLine, evaluatedVars) => { | ||
const operator = getVariable(evaluatedVars, "OPERATOR"); | ||
let pos = 0; | ||
const newArray = newLine.split(operator).map(str => { | ||
pos++; | ||
if (pos === 1) { | ||
return str.trimEnd(); | ||
} else { | ||
return str.trim(); | ||
} | ||
}); | ||
return newArray.join(" " + operator + " "); | ||
fix: { | ||
type: "function", | ||
func: (line, evaluatedVars) => { | ||
let operator = getVariable(evaluatedVars, "OPERATOR", { mandatory: true, htmlToString: true }); | ||
return addSpaceAroundChar(line, operator); | ||
} | ||
} | ||
}, | ||
// Add space after a comma | ||
SpaceAfterComma: { | ||
fix: { | ||
type: "function", | ||
func: line => { | ||
return addSpaceAroundChar(line, ","); | ||
} | ||
} | ||
}, | ||
// Space before opening brace | ||
SpaceBeforeOpeningBrace: { | ||
fix: { | ||
type: "function", | ||
func: line => { | ||
const regexMatch = line.match(new RegExp(/[^ ]{/, "g")); | ||
if (regexMatch && regexMatch[0]) { | ||
line = line.replace(regexMatch[0], regexMatch[0][0] + " {"); | ||
} | ||
return line; | ||
} | ||
] | ||
} | ||
}, | ||
@@ -117,9 +164,7 @@ | ||
UnnecessaryDefInFieldDeclaration: { | ||
fixes: [ | ||
{ | ||
type: "replaceString", | ||
before: "static def ", | ||
after: "static " | ||
} | ||
] | ||
fix: { | ||
type: "replaceString", | ||
before: "static def ", | ||
after: "static " | ||
} | ||
}, | ||
@@ -135,9 +180,7 @@ | ||
], | ||
fixes: [ | ||
{ | ||
type: "replaceString", | ||
before: "{{DOUBLE_QUOTE}}{{STRING}}{{DOUBLE_QUOTE}}", | ||
after: "{{SINGLE_QUOTE}}{{STRING}}{{SINGLE_QUOTE}}" | ||
} | ||
] | ||
fix: { | ||
type: "replaceString", | ||
before: '"{{STRING}}"', | ||
after: "'{{STRING}}'" | ||
} | ||
}, | ||
@@ -147,9 +190,7 @@ | ||
UnnecessaryPublicModifier: { | ||
fixes: [ | ||
{ | ||
type: "replaceString", | ||
before: "public ", | ||
after: "" | ||
} | ||
] | ||
fix: { | ||
type: "replaceString", | ||
before: "public ", | ||
after: "" | ||
} | ||
}, | ||
@@ -159,14 +200,13 @@ | ||
UnnecessarySemicolon: { | ||
fixes: [ | ||
{ | ||
type: "function", | ||
func: newLine => { | ||
newLine = newLine.trimEnd(); | ||
if (newLine.lastIndexOf(";") === newLine.length - 1) { | ||
newLine = newLine.substring(0, newLine.length - 1).trimEnd(); | ||
} | ||
return newLine; | ||
fix: { | ||
type: "function", | ||
func: line => { | ||
const pos = line.lastIndexOf(";"); | ||
if (pos === line.length - 1) { | ||
return line.slice(0, -1).trimEnd(); | ||
} else { | ||
return (line.slice(0, pos) + line.slice(pos + 1)).trimEnd(); | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
@@ -176,22 +216,15 @@ | ||
TrailingWhitespace: { | ||
fixes: [ | ||
{ | ||
type: "function", | ||
func: newLine => { | ||
return newLine.trimEnd(); | ||
} | ||
fix: { | ||
type: "function", | ||
func: line => { | ||
return line.trimEnd(); | ||
} | ||
] | ||
} | ||
} | ||
}; | ||
const npmGroovyLintGlobalReplacements = [ | ||
{ name: "DOUBLE_QUOTE", value: '"' }, | ||
{ name: "SINGLE_QUOTE", value: "'" } | ||
]; | ||
function getVariable(evaluatedVars, name, optns = { mandatory: false }) { | ||
function getVariable(evaluatedVars, name, optns = { mandatory: false, decodeHtml: false }) { | ||
const matchingVars = evaluatedVars.filter(evaluatedVar => evaluatedVar.name === name); | ||
if (matchingVars && matchingVars.length > 0) { | ||
return matchingVars[0].value; | ||
return optns.decodeHtml ? decodeHtml(matchingVars[0].value) : matchingVars[0].value; | ||
} else if (optns.mandatory) { | ||
@@ -204,2 +237,18 @@ throw new Error("NGL fix: missing mandatory variable " + name + " in " + JSON.stringify(evaluatedVars)); | ||
module.exports = { npmGroovyLintRules, npmGroovyLintGlobalReplacements }; | ||
function addSpaceAroundChar(line, char) { | ||
let pos = 0; | ||
const splits = line.split(char); | ||
const newArray = splits.map(str => { | ||
pos++; | ||
if (pos === 1) { | ||
return str.trimEnd(); | ||
} else if (pos === splits.length) { | ||
return str.trimStart(); | ||
} else { | ||
return str.trim(); | ||
} | ||
}); | ||
return newArray.join(" " + char + " "); | ||
} | ||
module.exports = { npmGroovyLintRules }; |
@@ -11,2 +11,3 @@ #! /usr/bin/env node | ||
const NpmGroovyLintFix = require("./groovy-lint-fix.js"); | ||
const optionsDefinition = require("./options"); | ||
@@ -19,3 +20,3 @@ class NpmGroovyLint { | ||
// Config | ||
// Internal | ||
jdeployFile; | ||
@@ -26,3 +27,3 @@ jdeployRootPath; | ||
// Codenarc | ||
codenarcArgs; | ||
codenarcArgs = []; | ||
codeNarcBaseDir; | ||
@@ -33,5 +34,4 @@ codeNarcStdOut; | ||
// npm-groovy-lint | ||
nglFix = false; | ||
onlyCodeNarc = false; | ||
lintResult = {}; | ||
nglOutput; | ||
nglOutputString = ""; | ||
@@ -43,20 +43,19 @@ status = 0; | ||
barTimer; | ||
verbose = false; | ||
// Construction: initialize options & args | ||
constructor(optionsIn, argsIn) { | ||
this.options = optionsIn; | ||
constructor(argsIn, internalOpts = {}) { | ||
if (argsIn) { | ||
this.args = argsIn; | ||
} | ||
this.verbose = this.options.verbose || this.findArg(this.args, "--ngl-verbose") || false; | ||
this.jdeployFile = this.options.jdeployFile || process.env.JDEPLOY_FILE || "originaljdeploy.js"; | ||
this.jdeployRootPath = this.options.jdeployRootPath || process.env.JDEPLOY_ROOT_PATH || __dirname; | ||
this.tmpXmlFileName = this.options.tmpXmlFileName || os.tmpdir() + "/CodeNarcReportXml_" + Math.random() + ".xml"; | ||
this.jdeployFile = internalOpts.jdeployFile || process.env.JDEPLOY_FILE || "originaljdeploy.js"; | ||
this.jdeployRootPath = internalOpts.jdeployRootPath || process.env.JDEPLOY_ROOT_PATH || __dirname; | ||
this.tmpXmlFileName = internalOpts.tmpXmlFileName || os.tmpdir() + "/CodeNarcReportXml_" + Math.random() + ".xml"; | ||
} | ||
async run() { | ||
await this.preProcess(); | ||
await this.callCodeNarc(); | ||
await this.postProcess(); | ||
const doProcess = await this.preProcess(); | ||
if (doProcess) { | ||
await this.callCodeNarc(); | ||
await this.postProcess(); | ||
} | ||
return this; | ||
@@ -67,30 +66,59 @@ } | ||
async preProcess() { | ||
this.codenarcArgs = this.args.slice(2); | ||
// Manage when the user wants to use only codenarc args | ||
if (this.args.includes("--codenarcargs")) { | ||
this.codenarcArgs = this.args.slice(2).filter(userArg => userArg !== "--codenarcargs"); | ||
this.onlyCodeNarc = true; | ||
return true; | ||
} | ||
// Define codeNarcBaseDir for later use ( postProcess ) | ||
const codeNarcBaseDirInit = this.findArg(this.codenarcArgs, "-basedir", { stripQuotes: true }); | ||
if (codeNarcBaseDirInit) { | ||
this.codeNarcBaseDir = process.cwd() + "/" + codeNarcBaseDirInit; | ||
} else { | ||
this.codeNarcBaseDir = process.cwd(); | ||
// Parse options | ||
try { | ||
this.options = optionsDefinition.parse(this.args); | ||
} catch (error) { | ||
this.status = 2; | ||
throw new Error(error.message); | ||
} | ||
// Manage files prettifying ( not working yet) | ||
if (this.findArg(this.codenarcArgs, "--ngl-format")) { | ||
//await prettifyFiles(); // NV: not implemented yet | ||
// Show version (to do more clean) | ||
if (this.options.version) { | ||
console.info("v2.0.0"); | ||
return false; | ||
} | ||
// Manage --ngl-fix option | ||
if (this.findArg(this.codenarcArgs, "--ngl-fix")) { | ||
this.nglFix = true; | ||
this.codenarcArgs.push("--ngl-output:text"); | ||
// Show help ( index or for an options) | ||
if (this.options.help) { | ||
if (this.options._.length) { | ||
this.nglOutputString = optionsDefinition.generateHelpForOption(this.options._[0]); | ||
} else { | ||
this.nglOutputString = optionsDefinition.generateHelp(); | ||
} | ||
console.info(this.nglOutputString); | ||
return false; | ||
} | ||
// Check if npm-groovy-lint reformatted output has been requested | ||
this.nglOutput = this.findArg(this.codenarcArgs, "--ngl-output"); | ||
// Remove -report userArg if existing, and add XML type to generate temp xml file that will be parsed later | ||
if (this.nglOutput !== false) { | ||
this.codenarcArgs = this.codenarcArgs.filter(userArg => !userArg.includes("-report")); | ||
this.codenarcArgs.push("-report=xml:" + this.tmpXmlFileName); | ||
// Complete options | ||
// Build codenarc options | ||
// base directory | ||
this.codeNarcBaseDir = this.options.path != "." ? process.cwd() + "/" + this.options.path.replace(/^"(.*)"$/, "$1") : process.cwd(); | ||
this.codenarcArgs.push('-basedir="' + this.codeNarcBaseDir + '"'); | ||
// Matching files pattern(s) | ||
this.codenarcArgs.push('-includes="' + this.options.files.replace(/^"(.*)"$/, "$1") + '"'); | ||
// Ruleset(s) | ||
if (this.options.rulesets) { | ||
this.codenarcArgs.push('-rulesetfiles="file:' + this.options.rulesets.replace(/^"(.*)"$/, "$1") + '"'); | ||
} | ||
const output = this.options.output.replace(/^"(.*)"$/, "$1"); | ||
if (["txt", "json"].includes(output)) { | ||
this.codenarcArgs.push('-report=xml:"' + this.tmpXmlFileName + '"'); | ||
} else if (["html", "xml"].includes(output.split(".").pop())) { | ||
const ext = output.split(".").pop(); | ||
this.codenarcArgs.push('-report="' + ext + ":" + output + '"'); | ||
} else { | ||
this.status = 2; | ||
throw new Error("For now, only output formats are txt and json in console, and html and xml as files"); | ||
} | ||
return true; | ||
} | ||
@@ -101,9 +129,12 @@ | ||
// Build jdeploy codenarc command , filter non-codenarc arguments | ||
this.codenarcArgs = this.codenarcArgs.filter(userArg => !userArg.includes("-ngl-")); | ||
const jDeployCommand = '"' + this.args[0] + '" "' + this.jdeployRootPath.trim() + "/" + this.jdeployFile + '" ' + this.codenarcArgs.join(" "); | ||
// Start progress bar | ||
if (this.options.verbose) { | ||
console.log("Running CodeNarc with arguments " + this.codenarcArgs.join(" ")); | ||
} | ||
this.bar = new cliProgress.SingleBar( | ||
{ | ||
format: "[{bar}] Running CodeNarc with arguments " + this.codenarcArgs.join(" "), | ||
format: "[{bar}] Running CodeNarc for {duration_formatted}", | ||
hideCursor: true, | ||
clearOnComplete: true | ||
@@ -119,7 +150,14 @@ }, | ||
} | ||
}, 1000); | ||
}, 500); | ||
// originalJDeploy.js Execution using child process | ||
const exec = util.promisify(require("child_process").exec); | ||
const { stdout, stderr } = await exec(jDeployCommand); | ||
let execRes; | ||
try { | ||
execRes = await exec(jDeployCommand); | ||
} catch (e) { | ||
clearInterval(this.barTimer); | ||
this.bar.stop(); | ||
throw new Error("NGL: CodeNarc crash: \n" + e.message); | ||
} | ||
@@ -130,4 +168,4 @@ // Stop progress bar | ||
this.codeNarcStdOut = stdout; | ||
this.codeNarcStdErr = stderr; | ||
this.codeNarcStdOut = execRes.stdout; | ||
this.codeNarcStdErr = execRes.stderr; | ||
} | ||
@@ -143,6 +181,6 @@ | ||
// no --ngl* options | ||
else if (this.nglOutput === false) { | ||
else if (this.onlyCodeNarc) { | ||
console.log("NGL: Successfully processed CodeNarc: \n" + this.codeNarcStdOut); | ||
} | ||
// process --ngl* options | ||
// process npm-groovy-lint options ( output, fix, formatting ...) | ||
else { | ||
@@ -152,4 +190,4 @@ // Parse XML result as js object | ||
// Fix when possible | ||
if (this.nglFix) { | ||
this.fixer = new NpmGroovyLintFix(this.lintResult, { verbose: this.verbose }); | ||
if (this.options.fix) { | ||
this.fixer = new NpmGroovyLintFix(this.lintResult, { verbose: this.options.verbose }); | ||
await this.fixer.run(); | ||
@@ -168,2 +206,3 @@ this.lintResult = this.fixer.updatedLintResult; | ||
console.error(JSON.stringify(tempXmlFileContent)); | ||
this.status = 3; | ||
throw new Error("Unable to parse temporary codenarc xml report file " + this.tmpXmlFileName); | ||
@@ -221,3 +260,3 @@ } | ||
// Display as console log | ||
if (this.nglOutput === "text" || this.nglOutput === true) { | ||
if (this.options.output === "txt") { | ||
// Errors | ||
@@ -239,4 +278,2 @@ for (const fileNm of Object.keys(this.lintResult.files)) { | ||
break; | ||
default: | ||
color = "magenta"; // should not happen | ||
} | ||
@@ -291,6 +328,6 @@ // Display fixed errors only if --verbose is called | ||
console.log(this.nglOutputString); | ||
console.table(summaryTable, ["Severity", "Total found", "Total fixed", "Total remaining"]); | ||
console.table(summaryTable, this.fixer ? ["Severity", "Total found", "Total fixed", "Total remaining"] : ["Severity", "Total found"]); | ||
} | ||
// Display as json | ||
else if (this.nglOutput === "json") { | ||
else if (this.options.output === "json") { | ||
this.nglOutputString = JSON.stringify(this.lintResult); | ||
@@ -300,22 +337,4 @@ console.log(this.nglOutputString); | ||
} | ||
// Find argument (and associated value) in user args | ||
findArg(args, name, options = {}) { | ||
const argsRes = args.filter(userArg => userArg.includes(name)); | ||
if (argsRes.length > 0) { | ||
if (argsRes[0].includes("=")) { | ||
let value = argsRes[0].split("=")[1]; | ||
if (options.stripQuotes) { | ||
value = value.replace(/^"(.*)"$/, "$1"); | ||
value = value.replace(/^'(.*)'$/, "$1"); | ||
} | ||
return value; | ||
} else { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
module.exports = NpmGroovyLint; |
@@ -6,3 +6,3 @@ #! /usr/bin/env node | ||
const linter = new NpmGroovyLint({}, process.argv); | ||
const linter = new NpmGroovyLint(process.argv, {}); | ||
linter.run(); |
@@ -0,0 +0,0 @@ ANTLR 2 License |
@@ -0,0 +0,0 @@ ANTLR 4 License |
@@ -0,0 +0,0 @@ ASM License |
@@ -0,0 +0,0 @@ BSD License |
@@ -0,0 +0,0 @@ Copyright (c) 2002-2012, the original author or authors. |
@@ -0,0 +0,0 @@ The person or persons who have associated work with this document (the |
@@ -0,0 +0,0 @@ Copyright (c) 2006, Sun Microsystems, Inc. |
@@ -0,0 +0,0 @@ Eclipse Public License - v 1.0 |
@@ -0,0 +0,0 @@ Eclipse Public License - v 2.0 |
@@ -0,0 +0,0 @@ Copyright (c) 2003-2006, Joe Walnes |
{ | ||
"name": "npm-groovy-lint", | ||
"version": "2.0.0-beta.2", | ||
"version": "2.0.0-beta3", | ||
"description": "NPM CodeNarc wrapper to easily lint Groovy files", | ||
@@ -44,4 +44,6 @@ "main": "index.js", | ||
"cli-progress": "^3.6.0", | ||
"decode-html": "^2.0.0", | ||
"fs-extra": "^8.1.0", | ||
"glob": "^7.1.6", | ||
"optionator": "^0.8.3", | ||
"shelljs": "^0.7.5", | ||
@@ -48,0 +50,0 @@ "xml2js": "^0.4.23" |
@@ -10,2 +10,3 @@ # NPM GROOVY LINT | ||
[![License](https://img.shields.io/npm/l/npm-groovy-lint.svg)](https://github.com/nvuillam/npm-groovy-lint/blob/master/package.json) | ||
[![HitCount](https://hits.dwyl.com/nvuillam/npm-groovy-lint.svg)](https://hits.dwyl.com/nvuillam/npm-groovy-lint) | ||
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) | ||
@@ -31,3 +32,3 @@ [![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/nicolas.vuillamy@gmail.com) | ||
- [All rules](https://github.com/nvuillam/npm-groovy-lint/blob/master/lib/example/RuleSet-All.groovy) | ||
- [Base rules](https://github.com/nvuillam/npm-groovy-lint/blob/master/lib/example/RuleSet-Base.groovy) | ||
- [Base rules](https://github.com/nvuillam/npm-groovy-lint/blob/master/lib/example/RuleSet-Groovy.groovy) | ||
@@ -70,3 +71,3 @@ # USAGE | ||
$ npm-groovy-lint -report="xml:MyGroovyLinterReport.xml" --ngl-output=text | ||
$ npm-groovy-lint --ngl-output=text | ||
@@ -76,3 +77,3 @@ | ||
$ npm-groovy-lint -includes=**/Jenkinsfile -rulesetfiles="file:config/codenarc/RuleSet-Base.groovy" --ngl-output=json | ||
$ npm-groovy-lint -includes=**/Jenkinsfile -rulesetfiles="file:config/codenarc/RuleSet-Groovy.groovy" --ngl-output=json | ||
@@ -87,3 +88,3 @@ | ||
$ npm-groovy-lint -basedir="src" -rulesetfiles="file:config/codenarc/RuleSet-Base.groovy" -title="MyGroovyLinterReport" -maxPriority1Violations=0 -report="html:MyGroovyLinterReport.html" | ||
$ npm-groovy-lint -basedir="src" -rulesetfiles="file:config/codenarc/RuleSet-Groovy.groovy" -title="MyGroovyLinterReport" -maxPriority1Violations=0 -report="html:MyGroovyLinterReport.html" | ||
``` | ||
@@ -90,0 +91,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
15882859
37
984
110
8
+ Addeddecode-html@^2.0.0
+ Addedoptionator@^0.8.3
+ Addeddecode-html@2.0.0(transitive)
+ Addeddeep-is@0.1.4(transitive)
+ Addedfast-levenshtein@2.0.6(transitive)
+ Addedlevn@0.3.0(transitive)
+ Addedoptionator@0.8.3(transitive)
+ Addedprelude-ls@1.1.2(transitive)
+ Addedtype-check@0.3.2(transitive)
+ Addedword-wrap@1.2.5(transitive)