Skip to content

Commit

Permalink
test: flow comment types
Browse files Browse the repository at this point in the history
  • Loading branch information
fgnass authored and tmcw committed May 26, 2018
1 parent 85d50f9 commit 46ee751
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
17 changes: 17 additions & 0 deletions __tests__/fixture/flow/comment-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @flow

/*::
type Foo = {
foo: number,
bar: boolean,
baz: string
};
*/

class MyClass {
/*:: prop: Foo; */

method(value /*: Foo */) /*: boolean */ {
return value.bar;
}
}
23 changes: 23 additions & 0 deletions __tests__/lib/parsers/parse_to_ast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs');
const {
commentToFlow,
parseToAst
} = require('../../../src/parsers/parse_to_ast');

describe('flow comments', () => {
const f = require.resolve('../../fixture/flow/comment-types');
const src = fs.readFileSync(f, 'utf8');

test('preserve line numbers', () => {
const out = commentToFlow(src);
const linesSrc = src.split(/\n/);
const linesOut = out.split(/\n/);

expect(linesOut).toHaveLength(linesSrc.length);
expect(linesSrc[14]).toEqual(linesOut[14]);
});

test('valid js', () => {
expect(() => parseToAst(src)).not.toThrowError();
});
});
2 changes: 1 addition & 1 deletion src/parsers/parse_to_ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const opts = {
* @param {*} source code with flow type comments
* @returns {string} code with flow annotations
*/
function commentToFlow(source) {
export function commentToFlow(source: string) {
if (!/@flow/.test(source)) return source;
return source
.replace(/\/\*::([^]+?)\*\//g, '$1')
Expand Down

0 comments on commit 46ee751

Please sign in to comment.