Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Accepted values for the `type` option:
| `tsx` | [detective-typescript](https://github.com/dependents/detective-typescript) (tsx variant) |
| `vue` | [detective-vue2](https://github.com/dependents/detective-vue2) |

`paperwork()` infers the type from the filename extension; `.styl` maps to `stylus` and `.cjs` maps to `commonjs`. Any other extension becomes the type with the leading dot stripped. `.js` and `.jsx` are sniffed at the source level rather than by extension.
`paperwork()` infers the type from the filename extension; `.styl` maps to `stylus`, `.cjs` maps to `commonjs`, and `.mts` and `.cts` map to `ts` (for `.cts`, `ts.mixedImports` defaults to `true` so `require()` calls are found). Any other extension becomes the type with the leading dot stripped. `.js` and `.jsx` are sniffed at the source level rather than by extension.

## CLI

Expand Down
49 changes: 38 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,44 @@ function paperwork(filename, options = {}) {
const extension = path.extname(filename);
let type;

if (extension === '.styl') {
debug('paperwork: converting .styl into the stylus type');
type = 'stylus';
} else if (extension === '.cjs') {
debug('paperwork: converting .cjs into the commonjs type');
type = 'commonjs';
// We need to sniff the JS module to find its type, not by extension.
// Other possible types pass through normally
} else if (!['.js', '.jsx'].includes(extension)) {
debug('paperwork: stripping the dot from the extension to serve as the type');
type = extension.replace('.', '');
switch (extension) {
case '.styl': {
debug('paperwork: converting .styl into the stylus type');
type = 'stylus';
break;
}

case '.cjs': {
debug('paperwork: converting .cjs into the commonjs type');
type = 'commonjs';
break;
}

case '.mts': {
debug('paperwork: converting .mts into the ts type');
type = 'ts';
break;
}

case '.cts': {
debug('paperwork: converting .cts into the ts type, with require() calls');
type = 'ts';
// .cts files are CommonJS, so find require() calls unless told otherwise
options = { ...options, ts: { mixedImports: true, ...options.ts } };
break;
}

// We need to sniff the JS module to find its type, not by extension.
case '.js':
case '.jsx': {
break;
}

// Other possible types pass through normally
default: {
debug('paperwork: stripping the dot from the extension to serve as the type');
type = extension.replace('.', '');
}
}

if (type) {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/typescript.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import fs = require("node:fs");
const { b } = require("./b.cjs");
const c: number = b;
module.exports = { c };
3 changes: 3 additions & 0 deletions test/fixtures/typescript.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { b } from "./b.mts";
import type { T } from "./types.mts";
export const a: number = b + 1;
24 changes: 24 additions & 0 deletions test/paperwork.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ describe('paperwork', () => {
expect(result).toContain('./b');
});

it('handles mts files as typescript', () => {
const fixture = fixturePath('typescript.mts');
const result = precinct.paperwork(fixture);
expect(result).toStrictEqual(['./b.mts', './types.mts']);
});

it('passes typescript configuration for mts files', () => {
const fixture = fixturePath('typescript.mts');
const result = precinct.paperwork(fixture, { ts: { skipTypeImports: true } });
expect(result).toStrictEqual(['./b.mts']);
});

it('handles cts files as typescript with require calls', () => {
const fixture = fixturePath('typescript.cts');
const result = precinct.paperwork(fixture, { includeCore: false });
expect(result).toStrictEqual(['./b.cjs']);
});

it('lets the typescript configuration turn off require calls for cts files', () => {
const fixture = fixturePath('typescript.cts');
const result = precinct.paperwork(fixture, { ts: { mixedImports: false } });
expect(result).toStrictEqual(['node:fs']);
});

it('passes detective configuration to the underlying detective', () => {
const fixture = fixturePath('amdLazy.js');
const withLazy = precinct.paperwork(fixture);
Expand Down