Skip to content

Commit

Permalink
Merge pull request #18 from sasjs/lint-file-paths
Browse files Browse the repository at this point in the history
feat(*): group folder and project diagnostics by file path
  • Loading branch information
krishna-acondy authored Apr 2, 2021
2 parents 7aa4bfc + 09e2d05 commit fb4cc2d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
22 changes: 13 additions & 9 deletions src/lint/lintFolder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,61 @@ describe('lintFolder', () => {
it('should identify lint issues in a given folder', async () => {
const results = await lintFolder(path.join(__dirname, '..'))

expect(results.length).toEqual(8)
expect(results).toContainEqual({
expect(results.size).toEqual(1)
const diagnostics = results.get(
path.join(__dirname, '..', 'Example File.sas')
)!
expect(diagnostics.length).toEqual(8)
expect(diagnostics).toContainEqual({
message: 'Line contains trailing spaces',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 2,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'Line contains trailing spaces',
lineNumber: 2,
startColumnNumber: 1,
endColumnNumber: 2,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'File name contains spaces',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'File name contains uppercase characters',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'File missing Doxygen header',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'Line contains encoded password',
lineNumber: 5,
startColumnNumber: 10,
endColumnNumber: 18,
severity: Severity.Error
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'Line is indented with a tab',
lineNumber: 7,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'Line has incorrect indentation - 3 spaces',
lineNumber: 6,
startColumnNumber: 1,
Expand Down
15 changes: 8 additions & 7 deletions src/lint/lintFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,30 @@ const excludeFolders = [
* 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.
* @returns {Promise<Map<string, Diagnostic[]>>} Resolves with a map with array of diagnostic objects, each containing a warning, line number and column number, and grouped by file path.
*/
export const lintFolder = async (
folderPath: string,
configuration?: LintConfig
) => {
const config = configuration || (await getLintConfig())
const diagnostics: Diagnostic[] = []
let diagnostics: Map<string, Diagnostic[]> = new Map<string, Diagnostic[]>()
const fileNames = await listSasFiles(folderPath)
await asyncForEach(fileNames, async (fileName) => {
diagnostics.push(
...(await lintFile(path.join(folderPath, fileName), config))
)
const filePath = path.join(folderPath, fileName)
diagnostics.set(filePath, await lintFile(filePath, 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))
const subFolderDiagnostics = await lintFolder(
path.join(folderPath, subFolder),
config
)
diagnostics = new Map([...diagnostics, ...subFolderDiagnostics])
})

return diagnostics
Expand Down
37 changes: 28 additions & 9 deletions src/lint/lintProject.spec.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,86 @@
import { lintProject } from './lintProject'
import { Severity } from '../types/Severity'
import * as utils from '../utils'
import path from 'path'
jest.mock('../utils')

describe('lintProject', () => {
it('should identify lint issues in a given project', async () => {
jest
.spyOn(utils, 'getProjectRoot')
.mockImplementationOnce(() => Promise.resolve(path.join(__dirname, '..')))
const results = await lintProject()

expect(results.length).toEqual(8)
expect(results).toContainEqual({
expect(results.size).toEqual(1)
const diagnostics = results.get(
path.join(__dirname, '..', 'Example File.sas')
)!
expect(diagnostics.length).toEqual(8)
expect(diagnostics).toContainEqual({
message: 'Line contains trailing spaces',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 2,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'Line contains trailing spaces',
lineNumber: 2,
startColumnNumber: 1,
endColumnNumber: 2,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'File name contains spaces',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'File name contains uppercase characters',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'File missing Doxygen header',
lineNumber: 1,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'Line contains encoded password',
lineNumber: 5,
startColumnNumber: 10,
endColumnNumber: 18,
severity: Severity.Error
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'Line is indented with a tab',
lineNumber: 7,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
expect(results).toContainEqual({
expect(diagnostics).toContainEqual({
message: 'Line has incorrect indentation - 3 spaces',
lineNumber: 6,
startColumnNumber: 1,
endColumnNumber: 1,
severity: Severity.Warning
})
})

it('should throw an error when a project root is not found', async () => {
jest
.spyOn(utils, 'getProjectRoot')
.mockImplementationOnce(() => Promise.resolve(''))

await expect(lintProject()).rejects.toThrowError(
'SASjs Project Root was not found.'
)
})
})
2 changes: 1 addition & 1 deletion src/lint/lintProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { lintFolder } from './lintFolder'

/**
* 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.
* @returns {Promise<Map<string, Diagnostic[]>>} Resolves with a map with array of diagnostic objects, each containing a warning, line number and column number, and grouped by file path.
*/
export const lintProject = async () => {
const projectRoot =
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
],
"target": "es5",
"module": "commonjs",
"downlevelIteration": true,
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
Expand Down

0 comments on commit fb4cc2d

Please sign in to comment.