Skip to content

Commit f615ef1

Browse files
committed
Convert resource plugin to JS
I'm having a hard time getting the transformer to load & transpile. There are too many different loading variations between Nest, Jest, ts-node, ts-patch. It was giving a cryptic error with gel:gen (ts-node). I don't want to get stuck here because the landscape is going to change as we switch to SWC, vitest, etc. There's possibly a good performance gain to do this anyway, unsure, unmeasured. The JS with jsdocs is _almost_ as good - TS does check it.
1 parent 9fdca66 commit f615ef1

File tree

4 files changed

+80
-50
lines changed

4 files changed

+80
-50
lines changed

src/core/resources/plugin/index.cts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/core/resources/plugin/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/** @import ts from 'typescript'; */
2+
import {
3+
ResourceReadonlyVisitor,
4+
ResourceVisitor,
5+
} from './resources.visitor.js';
6+
7+
const visitor = new ResourceVisitor();
8+
9+
/**
10+
* For ts-patch
11+
* @param program {ts.Program}
12+
* @returns {function(ts.TransformationContext): function(ts.SourceFile): ts.Node}
13+
*/
14+
// eslint-disable-next-line import/no-default-export
15+
export default function (program) {
16+
return before(undefined, program);
17+
}
18+
19+
// For Nest CLI
20+
export { ResourceReadonlyVisitor as ReadonlyVisitor };
21+
/**
22+
* @param _ {*}
23+
* @param program {ts.Program}
24+
* @returns {(ctx: ts.TransformationContext) => (sf: ts.SourceFile) => ts.Node}
25+
*/
26+
export const before = (_, program) => {
27+
return (ctx) => {
28+
return (sf) => {
29+
return visitor.visit(sf, ctx, program);
30+
};
31+
};
32+
};

src/core/resources/plugin/resources.visitor.cts renamed to src/core/resources/plugin/resources.visitor.js

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1-
import type { ReadonlyVisitor } from '@nestjs/cli/lib/compiler/interfaces/readonly-visitor.interface';
21
import { hasDecorators } from '@nestjs/graphql/dist/plugin/utils/ast-utils.js';
32
import * as ts from 'typescript';
43

54
const securedKeys = ['value', 'canRead', 'canEdit'];
65

76
export class ResourceVisitor {
8-
constructor(readonly readonly = false) {}
7+
constructor(readonly = false) {
8+
this.readonly = readonly;
9+
}
910

10-
visit(sf: ts.SourceFile, ctx: ts.TransformationContext, program: ts.Program) {
11+
/**
12+
* @param sf {ts.SourceFile}
13+
* @param ctx {ts.TransformationContext}
14+
* @param program {ts.Program}
15+
* @returns {ts.Node}
16+
*/
17+
visit(sf, ctx, program) {
1118
if (!sf.fileName.endsWith('dto.ts')) {
1219
return sf;
1320
}
1421
const { factory } = ctx;
15-
const visitNode = (node: ts.Node): ts.Node => {
22+
/**
23+
* @param node {ts.Node}
24+
* @returns {ts.Node}
25+
*/
26+
const visitNode = (node) => {
1627
const decorators =
1728
(ts.canHaveDecorators(node) && ts.getDecorators(node)) || [];
1829
if (
@@ -32,11 +43,13 @@ export class ResourceVisitor {
3243
return ts.visitNode(sf, visitNode);
3344
}
3445

35-
private enhanceDtoClass(
36-
classNode: ts.ClassDeclaration,
37-
program: ts.Program,
38-
factory: ts.NodeFactory,
39-
) {
46+
/**
47+
* @param classNode {ts.ClassDeclaration}
48+
* @param program {ts.Program}
49+
* @param factory {ts.NodeFactory}
50+
* @returns {ts.ClassDeclaration}
51+
*/
52+
enhanceDtoClass(classNode, program, factory) {
4053
const typeChecker = program.getTypeChecker();
4154

4255
const classProps = typeChecker
@@ -61,11 +74,13 @@ export class ResourceVisitor {
6174
]);
6275
}
6376

64-
private createStaticPropArray(
65-
factory: ts.NodeFactory,
66-
name: string,
67-
members: ts.Symbol[],
68-
) {
77+
/**
78+
* @param factory {ts.NodeFactory}
79+
* @param name {string}
80+
* @param members {ts.Symbol[]}
81+
* @returns {ts.PropertyDeclaration}
82+
*/
83+
createStaticPropArray(factory, name, members) {
6984
return factory.createPropertyDeclaration(
7085
[
7186
factory.createModifier(ts.SyntaxKind.StaticKeyword),
@@ -80,11 +95,13 @@ export class ResourceVisitor {
8095
);
8196
}
8297

83-
private updateClassMembers(
84-
factory: ts.NodeFactory,
85-
classNode: ts.ClassDeclaration,
86-
newMembers: readonly ts.ClassElement[],
87-
) {
98+
/**
99+
* @param factory {ts.NodeFactory}
100+
* @param classNode {ts.ClassDeclaration}
101+
* @param newMembers {readonly ts.ClassElement[]}
102+
* @returns {ts.ClassDeclaration}
103+
*/
104+
updateClassMembers(factory, classNode, newMembers) {
88105
return factory.updateClassDeclaration(
89106
classNode,
90107
classNode.modifiers,
@@ -96,16 +113,22 @@ export class ResourceVisitor {
96113
}
97114
}
98115

99-
export class ResourceReadonlyVisitor implements ReadonlyVisitor {
100-
readonly key = '@cord/resources';
101-
private readonly visitor = new ResourceVisitor(true);
116+
export class ResourceReadonlyVisitor {
117+
key = '@cord/resources';
118+
visitor = new ResourceVisitor(true);
102119

103120
get typeImports() {
104121
return {};
105122
}
106123

107-
visit(program: ts.Program, sf: ts.SourceFile) {
108-
const factoryHost = { factory: ts.factory } as any;
124+
/**
125+
* @param program {ts.Program}
126+
* @param sf {ts.SourceFile}
127+
* @returns {ts.Node}
128+
*/
129+
visit(program, sf) {
130+
/** @type {*} */
131+
const factoryHost = { factory: ts.factory };
109132
return this.visitor.visit(sf, factoryHost, program);
110133
}
111134

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
// Transform plugins
3737
"plugins": [
3838
{ "transform": "typescript-transform-paths" },
39-
{ "transform": "./src/core/resources/plugin/index.cts" },
39+
{ "transform": "./src/core/resources/plugin/index.js" },
4040
],
4141

4242
// Path resolution

0 commit comments

Comments
 (0)