Skip to content

Commit 229d010

Browse files
author
Brian Lonsdorf
authored
Merge pull request #1 from salesforce-ux/fix/comments
Ignore comments inside a code fence
2 parents 802c73d + 83f6fa4 commit 229d010

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
exports[`test ignores annotations inside a code fence 1`] = `
2+
Array [
3+
Object {
4+
"annotations": Object {
5+
"a": true,
6+
"b": true,
7+
},
8+
"description": "This comment contains a code fence
9+
\`\`\`scss
10+
@media () {}
11+
\`\`\`",
12+
},
13+
]
14+
`;
15+
16+
exports[`test parses description and annotations (multi) 1`] = `
17+
Array [
18+
Object {
19+
"annotations": Object {
20+
"a": "hello",
21+
"b": "world",
22+
},
23+
"description": "This is a multi-line
24+
description",
25+
},
26+
]
27+
`;
28+
29+
exports[`test parses description and annotations 1`] = `
30+
Array [
31+
Object {
32+
"annotations": Object {
33+
"world": true,
34+
},
35+
"description": "Hello",
36+
},
37+
]
38+
`;

lib/__tests__/comments.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const comments = require('../comments')
2+
3+
it('parses description and annotations', () => {
4+
expect(comments(`
5+
/**
6+
* Hello
7+
* @world
8+
*/
9+
`)).toMatchSnapshot()
10+
})
11+
12+
it('parses description and annotations (multi)', () => {
13+
expect(comments(`
14+
/**
15+
* This is a multi-line
16+
* description
17+
* @a hello
18+
* @b world
19+
*/
20+
`)).toMatchSnapshot()
21+
})
22+
23+
it('ignores annotations inside a code fence', () => {
24+
expect(comments(`
25+
/**
26+
* This comment contains a code fence
27+
* \`\`\`scss
28+
* @media () {}
29+
* \`\`\`
30+
* @a
31+
* @b
32+
*/
33+
`)).toMatchSnapshot()
34+
})

lib/comments.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ const remove = pattern => s => s.replace(pattern, '')
2525
*
2626
*/
2727
const reduceComment = (comment, line) => {
28-
let match = line.match(/^@([^\s]+)(?:\s(.+))?/)
29-
if (!match) {
28+
let annotation = comment.get('__codeFence')
29+
? false
30+
: line.match(/^@([^\s]+)(?:\s(.+))?/)
31+
if (/```/.test(line)) {
32+
comment = comment.set('__codeFence', !comment.get('__codeFence', false))
33+
}
34+
if (!annotation) {
3035
return comment.update('description', description =>
3136
description.push(line)
3237
)
3338
} else {
34-
return comment.setIn(['annotations', match[1]],
35-
_.isUndefined(match[2]) ? true : match[2]
39+
return comment.setIn(['annotations', annotation[1]],
40+
_.isUndefined(annotation[2]) ? true : annotation[2]
3641
)
3742
}
3843
}
@@ -51,6 +56,8 @@ module.exports = styles => {
5156
)
5257
.map(comment => comment
5358
.reduce(reduceComment, baseComment)
59+
.update(comment =>
60+
comment.keySeq().reduce((a, b) => /^__/.test(b) ? a.delete(b) : a, comment))
5461
.update(comment => comment
5562
.update('description', description =>
5663
description.join('\n').trim()

0 commit comments

Comments
 (0)