Skip to content

Commit

Permalink
Add support for JSX commments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Gruber committed Jul 12, 2019
1 parent 186c630 commit c6155aa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-reflow",
"version": "0.1.3",
"version": "0.2.0",
"description": "Babel plugin to transpile Flow code to TypeScript",
"author": "Jonathan Gruber <gruberjonathan@gmail.com>",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow
import React from 'react';
// $FlowFixMe
import styled from 'styled';

Expand Down Expand Up @@ -35,7 +36,13 @@ const StyledComponent = styled.div`
const doubleSlashString1 = 'http://www.w3.org/2000/svg'; /* block comment */
const doubleSlashString2 = '//domain/a, //domain/b'; // line comment


// Muliple
// line comments
type P2 = number | null;

const ReactComponent = props => (
<div>
Hello, World!
{/* JSX comment */}
</div>
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import React from 'react';
// $FlowFixMe
import styled from 'styled';

Expand Down Expand Up @@ -38,3 +39,10 @@ const doubleSlashString2 = '//domain/a, //domain/b'; // line comment
// Muliple
// line comments
type P2 = number | null;

const ReactComponent = props => (
<div>
Hello, World!
{/* JSX comment */}
</div>
);
7 changes: 6 additions & 1 deletion src/plugin/util/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const BLANK_LINE = /^[ \t]*$/;
export const LINE_BREAK = /\r?\n/;

const BLOCK_COMMENT_AT_BEGINNING_OF_LINE = /^\s*\/\*/;
const JSX_COMMENT = /\{\s*\/\*.*\*\/\s*\}/;
const LINE_COMMENT_AT_BEGINNING_OF_LINE = /^\s*\/\/.*$/;

const FLOW_DIRECTIVE = /(@flow(\s+(strict(-local)?|weak))?|@noflow)/;
Expand Down Expand Up @@ -60,14 +61,18 @@ function copyComments(
);

if (comment) {
const originalLine = originalLines[lineNumber];

if (comment.type === 'CommentBlock') {
if (BLOCK_COMMENT_AT_BEGINNING_OF_LINE.test(originalLines[lineNumber])) {
if (BLOCK_COMMENT_AT_BEGINNING_OF_LINE.test(originalLine)) {
`/*${comment.value}*/`
.split('\n')
.reverse()
.forEach(commentLine => {
outputLines.splice(lineNumber, 0, commentLine);
});
} else if (JSX_COMMENT.test(originalLine)) {
outputLines[lineNumber] = `{/*${comment.value}*/}`;
} else {
outputLines[lineNumber] = `${outputLine} /*${comment.value}*/`;
}
Expand Down

0 comments on commit c6155aa

Please sign in to comment.