Skip to content

Commit

Permalink
fix(lint): ignore indentation multiple when set to zero
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna-acondy committed Mar 29, 2021
1 parent f1adcb8 commit 52b63ba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/rules/indentationMultiple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ describe('indentationMultiple', () => {
expect(indentationMultiple.test(line, 1, config)).toEqual([])
})

it('should ignore indentation when the multiple is set to 0', () => {
const line = " %put 'hello';"
const config = new LintConfig({ indentationMultiple: 0 })
expect(indentationMultiple.test(line, 1, config)).toEqual([])
})

it('should return an empty array when the line is not indented', () => {
const line = "%put 'hello';"
const config = new LintConfig({ indentationMultiple: 2 })
Expand Down
8 changes: 6 additions & 2 deletions src/rules/indentationMultiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ const message = 'Line has incorrect indentation'
const test = (value: string, lineNumber: number, config?: LintConfig) => {
if (!value.startsWith(' ')) return []

const indentationMultiple = config?.indentationMultiple || 2
const indentationMultiple = isNaN(config?.indentationMultiple as number)
? 2
: config?.indentationMultiple

if (indentationMultiple === 0) return []
const numberOfSpaces = value.search(/\S|$/)
if (numberOfSpaces % indentationMultiple === 0) return []
if (numberOfSpaces % indentationMultiple! === 0) return []
return [
{
message: `${message} - ${numberOfSpaces} ${
Expand Down
14 changes: 14 additions & 0 deletions src/types/LintConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ describe('LintConfig', () => {
expect(config.fileLintRules[0].type).toEqual(LintRuleType.File)
})

it('should create an instance with the indentation multiple set', () => {
const config = new LintConfig({ indentationMultiple: 5 })

expect(config).toBeTruthy()
expect(config.indentationMultiple).toEqual(5)
})

it('should create an instance with the indentation multiple turned off', () => {
const config = new LintConfig({ indentationMultiple: 0 })

expect(config).toBeTruthy()
expect(config.indentationMultiple).toEqual(0)
})

it('should create an instance with all flags set', () => {
const config = new LintConfig({
noTrailingSpaces: true,
Expand Down
8 changes: 4 additions & 4 deletions src/types/LintConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class LintConfig {
readonly lineLintRules: LineLintRule[] = []
readonly fileLintRules: FileLintRule[] = []
readonly pathLintRules: PathLintRule[] = []
readonly maxLineLength = 80
readonly indentationMultiple = 2
readonly maxLineLength: number = 80
readonly indentationMultiple: number = 2

constructor(json?: any) {
if (json?.noTrailingSpaces) {
Expand All @@ -40,8 +40,8 @@ export class LintConfig {
this.lineLintRules.push(maxLineLength)
}

if (json?.indentationMultiple) {
this.indentationMultiple = json.indentationMultiple
if (!isNaN(json?.indentationMultiple)) {
this.indentationMultiple = json.indentationMultiple as number
this.lineLintRules.push(indentationMultiple)
}

Expand Down

0 comments on commit 52b63ba

Please sign in to comment.