Skip to content

Commit

Permalink
Add support for multi line single JS line docs
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulioRandall committed May 5, 2024
1 parent b94e3ed commit f11999e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Simple tool for adding parsable metadata as comments within Svelte components.",
"type": "module",
"license": "MIT",
"version": "0.5.1",
"version": "0.6.0",
"engines": {
"node": ">=18"
},
Expand Down
13 changes: 13 additions & 0 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const extractNodes = (data, options) => {
//p23.group.name: Abc
const jsLineRegexp = regexp.newJsLine(options.prefix)
const jsLineNodes = extractNodesWithRegexp(data, jsLineRegexp)
tidyJsLineNodes(jsLineNodes)

/*p23.name: Abc*/
/*p23.group.name:
Expand Down Expand Up @@ -97,6 +98,18 @@ const extractNodesWithRegexp = (data, regexp) => {
return nodes
}

const tidyJsLineNodes = (nodes) => {
for (const k in nodes) {
const v = nodes[k]

if (isObject(v)) {
tidyJsLineNodes(v)
} else {
nodes[k] = v.replace(/^[^\S\r\n]*\/\//gm, '')
}
}
}

const mergeNodes = (dst, src) => {
for (const k in src) {
if (isObject(dst[k]) && isObject(src[k])) {
Expand Down
15 changes: 15 additions & 0 deletions src/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ describe('parser.js', () => {
])
})

test('Parses multiple lines in series that represent a single node', () => {
const file = createSvelteFilePath('JsLine_MultiLine')
const metadata = parse(file)

expect(metadata).toEqual([
{
...generateFileFields(file),
nodes: {
name: 'Meh',
description: [' Abc', ' a', ' b', '', ' y', ' z'].join('\n'),
},
},
])
})

test('Parses non-nested single line node (lowercase p23)', () => {
const file = createSvelteFilePath('JsLine_NonNested_Lowercase')
const metadata = parse(file)
Expand Down
14 changes: 10 additions & 4 deletions src/regexp.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
const newJsLine = (prefix = 'p23') => {
// TODO: Lookahead to see if "\\\n[^S\\\r\\\n]*//" on the next line
// TODO: if so the next line forms part of the comment.
//
// TODO: But the '[^S]*//' at the start will be replaced with '\n'
//
// TODO: Might need to add the s flag.
return RegExp(
`^[^S\\\r\\\n]*//${prefix}((?:.[$a-zA-Z_][$a-zA-Z_0-9]*)+):(.*)$`,
'igm'
`^[^\\\S\\\r\\\n]*\\\/\\\/${prefix}((?:.[$a-zA-Z_][$a-zA-Z_0-9]*)+):([^\\\n]*(?:\\\n[^\\\S\\\r\\\n]*\\\/\\\/(?!${prefix})[^\\\n]*)*)`,
'igms'
)
}

const newJsBlock = (prefix = 'p23') => {
return RegExp(
`^[^S\\\r\\\n]*\\\/\\\*${prefix}((?:.[$a-zA-Z_][$a-zA-Z_0-9]*)+):(.*?)\\\*\\\/`,
`^[^\S\\\r\\\n]*\\\/\\\*${prefix}((?:.[$a-zA-Z_][$a-zA-Z_0-9]*)+):(.*?)\\\*\\\/`,
'igms'
)
}

const newHtml = (prefix = 'p23') => {
return RegExp(
`^[^S\\\r\\\n]*<!--${prefix}((?:.[$a-zA-Z_][$a-zA-Z_0-9]*)+):(.*?)-->`,
`^[^\S\\\r\\\n]*<!--${prefix}((?:.[$a-zA-Z_][$a-zA-Z_0-9]*)+):(.*?)-->`,
'igms'
)
}
Expand Down
21 changes: 20 additions & 1 deletion src/regexp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ describe('regexp.js', () => {
expect(act).toEqual(exp)
})

test('Multiple single line comments in series', () => {
const input = [
' //p23.description:',
' // Abc',
' // 123',
' //',
' // Xyz',
].join('\n')

const expValue = ['', ' // Abc', ' // 123', ' //', ' // Xyz'].join(
'\n'
)

const act = regexp.newJsLine().exec(input)

const exp = expect.arrayContaining([input, '.description', expValue])
expect(act).toEqual(exp)
})

test('Custom prefix', () => {
const act = regexp
.newJsLine('my_custom_prefix')
Expand Down Expand Up @@ -70,7 +89,7 @@ describe('regexp.js', () => {
})

const multiLineKey = '.name'
const multiLineValue = 'value\n\tAbc\n\tXyz\n\t'
const multiLineValue = '\n\tvalue\n\tAbc\n\tXyz\n\t'
const multiLineInput = `p23${multiLineKey}:${multiLineValue}`

describe('newJsBlock', () => {
Expand Down
11 changes: 11 additions & 0 deletions src/testdata/files/JsLine_MultiLine.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
//p23.name:Meh
//p23.description: Abc
// a
// b
//
// y
// z
</script>

<div />

0 comments on commit f11999e

Please sign in to comment.