mirror of
https://github.com/sasjs/lint.git
synced 2026-01-16 16:50:05 +00:00
feat(format-diagnostics): add diagnostic information to format result payload
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
import { createFile, readFile } from '@sasjs/utils/file'
|
import { createFile, readFile } from '@sasjs/utils/file'
|
||||||
|
import { lintFile } from '../lint'
|
||||||
|
import { FormatResult } from '../types'
|
||||||
import { LintConfig } from '../types/LintConfig'
|
import { LintConfig } from '../types/LintConfig'
|
||||||
import { getLintConfig } from '../utils/getLintConfig'
|
import { getLintConfig } from '../utils/getLintConfig'
|
||||||
import { processText } from './shared'
|
import { processText } from './shared'
|
||||||
@@ -7,16 +9,37 @@ import { processText } from './shared'
|
|||||||
* Applies automatic formatting to the file at the given path.
|
* Applies automatic formatting to the file at the given path.
|
||||||
* @param {string} filePath - the path to the file to be formatted.
|
* @param {string} filePath - the path to the file to be formatted.
|
||||||
* @param {LintConfig} configuration - an optional configuration. When not passed in, this is read from the .sasjslint file.
|
* @param {LintConfig} configuration - an optional configuration. When not passed in, this is read from the .sasjslint file.
|
||||||
* @returns {Promise<void>} Resolves successfully when the file has been formatted.
|
* @returns {Promise<FormatResult>} Resolves successfully when the file has been formatted.
|
||||||
*/
|
*/
|
||||||
export const formatFile = async (
|
export const formatFile = async (
|
||||||
filePath: string,
|
filePath: string,
|
||||||
configuration?: LintConfig
|
configuration?: LintConfig
|
||||||
) => {
|
): Promise<FormatResult> => {
|
||||||
const config = configuration || (await getLintConfig())
|
const config = configuration || (await getLintConfig())
|
||||||
|
const diagnosticsBeforeFormat = await lintFile(filePath)
|
||||||
|
const diagnosticsCountBeforeFormat = diagnosticsBeforeFormat.length
|
||||||
|
|
||||||
const text = await readFile(filePath)
|
const text = await readFile(filePath)
|
||||||
|
|
||||||
const formattedText = processText(text, config)
|
const formattedText = processText(text, config)
|
||||||
|
|
||||||
await createFile(filePath, formattedText)
|
await createFile(filePath, formattedText)
|
||||||
|
|
||||||
|
const diagnosticsAfterFormat = await lintFile(filePath)
|
||||||
|
const diagnosticsCountAfterFormat = diagnosticsAfterFormat.length
|
||||||
|
|
||||||
|
const fixedDiagnosticsCount =
|
||||||
|
diagnosticsCountBeforeFormat - diagnosticsCountAfterFormat
|
||||||
|
|
||||||
|
const updatedFilePaths: string[] = []
|
||||||
|
|
||||||
|
if (fixedDiagnosticsCount) {
|
||||||
|
updatedFilePaths.push(filePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
updatedFilePaths,
|
||||||
|
fixedDiagnosticsCount,
|
||||||
|
unfixedDiagnostics: diagnosticsAfterFormat
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { listSubFoldersInFolder } from '@sasjs/utils/file'
|
import { listSubFoldersInFolder } from '@sasjs/utils/file'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
import { lintFolder } from '../lint'
|
||||||
|
import { FormatResult } from '../types'
|
||||||
import { LintConfig } from '../types/LintConfig'
|
import { LintConfig } from '../types/LintConfig'
|
||||||
import { asyncForEach } from '../utils/asyncForEach'
|
import { asyncForEach } from '../utils/asyncForEach'
|
||||||
import { getLintConfig } from '../utils/getLintConfig'
|
import { getLintConfig } from '../utils/getLintConfig'
|
||||||
@@ -19,13 +21,18 @@ const excludeFolders = [
|
|||||||
* Automatically formats all SAS files in the folder at the given path.
|
* Automatically formats all SAS files in the folder at the given path.
|
||||||
* @param {string} folderPath - the path to the folder to be formatted.
|
* @param {string} folderPath - the path to the folder to be formatted.
|
||||||
* @param {LintConfig} configuration - an optional configuration. When not passed in, this is read from the .sasjslint file.
|
* @param {LintConfig} configuration - an optional configuration. When not passed in, this is read from the .sasjslint file.
|
||||||
* @returns {Promise<void>} Resolves successfully when all SAS files in the given folder have been formatted.
|
* @returns {Promise<FormatResult>} Resolves successfully when all SAS files in the given folder have been formatted.
|
||||||
*/
|
*/
|
||||||
export const formatFolder = async (
|
export const formatFolder = async (
|
||||||
folderPath: string,
|
folderPath: string,
|
||||||
configuration?: LintConfig
|
configuration?: LintConfig
|
||||||
) => {
|
): Promise<FormatResult> => {
|
||||||
const config = configuration || (await getLintConfig())
|
const config = configuration || (await getLintConfig())
|
||||||
|
const diagnosticsBeforeFormat = await lintFolder(folderPath)
|
||||||
|
const diagnosticsCountBeforeFormat = Array.from(
|
||||||
|
diagnosticsBeforeFormat.values()
|
||||||
|
).reduce((a, b) => a + b.length, 0)
|
||||||
|
|
||||||
const fileNames = await listSasFiles(folderPath)
|
const fileNames = await listSasFiles(folderPath)
|
||||||
await asyncForEach(fileNames, async (fileName) => {
|
await asyncForEach(fileNames, async (fileName) => {
|
||||||
const filePath = path.join(folderPath, fileName)
|
const filePath = path.join(folderPath, fileName)
|
||||||
@@ -39,4 +46,29 @@ export const formatFolder = async (
|
|||||||
await asyncForEach(subFolders, async (subFolder) => {
|
await asyncForEach(subFolders, async (subFolder) => {
|
||||||
await formatFolder(path.join(folderPath, subFolder), config)
|
await formatFolder(path.join(folderPath, subFolder), config)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const diagnosticsAfterFormat = await lintFolder(folderPath)
|
||||||
|
const diagnosticsCountAfterFormat = Array.from(
|
||||||
|
diagnosticsAfterFormat.values()
|
||||||
|
).reduce((a, b) => a + b.length, 0)
|
||||||
|
|
||||||
|
const fixedDiagnosticsCount =
|
||||||
|
diagnosticsCountBeforeFormat - diagnosticsCountAfterFormat
|
||||||
|
|
||||||
|
const updatedFilePaths: string[] = []
|
||||||
|
|
||||||
|
Array.from(diagnosticsBeforeFormat.keys()).forEach((filePath) => {
|
||||||
|
const diagnosticsBefore = diagnosticsBeforeFormat.get(filePath) || []
|
||||||
|
const diagnosticsAfter = diagnosticsAfterFormat.get(filePath) || []
|
||||||
|
|
||||||
|
if (diagnosticsBefore.length !== diagnosticsAfter.length) {
|
||||||
|
updatedFilePaths.push(filePath)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
updatedFilePaths,
|
||||||
|
fixedDiagnosticsCount,
|
||||||
|
unfixedDiagnostics: diagnosticsAfterFormat
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
|
import { lintFolder } from '../lint/lintFolder'
|
||||||
|
import { FormatResult } from '../types/FormatResult'
|
||||||
import { getProjectRoot } from '../utils/getProjectRoot'
|
import { getProjectRoot } from '../utils/getProjectRoot'
|
||||||
import { formatFolder } from './formatFolder'
|
import { formatFolder } from './formatFolder'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Automatically formats all SAS files in the current project.
|
* Automatically formats all SAS files in the current project.
|
||||||
* @returns {Promise<void>} Resolves successfully when all SAS files in the current project have been formatted.
|
* @returns {Promise<FormatResult>} Resolves successfully when all SAS files in the current project have been formatted.
|
||||||
*/
|
*/
|
||||||
export const formatProject = async () => {
|
export const formatProject = async (): Promise<FormatResult> => {
|
||||||
const projectRoot =
|
const projectRoot =
|
||||||
(await getProjectRoot()) || process.projectDir || process.currentDir
|
(await getProjectRoot()) || process.projectDir || process.currentDir
|
||||||
if (!projectRoot) {
|
if (!projectRoot) {
|
||||||
throw new Error('SASjs Project Root was not found.')
|
throw new Error('SASjs Project Root was not found.')
|
||||||
}
|
}
|
||||||
|
|
||||||
return await formatFolder(projectRoot)
|
return await formatFolder(projectRoot)
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/types/FormatResult.ts
Normal file
7
src/types/FormatResult.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { Diagnostic } from './Diagnostic'
|
||||||
|
|
||||||
|
export interface FormatResult {
|
||||||
|
updatedFilePaths: string[]
|
||||||
|
fixedDiagnosticsCount: number
|
||||||
|
unfixedDiagnostics: Map<string, Diagnostic[]> | Diagnostic[]
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
export * from './Diagnostic'
|
export * from './Diagnostic'
|
||||||
|
export * from './FormatResult'
|
||||||
export * from './LintConfig'
|
export * from './LintConfig'
|
||||||
export * from './LintRule'
|
export * from './LintRule'
|
||||||
export * from './LintRuleType'
|
export * from './LintRuleType'
|
||||||
|
|||||||
Reference in New Issue
Block a user