Skip to content

Commit

Permalink
docs: update tsx compilation
Browse files Browse the repository at this point in the history
Signed-Off-By: Konstantinos Leimonis <[email protected]>
  • Loading branch information
leimonio authored and tchetwin committed Oct 2, 2024
1 parent 3c8f7c5 commit d0002be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,20 @@ When unsupported syntax is encountered, `ts-blank-space` will call the optional

## TSX/JSX

`.tsx` input will be `.jsx` output because the JSX parts are not transformed, but instead preserved in the output.
`.tsx` input will generate `.jsx` output. JSX parts are not transformed, but instead preserved in the output.

By default, `ts-blank-space` will parse the file assuming `.ts`. If the original file contains JSX syntax, then the [parsing should be done manually](#bring-your-own-ast). There is a TSX example in [`valid.test.ts`](./tests/valid.test.ts).

```typescript
import ts from "typescript";
import { blankSourceFile } from "ts-blank-space";

...

const tsxSource = ts.createSourceFile("input.tsx", tsxInput, ts.ScriptTarget.ESNext, false, ts.ScriptKind.TSX);
const jsxOutput = blankSourceFile(tsxSource, onError);
```

## Ensuring ESM output

TypeScript may add an `export {};` if all `import`s and `export`s were removed (because they were `import/export type`).
Expand Down
6 changes: 3 additions & 3 deletions tests/valid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ it("allows declared module value", () => {

it("TSX is preserved in the output", () => {
const onError = mock.fn();
const tsInput = `const elm = <div>{x as string}</div>;\n`;
const tsSource = ts.createSourceFile("input.tsx", tsInput, ts.ScriptTarget.ESNext, false, ts.ScriptKind.TSX);
const jsxOutput = blankSourceFile(tsSource, onError);
const tsxInput = `const elm = <div>{x as string}</div>;\n`;
const tsxSource = ts.createSourceFile("input.tsx", tsxInput, ts.ScriptTarget.ESNext, false, ts.ScriptKind.TSX);
const jsxOutput = blankSourceFile(tsxSource, onError);
assert.equal(onError.mock.callCount(), 0, "there should be no errors");
assert.equal(jsxOutput, "const elm = <div>{x }</div>;\n");
});
Expand Down

0 comments on commit d0002be

Please sign in to comment.