npm-groovy-lint 14.6.0 → 14.6.1-beta202408252305.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/lib/codenarc-caller.js +54 -53
- package/lib/codenarc-factory.js +41 -50
- package/lib/config.js +22 -19
- package/lib/filter.js +8 -15
- package/lib/groovy-lint-fix.js +32 -34
- package/lib/groovy-lint-rules.js +12 -12
- package/lib/groovy-lint.js +40 -41
- package/lib/index.js +3 -3
- package/lib/java/{CodeNarc-3.4.0-alpha+3346775f.jar → CodeNarc-3.5.0.jar} +0 -0
- package/lib/java/CodeNarcServer.jar +0 -0
- package/lib/options.js +45 -54
- package/lib/output.js +23 -23
- package/lib/rules/AssignmentInConditional.js +7 -7
- package/lib/rules/BlankLineBeforePackage.js +11 -11
- package/lib/rules/BlockEndsWithBlankLine.js +6 -6
- package/lib/rules/BlockStartsWithBlankLine.js +6 -6
- package/lib/rules/BracesForClass.js +8 -8
- package/lib/rules/BracesForForLoop.js +9 -9
- package/lib/rules/BracesForIfElse.js +9 -9
- package/lib/rules/BracesForMethod.js +11 -11
- package/lib/rules/BracesForTryCatchFinally.js +7 -7
- package/lib/rules/CatchException.js +4 -4
- package/lib/rules/ClassEndsWithBlankLine.js +6 -6
- package/lib/rules/ClassStartsWithBlankLine.js +6 -6
- package/lib/rules/ClosingBraceNotAlone.js +8 -8
- package/lib/rules/ConsecutiveBlankLines.js +7 -7
- package/lib/rules/DuplicateImport.js +6 -6
- package/lib/rules/DuplicateNumberLiteral.js +6 -6
- package/lib/rules/DuplicateStringLiteral.js +6 -6
- package/lib/rules/ElseBlockBraces.js +10 -10
- package/lib/rules/ExplicitArrayListInstantiation.js +7 -7
- package/lib/rules/ExplicitLinkedListInstantiation.js +7 -7
- package/lib/rules/FileEndsWithoutNewline.js +7 -7
- package/lib/rules/GStringExpressionWithinString.js +7 -7
- package/lib/rules/IfStatementBraces.js +10 -10
- package/lib/rules/Indentation.js +14 -14
- package/lib/rules/IndentationClosingBraces.js +8 -8
- package/lib/rules/IndentationComments.js +6 -6
- package/lib/rules/InsecureRandom.js +14 -14
- package/lib/rules/JavaIoPackageAccess.js +6 -6
- package/lib/rules/MethodCount.js +6 -6
- package/lib/rules/MethodParameterTypeRequired.js +6 -6
- package/lib/rules/MethodReturnTypeRequired.js +6 -6
- package/lib/rules/MisorderedStaticImports.js +14 -14
- package/lib/rules/MissingBlankLineAfterImports.js +6 -6
- package/lib/rules/MissingBlankLineAfterPackage.js +6 -6
- package/lib/rules/NoDef.js +4 -4
- package/lib/rules/NoJavaUtilDate.js +4 -4
- package/lib/rules/NoTabCharacter.js +7 -7
- package/lib/rules/SimpleDateFormatMissingLocale.js +4 -4
- package/lib/rules/SpaceAfterCatch.js +7 -7
- package/lib/rules/SpaceAfterComma.js +9 -9
- package/lib/rules/SpaceAfterFor.js +8 -8
- package/lib/rules/SpaceAfterIf.js +10 -10
- package/lib/rules/SpaceAfterMethodCallName.js +7 -7
- package/lib/rules/SpaceAfterOpeningBrace.js +9 -9
- package/lib/rules/SpaceAfterSemicolon.js +8 -8
- package/lib/rules/SpaceAfterSwitch.js +8 -8
- package/lib/rules/SpaceAfterWhile.js +7 -7
- package/lib/rules/SpaceAroundOperator.js +14 -14
- package/lib/rules/SpaceBeforeClosingBrace.js +8 -8
- package/lib/rules/SpaceBeforeOpeningBrace.js +9 -9
- package/lib/rules/SpaceInsideParentheses.js +17 -17
- package/lib/rules/SystemExit.js +4 -4
- package/lib/rules/TrailingWhitespace.js +8 -8
- package/lib/rules/UnnecessaryDefInFieldDeclaration.js +8 -8
- package/lib/rules/UnnecessaryDefInMethodDeclaration.js +7 -7
- package/lib/rules/UnnecessaryDefInVariableDeclaration.js +9 -9
- package/lib/rules/UnnecessaryDotClass.js +7 -7
- package/lib/rules/UnnecessaryFinalOnPrivateMethod.js +7 -7
- package/lib/rules/UnnecessaryGString.js +14 -14
- package/lib/rules/UnnecessaryGroovyImport.js +6 -6
- package/lib/rules/UnnecessaryPackageReference.js +9 -9
- package/lib/rules/UnnecessaryParenthesesForMethodCallWithClosure.js +9 -9
- package/lib/rules/UnnecessaryPublicModifier.js +4 -4
- package/lib/rules/UnnecessarySemicolon.js +10 -13
- package/lib/rules/UnnecessaryToString.js +8 -8
- package/lib/rules/UnusedImport.js +10 -10
- package/lib/rules/UnusedMethodParameter.js +6 -6
- package/lib/rules/UnusedVariable.js +6 -6
- package/lib/rules/VariableName.js +11 -11
- package/lib/rules/VariableTypeRequired.js +11 -11
- package/lib/utils.js +62 -43
- package/package.json +22 -19
package/lib/filter.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// Filter errors
|
|
2
|
-
"use strict";
|
|
3
2
|
|
|
4
3
|
// Parse source to list parts where errors must be ignored
|
|
5
4
|
function collectDisabledBlocks(allLines) {
|
|
@@ -15,10 +14,10 @@ function collectDisabledBlocks(allLines) {
|
|
|
15
14
|
else if (line.includes("groovylint-enable")) {
|
|
16
15
|
const enabledRules = parseGroovyLintComment("groovylint-enable", line);
|
|
17
16
|
const matchingIndex = disabledBlocks.findIndex(
|
|
18
|
-
disabledBlock =>
|
|
17
|
+
(disabledBlock) =>
|
|
19
18
|
disabledBlock.endLine == null &&
|
|
20
19
|
lineNb >= disabledBlock.startLine &&
|
|
21
|
-
disabledBlock.rules.sort().toString() == enabledRules.sort().toString()
|
|
20
|
+
disabledBlock.rules.sort().toString() == enabledRules.sort().toString(),
|
|
22
21
|
);
|
|
23
22
|
if (matchingIndex > -1) {
|
|
24
23
|
disabledBlocks[matchingIndex].endLine = lineNb;
|
|
@@ -29,7 +28,7 @@ function collectDisabledBlocks(allLines) {
|
|
|
29
28
|
lineNb++;
|
|
30
29
|
}
|
|
31
30
|
// Set last line pos as endLine for the groovylint-disable not matched with a groovylint-enable
|
|
32
|
-
return disabledBlocks.map(disabledBlock => {
|
|
31
|
+
return disabledBlocks.map((disabledBlock) => {
|
|
33
32
|
if (disabledBlock.endLine == null) {
|
|
34
33
|
disabledBlock.endLine = allLines.length;
|
|
35
34
|
}
|
|
@@ -44,7 +43,7 @@ function isFilteredError(errItem, allLines, disabledBlocks) {
|
|
|
44
43
|
|
|
45
44
|
// matches groovylint-disable blocks
|
|
46
45
|
const matchingDisabledBlocks = disabledBlocks.filter(
|
|
47
|
-
disabledBlock => disabledBlock.startLine <= linePos && disabledBlock.endLine >= linePos && checkMatch(disabledBlock.rules, ruleName)
|
|
46
|
+
(disabledBlock) => disabledBlock.startLine <= linePos && disabledBlock.endLine >= linePos && checkMatch(disabledBlock.rules, ruleName),
|
|
48
47
|
);
|
|
49
48
|
if (matchingDisabledBlocks.length > 0) {
|
|
50
49
|
return true;
|
|
@@ -68,11 +67,9 @@ function isFilteredError(errItem, allLines, disabledBlocks) {
|
|
|
68
67
|
// ex: "// groovylint-disable" will return "all"
|
|
69
68
|
function parseGroovyLintComment(type, line) {
|
|
70
69
|
if (line.includes(type)) {
|
|
71
|
-
const typeDetail = cleanFromCommentMarks(line)
|
|
72
|
-
.replace(type, "")
|
|
73
|
-
.trim();
|
|
70
|
+
const typeDetail = cleanFromCommentMarks(line).replace(type, "").trim();
|
|
74
71
|
if (typeDetail) {
|
|
75
|
-
const errors = typeDetail.split(",").map(errType => errType.trim());
|
|
72
|
+
const errors = typeDetail.split(",").map((errType) => errType.trim());
|
|
76
73
|
return errors;
|
|
77
74
|
}
|
|
78
75
|
return ["all"];
|
|
@@ -90,11 +87,7 @@ function cleanFromCommentMarks(str) {
|
|
|
90
87
|
if (str.indexOf("//")) {
|
|
91
88
|
str = str.substring(str.indexOf("//"));
|
|
92
89
|
}
|
|
93
|
-
return str
|
|
94
|
-
.replace("/*", "")
|
|
95
|
-
.replace("//", "")
|
|
96
|
-
.replace("*/", "")
|
|
97
|
-
.trim();
|
|
90
|
+
return str.replace("/*", "").replace("//", "").replace("*/", "").trim();
|
|
98
91
|
}
|
|
99
92
|
|
|
100
|
-
|
|
93
|
+
export { collectDisabledBlocks, isFilteredError };
|
package/lib/groovy-lint-fix.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
// Imports
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class NpmGroovyLintFix {
|
|
11
|
-
"use strict";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import * as cliProgress from "cli-progress";
|
|
4
|
+
import Debug from "debug";
|
|
5
|
+
const debug = Debug("npm-groovy-lint");
|
|
6
|
+
const trace = Debug("npm-groovy-lint-trace");
|
|
7
|
+
import * as os from "os";
|
|
8
|
+
import { getNpmGroovyLintRules, getFormattingRulesToAlwaysRun } from "./groovy-lint-rules.js";
|
|
9
|
+
import { evaluateVariables, getSourceLines } from "./utils.js";
|
|
12
10
|
|
|
11
|
+
export class NpmGroovyLintFix {
|
|
13
12
|
options = {};
|
|
14
13
|
|
|
15
14
|
updatedLintResult;
|
|
@@ -30,7 +29,6 @@ class NpmGroovyLintFix {
|
|
|
30
29
|
this.options = optionsIn;
|
|
31
30
|
this.verbose = optionsIn.verbose || false;
|
|
32
31
|
// Load available fix rules
|
|
33
|
-
this.npmGroovyLintRules = this.options.groovyLintRulesOverride ? require(this.options.groovyLintRulesOverride) : getNpmGroovyLintRules();
|
|
34
32
|
if (this.options.fixrules && this.options.fixrules !== "all") {
|
|
35
33
|
this.fixRules = this.options.fixrules.split(",");
|
|
36
34
|
}
|
|
@@ -41,13 +39,15 @@ class NpmGroovyLintFix {
|
|
|
41
39
|
// Fix errors using codenarc result and groovy lint rules
|
|
42
40
|
async run(optns = { errorIds: null, propagate: false }) {
|
|
43
41
|
debug(`<<<<<< NpmGroovyLintFix.run START >>>>>>`);
|
|
42
|
+
const npmGroovyLintRules = await getNpmGroovyLintRules();
|
|
43
|
+
this.npmGroovyLintRules = this.options.groovyLintRulesOverride ? await import(this.options.groovyLintRulesOverride) : npmGroovyLintRules;
|
|
44
44
|
// Start progress bar
|
|
45
45
|
this.bar = new cliProgress.SingleBar(
|
|
46
46
|
{
|
|
47
47
|
format: "GroovyLint [{bar}] Fixing {file}",
|
|
48
|
-
clearOnComplete: true
|
|
48
|
+
clearOnComplete: true,
|
|
49
49
|
},
|
|
50
|
-
cliProgress.Presets.shades_classic
|
|
50
|
+
cliProgress.Presets.shades_classic,
|
|
51
51
|
);
|
|
52
52
|
this.bar.start(Object.keys(this.updatedLintResult.files).length, 0, { file: "..." });
|
|
53
53
|
|
|
@@ -88,8 +88,8 @@ class NpmGroovyLintFix {
|
|
|
88
88
|
this.npmGroovyLintRules[err.rule],
|
|
89
89
|
this.options.rules && this.options.rules[err.rule] && typeof this.options.rules[err.rule] === "object"
|
|
90
90
|
? { config: this.options.rules[err.rule] }
|
|
91
|
-
: { config: {} }
|
|
92
|
-
)
|
|
91
|
+
: { config: {} },
|
|
92
|
+
),
|
|
93
93
|
};
|
|
94
94
|
this.addFixableError(fileNm, fixableError);
|
|
95
95
|
// Trigger other fixes if defined in the rule
|
|
@@ -107,8 +107,8 @@ class NpmGroovyLintFix {
|
|
|
107
107
|
this.options.rules[triggeredRuleName] &&
|
|
108
108
|
typeof this.options.rules[triggeredRuleName] === "object"
|
|
109
109
|
? { config: this.options.rules[triggeredRuleName] }
|
|
110
|
-
: { config: {} }
|
|
111
|
-
)
|
|
110
|
+
: { config: {} },
|
|
111
|
+
),
|
|
112
112
|
};
|
|
113
113
|
this.addFixableError(fileNm, fixableErrorTriggered);
|
|
114
114
|
}
|
|
@@ -125,7 +125,7 @@ class NpmGroovyLintFix {
|
|
|
125
125
|
ruleName: formattingRuleName,
|
|
126
126
|
lineNb: 0,
|
|
127
127
|
msg: `${formattingRuleName} triggered by format request`,
|
|
128
|
-
rule: rule
|
|
128
|
+
rule: rule,
|
|
129
129
|
};
|
|
130
130
|
this.addFixableError(fileNm, fixableErrorTriggered);
|
|
131
131
|
}
|
|
@@ -144,7 +144,7 @@ class NpmGroovyLintFix {
|
|
|
144
144
|
if (
|
|
145
145
|
fixableError.rule.scope === "file" &&
|
|
146
146
|
!fixableError.rule.unitary === true &&
|
|
147
|
-
this.fixableErrors[fileNm].filter(matchFixableError => matchFixableError.ruleName === fixableError.ruleName).length > 0
|
|
147
|
+
this.fixableErrors[fileNm].filter((matchFixableError) => matchFixableError.ruleName === fixableError.ruleName).length > 0
|
|
148
148
|
) {
|
|
149
149
|
return;
|
|
150
150
|
}
|
|
@@ -164,7 +164,7 @@ class NpmGroovyLintFix {
|
|
|
164
164
|
async fixErrors() {
|
|
165
165
|
// Process files in parallel
|
|
166
166
|
await Promise.all(
|
|
167
|
-
Object.keys(this.fixableErrors).map(async fileNm => {
|
|
167
|
+
Object.keys(this.fixableErrors).map(async (fileNm) => {
|
|
168
168
|
// Read file
|
|
169
169
|
let allLines = await getSourceLines(this.options.source, fileNm);
|
|
170
170
|
|
|
@@ -207,9 +207,9 @@ class NpmGroovyLintFix {
|
|
|
207
207
|
fileFixableError.id,
|
|
208
208
|
{
|
|
209
209
|
fixed: fixSuccess === true,
|
|
210
|
-
triggersAgainAfterFix: fileFixableError.rule.triggersAgainAfterFix
|
|
210
|
+
triggersAgainAfterFix: fileFixableError.rule.triggersAgainAfterFix,
|
|
211
211
|
},
|
|
212
|
-
fileFixableError.rule.fixesSameErrorOnSameLine
|
|
212
|
+
fileFixableError.rule.fixesSameErrorOnSameLine,
|
|
213
213
|
);
|
|
214
214
|
fixedInFileNb = fixedInFileNb + fixedNb;
|
|
215
215
|
}
|
|
@@ -217,9 +217,9 @@ class NpmGroovyLintFix {
|
|
|
217
217
|
this.updatedLintResult.files[fileNm].updatedSource = newSources;
|
|
218
218
|
// Write new file content if it has been updated
|
|
219
219
|
if (this.options.save && fixedInFileNb > 0) {
|
|
220
|
-
|
|
220
|
+
fs.writeFileSync(fileNm, newSources);
|
|
221
221
|
}
|
|
222
|
-
})
|
|
222
|
+
}),
|
|
223
223
|
);
|
|
224
224
|
}
|
|
225
225
|
|
|
@@ -235,9 +235,9 @@ class NpmGroovyLintFix {
|
|
|
235
235
|
fixableError.id,
|
|
236
236
|
{
|
|
237
237
|
fixed: false,
|
|
238
|
-
triggersAgainAfterFix: fixableError.rule.triggersAgainAfterFix
|
|
238
|
+
triggersAgainAfterFix: fixableError.rule.triggersAgainAfterFix,
|
|
239
239
|
},
|
|
240
|
-
fixableError.rule.fixesSameErrorOnSameLine
|
|
240
|
+
fixableError.rule.fixesSameErrorOnSameLine,
|
|
241
241
|
);
|
|
242
242
|
return lineOrAllLines;
|
|
243
243
|
}
|
|
@@ -296,7 +296,7 @@ class NpmGroovyLintFix {
|
|
|
296
296
|
|
|
297
297
|
// Update lint result of an identified error
|
|
298
298
|
updateLintResult(fileNm, errId, errDataToSet, fixesSameErrorOnSameLine = false) {
|
|
299
|
-
const errIndex = this.updatedLintResult.files[fileNm].errors.findIndex(error => error.id === errId);
|
|
299
|
+
const errIndex = this.updatedLintResult.files[fileNm].errors.findIndex((error) => error.id === errId);
|
|
300
300
|
// Update error in lint result {mostly fixed: true}
|
|
301
301
|
// It not in list of errors, it means it's from a triggered error
|
|
302
302
|
let error = {};
|
|
@@ -305,7 +305,7 @@ class NpmGroovyLintFix {
|
|
|
305
305
|
Object.assign(error, errDataToSet);
|
|
306
306
|
// If same error has been fixed on the same line, mark failed fix and success as it has been corrected by a previous fix
|
|
307
307
|
if (fixesSameErrorOnSameLine && errDataToSet.fixed === false) {
|
|
308
|
-
const sameLineSameRuleFixedErrors = this.updatedLintResult.files[fileNm].errors.filter(err => {
|
|
308
|
+
const sameLineSameRuleFixedErrors = this.updatedLintResult.files[fileNm].errors.filter((err) => {
|
|
309
309
|
return err.line === error.line && err.rule === error.rule && err.fixed === true && err.id !== error.id;
|
|
310
310
|
});
|
|
311
311
|
if (sameLineSameRuleFixedErrors.length > 0) {
|
|
@@ -330,7 +330,7 @@ class NpmGroovyLintFix {
|
|
|
330
330
|
if (lengthDiff === 0) {
|
|
331
331
|
return;
|
|
332
332
|
}
|
|
333
|
-
this.fixableErrors[fileNm] = this.fixableErrors[fileNm].map(fixableError => {
|
|
333
|
+
this.fixableErrors[fileNm] = this.fixableErrors[fileNm].map((fixableError) => {
|
|
334
334
|
// Only update line number & range for next lines
|
|
335
335
|
if (fixableError.lineNb > lineNb) {
|
|
336
336
|
fixableError.lineNb = fixableError.lineNb + lengthDiff;
|
|
@@ -338,12 +338,12 @@ class NpmGroovyLintFix {
|
|
|
338
338
|
fixableError.range = {
|
|
339
339
|
start: {
|
|
340
340
|
line: fixableError.range.start.line + lengthDiff,
|
|
341
|
-
character: fixableError.range.start.character
|
|
341
|
+
character: fixableError.range.start.character,
|
|
342
342
|
},
|
|
343
343
|
end: {
|
|
344
344
|
line: fixableError.range.end.line + lengthDiff,
|
|
345
|
-
character: fixableError.range.end.character
|
|
346
|
-
}
|
|
345
|
+
character: fixableError.range.end.character,
|
|
346
|
+
},
|
|
347
347
|
};
|
|
348
348
|
}
|
|
349
349
|
}
|
|
@@ -351,5 +351,3 @@ class NpmGroovyLintFix {
|
|
|
351
351
|
});
|
|
352
352
|
}
|
|
353
353
|
}
|
|
354
|
-
|
|
355
|
-
module.exports = NpmGroovyLintFix;
|
package/lib/groovy-lint-rules.js
CHANGED
|
@@ -83,13 +83,14 @@ str = "lelamanul"
|
|
|
83
83
|
]
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
export default { rule }
|
|
87
87
|
|
|
88
88
|
*/
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
import fs from "fs-extra";
|
|
91
|
+
const { readdirSync } = fs;
|
|
92
|
+
import * as path from "path";
|
|
93
|
+
import { fileURLToPath } from "url";
|
|
93
94
|
|
|
94
95
|
// If you add a new global rule with a fix function, it's very important to think about their order.
|
|
95
96
|
// Rules modifying the number of lines must arrive last !
|
|
@@ -151,25 +152,26 @@ const rulesFixPriorityOrder = [
|
|
|
151
152
|
"IndentationClosingBraces",
|
|
152
153
|
"IndentationComments",
|
|
153
154
|
"ConsecutiveBlankLines",
|
|
154
|
-
"FileEndsWithoutNewline"
|
|
155
|
+
"FileEndsWithoutNewline",
|
|
155
156
|
];
|
|
156
157
|
|
|
157
158
|
// CodeNarc formatting fix rules are triggered after CodeNarc returns violations
|
|
158
159
|
// Non-CodeNarc formatting fix rules (existing only in npm-groovy-lint) must be run always
|
|
159
160
|
const formatRulesToAlwaysRun = ["IndentationClosingBraces", "IndentationComments"];
|
|
160
161
|
|
|
162
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
161
163
|
const RULES_FOLDER = __dirname + "/rules";
|
|
162
164
|
|
|
163
|
-
function getNpmGroovyLintRules(optns = { loadTests: false }) {
|
|
164
|
-
const ruleFiles =
|
|
165
|
+
export async function getNpmGroovyLintRules(optns = { loadTests: false }) {
|
|
166
|
+
const ruleFiles = readdirSync(RULES_FOLDER);
|
|
165
167
|
const npmGroovyLintRules = {};
|
|
166
168
|
for (const file of ruleFiles) {
|
|
167
169
|
const ruleName = file.replace(".js", "");
|
|
168
170
|
// Remove require cache if tests must be returned (other calls delete them in the cache)
|
|
169
171
|
if (optns && optns.loadTests === true) {
|
|
170
|
-
delete require.cache[require.resolve(`${RULES_FOLDER}/${file}`)];
|
|
172
|
+
// delete require.cache[require.resolve(`${RULES_FOLDER}/${file}`)]; Not ESM compliant
|
|
171
173
|
}
|
|
172
|
-
const { rule } =
|
|
174
|
+
const { rule } = await import("./rules/" + file);
|
|
173
175
|
if (rule.disabled) {
|
|
174
176
|
continue;
|
|
175
177
|
}
|
|
@@ -187,8 +189,6 @@ function getNpmGroovyLintRules(optns = { loadTests: false }) {
|
|
|
187
189
|
return npmGroovyLintRules;
|
|
188
190
|
}
|
|
189
191
|
|
|
190
|
-
function getFormattingRulesToAlwaysRun() {
|
|
192
|
+
export function getFormattingRulesToAlwaysRun() {
|
|
191
193
|
return formatRulesToAlwaysRun;
|
|
192
194
|
}
|
|
193
|
-
|
|
194
|
-
module.exports = { getNpmGroovyLintRules, getFormattingRulesToAlwaysRun };
|
package/lib/groovy-lint.js
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
// Imports
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
2
|
+
import Debug from "debug";
|
|
3
|
+
const debug = Debug("npm-groovy-lint");
|
|
4
|
+
const trace = Debug("npm-groovy-lint-trace");
|
|
5
|
+
import fs from "fs-extra";
|
|
6
|
+
import * as os from "os";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
import { performance } from "node:perf_hooks";
|
|
9
|
+
import { NpmGroovyLintFix } from "./groovy-lint-fix.js";
|
|
10
|
+
|
|
11
|
+
import { CodeNarcCaller } from "./codenarc-caller.js";
|
|
12
|
+
import { prepareCodeNarcCall, parseCodeNarcResult } from "./codenarc-factory.js";
|
|
13
|
+
import { NPM_GROOVY_LINT_CONSTANTS, loadConfig, getConfigFileName } from "./config.js";
|
|
14
|
+
import { optionsDefinition } from "./options.js";
|
|
15
|
+
import { computeStats, processOutput } from "./output.js";
|
|
16
|
+
import { getNpmGroovyLintVersion, getSourceLines, isErrorInLogLevelScope } from "./utils.js";
|
|
16
17
|
|
|
17
18
|
class NpmGroovyLint {
|
|
18
|
-
"use strict";
|
|
19
|
-
|
|
20
19
|
options = {}; // NpmGroovyLint options
|
|
21
20
|
args = []; // Command line arguments
|
|
22
21
|
|
|
@@ -83,9 +82,9 @@ class NpmGroovyLint {
|
|
|
83
82
|
verbose: optns.verbose || this.options.verbose,
|
|
84
83
|
fixrules: optns.fixrules || this.options.fixrules,
|
|
85
84
|
source: optns.source || this.options.source,
|
|
86
|
-
save: this.tmpGroovyFileName ? false : true
|
|
85
|
+
save: this.tmpGroovyFileName ? false : true,
|
|
87
86
|
},
|
|
88
|
-
{ origin: "externalCallToFix" }
|
|
87
|
+
{ origin: "externalCallToFix" },
|
|
89
88
|
);
|
|
90
89
|
await this.fixer.run({ errorIds: errorIds, propagate: true });
|
|
91
90
|
this.lintResult = this.fixer.updatedLintResult;
|
|
@@ -139,8 +138,8 @@ class NpmGroovyLint {
|
|
|
139
138
|
if (Array.isArray(this.args) && this.args.includes("--codenarcargs")) {
|
|
140
139
|
this.codenarcArgs = this.args
|
|
141
140
|
.slice(2)
|
|
142
|
-
.filter(userArg => userArg !== "--codenarcargs") // Strip codenarcargs.
|
|
143
|
-
.map(userArg => userArg.replace(/^-(\w+)="(.*)"$/, "-$1=$2").replace(/^-(\w+)='(.*)'$/, "-$1=$2")); // Strip quotes around values which CodeNarc doesn't support.
|
|
141
|
+
.filter((userArg) => userArg !== "--codenarcargs") // Strip codenarcargs.
|
|
142
|
+
.map((userArg) => userArg.replace(/^-(\w+)="(.*)"$/, "-$1=$2").replace(/^-(\w+)='(.*)'$/, "-$1=$2")); // Strip quotes around values which CodeNarc doesn't support.
|
|
144
143
|
this.onlyCodeNarc = true;
|
|
145
144
|
return true;
|
|
146
145
|
}
|
|
@@ -159,7 +158,7 @@ class NpmGroovyLint {
|
|
|
159
158
|
const configProperties = await loadConfig(
|
|
160
159
|
this.options.config || this.options.path,
|
|
161
160
|
this.options.format ? "format" : "lint",
|
|
162
|
-
this.options.sourcefilepath || this.options.path
|
|
161
|
+
this.options.sourcefilepath || this.options.path,
|
|
163
162
|
);
|
|
164
163
|
for (const configProp of Object.keys(configProperties)) {
|
|
165
164
|
if (this.options[configProp] == null) {
|
|
@@ -180,7 +179,7 @@ class NpmGroovyLint {
|
|
|
180
179
|
this.status = 2;
|
|
181
180
|
this.error = {
|
|
182
181
|
msg: `Parse options error: ${err.message}`,
|
|
183
|
-
stack: err.stack
|
|
182
|
+
stack: err.stack,
|
|
184
183
|
};
|
|
185
184
|
console.error(this.error.msg);
|
|
186
185
|
return false;
|
|
@@ -190,7 +189,7 @@ class NpmGroovyLint {
|
|
|
190
189
|
if (!isErrorInLogLevelScope(this.options.failon, this.options.loglevel)) {
|
|
191
190
|
this.status = 2;
|
|
192
191
|
this.error = {
|
|
193
|
-
msg: `failon option (${this.options.failon}) must be > loglevel option (${this.options.loglevel})
|
|
192
|
+
msg: `failon option (${this.options.failon}) must be > loglevel option (${this.options.loglevel})`,
|
|
194
193
|
};
|
|
195
194
|
console.error(this.error.msg);
|
|
196
195
|
return false;
|
|
@@ -209,7 +208,7 @@ class NpmGroovyLint {
|
|
|
209
208
|
if (this.options.killserver) {
|
|
210
209
|
const startPerf = performance.now();
|
|
211
210
|
const codeNarcCaller = new CodeNarcCaller(this.codenarcArgs, this.serverStatus, this.args, this.options, {
|
|
212
|
-
groovyFileName: this.tmpGroovyFileName
|
|
211
|
+
groovyFileName: this.tmpGroovyFileName,
|
|
213
212
|
});
|
|
214
213
|
this.outputString = await codeNarcCaller.killCodeNarcServer();
|
|
215
214
|
console.info(this.outputString);
|
|
@@ -270,7 +269,7 @@ class NpmGroovyLint {
|
|
|
270
269
|
codeNarcIncludes: this.codeNarcIncludes,
|
|
271
270
|
codeNarcExcludes: this.codeNarcExcludes,
|
|
272
271
|
onlyCodeNarc: this.onlyCodeNarc,
|
|
273
|
-
inputFileList: this.inputFileList
|
|
272
|
+
inputFileList: this.inputFileList,
|
|
274
273
|
});
|
|
275
274
|
if (!this.options.noserver) {
|
|
276
275
|
serverCallResult = await codeNarcCaller.callCodeNarcServer();
|
|
@@ -305,7 +304,7 @@ class NpmGroovyLint {
|
|
|
305
304
|
this.codeNarcBaseDir,
|
|
306
305
|
this.codeNarcJsonResult,
|
|
307
306
|
this.tmpGroovyFileName,
|
|
308
|
-
this.parseErrors
|
|
307
|
+
this.parseErrors,
|
|
309
308
|
);
|
|
310
309
|
// Fix all found errors if requested
|
|
311
310
|
if (this.options.fix || this.options.format) {
|
|
@@ -316,7 +315,7 @@ class NpmGroovyLint {
|
|
|
316
315
|
save: this.tmpGroovyFileName ? false : true,
|
|
317
316
|
origin: this.origin,
|
|
318
317
|
rules: this.options.rules,
|
|
319
|
-
verbose: this.options.verbose
|
|
318
|
+
verbose: this.options.verbose,
|
|
320
319
|
});
|
|
321
320
|
await this.fixer.run();
|
|
322
321
|
this.lintResult = this.fixer.updatedLintResult;
|
|
@@ -369,7 +368,7 @@ class NpmGroovyLint {
|
|
|
369
368
|
trace(`Fix is done, lint again with options ${JSON.stringify(lintAgainOptions)}`);
|
|
370
369
|
const newLinter = new NpmGroovyLint(lintAgainOptions, {
|
|
371
370
|
parseOptions: false,
|
|
372
|
-
origin: "lintAgainAfterFix"
|
|
371
|
+
origin: "lintAgainAfterFix",
|
|
373
372
|
});
|
|
374
373
|
// Run linter
|
|
375
374
|
await newLinter.run();
|
|
@@ -395,8 +394,8 @@ class NpmGroovyLint {
|
|
|
395
394
|
delete fixAgainOptions.rules;
|
|
396
395
|
delete fixAgainOptions.overriddenRules;
|
|
397
396
|
fixAgainOptions.rulesets = Object.keys(this.options.rules)
|
|
398
|
-
.filter(ruleName => fixRules.includes(ruleName))
|
|
399
|
-
.map(ruleName => `${ruleName}${JSON.stringify(this.options.rules[ruleName])}`)
|
|
397
|
+
.filter((ruleName) => fixRules.includes(ruleName))
|
|
398
|
+
.map((ruleName) => `${ruleName}${JSON.stringify(this.options.rules[ruleName])}`)
|
|
400
399
|
.join(",");
|
|
401
400
|
fixAgainOptions.fix = true;
|
|
402
401
|
fixAgainOptions.fixrules = fixRules.join(",");
|
|
@@ -416,7 +415,7 @@ class NpmGroovyLint {
|
|
|
416
415
|
for (const afterFixResFileNm of Object.keys(lintResAfterFix.files)) {
|
|
417
416
|
// Append fixed errors to errors found via control lint (post fix)
|
|
418
417
|
const afterFixFileErrors = lintResAfterFix.files[afterFixResFileNm].errors;
|
|
419
|
-
const fixedErrors = afterFixFileErrors.filter(err => err.fixed === true);
|
|
418
|
+
const fixedErrors = afterFixFileErrors.filter((err) => err.fixed === true);
|
|
420
419
|
const controlFileErrors =
|
|
421
420
|
lintResControl.files && lintResControl.files[afterFixResFileNm] ? lintResControl.files[afterFixResFileNm].errors || [] : [];
|
|
422
421
|
const mergedFileErrors = controlFileErrors.concat(fixedErrors);
|
|
@@ -469,7 +468,7 @@ class NpmGroovyLint {
|
|
|
469
468
|
origin: this.origin,
|
|
470
469
|
elapse: parseInt(performance.now() - startPerf),
|
|
471
470
|
options: optionsLog,
|
|
472
|
-
args: this.codenarcArgs
|
|
471
|
+
args: this.codenarcArgs,
|
|
473
472
|
});
|
|
474
473
|
}
|
|
475
474
|
}
|
|
@@ -484,12 +483,12 @@ class NpmGroovyLint {
|
|
|
484
483
|
this.options.failon && this.options.failon !== "none"
|
|
485
484
|
? this.options.failon
|
|
486
485
|
: this.options.failonerror
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
486
|
+
? "error"
|
|
487
|
+
: this.options.failonwarning
|
|
488
|
+
? "warning"
|
|
489
|
+
: this.options.failoninfo
|
|
490
|
+
? "info"
|
|
491
|
+
: "none";
|
|
493
492
|
if (failureLevel === "none") {
|
|
494
493
|
return;
|
|
495
494
|
}
|
|
@@ -513,7 +512,7 @@ class NpmGroovyLint {
|
|
|
513
512
|
else if (failureLevel === "warning" && (errorNb > 0 || warningNb > 0)) {
|
|
514
513
|
if (!["json", "sarif", "stdout"].includes(this.outputType)) {
|
|
515
514
|
console.error(
|
|
516
|
-
`Failure: ${this.lintResult.summary.totalRemainingErrorNumber} error(s) have been found \n ${this.lintResult.summary.totalRemainingWarningNumber} warning(s) have been found
|
|
515
|
+
`Failure: ${this.lintResult.summary.totalRemainingErrorNumber} error(s) have been found \n ${this.lintResult.summary.totalRemainingWarningNumber} warning(s) have been found`,
|
|
517
516
|
);
|
|
518
517
|
}
|
|
519
518
|
this.status = 1;
|
|
@@ -522,7 +521,7 @@ class NpmGroovyLint {
|
|
|
522
521
|
else if (failureLevel === "info" && (errorNb > 0 || warningNb > 0 || infoNb > 0)) {
|
|
523
522
|
if (!["json", "sarif", "stdout"].includes(this.outputType)) {
|
|
524
523
|
console.error(
|
|
525
|
-
`Failure: ${this.lintResult.summary.totalRemainingErrorNumber} error(s) have been found \n ${this.lintResult.summary.totalRemainingWarningNumber} warning(s) have been found \n ${this.lintResult.summary.totalRemainingInfoNumber} info(s) have been found
|
|
524
|
+
`Failure: ${this.lintResult.summary.totalRemainingErrorNumber} error(s) have been found \n ${this.lintResult.summary.totalRemainingWarningNumber} warning(s) have been found \n ${this.lintResult.summary.totalRemainingInfoNumber} info(s) have been found`,
|
|
526
525
|
);
|
|
527
526
|
}
|
|
528
527
|
this.status = 1;
|
|
@@ -530,4 +529,4 @@ class NpmGroovyLint {
|
|
|
530
529
|
}
|
|
531
530
|
}
|
|
532
531
|
|
|
533
|
-
|
|
532
|
+
export default NpmGroovyLint;
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#! /usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
import NpmGroovyLint from "./groovy-lint.js";
|
|
4
|
+
import { pathToFileURL } from "url";
|
|
5
5
|
|
|
6
6
|
// Create linter/formatter/fixer with arguments
|
|
7
7
|
const linter = new NpmGroovyLint(process.argv, { origin: "index" });
|
|
@@ -15,7 +15,7 @@ const linter = new NpmGroovyLint(process.argv, { origin: "index" });
|
|
|
15
15
|
console.error("Unexpected error: " + err.message + "\n" + err.stack);
|
|
16
16
|
process.exitCode = 2;
|
|
17
17
|
// Quit if called by CLI and not as a module
|
|
18
|
-
if (
|
|
18
|
+
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
19
19
|
process.exit();
|
|
20
20
|
}
|
|
21
21
|
}
|
|
Binary file
|
|
Binary file
|