npm-groovy-lint
Advanced tools
Comparing version 4.6.0 to 4.7.0
# Changelog | ||
## [4.7.0] 2020-05-06 | ||
- New fix rules | ||
- BracesForClass | ||
- BracesForForLoop | ||
- BracesForIfElse | ||
- BracesForMethod | ||
- BracesForTryCatchFinally | ||
- ExplicitArrayListInstantiation | ||
- MissingBlankLineAfterImports | ||
- MissingBlankLineAfterPackage | ||
- Updated fix rules | ||
- UnnecessaryGString: Fix replacements containing `\n` and `\r` | ||
## [4.6.0] 2020-05-01 | ||
@@ -4,0 +19,0 @@ |
@@ -102,2 +102,3 @@ // Additional definition for codenarc rules ( get position & available fix) | ||
"UnnecessaryToString", | ||
"ExplicitArrayListInstantiation", | ||
"SpaceBeforeOpeningBrace", | ||
@@ -127,4 +128,11 @@ "SpaceAfterOpeningBrace", | ||
"BlockEndsWithBlankLine", | ||
"BracesForClass", | ||
"BracesForForLoop", | ||
"BracesForIfElse", | ||
"BracesForMethod", | ||
"BracesForTryCatchFinally", | ||
"ClassStartsWithBlankLine", | ||
"ClassEndsWithBlankLine", | ||
"MissingBlankLineAfterPackage", | ||
"MissingBlankLineAfterImports", | ||
"MisorderedStaticImports", | ||
@@ -131,0 +139,0 @@ "IfStatementBraces", |
// Braces for if else | ||
const { findRangeBetweenStrings } = require("../utils"); | ||
const { moveOpeningBracket, findRangeBetweenStrings } = require("../utils"); | ||
const rule = { | ||
scope: "file", | ||
range: { | ||
@@ -11,5 +12,56 @@ type: "function", | ||
} | ||
} | ||
}, | ||
fix: { | ||
label: "Move opening brace on the same line", | ||
type: "function", | ||
func: (allLines, variables) => { | ||
return moveOpeningBracket(allLines, variables); | ||
} | ||
}, | ||
tests: [ | ||
{ | ||
sourceBefore: ` | ||
if (true) | ||
{ | ||
def a = 1 | ||
} | ||
`, | ||
sourceAfter: ` | ||
if (true) { | ||
def a = 1 | ||
} | ||
` | ||
}, | ||
{ | ||
sourceBefore: ` | ||
if (true) | ||
{ def a = 1 | ||
} | ||
`, | ||
sourceAfter: ` | ||
if (true) { | ||
def a = 1 | ||
} | ||
` | ||
}, | ||
{ | ||
sourceBefore: ` | ||
if (true && | ||
true && | ||
true ) | ||
{ | ||
def a = 1 | ||
} | ||
`, | ||
sourceAfter: ` | ||
if (true && | ||
true && | ||
true ) { | ||
def a = 1 | ||
} | ||
` | ||
} | ||
] | ||
}; | ||
module.exports = { rule }; |
@@ -32,2 +32,26 @@ // Unnecessary Groovy String | ||
` | ||
}, | ||
{ | ||
sourceBefore: ` | ||
String str = 'lelamanul' + "\\n" | ||
`, | ||
sourceAfter: ` | ||
String str = 'lelamanul' + '\\n' | ||
` | ||
}, | ||
{ | ||
sourceBefore: ` | ||
String str = 'lelamanul' + "\\n\\r" | ||
`, | ||
sourceAfter: ` | ||
String str = 'lelamanul' + '\\n\\r' | ||
` | ||
}, | ||
{ | ||
sourceBefore: ` | ||
String str = 'lelamanul' + "\\n\\r\\n" + "titi\\n" + "\\n\\r" + "lelamanul\\nwesh" | ||
`, | ||
sourceAfter: ` | ||
String str = 'lelamanul' + '\\n\\r\\n' + 'titi\\n' + '\\n\\r' + 'lelamanul\\nwesh' | ||
` | ||
} | ||
@@ -34,0 +58,0 @@ ] |
@@ -28,2 +28,3 @@ // Shared functions | ||
if (varDef.regex) { | ||
msg = msg.replace(/\n/g, "\\n").replace(/\r/g, "\\r"); | ||
const regexRes = msg.match(varDef.regex); | ||
@@ -222,2 +223,18 @@ if (regexRes && regexRes.length > 1) { | ||
// Move the opening bracket at the same position than its related expression | ||
function moveOpeningBracket(allLines, variables) { | ||
const range = getVariable(variables, "range", { mandatory: true }); | ||
// Add bracket after if | ||
const addedBracketLine = allLines[range.end.line - 2].trimEnd() + " {"; | ||
allLines[range.end.line - 2] = addedBracketLine; | ||
// Remove bracket which was on the wrong line | ||
const removedBracketLine = allLines[range.end.line - 1].substring(allLines[range.end.line - 1].indexOf("{") + 1).trimEnd(); | ||
allLines[range.end.line - 1] = removedBracketLine; | ||
// Remove removed bracket line if empty | ||
if (allLines[range.end.line - 1].trim() === "") { | ||
allLines.splice(range.end.line - 1, 1); | ||
} | ||
return allLines; | ||
} | ||
// Check if a substring is between quotes in a string | ||
@@ -272,3 +289,4 @@ function notBetweenQuotesOrComment(str, substr) { | ||
getVariableRange, | ||
isValidCodeLine | ||
isValidCodeLine, | ||
moveOpeningBracket | ||
}; |
{ | ||
"name": "npm-groovy-lint", | ||
"version": "4.6.0", | ||
"version": "4.7.0", | ||
"description": "NPM CodeNarc wrapper to easily lint Groovy files", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
122
README.md
@@ -1,6 +0,6 @@ | ||
# NPM GROOVY LINT (+ Format & Auto-fix !) | ||
# NPM GROOVY LINT (+ Format & Auto-fix) | ||
[![Version](https://img.shields.io/npm/v/npm-groovy-lint.svg)](https://npmjs.org/package/npm-groovy-lint) | ||
[![Downloads/week](https://img.shields.io/npm/dw/npm-groovy-lint.svg)](https://npmjs.org/package/npm-groovy-lint) | ||
[![Downloads/total](https://img.shields.io/npm/dt/npm-groovy-lint.svg)](https://npmjs.org/package/npm-groovy-lint) | ||
[![Downloads/week](https://img.shields.io/npm/dw/npm-groovy-lint.svg)](https://npmjs.org/package/npm-groovy-lint) | ||
[![Downloads/total](https://img.shields.io/npm/dt/npm-groovy-lint.svg)](https://npmjs.org/package/npm-groovy-lint) | ||
[![CircleCI](https://circleci.com/gh/nvuillam/npm-groovy-lint/tree/master.svg?style=shield)](https://circleci.com/gh/nvuillam/npm-groovy-lint/tree/master) | ||
@@ -10,8 +10,8 @@ [![codecov](https://codecov.io/gh/nvuillam/npm-groovy-lint/branch/master/graph/badge.svg)](https://codecov.io/gh/nvuillam/npm-groovy-lint) | ||
[![GitHub stars](https://img.shields.io/github/stars/nvuillam/npm-groovy-lint?label=Star&maxAge=2592000)](https://GitHub.com/nvuillam/npm-groovy-lint/stargazers/) | ||
[![License](https://img.shields.io/npm/l/npm-groovy-lint.svg)](https://github.com/nvuillam/npm-groovy-lint/blob/master/package.json) | ||
[![License](https://img.shields.io/npm/l/npm-groovy-lint.svg)](https://github.com/nvuillam/npm-groovy-lint/blob/master/package.json) | ||
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) | ||
**Groovy / Jenkinsfile linter and autofixer** | ||
## Groovy & Jenkinsfile Linter, Formatter and Auto-fixer | ||
Based on [CodeNarc](http://codenarc.github.io/CodeNarc/) , this out of the box package allows to track groovy errors and correct a part of them | ||
Based on [CodeNarc](http://codenarc.github.io/CodeNarc/) , this out of the box package allows to **track groovy errors** and **correct a part of them** | ||
@@ -32,15 +32,15 @@ - Use option **--format** to format & prettify source code (beta) | ||
# INSTALLATION | ||
## INSTALLATION | ||
```shell | ||
npm install -g npm-groovy-lint | ||
``` | ||
$ npm install -g npm-groovy-lint | ||
``` | ||
Node.js >= 12 is required to run this package. If you can't upgrade, you can use [nvm](https://github.com/nvm-sh/nvm) to have [different node versions on your computer](https://www.sitepoint.com/quick-tip-multiple-versions-node-nvm/) | ||
# USAGE | ||
## USAGE | ||
```shell | ||
npm-groovy-lint OPTIONS | ||
``` | ||
$ npm-groovy-lint OPTIONS | ||
``` | ||
@@ -67,6 +67,6 @@ | Parameter | Type | Description | | ||
| --returnrules | Boolean | Return rules descriptions and URL if set | | ||
| --codenarcargs | String | Use core CodeNarc arguments (all npm-groovy-lint arguments will be ignored)<br/> Doc: http://codenarc.github.io/CodeNarc/codenarc-command-line.html<br/> Example: `npm-groovy-lint --codenarcargs -basedir="jdeploy-bundle/lib/example" -rulesetfiles="file:jdeploy-bundle/lib/example/RuleSet-Groovy.groovy" -maxPriority1Violations=0 -report="xml:ReportTestCodenarc.xml` | | ||
| -h<br/> --help | Boolean | Show help (npm-groovy-lint -h OPTIONNAME to see option detail with examples) | ||
| --codenarcargs | String | Use core CodeNarc arguments (all npm-groovy-lint arguments will be ignored)<br/> Doc: <http://codenarc.github.io/CodeNarc/codenarc-command-line.html><br/> Example: `npm-groovy-lint --codenarcargs -basedir="jdeploy-bundle/lib/example" -rulesetfiles="file:jdeploy-bundle/lib/example/RuleSet-Groovy.groovy" -maxPriority1Violations=0 -report="xml:ReportTestCodenarc.xml` | | ||
| -h<br/> --help | Boolean | Show help (npm-groovy-lint -h OPTIONNAME to see option detail with examples) | ||
# CONFIGURATION | ||
## CONFIGURATION | ||
@@ -79,11 +79,11 @@ Default rules definition ([`recommended`](https://github.com/nvuillam/npm-groovy-lint/blob/master/.groovylintrc-recommended.json), based on [`all`](https://github.com/nvuillam/npm-groovy-lint/blob/master/.groovylintrc-all.json) tracks a lot of errors, do not hesitate to ignore some of them (like NoDef ou RequiredVariableType) if they are too mean for your project. | ||
Format : | ||
Format : | ||
- extends: Name of a base configuration ([`recommended`](https://github.com/nvuillam/npm-groovy-lint/blob/master/.groovylintrc-recommended.json), [`recommended-jenkinsfile`](https://github.com/nvuillam/npm-groovy-lint/blob/master/.groovylintrc-recommended-jenkinsfile.json), [`all`](https://github.com/nvuillam/npm-groovy-lint/blob/master/.groovylintrc-all.json)) | ||
- rules: List of rules definition, following format `"RuleSection.RuleName": ruleParameters` or `"RuleName": ruleParameters` | ||
- "RuleSection.RuleName": any of the **[CodeNarc rules](https://codenarc.github.io/CodeNarc/codenarc-rule-index.html)** | ||
- "RuleSection.RuleName": any of the **[CodeNarc rules](https://codenarc.github.io/CodeNarc/codenarc-rule-index.html)** | ||
- ruleParameters: can be just a severity override ( `"off"`, `"error"`, `"warning"`, `"info"` ) , or a property list : | ||
- severity : off,error,warning,info | ||
- enabled : true (default) or false | ||
- any of the [rule advanced properties](https://codenarc.github.io/CodeNarc/codenarc-rule-index.html) | ||
- severity : off,error,warning,info | ||
- enabled : true (default) or false | ||
- any of the [rule advanced properties](https://codenarc.github.io/CodeNarc/codenarc-rule-index.html) | ||
@@ -119,21 +119,23 @@ Examples: | ||
# EXAMPLES | ||
## EXAMPLES | ||
- Lint groovy with JSON output | ||
```shell | ||
npm-groovy-lint --output json | ||
``` | ||
$ npm-groovy-lint --output json | ||
``` | ||
- Advanced config | ||
```shell | ||
npm-groovy-lint --path "./path/to/my/groovy/files" --files "**/*.groovy" --config "./config/codenarc/.groovylintrcCustom.js" --loglevel warning --output txt | ||
``` | ||
$ npm-groovy-lint --path "./path/to/my/groovy/files" --files "**/*.groovy" --config "./config/codenarc/.groovylintrcCustom.js" --loglevel warning --output txt | ||
``` | ||
- Lint using core CodeNarc parameters and generate HTML report file | ||
```shell | ||
npm-groovy-lint --codenarcargs -basedir="jdeploy-bundle/lib/example" -rulesetfiles="file:jdeploy-bundle/lib/example/RuleSet-Groovy.groovy" -title="TestTitleCodenarc" -maxPriority1Violations=0' -report="html:ReportTestCodenarc.html" | ||
``` | ||
$ npm-groovy-lint --codenarcargs -basedir="jdeploy-bundle/lib/example" -rulesetfiles="file:jdeploy-bundle/lib/example/RuleSet-Groovy.groovy" -title="TestTitleCodenarc" -maxPriority1Violations=0' -report="html:ReportTestCodenarc.html" | ||
``` | ||
# DISABLING RULES IN SOURCE | ||
## DISABLING RULES IN SOURCE | ||
@@ -143,2 +145,3 @@ You can disable rules directly by adding comment in file, using [eslint style](https://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-comments) | ||
To temporarily disable rule warnings in your file, use block comments in the following format: | ||
```groovy | ||
@@ -151,3 +154,5 @@ /* groovylint-disable */ | ||
``` | ||
You can also disable or enable warnings for specific rules: | ||
```groovy | ||
@@ -160,3 +165,5 @@ /* groovylint-disable NoDef, UnnecessarySemicolon */ | ||
``` | ||
To disable rule warnings in an entire file, put a /* groovylint-disable */ block comment at the top of the file: | ||
To disable rule warnings in an entire file, put a `/* groovylint-disable */` block comment at the top of the file: | ||
```groovy | ||
@@ -167,3 +174,5 @@ /* groovylint-disable */ | ||
``` | ||
You can also disable or enable specific rules for an entire file: | ||
```groovy | ||
@@ -174,3 +183,5 @@ /* groovylint-disable NoDef */ | ||
``` | ||
To disable all rules on a specific line, use a line or block comment in one of the following formats: | ||
```groovy | ||
@@ -187,3 +198,5 @@ def variable = 1; // groovylint-disable-line | ||
``` | ||
To disable a specific rule on a specific line: | ||
```groovy | ||
@@ -200,3 +213,5 @@ def variable = 1; // groovylint-disable-line NoDef | ||
``` | ||
To disable multiple rules on a specific line: | ||
```groovy | ||
@@ -214,13 +229,19 @@ def variable = 1; // groovylint-disable-line NoDef, UnnecessarySemicolon | ||
# AUTO-FIXABLE RULES (beta) | ||
## AUTO-FIXABLE RULES (beta) | ||
- BlockEndsWithBlankLine | ||
- BlockEndsWithBlankLine | ||
- BlockStartsWithBlankLine | ||
- ClassEndsWithBlankLine | ||
- BracesForClass | ||
- BracesForForLoop | ||
- BracesForIfElse | ||
- BracesForMethod | ||
- BracesForTryCatchFinally | ||
- ClassEndsWithBlankLine | ||
- ClassStartsWithBlankLine | ||
- ClosingBraceNotAlone | ||
- ConsecutiveBlankLines | ||
- ClosingBraceNotAlone | ||
- ConsecutiveBlankLines | ||
- ElseBlockBraces | ||
- FileEndsWithoutNewline | ||
- IfStatementBraces | ||
- ExplicitArrayListInstantiation | ||
- FileEndsWithoutNewline | ||
- IfStatementBraces | ||
- Indentation | ||
@@ -230,2 +251,4 @@ - IndentationClosingBraces | ||
- MisorderedStaticImports | ||
- MissingBlankLineAfterImports | ||
- MissingBlankLineAfterPackage | ||
- NoTabCharacter | ||
@@ -256,3 +279,3 @@ - SpaceAfterCatch | ||
# CALL VIA JS MODULE | ||
## CALL VIA JS MODULE | ||
@@ -262,5 +285,6 @@ You can import npm-groovy-lint into your NPM package and call lint & fix via module, using the same options than from npm-groovy-lint command line | ||
Example | ||
```shell | ||
npm install npm-groovy-lint --save | ||
``` | ||
$ npm install npm-groovy-lint --save | ||
``` | ||
@@ -270,3 +294,3 @@ ```javascript | ||
const fse = require("fs-extra"); | ||
const npmGroovyLintConfig = { | ||
@@ -283,7 +307,7 @@ source: fse.readFileSync('./lib/example/SampleFile.groovy').toString(), | ||
# TROUBLESHOOTING | ||
## TROUBLESHOOTING | ||
- On some environments, it has been observed that installed Groovy version must match Groovy embedded jars delivered with npm-groovy-lint (Groovy 3.0.3) | ||
# CONTRIBUTE | ||
## CONTRIBUTE | ||
@@ -294,13 +318,11 @@ Contributions are very welcome ! | ||
# THANKS | ||
## THANKS | ||
This package uses : | ||
- CodeNarc : https://github.com/CodeNarc/CodeNarc (groovy lint) | ||
- jdeploy : https://github.com/shannah/jdeploy (jar deployment and run) | ||
- slf4j : http://www.slf4j.org/ (logging for CodeNarc) | ||
- log4j : https://logging.apache.org/log4j/2.x/ (logging for CodeNarc) | ||
- GMetrics : https://dx42.github.io/gmetrics/ (Code measures for CodeNarc) | ||
- CodeNarc : <https://github.com/CodeNarc/CodeNarc> (groovy lint) | ||
- jdeploy : <https://github.com/shannah/jdeploy> (jar deployment and run) | ||
- slf4j : <http://www.slf4j.org/> (logging for CodeNarc) | ||
- log4j : <https://logging.apache.org/log4j/2.x/> (logging for CodeNarc) | ||
- GMetrics : <https://dx42.github.io/gmetrics/> (Code measures for CodeNarc) | ||
- Inspiration from [eslint](https://eslint.org/) about configuration and run patterns | ||
16403802
113
5618
310