Skip to content

Commit

Permalink
feat(lint): add rule for indentation multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna-acondy committed Mar 29, 2021
1 parent 8fc3c39 commit f1adcb8
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .sasjslint
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"noSpacesInFileNames": true,
"maxLineLength": 80,
"lowerCaseFileNames": true,
"noTabIndentation": true
"noTabIndentation": true,
"indentationMultiple": 2
}
18 changes: 18 additions & 0 deletions src/Example File.sas
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


%macro mf_getuniquelibref(prefix=mclib,maxtries=1000);
%local x libref;
%let x={SAS002};
%do x=0 %to &maxtries;
%if %sysfunc(libref(&prefix&x)) ne 0 %then %do;
%let libref=&prefix&x;
%let rc=%sysfunc(libname(&libref,%sysfunc(pathname(work))));
%if &rc %then %put %sysfunc(sysmsg());
&prefix&x
%*put &sysmacroname: Libref &libref assigned as WORK and returned;
%return;
%end;
%end;
%put unable to find available libref in range &prefix.0-&maxtries;
%mend;

17 changes: 0 additions & 17 deletions src/example file.sas

This file was deleted.

57 changes: 28 additions & 29 deletions src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,45 @@ import path from 'path'
*/

const text = `/**
@file
@brief Returns an unused libref
@details Use as follows:
@file
@brief Returns an unused libref
@details Use as follows:
libname mclib0 (work);
libname mclib1 (work);
libname mclib2 (work);
libname mclib0 (work);
libname mclib1 (work);
libname mclib2 (work);
%let libref=%mf_getuniquelibref({SAS001});
%put &=libref;
%let libref=%mf_getuniquelibref({SAS001});
%put &=libref;
which returns:
which returns:
> mclib3
@param prefix= first part of libref. Remember that librefs can only be 8 characters,
so a 7 letter prefix would mean that maxtries should be 10.
@param maxtries= the last part of the libref. Provide an integer value.
@param prefix= first part of libref. Remember that librefs can only be 8 characters,
so a 7 letter prefix would mean that maxtries should be 10.
@param maxtries= the last part of the libref. Provide an integer value.
@version 9.2
@author Allan Bowe
@version 9.2
@author Allan Bowe
**/
%macro mf_getuniquelibref(prefix=mclib,maxtries=1000);
%local x libref;
%let x={SAS002};
%macro mf_getuniquelibref(prefix=mclib,maxtries=1000);
%local x libref;
%let x={SAS002};
%do x=0 %to &maxtries;
%if %sysfunc(libref(&prefix&x)) ne 0 %then %do;
%let libref=&prefix&x;
%let rc=%sysfunc(libname(&libref,%sysfunc(pathname(work))));
%if &rc %then %put %sysfunc(sysmsg());
&prefix&x
%*put &sysmacroname: Libref &libref assigned as WORK and returned;
%return;
%end;
%end;
%put unable to find available libref in range &prefix.0-&maxtries;
%mend;
%if %sysfunc(libref(&prefix&x)) ne 0 %then %do;
%let libref=&prefix&x;
%let rc=%sysfunc(libname(&libref,%sysfunc(pathname(work))));
%if &rc %then %put %sysfunc(sysmsg());
&prefix&x
%*put &sysmacroname: Libref &libref assigned as WORK and returned;
%return;
%end;
%end;
%put unable to find available libref in range &prefix.0-&maxtries;
%mend;
`

lintText(text).then((diagnostics) => {
Expand Down
29 changes: 25 additions & 4 deletions src/lint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ describe('lintText', () => {

describe('lintFile', () => {
it('should identify lint issues in a given file', async () => {
const results = await lintFile(path.join(__dirname, 'example file.sas'))
const results = await lintFile(path.join(__dirname, 'Example File.sas'))

expect(results.length).toEqual(5)
expect(results.length).toEqual(8)
expect(results).toContainEqual({
message: 'Line contains trailing spaces',
lineNumber: 1,
Expand All @@ -95,6 +95,13 @@ describe('lintFile', () => {
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
message: 'File name contains uppercase characters',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
message: 'File missing Doxygen header',
lineNumber: 1,
Expand All @@ -105,10 +112,24 @@ describe('lintFile', () => {
expect(results).toContainEqual({
message: 'Line contains encoded password',
lineNumber: 5,
startColumnNumber: 11,
endColumnNumber: 19,
startColumnNumber: 10,
endColumnNumber: 18,
severity: Severity.Error
})
expect(results).toContainEqual({
message: 'Line is indented with a tab',
lineNumber: 7,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
message: 'Line has incorrect indentation - 3 spaces',
lineNumber: 6,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
})
})

Expand Down
68 changes: 68 additions & 0 deletions src/rules/indentationMultiple.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { LintConfig, Severity } from '../types'
import { indentationMultiple } from './indentationMultiple'

describe('indentationMultiple', () => {
it('should return an empty array when the line is indented by two spaces', () => {
const line = " %put 'hello';"
const config = new LintConfig({ indentationMultiple: 2 })
expect(indentationMultiple.test(line, 1, config)).toEqual([])
})

it('should return an empty array when the line is indented by a multiple of 2 spaces', () => {
const line = " %put 'hello';"
const config = new LintConfig({ indentationMultiple: 2 })
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 })
expect(indentationMultiple.test(line, 1, config)).toEqual([])
})

it('should return an array with a single diagnostic when the line is indented incorrectly', () => {
const line = " %put 'hello';"
const config = new LintConfig({ indentationMultiple: 2 })
expect(indentationMultiple.test(line, 1, config)).toEqual([
{
message: `Line has incorrect indentation - 3 spaces`,
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
}
])
})

it('should return an array with a single diagnostic when the line is indented incorrectly', () => {
const line = " %put 'hello';"
const config = new LintConfig({ indentationMultiple: 3 })
expect(indentationMultiple.test(line, 1, config)).toEqual([
{
message: `Line has incorrect indentation - 2 spaces`,
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
}
])
})

it('should fall back to a default of 2 spaces', () => {
const line = " %put 'hello';"
expect(indentationMultiple.test(line, 1)).toEqual([
{
message: `Line has incorrect indentation - 1 space`,
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
}
])
})

it('should return an empty array for lines within the default indentation', () => {
const line = " %put 'hello';"
expect(indentationMultiple.test(line, 1)).toEqual([])
})
})
37 changes: 37 additions & 0 deletions src/rules/indentationMultiple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { LintConfig } from '../types'
import { LineLintRule } from '../types/LintRule'
import { LintRuleType } from '../types/LintRuleType'
import { Severity } from '../types/Severity'

const name = 'indentationMultiple'
const description = 'Ensure indentation by a multiple of the configured number.'
const message = 'Line has incorrect indentation'
const test = (value: string, lineNumber: number, config?: LintConfig) => {
if (!value.startsWith(' ')) return []

const indentationMultiple = config?.indentationMultiple || 2
const numberOfSpaces = value.search(/\S|$/)
if (numberOfSpaces % indentationMultiple === 0) return []
return [
{
message: `${message} - ${numberOfSpaces} ${
numberOfSpaces === 1 ? 'space' : 'spaces'
}`,
lineNumber,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
}
]
}

/**
* Lint rule that checks if a line is indented by a multiple of the configured indentation multiple.
*/
export const indentationMultiple: LineLintRule = {
type: LintRuleType.Line,
name,
description,
message,
test
}
7 changes: 7 additions & 0 deletions src/types/LintConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { hasDoxygenHeader } from '../rules/hasDoxygenHeader'
import { indentationMultiple } from '../rules/indentationMultiple'
import { lowerCaseFileNames } from '../rules/lowerCaseFileNames'
import { maxLineLength } from '../rules/maxLineLength'
import { noEncodedPasswords } from '../rules/noEncodedPasswords'
Expand All @@ -19,6 +20,7 @@ export class LintConfig {
readonly fileLintRules: FileLintRule[] = []
readonly pathLintRules: PathLintRule[] = []
readonly maxLineLength = 80
readonly indentationMultiple = 2

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

if (json?.indentationMultiple) {
this.indentationMultiple = json.indentationMultiple
this.lineLintRules.push(indentationMultiple)
}

if (json?.hasDoxygenHeader) {
this.fileLintRules.push(hasDoxygenHeader)
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getLintConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('getLintConfig', () => {

expect(config).toBeInstanceOf(LintConfig)
expect(config.fileLintRules.length).toEqual(1)
expect(config.lineLintRules.length).toEqual(4)
expect(config.lineLintRules.length).toEqual(5)
expect(config.pathLintRules.length).toEqual(2)
})
})
3 changes: 2 additions & 1 deletion src/utils/getLintConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const DefaultLintConfiguration = {
noSpacesInFileNames: true,
lowerCaseFileNames: true,
maxLineLength: 80,
noTabIndentation: true
noTabIndentation: true,
indentationMultiple: 2
}

/**
Expand Down

0 comments on commit f1adcb8

Please sign in to comment.