Skip to content

Commit

Permalink
Merge pull request #55 from sasjs/macro-declaration-multi-line
Browse files Browse the repository at this point in the history
feat: mult-line macro declarations
  • Loading branch information
krishna-acondy authored Jun 9, 2021
2 parents 11182aa + 0790a44 commit b13302a
Show file tree
Hide file tree
Showing 20 changed files with 718 additions and 310 deletions.
8 changes: 4 additions & 4 deletions src/format/formatText.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ describe('formatText', () => {
new LintConfig(getLintConfigModule.DefaultLintConfiguration)
)
)
const text = `%macro test
const text = `%macro test;
%put 'hello';\r\n%mend; `

const expectedOutput = `/**
@file
@brief <Your brief here>
<h4> SAS Macros </h4>
**/\n%macro test
**/\n%macro test;
%put 'hello';\n%mend test;`

const output = await formatText(text)
Expand All @@ -38,9 +38,9 @@ describe('formatText', () => {
})
)
)
const text = `%macro test\n %put 'hello';\r\n%mend; `
const text = `%macro test;\n %put 'hello';\r\n%mend; `

const expectedOutput = `/**\r\n @file\r\n @brief <Your brief here>\r\n <h4> SAS Macros </h4>\r\n**/\r\n%macro test\r\n %put 'hello';\r\n%mend test;`
const expectedOutput = `/**\r\n @file\r\n @brief <Your brief here>\r\n <h4> SAS Macros </h4>\r\n**/\r\n%macro test;\r\n %put 'hello';\r\n%mend test;`

const output = await formatText(text)

Expand Down
2 changes: 1 addition & 1 deletion src/rules/file/hasMacroNameInMend.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('hasMacroNameInMend - test', () => {
it('should return an array with a diagnostic for each macro missing an %mend statement', () => {
const content = `%macro somemacro;
%put &sysmacroname;
%macro othermacro`
%macro othermacro;`

expect(hasMacroNameInMend.test(content)).toEqual([
{
Expand Down
16 changes: 11 additions & 5 deletions src/rules/file/hasMacroNameInMend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const test = (value: string, config?: LintConfig) => {
const macros = parseMacros(value, config)
const diagnostics: Diagnostic[] = []
macros.forEach((macro) => {
if (macro.startLineNumber === null && macro.endLineNumber !== null) {
if (macro.startLineNumbers.length === 0 && macro.endLineNumber !== null) {
const endLine = lines[macro.endLineNumber - 1]
diagnostics.push({
message: `%mend statement is redundant`,
Expand All @@ -27,10 +27,13 @@ const test = (value: string, config?: LintConfig) => {
getColumnNumber(endLine, '%mend') + macro.termination.length,
severity: Severity.Warning
})
} else if (macro.endLineNumber === null && macro.startLineNumber !== null) {
} else if (
macro.endLineNumber === null &&
macro.startLineNumbers.length !== 0
) {
diagnostics.push({
message: `Missing %mend statement for macro - ${macro.name}`,
lineNumber: macro.startLineNumber,
lineNumber: macro.startLineNumbers![0],
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
Expand Down Expand Up @@ -73,7 +76,7 @@ const fix = (value: string, config?: LintConfig): string => {
const macros = parseMacros(value, config)

macros.forEach((macro) => {
if (macro.startLineNumber === null && macro.endLineNumber !== null) {
if (macro.startLineNumbers.length === 0 && macro.endLineNumber !== null) {
// %mend statement is redundant
const endLine = lines[macro.endLineNumber - 1]
const startColumnNumber = getColumnNumber(endLine, '%mend')
Expand All @@ -83,7 +86,10 @@ const fix = (value: string, config?: LintConfig): string => {
const beforeStatement = endLine.slice(0, startColumnNumber - 1)
const afterStatement = endLine.slice(endColumnNumber)
lines[macro.endLineNumber - 1] = beforeStatement + afterStatement
} else if (macro.endLineNumber === null && macro.startLineNumber !== null) {
} else if (
macro.endLineNumber === null &&
macro.startLineNumbers.length !== 0
) {
// missing %mend statement
} else if (macro.mismatchedMendMacroName) {
// mismatched macro name
Expand Down
14 changes: 0 additions & 14 deletions src/rules/file/hasMacroParentheses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,4 @@ describe('hasMacroParentheses', () => {
])
})
})

it('should return an array with a single diagnostic when a macro definition contains a space', () => {
const content = `%macro test ()`

expect(hasMacroParentheses.test(content)).toEqual([
{
message: 'Macro definition contains space(s)',
lineNumber: 1,
startColumnNumber: 8,
endColumnNumber: 14,
severity: Severity.Warning
}
])
})
})
38 changes: 19 additions & 19 deletions src/rules/file/hasMacroParentheses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@ const test = (value: string, config?: LintConfig) => {
if (!macro.name) {
diagnostics.push({
message: 'Macro definition missing name',
lineNumber: macro.startLineNumber!,
startColumnNumber: getColumnNumber(macro.declarationLine, '%macro'),
lineNumber: macro.startLineNumbers![0],
startColumnNumber: getColumnNumber(
macro.declarationLines![0],
'%macro'
),
endColumnNumber:
getColumnNumber(macro.declarationLine, '%macro') +
getColumnNumber(macro.declarationLines![0], '%macro') +
macro.declaration.length,
severity: Severity.Warning
})
} else if (!macro.declarationLine.includes('(')) {
} else if (!macro.declarationLines.find((dl) => dl.includes('('))) {
const macroNameLineIndex = macro.declarationLines.findIndex((dl) =>
dl.includes(macro.name)
)
diagnostics.push({
message,
lineNumber: macro.startLineNumber!,
startColumnNumber: getColumnNumber(macro.declarationLine, macro.name),
lineNumber: macro.startLineNumbers![macroNameLineIndex],
startColumnNumber: getColumnNumber(
macro.declarationLines[macroNameLineIndex],
macro.name
),
endColumnNumber:
getColumnNumber(macro.declarationLine, macro.name) +
getColumnNumber(
macro.declarationLines[macroNameLineIndex],
macro.name
) +
macro.name.length -
1,
severity: Severity.Warning
})
} else if (macro.name !== macro.name.trim()) {
diagnostics.push({
message: 'Macro definition contains space(s)',
lineNumber: macro.startLineNumber!,
startColumnNumber: getColumnNumber(macro.declarationLine, macro.name),
endColumnNumber:
getColumnNumber(macro.declarationLine, macro.name) +
macro.name.length -
1 +
`()`.length,
severity: Severity.Warning
})
}
})

Expand Down
1 change: 1 addition & 0 deletions src/rules/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { hasMacroNameInMend } from './hasMacroNameInMend'
export { hasMacroParentheses } from './hasMacroParentheses'
export { lineEndings } from './lineEndings'
export { noNestedMacros } from './noNestedMacros'
export { strictMacroDefinition } from './strictMacroDefinition'
8 changes: 4 additions & 4 deletions src/rules/file/noNestedMacros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ const test = (value: string, config?: LintConfig) => {
message: message
.replace('{macro}', macro.name)
.replace('{parent}', macro.parentMacro),
lineNumber: macro.startLineNumber as number,
lineNumber: macro.startLineNumbers![0] as number,
startColumnNumber: getColumnNumber(
lines[(macro.startLineNumber as number) - 1],
lines[(macro.startLineNumbers![0] as number) - 1],
'%macro'
),
endColumnNumber:
getColumnNumber(
lines[(macro.startLineNumber as number) - 1],
lines[(macro.startLineNumbers![0] as number) - 1],
'%macro'
) +
lines[(macro.startLineNumber as number) - 1].trim().length -
lines[(macro.startLineNumbers![0] as number) - 1].trim().length -
1,
severity: Severity.Warning
})
Expand Down
Loading

0 comments on commit b13302a

Please sign in to comment.