Skip to content

Commit

Permalink
inJsxText closing element bug
Browse files Browse the repository at this point in the history
- inJsxText would always return false if the position it was given was
a closing tag. This caused ts-ignore to not properly comment
@ts-expect-error messages when they were added to tsx files.
- Issue airbnb#150
  • Loading branch information
Calidus committed Nov 7, 2022
1 parent edf5ce8 commit 35f9e42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/ts-migrate-plugins/src/plugins/ts-ignore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */
import ts from 'typescript';
import ts, { isJsxFragment } from 'typescript';
import { Plugin } from 'ts-migrate-server';
import { isDiagnosticWithLinePosition } from '../utils/type-guards';
import updateSourceText, { SourceTextUpdate } from '../utils/updateSourceText';
Expand Down Expand Up @@ -166,7 +166,9 @@ function inJsxText(sourceFile: ts.SourceFile, pos: number) {
const isJsxTextChild = node.children.some(
(child) => ts.isJsxText(child) && child.pos <= pos && pos < child.end,
);
if (isJsxTextChild) {

const isClosingElement = !isJsxFragment(node) && node.closingElement.pos === pos;
if (isJsxTextChild || isClosingElement) {
return true;
}
}
Expand Down
22 changes: 22 additions & 0 deletions packages/ts-migrate-plugins/tests/src/ts-ignore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,26 @@ export default Foo;
"
`);
});

it('add comment to closing tag in tsx file', async () => {
const text = `<div>
<span>text</span>
</div>
`;
const result = await tsIgnorePlugin.run(
mockPluginParams({
fileName: 'Foo.tsx',
text,
semanticDiagnostics: [mockDiagnostic(text, '/div')],
options: { messagePrefix: 'FIXME' },
}),
);
expect(result).toMatchInlineSnapshot(`
"<div>
<span>text</span>
{/* @ts-expect-error TS(123) FIXME: diagnostic message */}
</div>
"
`);
});
});

0 comments on commit 35f9e42

Please sign in to comment.