From dce94536802a60d89eb9feb633a1388d8ef310a0 Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Tue, 4 May 2021 08:27:06 +0100 Subject: [PATCH] feat(format-diagnostics): add diagnostic information to format result payload --- src/format/formatFile.ts | 27 +++++++++++++++++++++++++-- src/format/formatFolder.ts | 36 ++++++++++++++++++++++++++++++++++-- src/format/formatProject.ts | 7 +++++-- src/types/FormatResult.ts | 7 +++++++ src/types/index.ts | 1 + 5 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 src/types/FormatResult.ts diff --git a/src/format/formatFile.ts b/src/format/formatFile.ts index fa6950f..87bd36f 100644 --- a/src/format/formatFile.ts +++ b/src/format/formatFile.ts @@ -1,4 +1,6 @@ import { createFile, readFile } from '@sasjs/utils/file' +import { lintFile } from '../lint' +import { FormatResult } from '../types' import { LintConfig } from '../types/LintConfig' import { getLintConfig } from '../utils/getLintConfig' import { processText } from './shared' @@ -7,16 +9,37 @@ import { processText } from './shared' * Applies automatic formatting to the file at the given path. * @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. - * @returns {Promise} Resolves successfully when the file has been formatted. + * @returns {Promise} Resolves successfully when the file has been formatted. */ export const formatFile = async ( filePath: string, configuration?: LintConfig -) => { +): Promise => { const config = configuration || (await getLintConfig()) + const diagnosticsBeforeFormat = await lintFile(filePath) + const diagnosticsCountBeforeFormat = diagnosticsBeforeFormat.length + const text = await readFile(filePath) const formattedText = processText(text, config) 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 + } } diff --git a/src/format/formatFolder.ts b/src/format/formatFolder.ts index b7ec86f..0685822 100644 --- a/src/format/formatFolder.ts +++ b/src/format/formatFolder.ts @@ -1,5 +1,7 @@ import { listSubFoldersInFolder } from '@sasjs/utils/file' import path from 'path' +import { lintFolder } from '../lint' +import { FormatResult } from '../types' import { LintConfig } from '../types/LintConfig' import { asyncForEach } from '../utils/asyncForEach' import { getLintConfig } from '../utils/getLintConfig' @@ -19,13 +21,18 @@ const excludeFolders = [ * Automatically formats all SAS files in the folder at the given path. * @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. - * @returns {Promise} Resolves successfully when all SAS files in the given folder have been formatted. + * @returns {Promise} Resolves successfully when all SAS files in the given folder have been formatted. */ export const formatFolder = async ( folderPath: string, configuration?: LintConfig -) => { +): Promise => { 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) await asyncForEach(fileNames, async (fileName) => { const filePath = path.join(folderPath, fileName) @@ -39,4 +46,29 @@ export const formatFolder = async ( await asyncForEach(subFolders, async (subFolder) => { 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 + } } diff --git a/src/format/formatProject.ts b/src/format/formatProject.ts index c0eb2fd..7c8033d 100644 --- a/src/format/formatProject.ts +++ b/src/format/formatProject.ts @@ -1,15 +1,18 @@ +import { lintFolder } from '../lint/lintFolder' +import { FormatResult } from '../types/FormatResult' import { getProjectRoot } from '../utils/getProjectRoot' import { formatFolder } from './formatFolder' /** * Automatically formats all SAS files in the current project. - * @returns {Promise} Resolves successfully when all SAS files in the current project have been formatted. + * @returns {Promise} Resolves successfully when all SAS files in the current project have been formatted. */ -export const formatProject = async () => { +export const formatProject = async (): Promise => { const projectRoot = (await getProjectRoot()) || process.projectDir || process.currentDir if (!projectRoot) { throw new Error('SASjs Project Root was not found.') } + return await formatFolder(projectRoot) } diff --git a/src/types/FormatResult.ts b/src/types/FormatResult.ts new file mode 100644 index 0000000..39aee10 --- /dev/null +++ b/src/types/FormatResult.ts @@ -0,0 +1,7 @@ +import { Diagnostic } from './Diagnostic' + +export interface FormatResult { + updatedFilePaths: string[] + fixedDiagnosticsCount: number + unfixedDiagnostics: Map | Diagnostic[] +} diff --git a/src/types/index.ts b/src/types/index.ts index 148327e..aba5333 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,5 @@ export * from './Diagnostic' +export * from './FormatResult' export * from './LintConfig' export * from './LintRule' export * from './LintRuleType'