From be9d5b8e686fb4cbd9a539a969121dc1297bc834 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Mon, 14 Jun 2021 18:06:37 +0500 Subject: [PATCH 1/2] fix: doxygen header enforces double asterisks --- src/rules/file/hasDoxygenHeader.spec.ts | 54 ++++++++++++++++++++++++- src/rules/file/hasDoxygenHeader.ts | 29 +++++++++++-- 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/rules/file/hasDoxygenHeader.spec.ts b/src/rules/file/hasDoxygenHeader.spec.ts index e29c59c..a9eaff4 100644 --- a/src/rules/file/hasDoxygenHeader.spec.ts +++ b/src/rules/file/hasDoxygenHeader.spec.ts @@ -17,6 +17,32 @@ describe('hasDoxygenHeader - test', () => { expect(hasDoxygenHeader.test(content)).toEqual([]) }) + it('should return an empty array when the file starts with a doxygen header', () => { + const content = ` + + + /* + @file + @brief Returns an unused libref + */ + + %macro mf_getuniquelibref(prefix=mclib,maxtries=1000); + %local x libref; + %let x={SAS002}; + %do x=0 %to &maxtries;` + + expect(hasDoxygenHeader.test(content)).toEqual([ + { + message: + 'File not following Doxygen header style, use double asterisks', + lineNumber: 4, + startColumnNumber: 1, + endColumnNumber: 1, + severity: Severity.Warning + } + ]) + }) + it('should return an array with a single diagnostic when the file has no header', () => { const content = ` %macro mf_getuniquelibref(prefix=mclib,maxtries=1000); @@ -86,7 +112,33 @@ describe('hasDoxygenHeader - fix', () => { expect(hasDoxygenHeader.fix!(content)).toEqual(content) }) - it('should should add a doxygen header if not present', () => { + it('should update single asterisks to double if a doxygen header is already present', () => { + const contentOriginal = ` + /* + @file + @brief Returns an unused libref + */ + + %macro mf_getuniquelibref(prefix=mclib,maxtries=1000); + %local x libref; + %let x={SAS002}; + %do x=0 %to &maxtries;` + + const contentExpected = ` + /** + @file + @brief Returns an unused libref + **/ + + %macro mf_getuniquelibref(prefix=mclib,maxtries=1000); + %local x libref; + %let x={SAS002}; + %do x=0 %to &maxtries;` + + expect(hasDoxygenHeader.fix!(contentOriginal)).toEqual(contentExpected) + }) + + it('should add a doxygen header if not present', () => { const content = `%macro mf_getuniquelibref(prefix=mclib,maxtries=1000); %local x libref; %let x={SAS002}; diff --git a/src/rules/file/hasDoxygenHeader.ts b/src/rules/file/hasDoxygenHeader.ts index 2a5c75b..1788ed5 100644 --- a/src/rules/file/hasDoxygenHeader.ts +++ b/src/rules/file/hasDoxygenHeader.ts @@ -10,10 +10,28 @@ const name = 'hasDoxygenHeader' const description = 'Enforce the presence of a Doxygen header at the start of each file.' const message = 'File missing Doxygen header' -const test = (value: string) => { +const messageForSingleAsterisk = + 'File not following Doxygen header style, use double asterisks' +const test = (value: string, config?: LintConfig) => { + const lineEnding = config?.lineEndings === LineEndings.CRLF ? '\r\n' : '\n' try { - const hasFileHeader = value.trimStart().startsWith('/*') + const hasFileHeader = value.trimStart().startsWith('/**') if (hasFileHeader) return [] + + const hasFileHeaderWithSingleAsterisk = value.trimStart().startsWith('/*') + if (hasFileHeaderWithSingleAsterisk) + return [ + { + message: messageForSingleAsterisk, + lineNumber: + (value.split('/*')![0]!.match(new RegExp(lineEnding, 'g')) ?? []) + .length + 1, + startColumnNumber: 1, + endColumnNumber: 1, + severity: Severity.Warning + } + ] + return [ { message, @@ -37,9 +55,12 @@ const test = (value: string) => { } const fix = (value: string, config?: LintConfig): string => { - if (test(value).length === 0) { + const result = test(value, config) + if (result.length === 0) { return value - } + } else if (result[0].message == messageForSingleAsterisk) + return value.replace('/*', '/**').replace('*/', '**/') + const lineEndingConfig = config?.lineEndings || LineEndings.LF const lineEnding = lineEndingConfig === LineEndings.LF ? '\n' : '\r\n' From 95502647e8dd95104956948e6302bf9e4e4ae92a Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Mon, 14 Jun 2021 18:16:05 +0500 Subject: [PATCH 2/2] fix: doxygen header requires to only start with double asterisks --- src/rules/file/hasDoxygenHeader.spec.ts | 2 +- src/rules/file/hasDoxygenHeader.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rules/file/hasDoxygenHeader.spec.ts b/src/rules/file/hasDoxygenHeader.spec.ts index a9eaff4..23af9c6 100644 --- a/src/rules/file/hasDoxygenHeader.spec.ts +++ b/src/rules/file/hasDoxygenHeader.spec.ts @@ -128,7 +128,7 @@ describe('hasDoxygenHeader - fix', () => { /** @file @brief Returns an unused libref - **/ + */ %macro mf_getuniquelibref(prefix=mclib,maxtries=1000); %local x libref; diff --git a/src/rules/file/hasDoxygenHeader.ts b/src/rules/file/hasDoxygenHeader.ts index 1788ed5..2a81d55 100644 --- a/src/rules/file/hasDoxygenHeader.ts +++ b/src/rules/file/hasDoxygenHeader.ts @@ -59,7 +59,7 @@ const fix = (value: string, config?: LintConfig): string => { if (result.length === 0) { return value } else if (result[0].message == messageForSingleAsterisk) - return value.replace('/*', '/**').replace('*/', '**/') + return value.replace('/*', '/**') const lineEndingConfig = config?.lineEndings || LineEndings.LF const lineEnding = lineEndingConfig === LineEndings.LF ? '\n' : '\r\n'