Skip to content

Commit

Permalink
fix: #665 - Getting descendants where jsx was used in a non-jsx file …
Browse files Browse the repository at this point in the history
…would throw an error.
  • Loading branch information
dsherret committed Jul 17, 2019
1 parent cf46b79 commit fa3c3bb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/compiler/ast/utils/CommentNodeParser.ts
Expand Up @@ -61,7 +61,10 @@ export class CommentNodeParser {

static shouldParseChildren(container: ts.Node): container is ContainerNodes {
// this needs to be really fast because it's used whenever getting the children, so use a map
return extendedCommentParserKinds.has(container.kind);
return extendedCommentParserKinds.has(container.kind)
// Ignore zero length nodes... for some reason this might happen when parsing
// jsx in non-jsx files.
&& container.pos !== container.end;
}

static hasParsedChildren(container: ContainerNodes | ts.SyntaxList) {
Expand Down
35 changes: 35 additions & 0 deletions src/tests/issues/665tests.ts
@@ -0,0 +1,35 @@
import { expect } from "chai";
import { Project } from "../../Project";

describe("tests for issue #665", () => {
it("should not error when getting the descendants when there's jsx in a ts file", () => {
const project = new Project({ useVirtualFileSystem: true });
const file = project.createSourceFile("test.ts", `
function oi(){
const snapshot = { isDraggingOver: false };
function getSourceStyle(opts) {
return {};
}
return <div>
<div></div>
<p style={getSourceStyle(snapshot.isDraggingOver)}></p>
</div>
}
`);

expect(() => file.getDescendants()).to.not.throw();
});

it("should not error when getting the descendants when there's jsx in a ts file for second example", () => {
const project = new Project({ useVirtualFileSystem: true });
const file = project.createSourceFile("test.ts", `
import * as React from 'react';
export const Broken: React.SFC<{ arg: boolean }> = (props) => (
<div style={getStyle(props.arg)} >...</div>
)
`);

expect(() => file.getDescendants()).to.not.throw();
});
});

0 comments on commit fa3c3bb

Please sign in to comment.