mirror of
https://github.com/sasjs/lint.git
synced 2026-01-04 03:10:06 +00:00
feat(*): add line endings rule, add automatic formatting for fixable violations
This commit is contained in:
4
src/types/LineEndings.ts
Normal file
4
src/types/LineEndings.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export enum LineEndings {
|
||||
LF = 'lf',
|
||||
CRLF = 'crlf'
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { LineEndings } from './LineEndings'
|
||||
import { LintConfig } from './LintConfig'
|
||||
import { LintRuleType } from './LintRuleType'
|
||||
|
||||
@@ -108,6 +109,33 @@ describe('LintConfig', () => {
|
||||
expect(config.indentationMultiple).toEqual(0)
|
||||
})
|
||||
|
||||
it('should create an instance with the line endings set to LF', () => {
|
||||
const config = new LintConfig({ lineEndings: 'lf' })
|
||||
|
||||
expect(config).toBeTruthy()
|
||||
expect(config.lineEndings).toEqual(LineEndings.LF)
|
||||
})
|
||||
|
||||
it('should create an instance with the line endings set to CRLF', () => {
|
||||
const config = new LintConfig({ lineEndings: 'crlf' })
|
||||
|
||||
expect(config).toBeTruthy()
|
||||
expect(config.lineEndings).toEqual(LineEndings.CRLF)
|
||||
})
|
||||
|
||||
it('should create an instance with the line endings set to LF by default', () => {
|
||||
const config = new LintConfig({})
|
||||
|
||||
expect(config).toBeTruthy()
|
||||
expect(config.lineEndings).toEqual(LineEndings.LF)
|
||||
})
|
||||
|
||||
it('should throw an error with an invalid value for line endings', () => {
|
||||
expect(() => new LintConfig({ lineEndings: 'test' })).toThrowError(
|
||||
`Invalid value for lineEndings: can be ${LineEndings.LF} or ${LineEndings.CRLF}`
|
||||
)
|
||||
})
|
||||
|
||||
it('should create an instance with all flags set', () => {
|
||||
const config = new LintConfig({
|
||||
noTrailingSpaces: true,
|
||||
|
||||
@@ -2,7 +2,8 @@ import {
|
||||
hasDoxygenHeader,
|
||||
hasMacroNameInMend,
|
||||
noNestedMacros,
|
||||
hasMacroParentheses
|
||||
hasMacroParentheses,
|
||||
lineEndings
|
||||
} from '../rules/file'
|
||||
import {
|
||||
indentationMultiple,
|
||||
@@ -12,6 +13,7 @@ import {
|
||||
noTrailingSpaces
|
||||
} from '../rules/line'
|
||||
import { lowerCaseFileNames, noSpacesInFileNames } from '../rules/path'
|
||||
import { LineEndings } from './LineEndings'
|
||||
import { FileLintRule, LineLintRule, PathLintRule } from './LintRule'
|
||||
|
||||
/**
|
||||
@@ -27,6 +29,7 @@ export class LintConfig {
|
||||
readonly pathLintRules: PathLintRule[] = []
|
||||
readonly maxLineLength: number = 80
|
||||
readonly indentationMultiple: number = 2
|
||||
readonly lineEndings: LineEndings = LineEndings.LF
|
||||
|
||||
constructor(json?: any) {
|
||||
if (json?.noTrailingSpaces) {
|
||||
@@ -46,6 +49,19 @@ export class LintConfig {
|
||||
this.lineLintRules.push(maxLineLength)
|
||||
}
|
||||
|
||||
if (json?.lineEndings) {
|
||||
if (
|
||||
json.lineEndings !== LineEndings.LF &&
|
||||
json.lineEndings !== LineEndings.CRLF
|
||||
) {
|
||||
throw new Error(
|
||||
`Invalid value for lineEndings: can be ${LineEndings.LF} or ${LineEndings.CRLF}`
|
||||
)
|
||||
}
|
||||
this.lineEndings = json.lineEndings
|
||||
this.fileLintRules.push(lineEndings)
|
||||
}
|
||||
|
||||
if (!isNaN(json?.indentationMultiple)) {
|
||||
this.indentationMultiple = json.indentationMultiple as number
|
||||
this.lineLintRules.push(indentationMultiple)
|
||||
|
||||
@@ -19,6 +19,7 @@ export interface LintRule {
|
||||
export interface LineLintRule extends LintRule {
|
||||
type: LintRuleType.Line
|
||||
test: (value: string, lineNumber: number, config?: LintConfig) => Diagnostic[]
|
||||
fix?: (value: string, config?: LintConfig) => string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -26,7 +27,8 @@ export interface LineLintRule extends LintRule {
|
||||
*/
|
||||
export interface FileLintRule extends LintRule {
|
||||
type: LintRuleType.File
|
||||
test: (value: string) => Diagnostic[]
|
||||
test: (value: string, config?: LintConfig) => Diagnostic[]
|
||||
fix?: (value: string, config?: LintConfig) => string
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user