Skip to content

Commit

Permalink
fix(strictMacroDefinition): updated logic for getting options
Browse files Browse the repository at this point in the history
  • Loading branch information
saadjutt01 committed May 21, 2021
1 parent cbfa1f4 commit 5701064
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/rules/file/strictMacroDefinition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ describe('strictMacroDefinition', () => {

const content11 = '`%macro macroName() /* / store source */;'
expect(strictMacroDefinition.test(content11)).toEqual([])

const content12 =
'%macro macroName()/ /* some comment */ store des="some description";'
expect(strictMacroDefinition.test(content12)).toEqual([])
})

it('should return an array with a single diagnostic when Macro definition has space in param', () => {
Expand Down
55 changes: 35 additions & 20 deletions src/rules/file/strictMacroDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,43 @@ const test = (value: string, config?: LintConfig) => {
_declaration = declaration.split(`(${paramsPresent})`)[1]
}

const optionsPresent = _declaration.split('/')?.[1]?.trim().split(' ')
let optionsPresent = _declaration.split('/')?.[1]?.trim()

optionsPresent
?.filter((o) => !!o)
.forEach((option) => {
const trimmedOption = option.trim()
if (!validOptions.includes(trimmedOption.toUpperCase())) {
const declarationLineIndex = macro.declarationLines.findIndex(
(dl) => dl.indexOf(trimmedOption) !== -1
)
const declarationLine = macro.declarationLines[declarationLineIndex]
if (optionsPresent) {
const regex = new RegExp(/="(.*?)"/, 'g')

diagnostics.push({
message: `Option '${trimmedOption}' is not valid`,
lineNumber: macro.startLineNumbers[declarationLineIndex],
startColumnNumber: declarationLine.indexOf(trimmedOption) + 1,
endColumnNumber:
declarationLine.indexOf(trimmedOption) + trimmedOption.length,
severity: Severity.Warning
})
}
})
let result = regex.exec(optionsPresent)

while (result) {
optionsPresent =
optionsPresent.slice(0, result.index) +
optionsPresent.slice(result.index + result[0].length)

result = regex.exec(optionsPresent)
}

optionsPresent
.split(' ')
?.filter((o) => !!o)
.forEach((option) => {
const trimmedOption = option.trim()
if (!validOptions.includes(trimmedOption.toUpperCase())) {
const declarationLineIndex = macro.declarationLines.findIndex(
(dl) => dl.indexOf(trimmedOption) !== -1
)
const declarationLine = macro.declarationLines[declarationLineIndex]

diagnostics.push({
message: `Option '${trimmedOption}' is not valid`,
lineNumber: macro.startLineNumbers[declarationLineIndex],
startColumnNumber: declarationLine.indexOf(trimmedOption) + 1,
endColumnNumber:
declarationLine.indexOf(trimmedOption) + trimmedOption.length,
severity: Severity.Warning
})
}
})
}
})

return diagnostics
Expand Down

0 comments on commit 5701064

Please sign in to comment.