Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/types/node-15.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna-acondy authored May 6, 2021
2 parents a95c083 + 3dc304f commit 850cf85
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 20 deletions.
71 changes: 63 additions & 8 deletions src/format/formatFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,90 @@ describe('formatFile', () => {
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
await createFile(path.join(__dirname, 'format-file-test.sas'), content)
const expectedResult = {
updatedFilePaths: [path.join(__dirname, 'format-file-test.sas')],
fixedDiagnosticsCount: 3,
unfixedDiagnostics: []
}

await formatFile(path.join(__dirname, 'format-file-test.sas'))
const result = await readFile(path.join(__dirname, 'format-file-test.sas'))
const result = await formatFile(
path.join(__dirname, 'format-file-test.sas')
)
const formattedContent = await readFile(
path.join(__dirname, 'format-file-test.sas')
)

expect(result).toEqual(expectedContent)
expect(result).toEqual(expectedResult)
expect(formattedContent).toEqual(expectedContent)

await deleteFile(path.join(__dirname, 'format-file-test.sas'))
})

it('should use the provided config if available', async () => {
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
const expectedContent = `/**\r\n @file\r\n @brief <Your brief here>\r\n <h4> SAS Macros </h4>\r\n**/\r\n%macro somemacro();\r\n%put 'hello';\r\n%mend somemacro;`
const expectedContent = `/**\r\n @file\r\n @brief <Your brief here>\r\n <h4> SAS Macros </h4>\r\n**/\r\n%macro somemacro();\r\n%put 'hello';\r\n%mend;`
const expectedResult = {
updatedFilePaths: [path.join(__dirname, 'format-file-config.sas')],
fixedDiagnosticsCount: 2,
unfixedDiagnostics: [
{
endColumnNumber: 7,
lineNumber: 8,
message: '%mend statement is missing macro name - somemacro',
severity: 1,
startColumnNumber: 1
}
]
}
await createFile(path.join(__dirname, 'format-file-config.sas'), content)

await formatFile(
const result = await formatFile(
path.join(__dirname, 'format-file-config.sas'),
new LintConfig({
lineEndings: 'crlf',
hasMacroNameInMend: true,
hasMacroNameInMend: false,
hasDoxygenHeader: true,
noTrailingSpaces: true
})
)
const result = await readFile(
const formattedContent = await readFile(
path.join(__dirname, 'format-file-config.sas')
)

expect(result).toEqual(expectedContent)
expect(result).toEqual(expectedResult)
expect(formattedContent).toEqual(expectedContent)

await deleteFile(path.join(__dirname, 'format-file-config.sas'))
})

it('should not update any files if there are no formatting violations', async () => {
const content = `/**\r\n @file\r\n @brief <Your brief here>\r\n <h4> SAS Macros </h4>\r\n**/\r\n%macro somemacro();\r\n%put 'hello';\r\n%mend somemacro;`
const expectedResult = {
updatedFilePaths: [],
fixedDiagnosticsCount: 0,
unfixedDiagnostics: []
}
await createFile(
path.join(__dirname, 'format-file-no-violations.sas'),
content
)

const result = await formatFile(
path.join(__dirname, 'format-file-no-violations.sas'),
new LintConfig({
lineEndings: 'crlf',
hasMacroNameInMend: true,
hasDoxygenHeader: true,
noTrailingSpaces: true
})
)
const formattedContent = await readFile(
path.join(__dirname, 'format-file-no-violations.sas')
)

expect(result).toEqual(expectedResult)
expect(formattedContent).toEqual(content)

await deleteFile(path.join(__dirname, 'format-file-no-violations.sas'))
})
})
27 changes: 25 additions & 2 deletions src/format/formatFile.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<void>} Resolves successfully when the file has been formatted.
* @returns {Promise<FormatResult>} Resolves successfully when the file has been formatted.
*/
export const formatFile = async (
filePath: string,
configuration?: LintConfig
) => {
): Promise<FormatResult> => {
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
}
}
181 changes: 175 additions & 6 deletions src/format/formatFolder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,165 @@ import {
deleteFolder,
readFile
} from '@sasjs/utils/file'
import { Diagnostic, LintConfig } from '../types'

describe('formatFolder', () => {
it('should fix linting issues in a given folder', async () => {
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
const expectedResult = {
updatedFilePaths: [
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
],
fixedDiagnosticsCount: 3,
unfixedDiagnostics: new Map<string, Diagnostic[]>([
[
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
[]
]
])
}
await createFolder(path.join(__dirname, 'format-folder-test'))
await createFile(
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
content
)

await formatFolder(path.join(__dirname, 'format-folder-test'))
const result = await readFile(
const result = await formatFolder(
path.join(__dirname, 'format-folder-test')
)
const formattedContent = await readFile(
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
)

expect(formattedContent).toEqual(expectedContent)
expect(result).toEqual(expectedResult)

await deleteFolder(path.join(__dirname, 'format-folder-test'))
})

it('should fix linting issues in subfolders of a given folder', async () => {
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
const expectedResult = {
updatedFilePaths: [
path.join(
__dirname,
'format-folder-test',
'subfolder',
'format-folder-test.sas'
)
],
fixedDiagnosticsCount: 3,
unfixedDiagnostics: new Map<string, Diagnostic[]>([
[
path.join(
__dirname,
'format-folder-test',
'subfolder',
'format-folder-test.sas'
),
[]
]
])
}

await createFolder(path.join(__dirname, 'format-folder-test'))
await createFolder(path.join(__dirname, 'subfolder'))
await createFile(
path.join(
__dirname,
'format-folder-test',
'subfolder',
'format-folder-test.sas'
),
content
)

const result = await formatFolder(
path.join(__dirname, 'format-folder-test')
)
const formattedContent = await readFile(
path.join(
__dirname,
'format-folder-test',
'subfolder',
'format-folder-test.sas'
)
)

expect(result).toEqual(expectedResult)
expect(formattedContent).toEqual(expectedContent)

await deleteFolder(path.join(__dirname, 'format-folder-test'))
})

it('should use a custom configuration when provided', async () => {
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
const expectedResult = {
updatedFilePaths: [
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
],
fixedDiagnosticsCount: 3,
unfixedDiagnostics: new Map<string, Diagnostic[]>([
[
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
[]
]
])
}
await createFolder(path.join(__dirname, 'format-folder-test'))
await createFile(
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
content
)

const result = await formatFolder(
path.join(__dirname, 'format-folder-test'),
new LintConfig({
lineEndings: 'crlf',
hasMacroNameInMend: false,
hasDoxygenHeader: true,
noTrailingSpaces: true
})
)
const formattedContent = await readFile(
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
)

expect(result).toEqual(expectedContent)
expect(formattedContent).toEqual(expectedContent)
expect(result).toEqual(expectedResult)

await deleteFolder(path.join(__dirname, 'format-folder-test'))
})

it('should fix linting issues in subfolders of a given folder', async () => {
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
const expectedResult = {
updatedFilePaths: [
path.join(
__dirname,
'format-folder-test',
'subfolder',
'format-folder-test.sas'
)
],
fixedDiagnosticsCount: 3,
unfixedDiagnostics: new Map<string, Diagnostic[]>([
[
path.join(
__dirname,
'format-folder-test',
'subfolder',
'format-folder-test.sas'
),
[]
]
])
}

await createFolder(path.join(__dirname, 'format-folder-test'))
await createFolder(path.join(__dirname, 'subfolder'))
await createFile(
Expand All @@ -42,8 +177,10 @@ describe('formatFolder', () => {
content
)

await formatFolder(path.join(__dirname, 'format-folder-test'))
const result = await readFile(
const result = await formatFolder(
path.join(__dirname, 'format-folder-test')
)
const formattedContent = await readFile(
path.join(
__dirname,
'format-folder-test',
Expand All @@ -52,7 +189,39 @@ describe('formatFolder', () => {
)
)

expect(result).toEqual(expectedContent)
expect(result).toEqual(expectedResult)
expect(formattedContent).toEqual(expectedContent)

await deleteFolder(path.join(__dirname, 'format-folder-test'))
})

it('should not update any files when there are no violations', async () => {
const content = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
const expectedResult = {
updatedFilePaths: [],
fixedDiagnosticsCount: 0,
unfixedDiagnostics: new Map<string, Diagnostic[]>([
[
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
[]
]
])
}
await createFolder(path.join(__dirname, 'format-folder-test'))
await createFile(
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
content
)

const result = await formatFolder(
path.join(__dirname, 'format-folder-test')
)
const formattedContent = await readFile(
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
)

expect(formattedContent).toEqual(content)
expect(result).toEqual(expectedResult)

await deleteFolder(path.join(__dirname, 'format-folder-test'))
})
Expand Down
Loading

0 comments on commit 850cf85

Please sign in to comment.