mirror of
https://github.com/sasjs/lint.git
synced 2026-01-09 21:40:06 +00:00
feat: mult-line macro declarations
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user