mirror of
https://github.com/sasjs/lint.git
synced 2026-01-03 19:10:04 +00:00
feat(lint): implement v1 with 3 rules - trailing spaces, encoded passwords and Doxygen header
This commit is contained in:
10
src/utils/getLintConfig.spec.ts
Normal file
10
src/utils/getLintConfig.spec.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { LintConfig } from '../types/LintConfig'
|
||||
import { getLintConfig } from './getLintConfig'
|
||||
|
||||
describe('getLintConfig', () => {
|
||||
it('should get the lint config', async () => {
|
||||
const config = await getLintConfig()
|
||||
|
||||
expect(config).toBeInstanceOf(LintConfig)
|
||||
})
|
||||
})
|
||||
10
src/utils/getLintConfig.ts
Normal file
10
src/utils/getLintConfig.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import path from 'path'
|
||||
import { LintConfig } from '../types/LintConfig'
|
||||
import { readFile } from '@sasjs/utils/file'
|
||||
import { getProjectRoot } from './getProjectRoot'
|
||||
|
||||
export async function getLintConfig(): Promise<LintConfig> {
|
||||
const projectRoot = await getProjectRoot()
|
||||
const configuration = await readFile(path.join(projectRoot, '.sasjslint'))
|
||||
return new LintConfig(JSON.parse(configuration))
|
||||
}
|
||||
22
src/utils/getProjectRoot.spec.ts
Normal file
22
src/utils/getProjectRoot.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { getProjectRoot } from './getProjectRoot'
|
||||
import path from 'path'
|
||||
|
||||
describe('getProjectRoot', () => {
|
||||
it('should return the current location if it contains the lint config file', async () => {
|
||||
const projectRoot = await getProjectRoot()
|
||||
|
||||
expect(projectRoot).toEqual(process.cwd())
|
||||
})
|
||||
|
||||
it('should return the parent folder if it contains the lint config file', async () => {
|
||||
const currentLocation = process.cwd()
|
||||
jest
|
||||
.spyOn(process, 'cwd')
|
||||
.mockImplementationOnce(() =>
|
||||
path.join(currentLocation, 'folder', 'subfolder')
|
||||
)
|
||||
const projectRoot = await getProjectRoot()
|
||||
|
||||
expect(projectRoot).toEqual(currentLocation)
|
||||
})
|
||||
})
|
||||
27
src/utils/getProjectRoot.ts
Normal file
27
src/utils/getProjectRoot.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import path from 'path'
|
||||
import { fileExists } from '@sasjs/utils/file'
|
||||
|
||||
export async function getProjectRoot() {
|
||||
let root = ''
|
||||
let rootFound = false
|
||||
let i = 1
|
||||
let currentLocation = process.cwd()
|
||||
|
||||
const maxLevels = currentLocation.split(path.sep).length
|
||||
|
||||
while (i <= maxLevels && !rootFound) {
|
||||
const isRoot = await fileExists(path.join(currentLocation, '.sasjslint'))
|
||||
|
||||
if (isRoot) {
|
||||
rootFound = true
|
||||
root = currentLocation
|
||||
|
||||
break
|
||||
} else {
|
||||
currentLocation = path.join(currentLocation, '..')
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return root
|
||||
}
|
||||
Reference in New Issue
Block a user