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

fix(*): Add severity, start and end column numbers for diagnostics, change warning to message

This commit is contained in:
Krishna Acondy
2021-03-24 09:11:09 +00:00
parent 702756545b
commit de1fabc394
12 changed files with 126 additions and 37 deletions

View File

@@ -1,3 +1,4 @@
import { Severity } from '../types/Severity'
import { hasDoxygenHeader } from './hasDoxygenHeader'
describe('hasDoxygenHeader', () => {
@@ -23,7 +24,13 @@ describe('hasDoxygenHeader', () => {
%do x=0 %to &maxtries;`
expect(hasDoxygenHeader.test(content)).toEqual([
{ warning: 'File missing Doxygen header', lineNumber: 1, columnNumber: 1 }
{
message: 'File missing Doxygen header',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
}
])
})
@@ -31,7 +38,13 @@ describe('hasDoxygenHeader', () => {
const content = undefined
expect(hasDoxygenHeader.test((content as unknown) as string)).toEqual([
{ warning: 'File missing Doxygen header', lineNumber: 1, columnNumber: 1 }
{
message: 'File missing Doxygen header',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
}
])
})
})

View File

@@ -1,17 +1,34 @@
import { FileLintRule } from '../types/LintRule'
import { LintRuleType } from '../types/LintRuleType'
import { Severity } from '../types/Severity'
const name = 'hasDoxygenHeader'
const description =
'Enforce the presence of a Doxygen header at the start of each file.'
const warning = 'File missing Doxygen header'
const message = 'File missing Doxygen header'
const test = (value: string) => {
try {
const hasFileHeader = value.split('/**')[0] !== value
if (hasFileHeader) return []
return [{ warning, lineNumber: 1, columnNumber: 1 }]
return [
{
message,
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
}
]
} catch (e) {
return [{ warning, lineNumber: 1, columnNumber: 1 }]
return [
{
message,
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
}
]
}
}
@@ -22,6 +39,6 @@ export const hasDoxygenHeader: FileLintRule = {
type: LintRuleType.File,
name,
description,
warning,
message,
test
}

View File

@@ -1,3 +1,4 @@
import { Severity } from '../types/Severity'
import { noEncodedPasswords } from './noEncodedPasswords'
describe('noEncodedPasswords', () => {
@@ -10,9 +11,11 @@ describe('noEncodedPasswords', () => {
const line = "%put '{SASENC}'; "
expect(noEncodedPasswords.test(line, 1)).toEqual([
{
warning: 'Line contains encoded password',
message: 'Line contains encoded password',
lineNumber: 1,
columnNumber: 7
startColumnNumber: 7,
endColumnNumber: 15,
severity: Severity.Error
}
])
})
@@ -21,9 +24,11 @@ describe('noEncodedPasswords', () => {
const line = "%put '{SAS001}'; "
expect(noEncodedPasswords.test(line, 1)).toEqual([
{
warning: 'Line contains encoded password',
message: 'Line contains encoded password',
lineNumber: 1,
columnNumber: 7
startColumnNumber: 7,
endColumnNumber: 15,
severity: Severity.Error
}
])
})
@@ -32,14 +37,18 @@ describe('noEncodedPasswords', () => {
const line = "%put '{SAS001} {SAS002}'; "
expect(noEncodedPasswords.test(line, 1)).toEqual([
{
warning: 'Line contains encoded password',
message: 'Line contains encoded password',
lineNumber: 1,
columnNumber: 7
startColumnNumber: 7,
endColumnNumber: 15,
severity: Severity.Error
},
{
warning: 'Line contains encoded password',
message: 'Line contains encoded password',
lineNumber: 1,
columnNumber: 16
startColumnNumber: 16,
endColumnNumber: 24,
severity: Severity.Error
}
])
})

View File

@@ -1,17 +1,20 @@
import { LineLintRule } from '../types/LintRule'
import { LintRuleType } from '../types/LintRuleType'
import { Severity } from '../types/Severity'
const name = 'noEncodedPasswords'
const description = 'Disallow encoded passwords in SAS code.'
const warning = 'Line contains encoded password'
const message = 'Line contains encoded password'
const test = (value: string, lineNumber: number) => {
const regex = new RegExp(/{sas(\d{2,4}|enc)}[^;"'\s]*/, 'gi')
const matches = value.match(regex)
if (!matches || !matches.length) return []
return matches.map((match) => ({
warning,
message,
lineNumber,
columnNumber: value.indexOf(match) + 1
startColumnNumber: value.indexOf(match) + 1,
endColumnNumber: value.indexOf(match) + match.length + 1,
severity: Severity.Error
}))
}
@@ -22,6 +25,6 @@ export const noEncodedPasswords: LineLintRule = {
type: LintRuleType.Line,
name,
description,
warning,
message,
test
}

View File

@@ -1,3 +1,4 @@
import { Severity } from '../types/Severity'
import { noTrailingSpaces } from './noTrailingSpaces'
describe('noTrailingSpaces', () => {
@@ -10,9 +11,11 @@ describe('noTrailingSpaces', () => {
const line = "%put 'hello'; "
expect(noTrailingSpaces.test(line, 1)).toEqual([
{
warning: 'Line contains trailing spaces',
message: 'Line contains trailing spaces',
lineNumber: 1,
columnNumber: 14
startColumnNumber: 14,
endColumnNumber: 15,
severity: Severity.Warning
}
])
})

View File

@@ -1,13 +1,22 @@
import { LineLintRule } from '../types/LintRule'
import { LintRuleType } from '../types/LintRuleType'
import { Severity } from '../types/Severity'
const name = 'noTrailingSpaces'
const description = 'Disallow trailing spaces on lines.'
const warning = 'Line contains trailing spaces'
const message = 'Line contains trailing spaces'
const test = (value: string, lineNumber: number) =>
value.trimEnd() === value
? []
: [{ warning, lineNumber, columnNumber: value.trimEnd().length + 1 }]
: [
{
message,
lineNumber,
startColumnNumber: value.trimEnd().length + 1,
endColumnNumber: value.length,
severity: Severity.Warning
}
]
/**
* Lint rule that checks for the presence of trailing space(s) in a given line of text.
@@ -16,6 +25,6 @@ export const noTrailingSpaces: LineLintRule = {
type: LintRuleType.Line,
name,
description,
warning,
message,
test
}