1
0
mirror of https://github.com/sasjs/lint.git synced 2025-12-10 17:34:36 +00:00

fix(hasMacroNameInMend): added support for comments having code in it

This commit is contained in:
Saad Jutt
2021-04-06 14:34:51 +05:00
parent 2f07bfa0a1
commit 443bdc0a50
2 changed files with 65 additions and 6 deletions

View File

@@ -164,6 +164,48 @@ describe('hasMacroNameInMend', () => {
expect(hasMacroNameInMend.test(content)).toEqual([])
})
it('should return an array with a single diagnostic when %mend has correct macro name having code in comments', () => {
const content = `/**
@file examplemacro.sas
@brief an example of a macro to be used in a service
@details This macro is great. Yadda yadda yadda. Usage:
* code formatting applies when indented by 4 spaces; code formatting applies when indented by 4 spaces; code formatting applies when indented by 4 spaces; code formatting applies when indented by 4 spaces; code formatting applies when indented by 4 spaces;
some code
%macro examplemacro123();
%examplemacro()
<h4> SAS Macros </h4>
@li doesnothing.sas
@author Allan Bowe
**/
%macro examplemacro();
proc sql;
create table areas
as select area
from sashelp.springs;
%doesnothing();
%mend;`
expect(hasMacroNameInMend.test(content)).toEqual([
{
message: '%mend missing macro name',
lineNumber: 29,
startColumnNumber: 5,
endColumnNumber: 11,
severity: Severity.Warning
}
])
})
it('should return an array with a single diagnostic when %mend has incorrect macro name', () => {
const content = `
%macro somemacro;

View File

@@ -12,8 +12,14 @@ const test = (value: string) => {
const statements: string[] = value ? value.split(';') : []
const stack: string[] = []
let trimmedStatement = '',
commentStarted = false
statements.forEach((statement, index) => {
const trimmedStatement = trimComments(statement).trim()
;({ statement: trimmedStatement, commentStarted } = trimComments(
statement,
commentStarted
))
if (trimmedStatement.startsWith('%macro ')) {
const macroName = trimmedStatement
.split(' ')
@@ -58,13 +64,24 @@ const test = (value: string) => {
return diagnostics
}
const trimComments = (statement: string): string => {
const trimComments = (
statement: string,
commentStarted: boolean = false
): { statement: string; commentStarted: boolean } => {
let trimmed = statement.trim()
if (trimmed.startsWith('/*'))
trimmed = (trimmed.split('*/').pop() as string).trim()
return trimmed
if (commentStarted || trimmed.startsWith('/*')) {
const parts = trimmed.split('*/')
if (parts.length > 1) {
return {
statement: (parts.pop() as string).trim(),
commentStarted: false
}
} else {
return { statement: '', commentStarted: true }
}
}
return { statement: trimmed, commentStarted: false }
}
const getLineNumber = (statements: string[], index: number): number => {