diff --git a/CHANGELOG.md b/CHANGELOG.md index c56b24dca680..0081eadcd5c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ * `[babel-jest]` Revert "Remove retainLines from babel-jest" ([#5496](https://github.com/facebook/jest/pull/5496)) +* `[jest-docblock]` Support multiple of the same `@pragma`. + ([#5154](https://github.com/facebook/jest/pull/5502)) ### Features diff --git a/packages/jest-docblock/src/__tests__/index.test.js b/packages/jest-docblock/src/__tests__/index.test.js index 380c04212c50..38ea99797371 100644 --- a/packages/jest-docblock/src/__tests__/index.test.js +++ b/packages/jest-docblock/src/__tests__/index.test.js @@ -140,6 +140,26 @@ describe('docblock', () => { }); }); + it('parses >=3 of the same directives out of a docblock', () => { + const code = + '/**' + + os.EOL + + '' + + ' * @x foo' + + os.EOL + + '' + + ' * @x bar' + + os.EOL + + '' + + ' * @x baz' + + os.EOL + + '' + + ' */'; + expect(docblock.parse(code)).toEqual({ + x: ['foo', 'bar', 'baz'], + }); + }); + it('parses directives out of a docblock with comments', () => { const code = '/**' + @@ -433,7 +453,6 @@ describe('docblock', () => { ' */', ); }); - it('prints docblocks with pragmas', () => { const pragmas = { flow: 'foo', diff --git a/packages/jest-docblock/src/index.js b/packages/jest-docblock/src/index.js index e84e3e33b110..e2740dda1dc9 100644 --- a/packages/jest-docblock/src/index.js +++ b/packages/jest-docblock/src/index.js @@ -10,6 +10,8 @@ import detectNewline from 'detect-newline'; import {EOL} from 'os'; +type Pragmas = {[key: string]: string | string[], __proto__: null}; + const commentEndRe = /\*\/$/; const commentStartRe = /^\/\*\*/; const docblockRe = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/; @@ -31,15 +33,13 @@ export function strip(contents: string) { return match && match[0] ? contents.substring(match[0].length) : contents; } -export function parse( - docblock: string, -): {[key: string]: string, __proto__: null} { +export function parse(docblock: string): Pragmas { return parseWithComments(docblock).pragmas; } export function parseWithComments( docblock: string, -): {comments: string, pragmas: {[key: string]: string, __proto__: null}} { +): {comments: string, pragmas: Pragmas} { const line = detectNewline(docblock) || EOL; docblock = docblock @@ -82,7 +82,7 @@ export function print({ pragmas = {}, }: { comments?: string, - pragmas?: {[key: string]: string, __proto__: null}, + pragmas?: Pragmas, __proto__: null, }): string { const line = detectNewline(comments) || EOL; @@ -103,7 +103,8 @@ export function print({ return ''; } if (keys.length === 1 && !Array.isArray(pragmas[keys[0]])) { - return `${head} ${printKeyValues(keys[0], pragmas[keys[0]])}${tail}`; + const value = pragmas[keys[0]]; + return `${head} ${printKeyValues(keys[0], value)[0]}${tail}`; } }