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

feat: mult-line macro declarations

This commit is contained in:
Saad Jutt
2021-05-21 05:10:34 +05:00
parent 8bfb547427
commit af2d2c12c1
12 changed files with 325 additions and 86 deletions

View File

@@ -3,7 +3,7 @@ import { parseMacros } from './parseMacros'
describe('parseMacros', () => {
it('should return an array with a single macro', () => {
const text = `%macro test;
const text = ` %macro test;
%put 'hello';
%mend`
@@ -12,11 +12,11 @@ describe('parseMacros', () => {
expect(macros.length).toEqual(1)
expect(macros).toContainEqual({
name: 'test',
declarationLine: '%macro test;',
declarationLines: [' %macro test;'],
terminationLine: '%mend',
declaration: '%macro test',
termination: '%mend',
startLineNumber: 1,
startLineNumbers: [1],
endLineNumber: 3,
parentMacro: '',
hasMacroNameInMend: false,
@@ -34,11 +34,11 @@ describe('parseMacros', () => {
expect(macros.length).toEqual(1)
expect(macros).toContainEqual({
name: 'test',
declarationLine: '%macro test(var,sum);',
declarationLines: ['%macro test(var,sum);'],
terminationLine: '%mend',
declaration: '%macro test(var,sum)',
termination: '%mend',
startLineNumber: 1,
startLineNumbers: [1],
endLineNumber: 3,
parentMacro: '',
hasMacroNameInMend: false,
@@ -56,11 +56,11 @@ describe('parseMacros', () => {
expect(macros.length).toEqual(1)
expect(macros).toContainEqual({
name: 'test',
declarationLine: '%macro test/parmbuff;',
declarationLines: ['%macro test/parmbuff;'],
terminationLine: '%mend',
declaration: '%macro test/parmbuff',
termination: '%mend',
startLineNumber: 1,
startLineNumbers: [1],
endLineNumber: 3,
parentMacro: '',
hasMacroNameInMend: false,
@@ -79,11 +79,15 @@ describe('parseMacros', () => {
expect(macros.length).toEqual(1)
expect(macros).toContainEqual({
name: 'foobar',
declarationLine: '/* commentary */ %macro foobar(arg) /store source',
declarationLines: [
'/* commentary */ %macro foobar(arg) /store source',
' des="This macro does not do much";'
],
terminationLine: '%mend',
declaration: '%macro foobar(arg) /store source',
declaration:
'%macro foobar(arg) /store source des="This macro does not do much"',
termination: '%mend',
startLineNumber: 1,
startLineNumbers: [1, 2],
endLineNumber: 4,
parentMacro: '',
hasMacroNameInMend: false,
@@ -104,11 +108,11 @@ describe('parseMacros', () => {
expect(macros.length).toEqual(2)
expect(macros).toContainEqual({
name: 'foo',
declarationLine: '%macro foo;',
declarationLines: ['%macro foo;'],
terminationLine: '%mend;',
declaration: '%macro foo',
termination: '%mend',
startLineNumber: 1,
startLineNumbers: [1],
endLineNumber: 3,
parentMacro: '',
hasMacroNameInMend: false,
@@ -116,11 +120,11 @@ describe('parseMacros', () => {
})
expect(macros).toContainEqual({
name: 'bar',
declarationLine: '%macro bar();',
declarationLines: ['%macro bar();'],
terminationLine: '%mend bar;',
declaration: '%macro bar()',
termination: '%mend bar',
startLineNumber: 4,
startLineNumbers: [4],
endLineNumber: 6,
parentMacro: '',
hasMacroNameInMend: true,
@@ -129,9 +133,9 @@ describe('parseMacros', () => {
})
it('should detect nested macro definitions', () => {
const text = `%macro test()
const text = `%macro test();
%put 'hello';
%macro test2
%macro test2;
%put 'world;
%mend
%mend test`
@@ -141,11 +145,11 @@ describe('parseMacros', () => {
expect(macros.length).toEqual(2)
expect(macros).toContainEqual({
name: 'test',
declarationLine: '%macro test()',
declarationLines: ['%macro test();'],
terminationLine: '%mend test',
declaration: '%macro test()',
termination: '%mend test',
startLineNumber: 1,
startLineNumbers: [1],
endLineNumber: 6,
parentMacro: '',
hasMacroNameInMend: true,
@@ -153,15 +157,125 @@ describe('parseMacros', () => {
})
expect(macros).toContainEqual({
name: 'test2',
declarationLine: ' %macro test2',
declarationLines: [' %macro test2;'],
terminationLine: ' %mend',
declaration: '%macro test2',
termination: '%mend',
startLineNumber: 3,
startLineNumbers: [3],
endLineNumber: 5,
parentMacro: 'test',
hasMacroNameInMend: false,
mismatchedMendMacroName: ''
})
})
describe(`multi-line macro declarations`, () => {
it('should return an array with a single macro', () => {
const text = `%macro
test;
%put 'hello';
%mend`
const macros = parseMacros(text, new LintConfig())
expect(macros.length).toEqual(1)
expect(macros).toContainEqual({
name: 'test',
declarationLines: ['%macro ', ' test;'],
terminationLine: '%mend',
declaration: '%macro test',
termination: '%mend',
startLineNumbers: [1, 2],
endLineNumber: 4,
parentMacro: '',
hasMacroNameInMend: false,
mismatchedMendMacroName: ''
})
})
it('should return an array with a single macro having parameters', () => {
const text = `%macro
test(
var,
sum);%put 'hello';
%mend`
const macros = parseMacros(text, new LintConfig())
expect(macros.length).toEqual(1)
expect(macros).toContainEqual({
name: 'test',
declarationLines: [
'%macro ',
` test(`,
` var,`,
` sum);%put 'hello';`
],
terminationLine: '%mend',
declaration: '%macro test( var, sum)',
termination: '%mend',
startLineNumbers: [1, 2, 3, 4],
endLineNumber: 5,
parentMacro: '',
hasMacroNameInMend: false,
mismatchedMendMacroName: ''
})
})
it('should return an array with a single macro having PARMBUFF option', () => {
const text = `%macro test
/parmbuff;
%put 'hello';
%mend`
const macros = parseMacros(text, new LintConfig())
expect(macros.length).toEqual(1)
expect(macros).toContainEqual({
name: 'test',
declarationLines: ['%macro test', ' /parmbuff;'],
terminationLine: '%mend',
declaration: '%macro test /parmbuff',
termination: '%mend',
startLineNumbers: [1, 2],
endLineNumber: 4,
parentMacro: '',
hasMacroNameInMend: false,
mismatchedMendMacroName: ''
})
})
it('should return an array with a single macro having paramerter & SOURCE option', () => {
const text = `/* commentary */ %macro foobar/* commentary */(arg)
/* commentary */
/store
/* commentary */source
des="This macro does not do much";
%put 'hello';
%mend`
const macros = parseMacros(text, new LintConfig())
expect(macros.length).toEqual(1)
expect(macros).toContainEqual({
name: 'foobar',
declarationLines: [
'/* commentary */ %macro foobar/* commentary */(arg) ',
' /* commentary */',
' /store',
' /* commentary */source',
' des="This macro does not do much";'
],
terminationLine: '%mend',
declaration:
'%macro foobar(arg) /store source des="This macro does not do much"',
termination: '%mend',
startLineNumbers: [1, 2, 3, 4, 5],
endLineNumber: 7,
parentMacro: '',
hasMacroNameInMend: false,
mismatchedMendMacroName: ''
})
})
})
})

View File

@@ -4,9 +4,9 @@ import { trimComments } from './trimComments'
interface Macro {
name: string
startLineNumber: number | null
startLineNumbers: number[]
endLineNumber: number | null
declarationLine: string
declarationLines: string[]
terminationLine: string
declaration: string
termination: string
@@ -22,38 +22,92 @@ export const parseMacros = (text: string, config?: LintConfig): Macro[] => {
let isCommentStarted = false
let macroStack: Macro[] = []
lines.forEach((line, index) => {
let isReadingMacroDefinition = false
let isStatementContinues = true
let tempMacroDeclaration = ''
let tempMacroDeclarationLines: string[] = []
let tempStartLineNumbers: number[] = []
lines.forEach((line, lineIndex) => {
const { statement: trimmedLine, commentStarted } = trimComments(
line,
isCommentStarted
)
isCommentStarted = commentStarted
const statements: string[] = trimmedLine ? trimmedLine.split(';') : []
statements.forEach((statement) => {
isStatementContinues = !trimmedLine.endsWith(';')
const statements: string[] = trimmedLine.split(';')
statements.forEach((statement, statementIndex) => {
const { statement: trimmedStatement, commentStarted } = trimComments(
statement,
isCommentStarted
)
isCommentStarted = commentStarted
if (isReadingMacroDefinition) {
tempMacroDeclaration =
tempMacroDeclaration +
(trimmedStatement ? ' ' + trimmedStatement : '')
tempMacroDeclarationLines.push(line)
tempStartLineNumbers.push(lineIndex + 1)
if (!Object.is(statements.length - 1, statementIndex)) {
isReadingMacroDefinition = false
const name = tempMacroDeclaration
.slice(7, tempMacroDeclaration.length)
.trim()
.split('/')[0]
.split('(')[0]
.trim()
macroStack.push({
name,
startLineNumbers: tempStartLineNumbers,
endLineNumber: null,
parentMacro: macroStack.length
? macroStack[macroStack.length - 1].name
: '',
hasMacroNameInMend: false,
mismatchedMendMacroName: '',
declarationLines: tempMacroDeclarationLines,
terminationLine: '',
declaration: tempMacroDeclaration,
termination: ''
})
}
}
if (trimmedStatement.startsWith('%macro')) {
const startLineNumber = index + 1
const startLineNumber = lineIndex + 1
if (
isStatementContinues &&
Object.is(statements.length - 1, statementIndex)
) {
tempMacroDeclaration = trimmedStatement
tempMacroDeclarationLines = [line]
tempStartLineNumbers = [startLineNumber]
isReadingMacroDefinition = true
return
}
const name = trimmedStatement
.slice(7, trimmedStatement.length)
.trim()
.split('/')[0]
.split('(')[0]
.trim()
macroStack.push({
name,
startLineNumber,
startLineNumbers: [startLineNumber],
endLineNumber: null,
parentMacro: macroStack.length
? macroStack[macroStack.length - 1].name
: '',
hasMacroNameInMend: false,
mismatchedMendMacroName: '',
declarationLine: line,
declarationLines: [line],
terminationLine: '',
declaration: trimmedStatement,
termination: ''
@@ -63,7 +117,7 @@ export const parseMacros = (text: string, config?: LintConfig): Macro[] => {
const macro = macroStack.pop() as Macro
const mendMacroName =
trimmedStatement.split(' ').filter((s: string) => !!s)[1] || ''
macro.endLineNumber = index + 1
macro.endLineNumber = lineIndex + 1
macro.hasMacroNameInMend = mendMacroName === macro.name
macro.mismatchedMendMacroName = macro.hasMacroNameInMend
? ''
@@ -74,12 +128,12 @@ export const parseMacros = (text: string, config?: LintConfig): Macro[] => {
} else {
macros.push({
name: '',
startLineNumber: null,
endLineNumber: index + 1,
startLineNumbers: [],
endLineNumber: lineIndex + 1,
parentMacro: '',
hasMacroNameInMend: false,
mismatchedMendMacroName: '',
declarationLine: '',
declarationLines: [],
terminationLine: line,
declaration: '',
termination: trimmedStatement

View File

@@ -13,7 +13,7 @@ describe('trimComments', () => {
/* some comment */ CODE_Keyword1 /* some comment */ CODE_Keyword2/* some comment */;/* some comment */
/* some comment */`)
).toEqual({
statement: 'CODE_Keyword1 CODE_Keyword2;',
statement: 'CODE_Keyword1 CODE_Keyword2;',
commentStarted: false
})
})

View File

@@ -1,8 +1,9 @@
export const trimComments = (
statement: string,
commentStarted: boolean = false
commentStarted: boolean = false,
trimEnd: boolean = false
): { statement: string; commentStarted: boolean } => {
let trimmed = (statement || '').trim()
let trimmed = trimEnd ? (statement || '').trimEnd() : (statement || '').trim()
if (commentStarted || trimmed.startsWith('/*')) {
const parts = trimmed.split('*/')
@@ -20,13 +21,16 @@ export const trimComments = (
} else if (trimmed.includes('/*')) {
const statementBeforeCommentStarts = trimmed.slice(0, trimmed.indexOf('/*'))
const remainingStatement = trimmed.slice(
trimmed.indexOf('/*'),
trimmed.indexOf('*/') + 2,
trimmed.length
)
const result = trimComments(remainingStatement, false)
const result = trimComments(remainingStatement, false, true)
const completeStatement = statementBeforeCommentStarts + result.statement
return {
statement: statementBeforeCommentStarts + result.statement,
statement: trimEnd
? completeStatement.trimEnd()
: completeStatement.trim(),
commentStarted: result.commentStarted
}
}