diff --git a/src/create-module-declaration.js b/src/create-module-declaration.js index bb64d4f..81b8682 100644 --- a/src/create-module-declaration.js +++ b/src/create-module-declaration.js @@ -279,12 +279,13 @@ export function create_module_declaration(id, entry, created, resolve) { if (identifier && name) { const pos = identifier.getStart(module.ast); const loc = module.locator(pos); + if (loc) { - if (module.source) { - // the sourcemaps generated by TypeScript are very inaccurate, borderline useless. - // we need to fix them up here. TODO is it only inaccurate in the JSDoc case? - const segments = module.source.mappings?.[loc.line - 1]; + // the sourcemaps generated by TypeScript are very inaccurate, borderline useless. + // we need to fix them up here. TODO is it only inaccurate in the JSDoc case? + const segments = module.source?.mappings?.[loc.line - 1]; + if (module.source && segments) { // find the segments immediately before and after the generated column const index = segments.findIndex((segment) => segment[0] >= loc.column); diff --git a/test/samples/ignores-missing-sourcemap-segments/input/index.js b/test/samples/ignores-missing-sourcemap-segments/input/index.js new file mode 100644 index 0000000..ca4fc69 --- /dev/null +++ b/test/samples/ignores-missing-sourcemap-segments/input/index.js @@ -0,0 +1,5 @@ +export const object = { + answer: 42 +}; + +export function hello() {} diff --git a/test/samples/ignores-missing-sourcemap-segments/output/index.d.ts b/test/samples/ignores-missing-sourcemap-segments/output/index.d.ts new file mode 100644 index 0000000..cf27525 --- /dev/null +++ b/test/samples/ignores-missing-sourcemap-segments/output/index.d.ts @@ -0,0 +1,8 @@ +declare module 'ignores-missing-sourcemap-segments' { + export function hello(): void; + export namespace object { + const answer: number; + } +} + +//# sourceMappingURL=index.d.ts.map diff --git a/test/samples/ignores-missing-sourcemap-segments/output/index.d.ts.map b/test/samples/ignores-missing-sourcemap-segments/output/index.d.ts.map new file mode 100644 index 0000000..e1bbe17 --- /dev/null +++ b/test/samples/ignores-missing-sourcemap-segments/output/index.d.ts.map @@ -0,0 +1,17 @@ +{ + "version": 3, + "file": "index.d.ts", + "names": [ + "hello", + "object" + ], + "sources": [ + "../input/index.js", + "../input/index.d.ts" + ], + "sourcesContent": [ + null, + null + ], + "mappings": ";iBAIgBA,KAAKA;kBCHJC,MAAMA" +} \ No newline at end of file