Skip to content

Commit

Permalink
chore: fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
azz committed Feb 9, 2018
1 parent 229f1f8 commit 3fad3f7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* `[babel-jest]` moduleFileExtensions not passed to babel transformer.
([#4637](https://github.com/facebook/jest/issues/4637))
* `[jest-docblock]` Support multiple of the same `@pragma`.
([#5154](https://github.com/facebook/jest/pull/5502))

### Features

Expand Down
21 changes: 20 additions & 1 deletion packages/jest-docblock/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
'/**' +
Expand Down Expand Up @@ -433,7 +453,6 @@ describe('docblock', () => {
' */',
);
});

it('prints docblocks with pragmas', () => {
const pragmas = {
flow: 'foo',
Expand Down
13 changes: 7 additions & 6 deletions packages/jest-docblock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)*?\*\/)/;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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}`;
}
}

Expand Down

0 comments on commit 3fad3f7

Please sign in to comment.