From 5d2475f90ed90e499ca20b3cd13fb7e58317ecea Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Thu, 12 Nov 2026 13:28:34 +0530 Subject: [PATCH] Handle .mts and .cts files as TypeScript in paperwork paperwork used the extension without the dot as the type, and there is no detective for mts or cts, so these files always had no dependencies. Map both to ts. .cts files are CommonJS, so ts.mixedImports defaults to true for them and require() calls are found. Fixes #167 --- README.md | 2 +- index.js | 49 ++++++++++++++++++++++++++++-------- test/fixtures/typescript.cts | 4 +++ test/fixtures/typescript.mts | 3 +++ test/paperwork.test.js | 24 ++++++++++++++++++ 5 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/typescript.cts create mode 100644 test/fixtures/typescript.mts diff --git a/README.md b/README.md index b01983b..b8ae5af 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 5d81ecc..f5785c4 100644 --- a/index.js +++ b/index.js @@ -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) { diff --git a/test/fixtures/typescript.cts b/test/fixtures/typescript.cts new file mode 100644 index 0000000..5754aa8 --- /dev/null +++ b/test/fixtures/typescript.cts @@ -0,0 +1,4 @@ +import fs = require("node:fs"); +const { b } = require("./b.cjs"); +const c: number = b; +module.exports = { c }; diff --git a/test/fixtures/typescript.mts b/test/fixtures/typescript.mts new file mode 100644 index 0000000..56d089b --- /dev/null +++ b/test/fixtures/typescript.mts @@ -0,0 +1,3 @@ +import { b } from "./b.mts"; +import type { T } from "./types.mts"; +export const a: number = b + 1; diff --git a/test/paperwork.test.js b/test/paperwork.test.js index 286f1c5..9697e34 100644 --- a/test/paperwork.test.js +++ b/test/paperwork.test.js @@ -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);