1
0
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:
Krishna Acondy
2021-04-19 21:00:38 +01:00
parent 99813f04c0
commit 519a0164b5
32 changed files with 941 additions and 259 deletions

4
src/types/LineEndings.ts Normal file
View File

@@ -0,0 +1,4 @@
export enum LineEndings {
LF = 'lf',
CRLF = 'crlf'
}

View File

@@ -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,

View File

@@ -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)

View File

@@ -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
}
/**