1
0
mirror of https://github.com/sasjs/lint.git synced 2026-01-17 01:00:06 +00:00

Merge pull request #71 from sasjs/doxygen-header-enforces-double-asterisks

fix: doxygen header enforces double asterisks
This commit is contained in:
Allan Bowe
2021-06-15 10:41:18 +03:00
committed by GitHub
2 changed files with 78 additions and 5 deletions

View File

@@ -17,6 +17,32 @@ describe('hasDoxygenHeader - test', () => {
expect(hasDoxygenHeader.test(content)).toEqual([]) 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', () => { it('should return an array with a single diagnostic when the file has no header', () => {
const content = ` const content = `
%macro mf_getuniquelibref(prefix=mclib,maxtries=1000); %macro mf_getuniquelibref(prefix=mclib,maxtries=1000);
@@ -86,7 +112,33 @@ describe('hasDoxygenHeader - fix', () => {
expect(hasDoxygenHeader.fix!(content)).toEqual(content) 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); const content = `%macro mf_getuniquelibref(prefix=mclib,maxtries=1000);
%local x libref; %local x libref;
%let x={SAS002}; %let x={SAS002};

View File

@@ -10,10 +10,28 @@ const name = 'hasDoxygenHeader'
const description = const description =
'Enforce the presence of a Doxygen header at the start of each file.' 'Enforce the presence of a Doxygen header at the start of each file.'
const message = 'File missing Doxygen header' 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 { try {
const hasFileHeader = value.trimStart().startsWith('/*') const hasFileHeader = value.trimStart().startsWith('/**')
if (hasFileHeader) return [] 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 [ return [
{ {
message, message,
@@ -37,9 +55,12 @@ const test = (value: string) => {
} }
const fix = (value: string, config?: LintConfig): string => { const fix = (value: string, config?: LintConfig): string => {
if (test(value).length === 0) { const result = test(value, config)
if (result.length === 0) {
return value return value
} } else if (result[0].message == messageForSingleAsterisk)
return value.replace('/*', '/**')
const lineEndingConfig = config?.lineEndings || LineEndings.LF const lineEndingConfig = config?.lineEndings || LineEndings.LF
const lineEnding = lineEndingConfig === LineEndings.LF ? '\n' : '\r\n' const lineEnding = lineEndingConfig === LineEndings.LF ? '\n' : '\r\n'