Skip to content

Commit

Permalink
fix(snippets): fixed line ending
Browse files Browse the repository at this point in the history
  • Loading branch information
YuryShkoda committed Jul 20, 2023
1 parent a4185ef commit 63b6e44
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 55 deletions.
15 changes: 12 additions & 3 deletions src/commands/snippets/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ export async function generateSnippets(

const createMacro = async (file: string): Promise<Snippet> => {
const fileContent = await readFile(file)
const lines = fileContent.split('\n')

enum LineEndings {
CRLF = `\r\n`,
LF = `\n`
}
const lineEnding = new RegExp(LineEndings.CRLF).test(fileContent)
? LineEndings.CRLF
: LineEndings.LF

const lines = fileContent.split(lineEnding)

let brief = lines.filter((line) => briefRegExp.test(line))
let params = lines.filter((line) => paramRegExp.test(line))
Expand All @@ -99,12 +108,12 @@ const createMacro = async (file: string): Promise<Snippet> => {
if (params.length) {
brief.push('\r')

params = params.map((param) => param.replace(paramRegExp, '-') + '\r')
params = params.map((param) => param.replace(paramRegExp, '-'))
}

const description = [
...brief,
params.length ? 'Params:\r' : '',
params.length ? `Params:` : '',
...params
].filter((line) => line)

Expand Down
47 changes: 0 additions & 47 deletions src/commands/snippets/spec/expectedSnippets.json

This file was deleted.

54 changes: 49 additions & 5 deletions src/commands/snippets/spec/snippets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,55 @@ describe('sasjs snippets', () => {

await expect(fileExists(outputFilePath)).resolves.toEqual(true)

const expectedSnippets = await readFile(
path.join(__dirname, 'expectedSnippets.json')
)

const generatedSnippets = await readFile(outputFilePath)
const expectedSnippets = {
macro2: {
prefix: '%macro2',
body: '%macro2($1)',
description: [
'Macro 2',
'\r',
'Params:',
'-msg The message to be printed'
]
},
badMacro: {
prefix: '%badMacro',
body: '%badMacro($1)',
description: []
},
example: {
prefix: '%example',
body: '%example($1)',
description: [
'An example macro',
'\r',
'Params:',
'-msg The message to be printed'
]
},
subMacro: {
prefix: '%subMacro',
body: '%subMacro($1)',
description: [
'A sub macro',
'\r',
'Params:',
'-msg The message to be printed'
]
},
subSubMacro: {
prefix: '%subSubMacro',
body: '%subSubMacro($1)',
description: [
'A sub sub macro',
'\r',
'Params:',
'-msg The message to be printed'
]
}
}

const generatedSnippets = JSON.parse(await readFile(outputFilePath))

expect(generatedSnippets).toEqual(expectedSnippets)
})
Expand Down

0 comments on commit 63b6e44

Please sign in to comment.