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
27 changes: 27 additions & 0 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,32 @@ export function nodeVisitor(this: VisitorContext, node: ts.Node): ts.Node | unde
factory.updateModuleDeclaration(node, node.modifiers, p, node.body),
);

/*
* TypeScript suspends the surrounding lexical environment while visiting a
* function's return type. Starting another lexical environment for an
* accessor inside that type currently triggers a compiler assertion
* (microsoft/TypeScript#58020). Visit bodyless accessors manually so import
* types in their annotations are still transformed without entering another
* parameter-list lexical environment.
*/
if (tsInstance.isGetAccessorDeclaration(node) && !node.body)
return factory.updateGetAccessorDeclaration(
node,
node.modifiers,
node.name,
node.parameters,
tsInstance.visitNode(node.type, this.getVisitor(), tsInstance.isTypeNode),
node.body,
);

if (tsInstance.isSetAccessorDeclaration(node) && !node.body)
return factory.updateSetAccessorDeclaration(
node,
node.modifiers,
node.name,
tsInstance.visitNodes(node.parameters, this.getVisitor(), tsInstance.isParameter),
node.body,
);

return tsInstance.visitEachChild(node, this.getVisitor(), transformationContext);
}
15 changes: 15 additions & 0 deletions test/projects/general/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ const n: NoRuntimecodeHere = null as any;
subs(2, 3);
const a = new A("");

export function MaySkipHooks() {
class SkippableOnce {
get skipHooks() {
return undefined;
}
}

return SkippableOnce;
}

export declare function AccessorTypes(): {
get value(): import("@utils/types-only").NoRuntimecodeHere;
set value(value: import("@utils/types-only").NoRuntimecodeHere);
};

(async function () {
const Logger = await (await import("@dynamic/logger")).Logger;
const logger = new Logger();
Expand Down
11 changes: 11 additions & 0 deletions test/tests/transformer/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const getExpected = (tsInstance: typeof ts, fileName: string, original: string,
describe(`Transformer -> General Tests`, () => {
const projectRoot = path.join(projectsPaths, "general");
const tsConfigFile = path.join(projectRoot, "tsconfig.json");
const coreIndexFile = path.join(projectRoot, "core/index.ts");

for (const [s, tsInstance] of tsModules)
describe(`TypeScript ${s}`, () => {
Expand Down Expand Up @@ -63,5 +64,15 @@ describe(`Transformer -> General Tests`, () => {
test(`dts matches`, (t) => t.assert.strictEqual(transformed.dts, expected.dts));
});
}

test(`handles accessors in declaration return types`, (t) => {
const dts = transformedFiles[coreIndexFile].dts;

t.assert.match(dts, /export declare function MaySkipHooks\(\): \{[\s\S]*?get skipHooks\(\): any;[\s\S]*?\};/);
t.assert.match(
dts,
/export declare function AccessorTypes\(\): \{\s*get value\(\): import\("\.\.\/utils\/types-only"\)\.NoRuntimecodeHere;\s*set value\(value: import\("\.\.\/utils\/types-only"\)\.NoRuntimecodeHere\);\s*\};/,
);
});
});
});