Skip to content

Commit 5f04c84

Browse files
committed
Unify readonly visitor with ts compiler one
1 parent f615ef1 commit 5f04c84

File tree

2 files changed

+17
-42
lines changed

2 files changed

+17
-42
lines changed

src/core/resources/plugin/index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
/** @import ts from 'typescript'; */
2-
import {
3-
ResourceReadonlyVisitor,
4-
ResourceVisitor,
5-
} from './resources.visitor.js';
2+
import { ResourceVisitor } from './resources.visitor.js';
63

74
const visitor = new ResourceVisitor();
85

@@ -17,16 +14,12 @@ export default function (program) {
1714
}
1815

1916
// For Nest CLI
20-
export { ResourceReadonlyVisitor as ReadonlyVisitor };
17+
export { ResourceVisitor as ReadonlyVisitor };
2118
/**
2219
* @param _ {*}
2320
* @param program {ts.Program}
2421
* @returns {(ctx: ts.TransformationContext) => (sf: ts.SourceFile) => ts.Node}
2522
*/
26-
export const before = (_, program) => {
27-
return (ctx) => {
28-
return (sf) => {
29-
return visitor.visit(sf, ctx, program);
30-
};
31-
};
23+
export const before = (_, program) => (ctx) => (sf) => {
24+
return visitor.visit(program, sf, ctx);
3225
};

src/core/resources/plugin/resources.visitor.js

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@ import * as ts from 'typescript';
44
const securedKeys = ['value', 'canRead', 'canEdit'];
55

66
export class ResourceVisitor {
7-
constructor(readonly = false) {
8-
this.readonly = readonly;
7+
key = '@cord/resources';
8+
/** @type {Record<string, string>} */
9+
typeImports = {};
10+
11+
collect() {
12+
return {};
913
}
1014

1115
/**
12-
* @param sf {ts.SourceFile}
13-
* @param ctx {ts.TransformationContext}
1416
* @param program {ts.Program}
17+
* @param sf {ts.SourceFile}
18+
* @param [ctx] {ts.TransformationContext}
1519
* @returns {ts.Node}
1620
*/
17-
visit(sf, ctx, program) {
21+
visit(program, sf, ctx) {
1822
if (!sf.fileName.endsWith('dto.ts')) {
1923
return sf;
2024
}
21-
const { factory } = ctx;
25+
26+
const readonly = !ctx;
27+
const factory = ctx?.factory ?? ts.factory;
2228
/**
2329
* @param node {ts.Node}
2430
* @returns {ts.Node}
@@ -33,7 +39,7 @@ export class ResourceVisitor {
3339
return this.enhanceDtoClass(node, program, factory);
3440
}
3541

36-
if (this.readonly) {
42+
if (readonly) {
3743
ts.forEachChild(node, visitNode);
3844
} else {
3945
return ts.visitEachChild(node, visitNode, ctx);
@@ -112,27 +118,3 @@ export class ResourceVisitor {
112118
);
113119
}
114120
}
115-
116-
export class ResourceReadonlyVisitor {
117-
key = '@cord/resources';
118-
visitor = new ResourceVisitor(true);
119-
120-
get typeImports() {
121-
return {};
122-
}
123-
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 };
132-
return this.visitor.visit(sf, factoryHost, program);
133-
}
134-
135-
collect() {
136-
return {};
137-
}
138-
}

0 commit comments

Comments
 (0)