Skip to content

Commit

Permalink
fix(javascript): strip html tags from source code, fixes #110
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed Oct 17, 2020
1 parent 71c29f1 commit 6ea1d83
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ if (languageParser) {
const parser = new Parser();
parser.setLanguage(languageParser);

const sourceCode = fs.readFileSync(filepath, { encoding: 'utf8', flag: 'r' });
const tree = parser.parse(sourceCode);
// Strip HTML from the source code to ensure we can still use the TypeScript
// parser instead of the JSX parser.
const sourceCode = fs
.readFileSync(filepath, { encoding: 'utf8', flag: 'r' })
.replace(/<\/?[^>]+(>|$)/g, '');

const tree = parser.parse(sourceCode);
const parserService = getParserService(language, [
tree.rootNode,
lineNumber,
Expand Down
22 changes: 22 additions & 0 deletions test/filetypes/javascript/jsx.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ==============================================================================
# Functions that contain JSX.
# ==============================================================================
Given javascript (function that contains JSX):
const Test = ({ someVal }) => {
return <div>{true && someVal ? <small className="helper-text">{someVal}</small> : null}</div>;
};

Do (trigger doge):
\<C-d>

Expect javascript (generated comment with a description and a @return tag):
/**
* [TODO:description]
*
* @param {[TODO:type]} [TODO:name] - [TODO:description]
* @param {[TODO:type]} [TODO:name].someVal - [TODO:description]
* @return {[TODO:type]} [TODO:description]
*/
const Test = ({ someVal }) => {
return <div>{true && someVal ? <small className="helper-text">{someVal}</small> : null}</div>;
};

0 comments on commit 6ea1d83

Please sign in to comment.