mirror of
https://github.com/sasjs/lint.git
synced 2025-12-10 17:34:36 +00:00
feat(lint): add rule for indentation multiple
This commit is contained in:
@@ -5,5 +5,6 @@
|
||||
"noSpacesInFileNames": true,
|
||||
"maxLineLength": 80,
|
||||
"lowerCaseFileNames": true,
|
||||
"noTabIndentation": true
|
||||
"noTabIndentation": true,
|
||||
"indentationMultiple": 2
|
||||
}
|
||||
18
src/Example File.sas
Normal file
18
src/Example File.sas
Normal file
@@ -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;
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
|
||||
|
||||
%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;
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
68
src/rules/indentationMultiple.spec.ts
Normal file
68
src/rules/indentationMultiple.spec.ts
Normal file
@@ -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
src/rules/indentationMultiple.ts
Normal file
37
src/rules/indentationMultiple.ts
Normal file
@@ -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
|
||||
}
|
||||
@@ -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'
|
||||
@@ -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) {
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -13,7 +13,8 @@ export const DefaultLintConfiguration = {
|
||||
noSpacesInFileNames: true,
|
||||
lowerCaseFileNames: true,
|
||||
maxLineLength: 80,
|
||||
noTabIndentation: true
|
||||
noTabIndentation: true,
|
||||
indentationMultiple: 2
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user