Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into 'origin/feat/inli…
Browse files Browse the repository at this point in the history
…ne-external-types'
  • Loading branch information
marcus-sa committed Dec 5, 2023
2 parents 1978d31 + 6909fa8 commit de8b473
Show file tree
Hide file tree
Showing 18 changed files with 299 additions and 77 deletions.
2 changes: 1 addition & 1 deletion packages/bson/src/bson-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ export function createBSONSizer<T>(type?: ReceiveType<T>, serializer: BSONBinary
return compiler.build(code, 'data', 'state');
}

export function serializeWithoutOptimiser(data: any): Uint8Array {
export function serializeBSONWithoutOptimiser(data: any): Uint8Array {
const size = getValueSize(data);
const writer = new Writer(createBuffer(size));
writer.write(data);
Expand Down
16 changes: 8 additions & 8 deletions packages/injector/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@ export interface ProviderBase {
transient?: true;
}

/** @reflection never */
export interface ProviderScope {
scope?: 'module' | 'rpc' | 'http' | 'cli' | string;
}

/** @reflection never */
export type Token<T = any> = symbol | number | bigint | RegExp | boolean | string | AbstractClassType<T> | Type | T;

export function provide<T>(
provider:
| (ProviderBase &
| (ProviderBase & ProviderScope &
(
| { useValue: T }
| { useClass: ClassType }
| { useExisting: any }
| { useFactory: (...args: any[]) => T }
| { useFactory: (...args: any[]) => T | undefined }
))
| ClassType
| ((...args: any[]) => T)
Expand Down Expand Up @@ -86,7 +91,7 @@ export interface FactoryProvider<T> extends ProviderBase {
/**
* A function to invoke to create a value for this `token`.
*/
useFactory: (...args: any[]) => T;
useFactory: (...args: any[]) => T | undefined;
}

/** @reflection never */
Expand Down Expand Up @@ -154,11 +159,6 @@ export class Tag<T, TP extends TagProvider<T> = TagProvider<T>> {
}
}

/** @reflection never */
export interface ProviderScope {
scope?: 'module' | 'rpc' | 'http' | 'cli' | string;
}

/** @reflection never */
export type NormalizedProvider<T = any> = ProviderProvide<T> & ProviderScope;

Expand Down
9 changes: 6 additions & 3 deletions packages/rpc/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ import {
} from '@deepkit/type';
import { ControllerDefinition } from './model.js';

class RpcController {
export class RpcController {
// Defaults to the name of the class
name: string = '';

classType?: ClassType;

definition?: ControllerDefinition<any>;

actions = new Map<string, RpcAction>();

getPath(): string {
return this.definition ? this.definition.path : this.name;
const name = this.definition ? this.definition.path : this.name;
return name || (this.classType ? reflect(this.classType).typeName || this.classType.name : '');
}
}

Expand Down Expand Up @@ -59,7 +62,7 @@ class RpcClass {
}

onDecorator(classType: ClassType) {
this.t.name ||= reflect(classType).typeName || classType.name;
this.t.classType = classType;
}
}

Expand Down
10 changes: 6 additions & 4 deletions packages/rpc/tests/controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertType, entity, Positive, ReflectionClass, ReflectionKind } from '@deepkit/type';
import { expect, test } from '@jest/globals';
import { DirectClient } from '../src/client/client-direct.js';
import { getActions, rpc, rpcClass } from '../src/decorators.js';
import { getActions, rpc, rpcClass, RpcController } from '../src/decorators.js';
import { RpcKernel, RpcKernelConnection } from '../src/server/kernel.js';
import { Session, SessionState } from '../src/server/security.js';
import { BehaviorSubject } from 'rxjs';
Expand All @@ -12,9 +12,11 @@ test('default name', () => {
@rpc.controller()
class Controller {}

expect(rpcClass._fetch(Controller)).toMatchObject({
name: 'Controller',
});
const controller = new RpcController();

controller.classType = Controller;

expect(controller.getPath()).toBe('Controller');
});

test('decorator', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/sql/src/sql-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ export class RawQuery<T> implements FindQuery<T> {
const connection = await this.connectionPool.getConnection(this.session.logger, this.session.assignedTransaction, this.session.stopwatch);

try {
const caster = castFunction(undefined, undefined, undefined, this.type);
const caster = castFunction(undefined, undefined, this.type);
const res = await connection.execAndReturnAll(sql.sql, sql.params);
return (isArray(res) ? [...res] : []).map(v => caster(v)) as T[];
} finally {
Expand Down
38 changes: 38 additions & 0 deletions packages/type-compiler/compiler-debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { deepkitType } from './src/plugin.js';
import { readFileSync } from 'fs';

interface Arguments {
file: string;
config?: string;
}

function parseArguments(args: string[]): Arguments {
const result: Arguments = {
file: '',
};

for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--config') {
result.config = args[i + 1];
i++;
} else {
result.file = arg;
}
}

return result;
}

const args = parseArguments(process.argv.slice(2));



const transformer = deepkitType({
tsConfig: args.config,
});

const code = readFileSync(args.file, 'utf8');
const transformed = transformer(code, args.file);

console.log(transformed?.code);
5 changes: 5 additions & 0 deletions packages/type-compiler/deepkit-compiler-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

try {
require('./dist/cjs/compiler-debug.js');
} catch (error) {}
1 change: 1 addition & 0 deletions packages/type-compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { declarationTransformer, transformer } from './src/compiler.js';
import type { Program } from 'typescript';

export * from './src/compiler.js';
export * from './src/plugin.js';
export * from './src/loader.js';

export default function myTransformerPlugin(program: Program, opts: {}) {
Expand Down
3 changes: 2 additions & 1 deletion packages/type-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
}
},
"bin": {
"deepkit-type-install": "./deepkit-type-install.js"
"deepkit-type-install": "./deepkit-type-install.js",
"deepkit-compiler-debug": "./deepkit-compiler-debug.js"
},
"sideEffects": false,
"publishConfig": {
Expand Down
59 changes: 23 additions & 36 deletions packages/type-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ import ts from 'typescript';

import {
ensureImportIsEmitted,
extractJSDocAttribute, getEntityName,
extractJSDocAttribute,
getEntityName,
getGlobalsOfSourceFile,
getIdentifierName,
getNameAsString,
getPropertyName,
hasModifier, isBuiltType,
hasModifier,
isBuiltType,
isNodeWithLocals,
NodeConverter,
PackExpression,
Expand All @@ -90,9 +92,8 @@ import { existsSync, readFileSync } from 'fs';
import { dirname, isAbsolute, join, resolve } from 'path';
import stripJsonComments from 'strip-json-comments';
import { MappedModifier, ReflectionOp, TypeNumberBrand } from '@deepkit/type-spec';
import { Resolver } from './resolver.js';
import { patternMatch, ReflectionMode, reflectionModeMatcher, reflectionModes, Resolver } from './resolver.js';
import { knownLibFilesForCompilerOptions } from '@typescript/vfs';
import * as micromatch from 'micromatch';

// don't use from @deepkit/core since we don't want to have a dependency to @deepkit/core
export function isObject(obj: any): obj is { [key: string]: any } {
Expand Down Expand Up @@ -186,7 +187,6 @@ const serverEnv = 'undefined' !== typeof process;
* It can't be more ops than this given number
*/
export const packSize: number = 2 ** packSizeByte; //64
const reflectionModes = ['always', 'default', 'never'] as const;

export interface ReflectionOptions {
/**
Expand Down Expand Up @@ -546,7 +546,7 @@ export class ReflectionTransformer implements CustomTransformer {
'lib.es2017.typedarrays.d.ts',
];

public embedAssignType: boolean = false;
protected embedAssignType: boolean = false;

protected reflectionMode?: typeof reflectionModes[number];
protected reflectionOptions?: ReflectionOptions;
Expand Down Expand Up @@ -726,7 +726,7 @@ export class ReflectionTransformer implements CustomTransformer {
}
}

debug(`Transform file ${sourceFile.fileName} via config ${this.compilerOptions.configFilePath || 'none'}, reflection=${this.reflectionMode}.`);
debug(`Transform file ${sourceFile.fileName} via config ${this.compilerOptions.configFilePath || 'none'}, reflection=${this.reflectionMode} (${this.getModuleType()}).`);

if (this.reflectionMode === 'never') {
return sourceFile;
Expand Down Expand Up @@ -1023,15 +1023,15 @@ export class ReflectionTransformer implements CustomTransformer {
this.sourceFile = visitNode(this.sourceFile, compileDeclarations);

if (this.addImports.length) {
const compilerOptions = this.compilerOptions;
const handledIdentifier: string[] = [];
for (const imp of this.addImports) {
if (handledIdentifier.includes(getIdentifierName(imp.identifier))) continue;
handledIdentifier.push(getIdentifierName(imp.identifier));
if (compilerOptions.module === ModuleKind.CommonJS) {
if (this.getModuleType() === 'cjs') {
//var {identifier} = require('./bar')
const test = this.f.createIdentifier(getIdentifierName(imp.identifier));
const variable = this.f.createVariableStatement(undefined, this.f.createVariableDeclarationList([this.f.createVariableDeclaration(
this.f.createObjectBindingPattern([this.f.createBindingElement(undefined, undefined, imp.identifier)]),
this.f.createObjectBindingPattern([this.f.createBindingElement(undefined, undefined, test)]),
undefined, undefined,
this.f.createCallExpression(this.f.createIdentifier('require'), undefined, [imp.from])
)], NodeFlags.Const));
Expand All @@ -1046,7 +1046,7 @@ export class ReflectionTransformer implements CustomTransformer {
//import {identifier} from './bar.js'
// import { identifier as identifier } is used to avoid automatic elision of imports (in angular builds for example)
// that's probably a bit unstable.
const specifier = this.f.createImportSpecifier(false, imp.identifier, imp.identifier);
const specifier = this.f.createImportSpecifier(false, undefined, imp.identifier);
const namedImports = this.f.createNamedImports([specifier]);
const importStatement = this.f.createImportDeclaration(undefined,
this.f.createImportClause(false, undefined, namedImports), imp.from
Expand Down Expand Up @@ -1142,6 +1142,10 @@ export class ReflectionTransformer implements CustomTransformer {
return this.sourceFile;
}

protected getModuleType(): 'cjs' | 'esm' {
return this.compilerOptions.module === ModuleKind.CommonJS ? 'cjs' : 'esm';
}

protected injectResetΩ<T extends FunctionDeclaration | FunctionExpression | MethodDeclaration | ConstructorDeclaration>(node: T): T {
let hasReceiveType = false;
for (const param of node.parameters) {
Expand Down Expand Up @@ -2039,12 +2043,7 @@ export class ReflectionTransformer implements CustomTransformer {

protected isExcluded(filePath: string): boolean {
if (!this.currentReflectionConfig.options.exclude) return false;

const excluded = micromatch.contains(filePath, this.currentReflectionConfig.options.exclude, {
basename: true,
cwd: this.currentReflectionConfig.baseDir
});
return excluded;
return patternMatch(filePath, this.currentReflectionConfig.options.exclude, this.currentReflectionConfig.baseDir);
}

protected extractPackStructOfTypeReference(type: TypeReferenceNode | ExpressionWithTypeArguments, program: CompilerProgram): void {
Expand Down Expand Up @@ -2186,6 +2185,8 @@ export class ReflectionTransformer implements CustomTransformer {
return;
}

const runtimeTypeName = this.getDeclarationVariableName(typeName);

//to break recursion, we track which declaration has already been compiled
if (!this.compiledDeclarations.has(declaration) && !this.compileDeclarations.has(declaration)) {
const declarationSourceFile = findSourceFile(declaration) || this.sourceFile;
Expand Down Expand Up @@ -2225,11 +2226,7 @@ export class ReflectionTransformer implements CustomTransformer {
return;
}

//check if the referenced file has reflection info emitted. if not, any is emitted for that reference
const typeVar = this.getDeclarationVariableName(typeName);
//check if typeVar is exported in referenced file
const builtType = isBuiltType(typeVar, found);

const builtType = isBuiltType(runtimeTypeName, found);
if (!builtType) {
if (!this.shouldInlineExternalImport(resolved.importDeclaration, typeName, declarationReflection)) return;
this.embedDeclarations.set(declaration, {
Expand All @@ -2244,7 +2241,7 @@ export class ReflectionTransformer implements CustomTransformer {
return;
}

this.addImports.push({ identifier: this.getDeclarationVariableName(typeName), from: resolved.importDeclaration.moduleSpecifier });
this.addImports.push({ identifier: runtimeTypeName, from: resolved.importDeclaration.moduleSpecifier });
}
}
} else {
Expand All @@ -2263,7 +2260,7 @@ export class ReflectionTransformer implements CustomTransformer {
}

const index = program.pushStack(
program.forNode === declaration ? 0 : this.f.createArrowFunction(undefined, undefined, [], undefined, undefined, this.getDeclarationVariableName(typeName))
program.forNode === declaration ? 0 : this.f.createArrowFunction(undefined, undefined, [], undefined, undefined, runtimeTypeName)
);
if (type.typeArguments) {
for (const argument of type.typeArguments) {
Expand Down Expand Up @@ -2709,18 +2706,8 @@ export class ReflectionTransformer implements CustomTransformer {
);
}

protected parseReflectionMode(mode: typeof reflectionModes[number] | '' | boolean | string | string[] | undefined, configPathDir: string): typeof reflectionModes[number] {
if (Array.isArray(mode)) {
if (!configPathDir) return 'never';
const matches = micromatch.contains(this.sourceFile.fileName, mode, {
cwd: configPathDir
});

return matches ? 'default' : 'never';
}
if ('boolean' === typeof mode) return mode ? 'default' : 'never';
if (mode === 'default' || mode === 'always') return mode;
return 'never';
protected parseReflectionMode(mode: ReflectionMode, configPathDir: string): typeof reflectionModes[number] {
return reflectionModeMatcher(this.sourceFile.fileName, mode, configPathDir);
}

protected resolvedTsConfig: { [path: string]: { data: Record<string, any>, exists: boolean } } = {};
Expand Down
Loading

0 comments on commit de8b473

Please sign in to comment.