1
0
mirror of https://github.com/sasjs/lint.git synced 2026-01-06 20:20:06 +00:00

feat: honour .gitignore and ignoreList from config when linting filesystem

This commit is contained in:
2022-08-12 15:49:28 +05:00
parent e1bcf5b06b
commit 40aea383b7
7 changed files with 6679 additions and 18 deletions

View File

@@ -1,4 +1,6 @@
export * from './asyncForEach'
export * from './getLintConfig'
export * from './getProjectRoot'
export * from './isIgnored'
export * from './listSasFiles'
export * from './splitText'

View File

@@ -0,0 +1,83 @@
import * as fileModule from '@sasjs/utils/file'
import * as getProjectRootModule from './getProjectRoot'
import * as getLintConfigModule from './getLintConfig'
import { DefaultLintConfiguration } from './getLintConfig'
import { LintConfig } from '../types'
import { isIgnored } from './isIgnored'
describe('isIgnored', () => {
it('should return true if provided path matches the patterns from .gitignore', async () => {
jest
.spyOn(getLintConfigModule, 'getLintConfig')
.mockImplementationOnce(
async () => new LintConfig(DefaultLintConfiguration)
)
jest
.spyOn(fileModule, 'fileExists')
.mockImplementationOnce(async () => true)
jest
.spyOn(fileModule, 'readFile')
.mockImplementationOnce(async () => 'sasjs')
jest
.spyOn(getProjectRootModule, 'getProjectRoot')
.mockImplementationOnce(async () => '')
const ignored = await isIgnored('sasjs')
expect(ignored).toBeTruthy()
})
it('should return true if provided path matches any pattern from ignoreList (.sasjslint)', async () => {
jest
.spyOn(fileModule, 'fileExists')
.mockImplementationOnce(async () => false)
jest
.spyOn(getProjectRootModule, 'getProjectRoot')
.mockImplementationOnce(async () => '')
const ignored = await isIgnored(
'sasjs',
new LintConfig({
...DefaultLintConfiguration,
ignoreList: ['sasjs']
})
)
expect(ignored).toBeTruthy()
})
it('should return false if provided path does not matches any pattern from .gitignore and ignoreList (.sasjslint)', async () => {
jest
.spyOn(fileModule, 'fileExists')
.mockImplementationOnce(async () => true)
jest.spyOn(fileModule, 'readFile').mockImplementationOnce(async () => '')
jest
.spyOn(getProjectRootModule, 'getProjectRoot')
.mockImplementationOnce(async () => '')
const ignored = await isIgnored(
'sasjs',
new LintConfig(DefaultLintConfiguration)
)
expect(ignored).toBeFalsy()
})
it('should return false if provided path is equal to projectRoot', async () => {
jest
.spyOn(getProjectRootModule, 'getProjectRoot')
.mockImplementationOnce(async () => '')
const ignored = await isIgnored(
'',
new LintConfig(DefaultLintConfiguration)
)
expect(ignored).toBeFalsy()
})
})

33
src/utils/isIgnored.ts Normal file
View File

@@ -0,0 +1,33 @@
import { fileExists, readFile } from '@sasjs/utils'
import path from 'path'
import ignore from 'ignore'
import { getLintConfig, getProjectRoot } from '.'
import { LintConfig } from '../types'
/**
*
* @param fPath absolute path of file or folder
* @returns {Promise<boolean>} true if path matches the patterns from .gitignore file otherwise false
*/
export const isIgnored = async (
fPath: string,
configuration?: LintConfig
): Promise<boolean> => {
const config = configuration || (await getLintConfig())
const projectRoot = await getProjectRoot()
const gitIgnoreFilePath = path.join(projectRoot, '.gitignore')
const rootPath = projectRoot + path.sep
const relativePath = fPath.replace(rootPath, '')
if (fPath === projectRoot) return false
let gitIgnoreFileContent = ''
if (await fileExists(gitIgnoreFilePath))
gitIgnoreFileContent = await readFile(gitIgnoreFilePath)
return ignore()
.add(gitIgnoreFileContent)
.add(config.ignoreList)
.ignores(relativePath)
}