mirror of
https://github.com/sasjs/lint.git
synced 2026-01-05 03:30:06 +00:00
feat(lint): implement v1 with 3 rules - trailing spaces, encoded passwords and Doxygen header
This commit is contained in:
46
src/lint.ts
Normal file
46
src/lint.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Diagnostic } from './types/Diagnostic'
|
||||
import { LintConfig } from './types/LintConfig'
|
||||
import { getLintConfig } from './utils/getLintConfig'
|
||||
|
||||
export const lint = async (text: string) => {
|
||||
const config = await getLintConfig()
|
||||
return processText(text, config)
|
||||
}
|
||||
|
||||
export const splitText = (text: string): string[] => {
|
||||
if (!text) return []
|
||||
return text.replace(/\r\n/g, '\n').split('\n')
|
||||
}
|
||||
|
||||
const processText = (text: string, config: LintConfig) => {
|
||||
const lines = splitText(text)
|
||||
const diagnostics: Diagnostic[] = []
|
||||
diagnostics.push(...processFile(config, text))
|
||||
lines.forEach((line, index) => {
|
||||
diagnostics.push(...processLine(config, line, index + 1))
|
||||
})
|
||||
|
||||
return diagnostics
|
||||
}
|
||||
|
||||
const processFile = (config: LintConfig, fileContent: string): Diagnostic[] => {
|
||||
const diagnostics: Diagnostic[] = []
|
||||
config.fileLintRules.forEach((rule) => {
|
||||
diagnostics.push(...rule.test(fileContent))
|
||||
})
|
||||
|
||||
return diagnostics
|
||||
}
|
||||
|
||||
const processLine = (
|
||||
config: LintConfig,
|
||||
line: string,
|
||||
lineNumber: number
|
||||
): Diagnostic[] => {
|
||||
const diagnostics: Diagnostic[] = []
|
||||
config.lineLintRules.forEach((rule) => {
|
||||
diagnostics.push(...rule.test(line, lineNumber))
|
||||
})
|
||||
|
||||
return diagnostics
|
||||
}
|
||||
Reference in New Issue
Block a user