Skip to content

Commit 3d83335

Browse files
Merge pull request #3167 from nestjs/revert-3134-fix/compiler-option-paths-mapping
Revert "fix(plugin): support compiler options paths mapping"
2 parents 10573a0 + f456068 commit 3d83335

File tree

9 files changed

+40
-133
lines changed

9 files changed

+40
-133
lines changed

lib/plugin/utils/plugin-utils.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@ export function getTypeReferenceAsString(
115115
if (type.aliasSymbol) {
116116
return { typeName: 'Object', arrayDepth };
117117
}
118-
if (
119-
typeChecker.getApparentType(type).getSymbol().getEscapedName() ===
120-
'String'
121-
) {
118+
if (typeChecker.getApparentType(type).getSymbol().getEscapedName() === 'String') {
122119
return { typeName: String.name, arrayDepth };
123120
}
124121
return { typeName: undefined };
@@ -143,8 +140,7 @@ export function hasPropertyKey(
143140
export function replaceImportPath(
144141
typeReference: string,
145142
fileName: string,
146-
options: PluginOptions,
147-
compilerOptionsPaths: ts.MapLike<string[]>
143+
options: PluginOptions
148144
) {
149145
if (!typeReference.includes('import')) {
150146
return { typeReference, importPath: null };
@@ -171,19 +167,6 @@ export function replaceImportPath(
171167
? convertPath(options.pathToSource)
172168
: posix.dirname(convertPath(fileName));
173169

174-
for (const [key, value] of Object.entries(compilerOptionsPaths)) {
175-
const keyToMatch = key.replace('*', '');
176-
if (importPath.includes(keyToMatch)) {
177-
const newImportPath = posix.join(
178-
from,
179-
importPath.replace(keyToMatch, value[0].replace('*', ''))
180-
);
181-
typeReference = typeReference.replace(importPath, newImportPath);
182-
importPath = newImportPath;
183-
break;
184-
}
185-
}
186-
187170
let relativePath = posix.relative(from, importPath);
188171
relativePath = relativePath[0] !== '.' ? './' + relativePath : relativePath;
189172

@@ -267,8 +250,7 @@ export function isAutoGeneratedEnumUnion(
267250
return undefined;
268251
}
269252
const undefinedTypeIndex = type.types.findIndex(
270-
(type: any) =>
271-
type.intrinsicName === 'undefined' || type.intrinsicName === 'null'
253+
(type: any) => type.intrinsicName === 'undefined' || type.intrinsicName === 'null'
272254
);
273255
if (undefinedTypeIndex < 0) {
274256
return undefined;

lib/plugin/utils/type-reference-to-identifier.util.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export function typeReferenceToIdentifier(
1313
options: PluginOptions,
1414
factory: ts.NodeFactory,
1515
type: ts.Type,
16-
typeImports: Record<string, string>,
17-
compilerOptionsPaths: ts.MapLike<string[]>
16+
typeImports: Record<string, string>
1817
) {
1918
if (options.readonly) {
2019
assertReferenceableType(
@@ -28,8 +27,7 @@ export function typeReferenceToIdentifier(
2827
const { typeReference, importPath, typeName } = replaceImportPath(
2928
typeReferenceDescriptor.typeName,
3029
hostFilename,
31-
options,
32-
compilerOptionsPaths
30+
options
3331
);
3432

3533
let identifier: ts.Identifier;

lib/plugin/visitors/controller-class.visitor.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
5858
options: PluginOptions
5959
) {
6060
const typeChecker = program.getTypeChecker();
61-
const compilerOptionsPaths = program.getCompilerOptions().paths ?? {};
6261
if (!options.readonly) {
6362
sourceFile = this.updateImports(sourceFile, ctx.factory, program);
6463
}
@@ -73,8 +72,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
7372
typeChecker,
7473
options,
7574
sourceFile,
76-
metadata,
77-
compilerOptionsPaths
75+
metadata
7876
);
7977
if (!options.readonly) {
8078
return updatedNode;
@@ -123,8 +121,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
123121
typeChecker: ts.TypeChecker,
124122
options: PluginOptions,
125123
sourceFile: ts.SourceFile,
126-
metadata: ClassMetadata,
127-
compilerOptionsPaths: ts.MapLike<string[]>
124+
metadata: ClassMetadata
128125
): ts.MethodDeclaration {
129126
const hostFilename = sourceFile.fileName;
130127
const decorators =
@@ -168,8 +165,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
168165
factory.createNodeArray(),
169166
hostFilename,
170167
metadata,
171-
options,
172-
compilerOptionsPaths
168+
options
173169
);
174170
const updatedDecorators = [
175171
...apiOperationDecoratorsArray,
@@ -393,8 +389,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
393389
existingProperties: ts.NodeArray<ts.PropertyAssignment> = factory.createNodeArray(),
394390
hostFilename: string,
395391
metadata: ClassMetadata,
396-
options: PluginOptions,
397-
compilerOptionsPaths: ts.MapLike<string[]>
392+
options: PluginOptions
398393
): ts.ObjectLiteralExpression {
399394
let properties = [];
400395

@@ -412,8 +407,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
412407
typeChecker,
413408
existingProperties,
414409
hostFilename,
415-
options,
416-
compilerOptionsPaths
410+
options
417411
)
418412
]);
419413
const objectLiteralExpr = factory.createObjectLiteralExpression(
@@ -443,8 +437,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
443437
typeChecker: ts.TypeChecker,
444438
existingProperties: ts.NodeArray<ts.PropertyAssignment>,
445439
hostFilename: string,
446-
options: PluginOptions,
447-
compilerOptionsPaths: ts.MapLike<string[]>
440+
options: PluginOptions
448441
) {
449442
if (hasPropertyKey('type', existingProperties)) {
450443
return undefined;
@@ -467,8 +460,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
467460
options,
468461
factory,
469462
type,
470-
this._typeImports,
471-
compilerOptionsPaths
463+
this._typeImports
472464
);
473465
return factory.createPropertyAssignment('type', identifier);
474466
}

lib/plugin/visitors/model-class.visitor.ts

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import { posix } from 'path';
33
import * as ts from 'typescript';
44
import { factory, PropertyAssignment } from 'typescript';
55
import { ApiHideProperty, ApiProperty } from '../../decorators';
6-
import {
7-
decoratorsProperties,
8-
decoratorsPropertiesMappingType
9-
} from '../../services/decorators-properties';
106
import { PluginOptions } from '../merge-options';
117
import { METADATA_FACTORY_NAME } from '../plugin-constants';
128
import { pluginDebugLogger } from '../plugin-debug-logger';
@@ -20,10 +16,6 @@ import {
2016
getTsDocTagsOfNode,
2117
isEnum
2218
} from '../utils/ast-utils';
23-
import {
24-
getExternalImports,
25-
replaceExternalImportsInTypeReference
26-
} from '../utils/external-imports.util';
2719
import {
2820
canReferenceNode,
2921
convertPath,
@@ -36,6 +28,14 @@ import {
3628
} from '../utils/plugin-utils';
3729
import { typeReferenceToIdentifier } from '../utils/type-reference-to-identifier.util';
3830
import { AbstractFileVisitor } from './abstract.visitor';
31+
import {
32+
getExternalImports,
33+
replaceExternalImportsInTypeReference
34+
} from '../utils/external-imports.util';
35+
import {
36+
decoratorsProperties,
37+
decoratorsPropertiesMappingType
38+
} from '../../services/decorators-properties';
3939

4040
type ClassMetadata = Record<string, ts.ObjectLiteralExpression>;
4141

@@ -72,7 +72,6 @@ export class ModelClassVisitor extends AbstractFileVisitor {
7272
) {
7373
const externalImports = getExternalImports(sourceFile);
7474
const typeChecker = program.getTypeChecker();
75-
const compilerOptionsPaths = program.getCompilerOptions().paths ?? {};
7675
sourceFile = this.updateImports(sourceFile, ctx.factory, program);
7776

7877
const propertyNodeVisitorFactory =
@@ -87,8 +86,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
8786
options,
8887
sourceFile,
8988
metadata,
90-
externalImports,
91-
compilerOptionsPaths
89+
externalImports
9290
);
9391
} else if (
9492
options.parameterProperties &&
@@ -100,8 +98,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
10098
options,
10199
sourceFile,
102100
metadata,
103-
externalImports,
104-
compilerOptionsPaths
101+
externalImports
105102
);
106103
}
107104
return node;
@@ -168,8 +165,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
168165
options: PluginOptions,
169166
sourceFile: ts.SourceFile,
170167
metadata: ClassMetadata,
171-
externalImports: Record<string, string>,
172-
compilerOptionsPaths: ts.MapLike<string[]>
168+
externalImports: Record<string, string>
173169
) {
174170
const isPropertyStatic = (node.modifiers || []).some(
175171
(modifier: ts.Modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword
@@ -217,8 +213,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
217213
sourceFile.fileName,
218214
sourceFile,
219215
metadata,
220-
externalImports,
221-
compilerOptionsPaths
216+
externalImports
222217
);
223218
} catch (err) {
224219
return node;
@@ -231,8 +226,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
231226
options: PluginOptions,
232227
sourceFile: ts.SourceFile,
233228
metadata: ClassMetadata,
234-
externalImports: Record<string, string>,
235-
compilerOptionsPaths: ts.MapLike<string[]>
229+
externalImports: Record<string, string>
236230
) {
237231
constructorNode.forEachChild((node) => {
238232
if (
@@ -254,8 +248,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
254248
options,
255249
sourceFile.fileName,
256250
sourceFile,
257-
externalImports,
258-
compilerOptionsPaths
251+
externalImports
259252
);
260253

261254
const propertyName = node.name.getText();
@@ -322,8 +315,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
322315
hostFilename: string,
323316
sourceFile: ts.SourceFile,
324317
metadata: ClassMetadata,
325-
externalImports: Record<string, string>,
326-
compilerOptionsPaths: ts.MapLike<string[]>
318+
externalImports: Record<string, string>
327319
) {
328320
const objectLiteralExpr = this.createDecoratorObjectLiteralExpr(
329321
factory,
@@ -333,8 +325,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
333325
options,
334326
hostFilename,
335327
sourceFile,
336-
externalImports,
337-
compilerOptionsPaths
328+
externalImports
338329
);
339330
this.addClassMetadata(
340331
compilerNode,
@@ -355,8 +346,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
355346
options: PluginOptions = {},
356347
hostFilename = '',
357348
sourceFile?: ts.SourceFile,
358-
externalImports: Record<string, string> = {},
359-
compilerOptionsPaths: ts.MapLike<string[]> = {}
349+
externalImports: Record<string, string> = {}
360350
): ts.ObjectLiteralExpression {
361351
const isRequired = !node.questionToken;
362352

@@ -373,9 +363,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
373363
typeChecker,
374364
existingProperties,
375365
hostFilename,
376-
options,
377-
externalImports,
378-
compilerOptionsPaths
366+
options
379367
),
380368
...this.createDescriptionAndTsDocTagPropertyAssignments(
381369
factory,
@@ -398,8 +386,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
398386
existingProperties,
399387
hostFilename,
400388
options,
401-
externalImports,
402-
compilerOptionsPaths
389+
externalImports
403390
)
404391
];
405392
if (
@@ -424,9 +411,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
424411
typeChecker: ts.TypeChecker,
425412
existingProperties: ts.NodeArray<ts.PropertyAssignment>,
426413
hostFilename: string,
427-
options: PluginOptions,
428-
externalImports: Record<string, string>,
429-
compilerOptionsPaths: ts.MapLike<string[]>
414+
options: PluginOptions
430415
): ts.PropertyAssignment[] {
431416
const key = 'type';
432417
if (hasPropertyKey(key, existingProperties)) {
@@ -441,9 +426,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
441426
typeChecker,
442427
existingProperties,
443428
hostFilename,
444-
options,
445-
externalImports,
446-
compilerOptionsPaths
429+
options
447430
);
448431
return [factory.createPropertyAssignment(key, initializer)];
449432
} else if (ts.isUnionTypeNode(node)) {
@@ -460,9 +443,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
460443
typeChecker,
461444
existingProperties,
462445
hostFilename,
463-
options,
464-
externalImports,
465-
compilerOptionsPaths
446+
options
466447
);
467448
if (!isNullable) {
468449
return propertyAssignments;
@@ -494,8 +475,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
494475
options,
495476
factory,
496477
type,
497-
this._typeImports,
498-
compilerOptionsPaths
478+
this._typeImports
499479
);
500480

501481
const initializer = factory.createArrowFunction(
@@ -515,9 +495,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
515495
typeChecker: ts.TypeChecker,
516496
existingProperties: ts.NodeArray<ts.PropertyAssignment>,
517497
hostFilename: string,
518-
options: PluginOptions,
519-
externalImports: Record<string, string>,
520-
compilerOptionsPaths: ts.MapLike<string[]>
498+
options: PluginOptions
521499
) {
522500
const propertyAssignments = Array.from(node.members || []).map((member) => {
523501
const literalExpr = this.createDecoratorObjectLiteralExpr(
@@ -526,10 +504,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
526504
typeChecker,
527505
existingProperties,
528506
options,
529-
hostFilename,
530-
undefined,
531-
externalImports,
532-
compilerOptionsPaths
507+
hostFilename
533508
);
534509
return factory.createPropertyAssignment(
535510
factory.createIdentifier(member.name.getText()),
@@ -569,8 +544,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
569544
existingProperties: ts.NodeArray<ts.PropertyAssignment>,
570545
hostFilename: string,
571546
options: PluginOptions,
572-
externalImports: Record<string, string>,
573-
compilerOptionsPaths: ts.MapLike<string[]>
547+
externalImports: Record<string, string>
574548
) {
575549
const key = 'enum';
576550
if (hasPropertyKey(key, existingProperties)) {
@@ -618,8 +592,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
618592
options,
619593
factory,
620594
type,
621-
this._typeImports,
622-
compilerOptionsPaths
595+
this._typeImports
623596
);
624597

625598
const enumProperty = factory.createPropertyAssignment(key, enumIdentifier);

test/plugin/fixtures/project/cats/dto/absolute-owner.dto.ts

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

0 commit comments

Comments
 (0)