Skip to content

Commit

Permalink
Fix space before opening brace rule + parsing + add .gvy and .nf defa…
Browse files Browse the repository at this point in the history
…ult file extensions (#97)

- Fix [(#96)](#96) --fix adds redundant space into `${VARIABLE}` (SpaceBeforeOpeningBrace fix rule error)
- Fix grails framework detection
- Fix Groovy parsing parsing when multiple files
- Add `.gvy` and `.nf` in default browsed files extensions
  • Loading branch information
nvuillam committed Sep 3, 2020
1 parent abe0258 commit 7fcc25f
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 18 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [7.5.1] 2020-09-02

- Fix [(#96)](https://github.com/nvuillam/npm-groovy-lint/issues/96) --fix adds redundant space into `${VARIABLE}` (SpaceBeforeOpeningBrace fix rule error)
- Fix grails framework detection
- Fix Groovy parsing parsing when multiple files
- Add `.gvy` and `.nf` in default browsed files extensions

## [7.5.0] 2020-09-02

- Add anonymous framework usage stats for Groovy core Team
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@ Please follow [Contribution instructions](https://github.com/nvuillam/npm-groovy
## RELEASE NOTES
### [7.5.1] 2020-09-02
- Fix [(#96)](https://github.com/nvuillam/npm-groovy-lint/issues/96) --fix adds redundant space into `${VARIABLE}` (SpaceBeforeOpeningBrace fix rule error)
- Fix grails framework detection
- Fix Groovy parsing parsing when multiple files
- Add `.gvy` and `.nf` in default browsed files extensions
### [7.4.3] 2020-08-29
- Upgrade [java-caller](https://github.com/nvuillam/node-java-caller) to v2.2.0
Expand Down
18 changes: 12 additions & 6 deletions groovy/src/main/com/nvuillam/CodeNarcServer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,18 @@ class CodeNarcServer {
}
else if (bodyObj.codeNarcBaseDir) {
// Ant style pattern is used: list all files
println 'Ant file scanner in ' + bodyObj.codeNarcBaseDir + ', includes ' + bodyObj.codeNarcIncludes + ', excludes ' + bodyObj.codeNarcExcludes
println 'Ant file scanner in ' + bodyObj.codeNarcBaseDir + ', includes ' + bodyObj.codeNarcIncludes + ', excludes ' + ((bodyObj.codeNarcExcludes) ? bodyObj.codeNarcExcludes : 'no')
def ant = new groovy.ant.AntBuilder()
def scanner = ant.fileScanner {
fileset(dir: bodyObj.codeNarcBaseDir) {
include(name: bodyObj.codeNarcIncludes)
exclude(name : (bodyObj.codeNarcExcludes) ? bodyObj.codeNarcExcludes : 'no')
bodyObj.codeNarcIncludes.split(',').each { includeExpr ->
include(name: includeExpr)
}
if (bodyObj.codeNarcExcludes) {
bodyObj.codeNarcIncludes.split(',').each { excludeExpr ->
exclude(name: excludeExpr)
}
}
}
}
// Parse collected files
Expand Down Expand Up @@ -257,17 +263,17 @@ class CodeNarcServer {
catch (MultipleCompilationErrorsException ep) {
def excptnJsonTxt = JsonOutput.toJson(ep)
parseErrors = ep.getErrorCollector().getErrors()
println 'Parse error (MultipleCompilationErrorsException): ' + file.getAbsolutePath() + '\n' + excptnJsonTxt
println 'PARSE ERROR (MultipleCompilationErrorsException): ' + file.getAbsolutePath() + '\n' + excptnJsonTxt
}
catch (CompilationFailedException ep) {
def excptnJsonTxt = JsonOutput.toJson(ep)
parseErrors = ep.getErrorCollector().getErrors()
println 'Parse error (CompilationFailedException): ' + file.getAbsolutePath() + '\n' + excptnJsonTxt
println 'PARSE ERROR (CompilationFailedException): ' + file.getAbsolutePath() + '\n' + excptnJsonTxt
}
catch (Exception ep) {
def excptnJsonTxt = JsonOutput.toJson(ep)
parseErrors = ep.getErrorCollector().getErrors()
println 'Parse error (Other): ' + file.getAbsolutePath() + '\n' + excptnJsonTxt
println 'PARSE ERROR (Other): ' + file.getAbsolutePath() + '\n' + excptnJsonTxt
}
return parseErrors
}
Expand Down
24 changes: 18 additions & 6 deletions lib/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async function getFileStats(file) {

const frameworkDefs = [
{ name: "beakerx", priority: 3 }, // UNDEFINED
{ name: "codenarc", priority: 3, sourceIncludes: ["codenarc", "groovy-lint"] },
{ name: "codenarc", priority: 3, sourceIncludes: ["codenarc", "groovylint"] },
{ name: "dru", priority: 2, packages: ["com.agorapulse.dru"] },
{ name: "ersatz", priority: 2, packages: ["com.stehno.ersatz"] },
{ name: "gaelyk", priority: 2, packages: ["groovyx.gaelyk"] },
Expand All @@ -212,7 +212,7 @@ const frameworkDefs = [
{ name: "geb", priority: 3, packages: ["geb"] },
{ name: "gperfutils", priority: 3, packages: ["groovyx.gbench", "groovyx.gprof"] },
{ name: "gradle", priority: 1, fileExtensions: [".gradle"] },
{ name: "grails", priority: 1 }, // UNDEFINED
{ name: "grails", priority: 1, filePathIncludes: ["grails-app"] },
{ name: "grain", priority: 2, packages: ["com.sysgears"] },
{ name: "griffon", priority: 2, sourceIncludes: ["griffon"] },
{ name: "groocss", priority: 2, packages: ["org.groocss"] },
Expand Down Expand Up @@ -242,8 +242,10 @@ const frameworkDefs = [

function listFileUsedFrameworks(file, source) {
const frameworksUsed = [];
const fileBaseName = path.basename(file);
const fileExtname = path.extname(file);
for (const frameworkDef of frameworkDefs) {
if (isUsedFramework(frameworkDef, file, source)) {
if (isUsedFramework(frameworkDef, file, source, fileExtname, fileBaseName)) {
frameworksUsed.push(frameworkDef);
}
}
Expand All @@ -254,11 +256,11 @@ function listFileUsedFrameworks(file, source) {
return frameworkKeys;
}

function isUsedFramework(frameworkDef, file, source) {
function isUsedFramework(frameworkDef, file, source, fileExtname, fileBaseName) {
// Check file extension
if (frameworkDef.fileExtensions) {
for (const ext of frameworkDef.fileExtensions) {
if (path.extname(file) === ext) {
if (fileExtname === ext) {
return true;
}
}
Expand All @@ -282,11 +284,21 @@ function isUsedFramework(frameworkDef, file, source) {
// Check file name
if (frameworkDef.fileNameIncludes) {
for (const str of frameworkDef.fileNameIncludes) {
if (path.basename(file).includes(str)) {
if (fileBaseName.includes(str)) {
return true;
}
}
}

// Check file path
if (frameworkDef.filePathIncludes) {
for (const str of frameworkDef.filePathIncludes) {
if (file.includes(str)) {
return true;
}
}
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/codenarc-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function prepareCodeNarcCall(options) {
}

// Build ruleSet & file CodeNarc arguments
let defaultFilesPattern = "**/*.groovy,**/Jenkinsfile,**/*.gradle";
let defaultFilesPattern = "**/*.groovy,**/*.gvy,**/Jenkinsfile,**/*.gradle,**/*.nf";

// RuleSet codeNarc arg
const rulesetFileArgs = options.rulesets.startsWith("file:") ? options.rulesets : `file:${options.rulesets.replace(/^"(.*)"$/, "$1")}`;
Expand Down
2 changes: 1 addition & 1 deletion lib/example/SampleFileSmall.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import groovy.io.FileType
import groovy.json.*
import groovy.time.TimeCategory
import static groovyx.gpars.GParsPool.withPool
import static groovyx2.gpars.GParsPool.withPool

def script = new GroovyScriptEngine( '.' ).with{
loadScriptByName( 'Utils.groovy' ) ;
Expand Down
2 changes: 2 additions & 0 deletions lib/example/grails-app/dummy.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* groovylint-disable-next-line CompileStatic */
println 'Toto'
Binary file modified lib/java/CodeNarcServer.jar
Binary file not shown.
32 changes: 31 additions & 1 deletion lib/rules/SpaceBeforeOpeningBrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const rule = {
label: "Add space before opening brace",
type: "function",
func: line => {
const regexMatch = line.match(new RegExp(/[^ ]{/, "g"));
const regexMatch = line.match(new RegExp(/[^ |$]{/, "g"));
if (regexMatch && regexMatch[0]) {
line = line.replace(regexMatch[0], regexMatch[0][0] + " {");
}
Expand All @@ -28,6 +28,36 @@ class MyOtherClass extends AbstractClass{ }
sourceAfter: `
class MyClass { }
class MyOtherClass extends AbstractClass { }
`
},
{
sourceBefore: `
pipeline {
stages {
stage('CleanWorkspace') {
steps {
cleanWs()
dir("../\${JOB_NAME}@2"){
deleteDir()
}
}
}
}
}
`,
sourceAfter: `
pipeline {
stages {
stage('CleanWorkspace') {
steps {
cleanWs()
dir("../\${JOB_NAME}@2") {
deleteDir()
}
}
}
}
}
`
}
]
Expand Down
5 changes: 3 additions & 2 deletions lib/test/lint-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ describe("Lint with API", () => {
});

it("(API:files) should ignore fake_node_modules pattern", async () => {
const lintedFilesNb = 10;
const npmGroovyLintConfig = {
files: "**/*.groovy",
ignorepattern: "**/fake_node_modules/**",
Expand All @@ -138,8 +139,8 @@ describe("Lint with API", () => {
const linter = await new NpmGroovyLint(npmGroovyLintConfig, {}).run();
assert(!linter.outputString.includes(`ToIgnore.groovy`), "ToIgnore.groovy has been ignored");
assert(
linter.outputString.includes(`npm-groovy-lint results in ${c.bold(9)} linted files`),
"Number of linted files is displayed in summary"
linter.outputString.includes(`npm-groovy-lint results in ${c.bold(lintedFilesNb)} linted files`),
`Number of linted files is displayed in summary: ${c.bold(lintedFilesNb)}`
);
});

Expand Down
2 changes: 1 addition & 1 deletion lib/test/lint-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("Lint with executable", () => {
}
assert(stdout, "stdout is set");
assert(!stdout.includes(`ToIgnore.groovy`), `ToIgnore.groovy has been ignored \n${stdout}`);
assert(stdout.includes(`npm-groovy-lint results in ${c.bold(9)} linted files`), `Number of linted files is displayed in summary \n${stdout}`);
assert(stdout.includes(`npm-groovy-lint results in ${c.bold(10)} linted files`), `Number of linted files is displayed in summary \n${stdout}`);
});

it("(EXE:file) should generate codenarc HTML file report", async () => {
Expand Down
11 changes: 11 additions & 0 deletions lib/test/miscellaneous.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ describe("Miscellaneous", function() {
assert(linter.startElapse != null, "Anonymous stats has not been sent");
});

it("(API:source) send anonymous usage statistics (2)", async () => {
const npmGroovyLintConfig = {
path: "./lib/example/grails-app",
returnrules: true,
output: "txt"
};
const linter = await new NpmGroovyLint(npmGroovyLintConfig, {}).run();
assert(linter.status === 0, `Linter status is 0 (${linter.status} returned)`);
assert(linter.startElapse != null, "Anonymous stats has not been sent");
});

it("(API:source) should use a CodeNarc ruleset defined in groovylintrc.json", async () => {
const npmGroovyLintConfig = {
config: "./lib/example/.groovylintrc-codenarc-rulesets.json",
Expand Down

0 comments on commit 7fcc25f

Please sign in to comment.