1
0
mirror of https://github.com/sasjs/lint.git synced 2026-01-07 12:40:05 +00:00

feat(lint): add lintFolder and lintProject APIs

This commit is contained in:
Krishna Acondy
2021-03-30 08:59:38 +01:00
parent 1c09a10290
commit a8ca534b0b
7 changed files with 84 additions and 10 deletions

View File

@@ -1,7 +1,20 @@
import { readFile } from '@sasjs/utils/file'
import { readFile, listSubFoldersInFolder } from '@sasjs/utils/file'
import { Diagnostic } from './types/Diagnostic'
import { LintConfig } from './types/LintConfig'
import { asyncForEach } from './utils/asyncForEach'
import { getLintConfig } from './utils/getLintConfig'
import { listSasFiles } from './utils/listSasFiles'
import path from 'path'
import { getProjectRoot } from './utils'
const excludeFolders = [
'.git',
'.github',
'.vscode',
'node_modules',
'sasjsbuild',
'sasjsresults'
]
/**
* Analyses and produces a set of diagnostics for the given text content.
@@ -16,10 +29,14 @@ export const lintText = async (text: string) => {
/**
* Analyses and produces a set of diagnostics for the file at the given path.
* @param {string} filePath - the path to the file to be linted.
* @param {LintConfig} configuration - an optional configuration. When not passed in, this is read from the .sasjslint file.
* @returns {Diagnostic[]} array of diagnostic objects, each containing a warning, line number and column number.
*/
export const lintFile = async (filePath: string) => {
const config = await getLintConfig()
export const lintFile = async (
filePath: string,
configuration?: LintConfig
) => {
const config = configuration || (await getLintConfig())
const text = await readFile(filePath)
const fileDiagnostics = processFile(filePath, config)
@@ -28,6 +45,47 @@ export const lintFile = async (filePath: string) => {
return [...fileDiagnostics, ...textDiagnostics]
}
/**
* Analyses and produces a set of diagnostics for the folder at the given path.
* @param {string} folderPath - the path to the folder to be linted.
* @param {LintConfig} configuration - an optional configuration. When not passed in, this is read from the .sasjslint file.
* @returns {Diagnostic[]} array of diagnostic objects, each containing a warning, line number and column number.
*/
export const lintFolder = async (
folderPath: string,
configuration?: LintConfig
) => {
const config = configuration || (await getLintConfig())
const diagnostics: Diagnostic[] = []
const fileNames = await listSasFiles(folderPath)
await asyncForEach(fileNames, async (fileName) => {
diagnostics.push(
...(await lintFile(path.join(folderPath, fileName), config))
)
})
const subFolders = (await listSubFoldersInFolder(folderPath)).filter(
(f: string) => !excludeFolders.includes(f)
)
await asyncForEach(subFolders, async (subFolder) => {
diagnostics.push(
...(await lintFolder(path.join(folderPath, subFolder), config))
)
})
return diagnostics
}
/**
* Analyses and produces a set of diagnostics for the current project.
* @returns {Diagnostic[]} array of diagnostic objects, each containing a warning, line number and column number.
*/
export const lintProject = async () => {
const projectRoot = await getProjectRoot()
return await lintFolder(projectRoot)
}
/**
* Splits the given content into a list of lines, regardless of CRLF or LF line endings.
* @param {string} text - the text content to be split into lines.

View File

@@ -0,0 +1,8 @@
export async function asyncForEach(
array: any[],
callback: (item: any, index: number, originalArray: any[]) => any
) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}

View File

@@ -27,6 +27,7 @@ export async function getLintConfig(): Promise<LintConfig> {
const configuration = await readFile(
path.join(projectRoot, '.sasjslint')
).catch((_) => {
console.warn('Unable to load .sasjslint file. Using default configuration.')
return JSON.stringify(DefaultLintConfiguration)
})
return new LintConfig(JSON.parse(configuration))

View File

@@ -1,2 +1,3 @@
export * from './getLintConfig'
export * from './getProjectRoot'
export * from './listSasFiles'

View File

@@ -0,0 +1,6 @@
import { listFilesInFolder } from '@sasjs/utils/file'
export const listSasFiles = async (folderPath: string): Promise<string[]> => {
const files = await listFilesInFolder(folderPath)
return files.filter((f) => f.endsWith('.sas'))
}