Skip to content

Commit cd2ce61

Browse files
committed
feat(jest-docblock): support multiple of the same @pragma
1 parent 3942361 commit cd2ce61

3 files changed

Lines changed: 58 additions & 9 deletions

File tree

packages/jest-docblock/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ string (`""`).
9999
Strips the top docblock from a file and return the result. If a file does not
100100
have a docblock at the top, then return the file unchanged.
101101

102-
### `parse(docblock: string): {[key: string]: string}`
102+
### `parse(docblock: string): {[key: string]: string | string[] }`
103103

104104
Parses the pragmas in a docblock string into an object whose keys are the pragma
105105
tags and whose values are the arguments to those pragmas.
106106

107-
### `parseWithComments(docblock: string): { comments: string, pragmas: {[key: string]: string} }`
107+
### `parseWithComments(docblock: string): { comments: string, pragmas: {[key: string]: string | string[]} }`
108108

109109
Similar to `parse` except this method also returns the comments from the
110110
docblock. Useful when used with `print()`.
111111

112-
### `print({ comments?: string, pragmas?: {[key: string]: string} }): string`
112+
### `print({ comments?: string, pragmas?: {[key: string]: string | string[]} }): string`
113113

114114
Prints an object of key-value pairs back into a docblock. If `comments` are
115115
provided, they will be positioned on the top of the docblock.

packages/jest-docblock/src/__tests__/index.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,27 @@ describe('docblock', () => {
119119
});
120120
});
121121

122+
it('parses multiple of the same directives out of a docblock', () => {
123+
const code =
124+
'/**' +
125+
os.EOL +
126+
'' +
127+
' * @x foo' +
128+
os.EOL +
129+
'' +
130+
' * @x bar' +
131+
os.EOL +
132+
'' +
133+
' * @y' +
134+
os.EOL +
135+
'' +
136+
' */';
137+
expect(docblock.parse(code)).toEqual({
138+
x: ['foo', 'bar'],
139+
y: '',
140+
});
141+
});
142+
122143
it('parses directives out of a docblock with comments', () => {
123144
const code =
124145
'/**' +
@@ -395,6 +416,24 @@ describe('docblock', () => {
395416
);
396417
});
397418

419+
it('prints docblocks with multiple of the same pragma', () => {
420+
const pragmas = {
421+
x: ['a', 'b'],
422+
y: 'c',
423+
};
424+
expect(docblock.print({pragmas})).toEqual(
425+
'/**' +
426+
os.EOL +
427+
' * @x a' +
428+
os.EOL +
429+
' * @x b' +
430+
os.EOL +
431+
' * @y c' +
432+
os.EOL +
433+
' */',
434+
);
435+
});
436+
398437
it('prints docblocks with pragmas', () => {
399438
const pragmas = {
400439
flow: 'foo',

packages/jest-docblock/src/index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ export function parseWithComments(
6464
let match;
6565
while ((match = propertyRe.exec(docblock))) {
6666
// strip linecomments from pragmas
67-
result[match[1]] = match[2].replace(lineCommentRe, '');
67+
const nextPragma = match[2].replace(lineCommentRe, '');
68+
if (
69+
typeof result[match[1]] === 'string' ||
70+
Array.isArray(result[match[1]])
71+
) {
72+
result[match[1]] = [].concat(result[match[1]], nextPragma);
73+
} else {
74+
result[match[1]] = nextPragma;
75+
}
6876
}
6977
return {comments, pragmas: result};
7078
}
@@ -85,15 +93,17 @@ export function print({
8593
const keys = Object.keys(pragmas);
8694

8795
const printedObject = keys
88-
.map(key => start + ' ' + printKeyValue(key, pragmas[key]) + line)
96+
.map(key => printKeyValues(key, pragmas[key]))
97+
.reduce((arr, next) => arr.concat(next), [])
98+
.map(keyValue => start + ' ' + keyValue + line)
8999
.join('');
90100

91101
if (!comments) {
92102
if (keys.length === 0) {
93103
return '';
94104
}
95-
if (keys.length === 1) {
96-
return `${head} ${printKeyValue(keys[0], pragmas[keys[0]])}${tail}`;
105+
if (keys.length === 1 && !Array.isArray(pragmas[keys[0]])) {
106+
return `${head} ${printKeyValues(keys[0], pragmas[keys[0]])}${tail}`;
97107
}
98108
}
99109

@@ -113,6 +123,6 @@ export function print({
113123
);
114124
}
115125

116-
function printKeyValue(key, value) {
117-
return `@${key} ${value}`.trim();
126+
function printKeyValues(key, valueOrArray) {
127+
return [].concat(valueOrArray).map(value => `@${key} ${value}`.trim());
118128
}

0 commit comments

Comments
 (0)