From 31eea6be61bdd41b911c953425bdde87b16f2013 Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Fri, 10 May 2024 16:58:56 -0400 Subject: [PATCH 01/19] feat: add support for basic imported types from node modules DX-389 --- package-lock.json | 2 +- packages/openapi-generator/package.json | 2 +- packages/openapi-generator/src/cli.ts | 13 ++++- packages/openapi-generator/src/codec.ts | 11 ++-- packages/openapi-generator/src/project.ts | 59 ++++++++++++++++++--- packages/openapi-generator/src/ref.ts | 20 +++++-- packages/openapi-generator/src/symbol.ts | 18 +++++++ packages/openapi-generator/test/ref.test.ts | 14 ++--- 8 files changed, 111 insertions(+), 28 deletions(-) diff --git a/package-lock.json b/package-lock.json index 908c4e95..6591aa4b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13750,7 +13750,7 @@ "@types/resolve": "1.20.6", "c8": "9.1.0", "memfs": "4.9.2", - "typescript": "4.7.4" + "typescript": "^4.7.4" }, "optionalDependencies": { "@swc/core-darwin-arm64": "1.5.7", diff --git a/packages/openapi-generator/package.json b/packages/openapi-generator/package.json index b4513fdb..1f2bdeee 100644 --- a/packages/openapi-generator/package.json +++ b/packages/openapi-generator/package.json @@ -34,7 +34,7 @@ "@types/resolve": "1.20.6", "c8": "9.1.0", "memfs": "4.9.2", - "typescript": "4.7.4" + "typescript": "^4.7.4" }, "optionalDependencies": { "@swc/core-linux-x64-gnu": "1.5.7", diff --git a/packages/openapi-generator/src/cli.ts b/packages/openapi-generator/src/cli.ts index fbd1039f..d7bd452a 100644 --- a/packages/openapi-generator/src/cli.ts +++ b/packages/openapi-generator/src/cli.ts @@ -94,12 +94,16 @@ const app = command({ knownImports = { ...knownImports, ...customCodecs }; } - const project = await new Project({}, knownImports).parseEntryPoint(filePath); + const project = await new Project({}, knownImports, [ + '@bitgo/public-types', + ]).parseEntryPoint(filePath); if (E.isLeft(project)) { console.error(project.left); process.exit(1); } + // console.log(JSON.parse(JSON.stringify(project.right.getTypes(), undefined, 4))) + const entryPoint = project.right.get(filePath); if (entryPoint === undefined) { console.error(`Could not find entry point ${filePath}`); @@ -149,6 +153,8 @@ const app = command({ process.exit(1); } + // console.error(JSON.stringify(apiSpec)) + const components: Record = {}; const queue: Schema[] = apiSpec.flatMap((route) => { return [ @@ -159,7 +165,7 @@ const app = command({ }); let schema: Schema | undefined; while (((schema = queue.pop()), schema !== undefined)) { - const refs = getRefs(schema); + const refs = getRefs(schema, project.right.getTypes()); for (const ref of refs) { if (components[ref.name] !== undefined) { continue; @@ -169,6 +175,7 @@ const app = command({ console.error(`Could not find '${ref.name}' from '${ref.location}'`); process.exit(1); } + const initE = findSymbolInitializer(project.right, sourceFile, ref.name); if (E.isLeft(initE)) { console.error( @@ -177,6 +184,8 @@ const app = command({ process.exit(1); } const [newSourceFile, init] = initE.right; + console.error({ newSourceFile, init }); + const codecE = parseCodecInitializer(project.right, newSourceFile, init); if (E.isLeft(codecE)) { console.error( diff --git a/packages/openapi-generator/src/codec.ts b/packages/openapi-generator/src/codec.ts index 3e52fd33..60490747 100644 --- a/packages/openapi-generator/src/codec.ts +++ b/packages/openapi-generator/src/codec.ts @@ -96,11 +96,12 @@ function codecIdentifier( } const name = id.property.value; - if (!objectImportSym.from.startsWith('.')) { - return E.left( - `Unimplemented named member reference '${objectImportSym.localName}.${name}' from '${objectImportSym.from}'`, - ); - } + // TODO: Add support for types/enum imports from node_modules + // if (!objectImportSym.from.startsWith('.')) { + // return E.left( + // `Unimplemented named member reference '${objectImportSym.localName}.${name}' from '${objectImportSym.from}'`, + // ); + // } const newInitE = findSymbolInitializer(project, source, [ objectImportSym.localName, diff --git a/packages/openapi-generator/src/project.ts b/packages/openapi-generator/src/project.ts index 227b8eb5..87aa4aca 100644 --- a/packages/openapi-generator/src/project.ts +++ b/packages/openapi-generator/src/project.ts @@ -13,10 +13,18 @@ export class Project { private readonly knownImports: Record>; private files: Record; - - constructor(files: Record = {}, knownImports = KNOWN_IMPORTS) { + private types: Record; + private type_packages: Array; + + constructor( + files: Record = {}, + knownImports = KNOWN_IMPORTS, + type_packages: Array = [], + ) { this.files = files; this.knownImports = knownImports; + this.types = {}; + this.type_packages = type_packages; } add(path: string, sourceFile: SourceFile): void { @@ -31,6 +39,10 @@ export class Project { return this.files.hasOwnProperty(path); } + getSourceFile(): Record { + return this.files; + } + async parseEntryPoint(entryPoint: string): Promise> { const queue: string[] = [entryPoint]; let path: string | undefined; @@ -42,13 +54,13 @@ export class Project { const src = await this.readFile(path); const sourceFile = await parseSource(path, src); + for (const exp of sourceFile.symbols.exports) { + this.types[exp.exportedName] = path; + } + this.add(path, sourceFile); for (const sym of Object.values(sourceFile.symbols.imports)) { - if (!sym.from.startsWith('.')) { - continue; - } - const filePath = p.dirname(path); const absImportPathE = this.resolve(filePath, sym.from); if (E.isLeft(absImportPathE)) { @@ -81,7 +93,32 @@ export class Project { basedir, extensions: ['.ts', '.js'], }); - return E.right(result); + + if (this.type_packages.includes(path)) { + const mapName = result.replace('.js', '.js.map'); + + if (fs.existsSync(mapName)) { + const mapJson = JSON.parse(fs.readFileSync(mapName, 'utf8')); + const dirName = p.dirname(result); + const source = mapJson.sources[0]; + + const response = resolve.sync(source, { basedir: dirName }); + + console.error({ mapJson, dirName, source, response, path }); + + return E.right(response); + } + + return E.left('Map file not found for ' + path); + } else { + const dTsName = result.replace('.js', '.d.ts'); + + if (fs.existsSync(dTsName)) { + return E.right(dTsName); + } + + return E.right(result); + } } catch (e: any) { if (typeof e === 'object' && e.hasOwnProperty('message')) { return E.left(e.message); @@ -95,4 +132,12 @@ export class Project { const baseKey = path.startsWith('.') ? '.' : path; return this.knownImports[baseKey]?.[name]; } + + getKnownImports() { + return this.knownImports; + } + + getTypes() { + return this.types; + } } diff --git a/packages/openapi-generator/src/ref.ts b/packages/openapi-generator/src/ref.ts index aadfa57a..5e10ca4e 100644 --- a/packages/openapi-generator/src/ref.ts +++ b/packages/openapi-generator/src/ref.ts @@ -1,24 +1,34 @@ import type { Schema, Reference } from './ir'; +import fs from 'fs'; -export function getRefs(schema: Schema): Reference[] { +export function getRefs(schema: Schema, typeMap: Record): Reference[] { if (schema.type === 'ref') { + if (!fs.existsSync(schema.location)) { + // The location is a node module - we need to populate the location here + const newPath = typeMap[schema.name]; + if (!newPath) { + return []; + } + + return [{ ...schema, location: newPath }]; + } return [schema]; } else if (schema.type === 'array') { - return getRefs(schema.items); + return getRefs(schema.items, typeMap); } else if ( schema.type === 'intersection' || schema.type === 'union' || schema.type === 'tuple' ) { return schema.schemas.reduce((acc, member) => { - return [...acc, ...getRefs(member)]; + return [...acc, ...getRefs(member, typeMap)]; }, []); } else if (schema.type === 'object') { return Object.values(schema.properties).reduce((acc, member) => { - return [...acc, ...getRefs(member)]; + return [...acc, ...getRefs(member, typeMap)]; }, []); } else if (schema.type === 'record') { - return getRefs(schema.codomain); + return getRefs(schema.codomain, typeMap); } else { return []; } diff --git a/packages/openapi-generator/src/symbol.ts b/packages/openapi-generator/src/symbol.ts index ddb75275..ff5b5f42 100644 --- a/packages/openapi-generator/src/symbol.ts +++ b/packages/openapi-generator/src/symbol.ts @@ -236,6 +236,24 @@ export function parseTopLevelSymbols( localName: item.expression.value, }); } + } else if (item.type === 'ExpressionStatement') { + if (item.expression.type === 'CallExpression') { + if ( + item.expression.callee.type === 'Identifier' && + item.expression.callee.value === '__exportStar' + ) { + const targetExpr = item?.expression?.arguments[0]?.expression; + + if (targetExpr && 'arguments' in targetExpr) { + const targetArgument = targetExpr.arguments; + if (targetArgument && 'value' in targetArgument[0]!.expression) { + if (targetArgument[0]!.expression.type === 'StringLiteral') { + symbols.exportStarFiles.push(targetArgument[0]!.expression.value); + } + } + } + } + } } }); diff --git a/packages/openapi-generator/test/ref.test.ts b/packages/openapi-generator/test/ref.test.ts index d69f4dae..8111c84f 100644 --- a/packages/openapi-generator/test/ref.test.ts +++ b/packages/openapi-generator/test/ref.test.ts @@ -10,7 +10,7 @@ test('simple ref is returned', () => { location: '/foo.ts', }; - assert.deepEqual(getRefs(schema), [schema]); + assert.deepEqual(getRefs(schema, {}), [schema]); }); test('array ref is returned', () => { @@ -23,7 +23,7 @@ test('array ref is returned', () => { }, }; - assert.deepEqual(getRefs(schema), [ + assert.deepEqual(getRefs(schema, {}), [ { type: 'ref', name: 'Foo', @@ -49,7 +49,7 @@ test('intersection ref is returned', () => { ], }; - assert.deepEqual(getRefs(schema), [ + assert.deepEqual(getRefs(schema, {}), [ { type: 'ref', name: 'Foo', @@ -80,7 +80,7 @@ test('union ref is returned', () => { ], }; - assert.deepEqual(getRefs(schema), [ + assert.deepEqual(getRefs(schema, {}), [ { type: 'ref', name: 'Foo', @@ -111,7 +111,7 @@ test('tuple ref is returned', () => { ], }; - assert.deepEqual(getRefs(schema), [ + assert.deepEqual(getRefs(schema, {}), [ { type: 'ref', name: 'Foo', @@ -143,7 +143,7 @@ test('object ref is returned', () => { required: ['foo', 'bar'], }; - assert.deepEqual(getRefs(schema), [ + assert.deepEqual(getRefs(schema, {}), [ { type: 'ref', name: 'Foo', @@ -167,7 +167,7 @@ test('record ref is returned', () => { }, }; - assert.deepEqual(getRefs(schema), [ + assert.deepEqual(getRefs(schema, {}), [ { type: 'ref', name: 'Foo', From 17d919a7ea20df6696d22a365baa54e1c40a7f49 Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Wed, 15 May 2024 14:20:34 -0400 Subject: [PATCH 02/19] refactor(openapi-generator): support node modules (excluding express) BREAKING CHANGE: supporting `node_modules` may cause current `-types` libraries to break which use `openapi-generator` --- packages/openapi-generator/src/cli.ts | 7 +--- packages/openapi-generator/src/codec.ts | 9 +++-- packages/openapi-generator/src/project.ts | 41 +++++++++++++++++------ 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/packages/openapi-generator/src/cli.ts b/packages/openapi-generator/src/cli.ts index d7bd452a..0dfcac2c 100644 --- a/packages/openapi-generator/src/cli.ts +++ b/packages/openapi-generator/src/cli.ts @@ -94,16 +94,12 @@ const app = command({ knownImports = { ...knownImports, ...customCodecs }; } - const project = await new Project({}, knownImports, [ - '@bitgo/public-types', - ]).parseEntryPoint(filePath); + const project = await new Project({}, knownImports).parseEntryPoint(filePath); if (E.isLeft(project)) { console.error(project.left); process.exit(1); } - // console.log(JSON.parse(JSON.stringify(project.right.getTypes(), undefined, 4))) - const entryPoint = project.right.get(filePath); if (entryPoint === undefined) { console.error(`Could not find entry point ${filePath}`); @@ -184,7 +180,6 @@ const app = command({ process.exit(1); } const [newSourceFile, init] = initE.right; - console.error({ newSourceFile, init }); const codecE = parseCodecInitializer(project.right, newSourceFile, init); if (E.isLeft(codecE)) { diff --git a/packages/openapi-generator/src/codec.ts b/packages/openapi-generator/src/codec.ts index 60490747..ff6818ae 100644 --- a/packages/openapi-generator/src/codec.ts +++ b/packages/openapi-generator/src/codec.ts @@ -361,9 +361,14 @@ export function parseCodecInitializer( if (schema.type !== 'ref') { return E.right(schema); } else { - const refSource = project.get(schema.location); + let refSource = project.get(schema.location); if (refSource === undefined) { - return E.left(`Cannot find '${schema.name}' from '${schema.location}'`); + const path = project.getTypes()[schema.name]; + if (path === undefined) return E.left('Should not happen'); + refSource = project.get(path); + if (refSource === undefined) { + return E.left(`Cannot find '${schema.name}' from '${schema.location}'`); + } } const initE = findSymbolInitializer(project, refSource, schema.name); if (E.isLeft(initE)) { diff --git a/packages/openapi-generator/src/project.ts b/packages/openapi-generator/src/project.ts index 87aa4aca..3f0fae7c 100644 --- a/packages/openapi-generator/src/project.ts +++ b/packages/openapi-generator/src/project.ts @@ -16,15 +16,25 @@ export class Project { private types: Record; private type_packages: Array; - constructor( - files: Record = {}, - knownImports = KNOWN_IMPORTS, - type_packages: Array = [], - ) { + constructor(files: Record = {}, knownImports = KNOWN_IMPORTS) { this.files = files; this.knownImports = knownImports; this.types = {}; - this.type_packages = type_packages; + this.type_packages = [ + '@bitgo-private/address-book-types', + '@bitgo-private/entity-validation-types', + '@bitgo-private/original-indexer-types', + '@bitgo-private/policy-service-types', + '@bitgo-private/query-param-types', + '@bitgo-private/request-types', + '@bitgo-private/schema-decoding', + '@bitgo-private/travel-rule-types', + '@bitgo-private/wallet-platform-types', + '@bitgo/public-types', + '@bitgo-private/compliance-tx-monitoring-types', + '@bitgo-private/event-service-types', + '@bitgo-private/ims-type', + ]; } add(path: string, sourceFile: SourceFile): void { @@ -54,6 +64,7 @@ export class Project { const src = await this.readFile(path); const sourceFile = await parseSource(path, src); + // map types to their file path for (const exp of sourceFile.symbols.exports) { this.types[exp.exportedName] = path; } @@ -87,11 +98,23 @@ export class Project { return await readFile(filename, 'utf8'); } + // add @types to beginning of path if path is a type package + maybeModifyPath(path: string) { + const typePackages = ['express', 'superagent', 'cookiejar']; + if (typePackages.includes(path)) return '@types/' + path; + return path; + } + resolve(basedir: string, path: string): E.Either { try { + path = this.maybeModifyPath(path); + + // express doesn't parse for some reason + if (path.includes('express')) return E.right('express'); + const result = resolve.sync(path, { basedir, - extensions: ['.ts', '.js'], + extensions: ['.ts', '.js', '.d.ts'], }); if (this.type_packages.includes(path)) { @@ -101,11 +124,7 @@ export class Project { const mapJson = JSON.parse(fs.readFileSync(mapName, 'utf8')); const dirName = p.dirname(result); const source = mapJson.sources[0]; - const response = resolve.sync(source, { basedir: dirName }); - - console.error({ mapJson, dirName, source, response, path }); - return E.right(response); } From 58a3429f44be91ad72274e224303c0133609a098 Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Wed, 15 May 2024 16:59:43 -0400 Subject: [PATCH 03/19] refactor: cleanup node_modules generator code DX-389 --- packages/openapi-generator/src/cli.ts | 2 - packages/openapi-generator/src/codec.ts | 12 ++-- packages/openapi-generator/src/project.ts | 82 ++++++++++++----------- packages/openapi-generator/src/symbol.ts | 18 ----- 4 files changed, 48 insertions(+), 66 deletions(-) diff --git a/packages/openapi-generator/src/cli.ts b/packages/openapi-generator/src/cli.ts index 0dfcac2c..2bf98fe7 100644 --- a/packages/openapi-generator/src/cli.ts +++ b/packages/openapi-generator/src/cli.ts @@ -149,8 +149,6 @@ const app = command({ process.exit(1); } - // console.error(JSON.stringify(apiSpec)) - const components: Record = {}; const queue: Schema[] = apiSpec.flatMap((route) => { return [ diff --git a/packages/openapi-generator/src/codec.ts b/packages/openapi-generator/src/codec.ts index ff6818ae..48bc0847 100644 --- a/packages/openapi-generator/src/codec.ts +++ b/packages/openapi-generator/src/codec.ts @@ -96,13 +96,6 @@ function codecIdentifier( } const name = id.property.value; - // TODO: Add support for types/enum imports from node_modules - // if (!objectImportSym.from.startsWith('.')) { - // return E.left( - // `Unimplemented named member reference '${objectImportSym.localName}.${name}' from '${objectImportSym.from}'`, - // ); - // } - const newInitE = findSymbolInitializer(project, source, [ objectImportSym.localName, name, @@ -362,9 +355,12 @@ export function parseCodecInitializer( return E.right(schema); } else { let refSource = project.get(schema.location); + if (refSource === undefined) { + // schema.location might be a package name -> need to resolve the path from the project types const path = project.getTypes()[schema.name]; - if (path === undefined) return E.left('Should not happen'); + if (path === undefined) + return E.left(`Cannot find '${schema.name}' in the project`); refSource = project.get(path); if (refSource === undefined) { return E.left(`Cannot find '${schema.name}' from '${schema.location}'`); diff --git a/packages/openapi-generator/src/project.ts b/packages/openapi-generator/src/project.ts index 3f0fae7c..2199bf59 100644 --- a/packages/openapi-generator/src/project.ts +++ b/packages/openapi-generator/src/project.ts @@ -14,27 +14,11 @@ export class Project { private files: Record; private types: Record; - private type_packages: Array; constructor(files: Record = {}, knownImports = KNOWN_IMPORTS) { this.files = files; this.knownImports = knownImports; this.types = {}; - this.type_packages = [ - '@bitgo-private/address-book-types', - '@bitgo-private/entity-validation-types', - '@bitgo-private/original-indexer-types', - '@bitgo-private/policy-service-types', - '@bitgo-private/query-param-types', - '@bitgo-private/request-types', - '@bitgo-private/schema-decoding', - '@bitgo-private/travel-rule-types', - '@bitgo-private/wallet-platform-types', - '@bitgo/public-types', - '@bitgo-private/compliance-tx-monitoring-types', - '@bitgo-private/event-service-types', - '@bitgo-private/ims-type', - ]; } add(path: string, sourceFile: SourceFile): void { @@ -98,37 +82,59 @@ export class Project { return await readFile(filename, 'utf8'); } - // add @types to beginning of path if path is a type package - maybeModifyPath(path: string) { - const typePackages = ['express', 'superagent', 'cookiejar']; - if (typePackages.includes(path)) return '@types/' + path; - return path; + private resolvePath(path: string, basedir: string): E.Either { + try { + const result = resolve.sync(path, { + basedir, + extensions: ['.ts', '.js', '.d.ts'], + }); + + return E.right(result); + } catch (e: any) { + if (typeof e === 'object' && e.hasOwnProperty('message')) { + return E.left(e.message); + } + + return E.left(JSON.stringify(e)); + } + } + + private findSourceFileFromPackage(path: string): E.Either { + const mapName = path.replace('.js', '.js.map'); + + if (fs.existsSync(mapName)) { + const mapJson = JSON.parse(fs.readFileSync(mapName, 'utf8')); + const dirName = p.dirname(path); + const source = mapJson.sources[0]; + const response = resolve.sync(source, { basedir: dirName }); + return E.right(response); + } + + return E.left('Map file not found for ' + path); } resolve(basedir: string, path: string): E.Either { + const BITGO_PREFIX = '@bitgo'; try { - path = this.maybeModifyPath(path); - - // express doesn't parse for some reason + // TODO: add support for non-strict mode (swc errors out when handling express as it is not in strict mode) if (path.includes('express')) return E.right('express'); - const result = resolve.sync(path, { - basedir, - extensions: ['.ts', '.js', '.d.ts'], - }); + let resolved = this.resolvePath(path, basedir); + if (E.isLeft(resolved)) { + // Could not resolve the path, try resolving in the types package + resolved = this.resolvePath('@types/' + path, basedir); + } - if (this.type_packages.includes(path)) { - const mapName = result.replace('.js', '.js.map'); + // Types package wasn't found, return an errord + if (E.isLeft(resolved)) { + return E.left('Could not resolve ' + path); + } - if (fs.existsSync(mapName)) { - const mapJson = JSON.parse(fs.readFileSync(mapName, 'utf8')); - const dirName = p.dirname(result); - const source = mapJson.sources[0]; - const response = resolve.sync(source, { basedir: dirName }); - return E.right(response); - } + const result = resolved.right; - return E.left('Map file not found for ' + path); + // If we are parsing an internal type package, we want to return the path to the source TS file + if (path.startsWith(BITGO_PREFIX)) { + return this.findSourceFileFromPackage(result); } else { const dTsName = result.replace('.js', '.d.ts'); diff --git a/packages/openapi-generator/src/symbol.ts b/packages/openapi-generator/src/symbol.ts index ff5b5f42..ddb75275 100644 --- a/packages/openapi-generator/src/symbol.ts +++ b/packages/openapi-generator/src/symbol.ts @@ -236,24 +236,6 @@ export function parseTopLevelSymbols( localName: item.expression.value, }); } - } else if (item.type === 'ExpressionStatement') { - if (item.expression.type === 'CallExpression') { - if ( - item.expression.callee.type === 'Identifier' && - item.expression.callee.value === '__exportStar' - ) { - const targetExpr = item?.expression?.arguments[0]?.expression; - - if (targetExpr && 'arguments' in targetExpr) { - const targetArgument = targetExpr.arguments; - if (targetArgument && 'value' in targetArgument[0]!.expression) { - if (targetArgument[0]!.expression.type === 'StringLiteral') { - symbols.exportStarFiles.push(targetArgument[0]!.expression.value); - } - } - } - } - } } }); From 4fb5588594abe6733ea8a1aeeed00a91ac8aa8c2 Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Wed, 15 May 2024 19:39:12 -0400 Subject: [PATCH 04/19] fix broken `ref.test.ts` tests --- packages/openapi-generator/test/ref.test.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/openapi-generator/test/ref.test.ts b/packages/openapi-generator/test/ref.test.ts index 8111c84f..93310b75 100644 --- a/packages/openapi-generator/test/ref.test.ts +++ b/packages/openapi-generator/test/ref.test.ts @@ -3,6 +3,11 @@ import test from 'node:test'; import { getRefs, type Schema } from '../src'; +const typeMap: Record = { + Foo: '/foo.ts', + Bar: '/bar.ts', +}; + test('simple ref is returned', () => { const schema: Schema = { type: 'ref', @@ -10,7 +15,7 @@ test('simple ref is returned', () => { location: '/foo.ts', }; - assert.deepEqual(getRefs(schema, {}), [schema]); + assert.deepEqual(getRefs(schema, typeMap), [schema]); }); test('array ref is returned', () => { @@ -23,7 +28,7 @@ test('array ref is returned', () => { }, }; - assert.deepEqual(getRefs(schema, {}), [ + assert.deepEqual(getRefs(schema, typeMap), [ { type: 'ref', name: 'Foo', @@ -49,7 +54,7 @@ test('intersection ref is returned', () => { ], }; - assert.deepEqual(getRefs(schema, {}), [ + assert.deepEqual(getRefs(schema, typeMap), [ { type: 'ref', name: 'Foo', @@ -80,7 +85,7 @@ test('union ref is returned', () => { ], }; - assert.deepEqual(getRefs(schema, {}), [ + assert.deepEqual(getRefs(schema, typeMap), [ { type: 'ref', name: 'Foo', @@ -111,7 +116,7 @@ test('tuple ref is returned', () => { ], }; - assert.deepEqual(getRefs(schema, {}), [ + assert.deepEqual(getRefs(schema, typeMap), [ { type: 'ref', name: 'Foo', @@ -143,7 +148,7 @@ test('object ref is returned', () => { required: ['foo', 'bar'], }; - assert.deepEqual(getRefs(schema, {}), [ + assert.deepEqual(getRefs(schema, typeMap), [ { type: 'ref', name: 'Foo', @@ -167,7 +172,7 @@ test('record ref is returned', () => { }, }; - assert.deepEqual(getRefs(schema, {}), [ + assert.deepEqual(getRefs(schema, typeMap), [ { type: 'ref', name: 'Foo', From 9637dcb077964d285a6370c831565ee704287d89 Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Thu, 16 May 2024 11:01:19 -0400 Subject: [PATCH 05/19] fix(openapi-generator): broken resolve.test.ts tests --- .../openapi-generator/test/resolve.test.ts | 47 +++++++++++++++++-- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/packages/openapi-generator/test/resolve.test.ts b/packages/openapi-generator/test/resolve.test.ts index 924c9444..c1e075c1 100644 --- a/packages/openapi-generator/test/resolve.test.ts +++ b/packages/openapi-generator/test/resolve.test.ts @@ -6,6 +6,35 @@ import test from 'node:test'; import { TestProject } from './testProject'; import { parseCodecInitializer, Project, type Schema } from '../src'; +const IO_TS = ` +export const type = (props: any) => ({ type: 'object', ...props }); +export const string = { type: 'string' }; +export const number = { type: 'number' }; +export const union = (schemas: any[]) => ({ type: 'union', schemas }); +export const keyof = (keys: any) => ({ type: 'union', schemas: Object.keys(keys).map((key) => ({ type: 'string', enum: [key] })) }); +export const literal = (value: any) => ({ type: typeof value, enum: [value] }); +`; + +const IO_TS_PACKAGE_JSON = `{ + "name": "io-ts", + "version": "1.0.0", + "main": "dist/src/index.js", + "types": "dist/src/index.d.ts" +}`; + +const IO_TS_OBJECT = { + '/node_modules': { + '/io-ts': { + '/dist': { + '/src': { + '/index.js': IO_TS, + }, + }, + '/package.json': IO_TS_PACKAGE_JSON, + }, + }, +}; + async function testCase( description: string, files: NestedDirectoryJSON, @@ -14,7 +43,7 @@ async function testCase( expectedErrors: string[] = [], ) { test(description, async () => { - let project: Project = new TestProject(files); + let project: Project = new TestProject({ ...files, ...IO_TS_OBJECT }); const projectE = await project.parseEntryPoint(entryPoint); if (E.isLeft(projectE)) { throw new Error(projectE.left); @@ -55,7 +84,9 @@ export const FOO = t.type(fooProps); testCase( 'const props initializer is parsed', - { '/index.ts': OBJECT_CONST }, + { + '/index.ts': OBJECT_CONST, + }, '/index.ts', { FOO: { @@ -75,7 +106,9 @@ export const FOO = t.union(fooUnion); testCase( 'const array initializer is parsed', - { '/index.ts': ARRAY_CONST }, + { + '/index.ts': ARRAY_CONST, + }, '/index.ts', { FOO: { @@ -97,7 +130,9 @@ export const FOO = t.keyof(fooKeys); testCase( 'const keyof initializer is parsed', - { '/index.ts': KEYOF_CONST }, + { + '/index.ts': KEYOF_CONST, + }, '/index.ts', { FOO: { @@ -119,7 +154,9 @@ export const FOO = t.literal(foo); testCase( 'const literal initializer is parsed', - { '/index.ts': LITERAL_CONST }, + { + '/index.ts': LITERAL_CONST, + }, '/index.ts', { FOO: { type: 'number', enum: [42] }, From 66be8232ca8c3ac8bbcda17142e334f422bcd924 Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Thu, 16 May 2024 13:22:11 -0400 Subject: [PATCH 06/19] fix(openapi-generator): fix more tests --- packages/openapi-generator/src/codec.ts | 2 +- .../openapi-generator/test/apiSpec.test.ts | 5 ++- .../openapi-generator/test/externalModules.ts | 45 +++++++++++++++++++ .../openapi-generator/test/resolve.test.ts | 32 +------------ 4 files changed, 51 insertions(+), 33 deletions(-) create mode 100644 packages/openapi-generator/test/externalModules.ts diff --git a/packages/openapi-generator/src/codec.ts b/packages/openapi-generator/src/codec.ts index 48bc0847..592eee0a 100644 --- a/packages/openapi-generator/src/codec.ts +++ b/packages/openapi-generator/src/codec.ts @@ -360,7 +360,7 @@ export function parseCodecInitializer( // schema.location might be a package name -> need to resolve the path from the project types const path = project.getTypes()[schema.name]; if (path === undefined) - return E.left(`Cannot find '${schema.name}' in the project`); + return E.left(`Cannot find module '${schema.location}' in the project`); refSource = project.get(path); if (refSource === undefined) { return E.left(`Cannot find '${schema.name}' from '${schema.location}'`); diff --git a/packages/openapi-generator/test/apiSpec.test.ts b/packages/openapi-generator/test/apiSpec.test.ts index 960e80a2..6c9958ab 100644 --- a/packages/openapi-generator/test/apiSpec.test.ts +++ b/packages/openapi-generator/test/apiSpec.test.ts @@ -5,6 +5,7 @@ import type { NestedDirectoryJSON } from 'memfs'; import { TestProject } from './testProject'; import { parseApiSpec, parseApiSpecComment, type Route } from '../src'; +import { MOCK_NODE_MODULES_DIR } from './externalModules'; async function testCase( description: string, @@ -14,7 +15,7 @@ async function testCase( expectedErrors: string[] = [], ) { test(description, async () => { - const project = new TestProject(files); + const project = new TestProject({ ...files, ...MOCK_NODE_MODULES_DIR }); await project.parseEntryPoint(entryPoint); const sourceFile = project.get(entryPoint); @@ -288,5 +289,5 @@ const MISSING_REFERENCE = { }; testCase('missing reference', MISSING_REFERENCE, '/index.ts', {}, [ - "Cannot find 'Foo' from 'foo'", + "Cannot find module 'foo' in the project", ]); diff --git a/packages/openapi-generator/test/externalModules.ts b/packages/openapi-generator/test/externalModules.ts new file mode 100644 index 00000000..e167c376 --- /dev/null +++ b/packages/openapi-generator/test/externalModules.ts @@ -0,0 +1,45 @@ +const IO_TS_CONTENTS = ` +export const type = (props: any) => ({ type: 'object', ...props }); +export const string = { type: 'string' }; +export const number = { type: 'number' }; +export const union = (schemas: any[]) => ({ type: 'union', schemas }); +export const keyof = (keys: any) => ({ type: 'union', schemas: Object.keys(keys).map((key) => ({ type: 'string', enum: [key] })) }); +export const literal = (value: any) => ({ type: typeof value, enum: [value] }); +`; + +const IO_TS_PACKAGE_JSON = `{ + "name": "io-ts", + "version": "1.0.0", + "main": "dist/src/index.js", + "types": "dist/src/index.d.ts" +}`; + +const IO_TS_HTTP_PACKAGE_JSON = `{ + "name": "@api-ts/io-ts-http", + "version": "1.0.0", + "main": "dist/src/index.js", + "types": "dist/src/index.d.ts" +}`; + +export const MOCK_NODE_MODULES_DIR = { + '/node_modules': { + '/@api-ts': { + '/io-ts-http': { + '/dist': { + '/src': { + '/index.js': ``, + }, + }, + '/package.json': IO_TS_HTTP_PACKAGE_JSON, + }, + }, + '/io-ts': { + '/dist': { + '/src': { + '/index.js': IO_TS_CONTENTS, + }, + }, + '/package.json': IO_TS_PACKAGE_JSON, + }, + }, +}; diff --git a/packages/openapi-generator/test/resolve.test.ts b/packages/openapi-generator/test/resolve.test.ts index c1e075c1..fe11bddb 100644 --- a/packages/openapi-generator/test/resolve.test.ts +++ b/packages/openapi-generator/test/resolve.test.ts @@ -5,35 +5,7 @@ import test from 'node:test'; import { TestProject } from './testProject'; import { parseCodecInitializer, Project, type Schema } from '../src'; - -const IO_TS = ` -export const type = (props: any) => ({ type: 'object', ...props }); -export const string = { type: 'string' }; -export const number = { type: 'number' }; -export const union = (schemas: any[]) => ({ type: 'union', schemas }); -export const keyof = (keys: any) => ({ type: 'union', schemas: Object.keys(keys).map((key) => ({ type: 'string', enum: [key] })) }); -export const literal = (value: any) => ({ type: typeof value, enum: [value] }); -`; - -const IO_TS_PACKAGE_JSON = `{ - "name": "io-ts", - "version": "1.0.0", - "main": "dist/src/index.js", - "types": "dist/src/index.d.ts" -}`; - -const IO_TS_OBJECT = { - '/node_modules': { - '/io-ts': { - '/dist': { - '/src': { - '/index.js': IO_TS, - }, - }, - '/package.json': IO_TS_PACKAGE_JSON, - }, - }, -}; +import { MOCK_NODE_MODULES_DIR } from './externalModules'; async function testCase( description: string, @@ -43,7 +15,7 @@ async function testCase( expectedErrors: string[] = [], ) { test(description, async () => { - let project: Project = new TestProject({ ...files, ...IO_TS_OBJECT }); + let project: Project = new TestProject({ ...files, ...MOCK_NODE_MODULES_DIR }); const projectE = await project.parseEntryPoint(entryPoint); if (E.isLeft(projectE)) { throw new Error(projectE.left); From fbcb5e48cc15798a203519b1d57ef073ef141af0 Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Thu, 16 May 2024 16:09:07 -0400 Subject: [PATCH 07/19] chore: ignore and log `swc` errors and write test --- packages/openapi-generator/package.json | 3 ++- packages/openapi-generator/src/project.ts | 2 ++ packages/openapi-generator/src/sourceFile.ts | 11 +++++--- .../openapi-generator/test/openapi.test.ts | 3 +++ .../openapi-generator/test/project.test.ts | 26 +++++++++++++++++++ 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/packages/openapi-generator/package.json b/packages/openapi-generator/package.json index 1f2bdeee..b1528427 100644 --- a/packages/openapi-generator/package.json +++ b/packages/openapi-generator/package.json @@ -17,7 +17,8 @@ "clean": "rm -rf -- dist", "format": "prettier --check .", "format:fix": "prettier --write .", - "test": "c8 --all --src src node --require @swc-node/register --test test/*.test.ts" + "test": "c8 --all --src src node --require @swc-node/register --test test/*.test.ts", + "test:target": "c8 --all --src src node --require @swc-node/register" }, "dependencies": { "@swc/core": "1.5.7", diff --git a/packages/openapi-generator/src/project.ts b/packages/openapi-generator/src/project.ts index 2199bf59..819a2385 100644 --- a/packages/openapi-generator/src/project.ts +++ b/packages/openapi-generator/src/project.ts @@ -48,6 +48,8 @@ export class Project { const src = await this.readFile(path); const sourceFile = await parseSource(path, src); + if (sourceFile === undefined) continue; + // map types to their file path for (const exp of sourceFile.symbols.exports) { this.types[exp.exportedName] = path; diff --git a/packages/openapi-generator/src/sourceFile.ts b/packages/openapi-generator/src/sourceFile.ts index e72d11a4..bd12baba 100644 --- a/packages/openapi-generator/src/sourceFile.ts +++ b/packages/openapi-generator/src/sourceFile.ts @@ -14,7 +14,10 @@ export type SourceFile = { // increasing counter for this, so we also need to track it globally here let lastSpanEnd = -1; -export async function parseSource(path: string, src: string): Promise { +export async function parseSource( + path: string, + src: string, +): Promise { try { const module = await swc.parse(src, { syntax: 'typescript', @@ -34,8 +37,8 @@ export async function parseSource(path: string, src: string): Promise { const sourceFile = await parseSource('./index.ts', src); + if (sourceFile === undefined) { + throw new Error('Failed to parse source file'); + } const project = new Project(); const routes: Route[] = []; diff --git a/packages/openapi-generator/test/project.test.ts b/packages/openapi-generator/test/project.test.ts index b1046853..02ef8cdd 100644 --- a/packages/openapi-generator/test/project.test.ts +++ b/packages/openapi-generator/test/project.test.ts @@ -93,3 +93,29 @@ testCase( }, }, ); + +const NON_STRICT_MODE_SRC = ` +import * as t from 'io-ts'; +import { bar } from './bar'; +var static: serveStatic.RequestHandlerConstructor; +export const FOO = t.type({ bar: bar }); +`; + +test('non-strict files are ignored and logged to stderr', async () => { + let errorCalled = false; + const originalConsoleError = console.error; + + console.error = (...args) => { + errorCalled = true; + originalConsoleError(...args); + }; + + const project = new TestProject({ '/index.ts': NON_STRICT_MODE_SRC }, {}); + await project.parseEntryPoint('/index.ts'); + const sourceFile = project.get('/index.ts'); + + assert.strictEqual(sourceFile, undefined); + assert.strictEqual(errorCalled, true, new Error('console.error was not called')); + + console.error = originalConsoleError; +}); From 940ed49efc90c93e5e635e0a81e80bb386a2f284 Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Thu, 16 May 2024 17:23:53 -0400 Subject: [PATCH 08/19] chore(openapi-generator): add tests for openapi node_modules generation DX-389 --- .prettierignore | 1 + package.json | 2 +- packages/openapi-generator/src/project.ts | 16 +- .../test/externalModule.test.ts | 219 ++++++++++++++++++ .../test/projects/.gitignore | 2 + .../test/projects/export-declaration/index.ts | 10 + .../@bitgo/foobar1/dist/src/index.js | 0 .../@bitgo/foobar1/dist/src/index.js.map | 3 + .../node_modules/@bitgo/foobar1/package.json | 16 ++ .../node_modules/@bitgo/foobar1/src/index.ts | 6 + .../node_modules/@bitgo/foobar1/tsconfig.json | 7 + .../@bitgo/random-types1/dist/src/index.js | 0 .../random-types1/dist/src/index.js.map | 3 + .../@bitgo/random-types1/package.json | 16 ++ .../@bitgo/random-types1/src/index.ts | 6 + .../@bitgo/random-types1/tsconfig.json | 7 + .../projects/export-declaration/package.json | 11 + .../test/projects/export-star/index.ts | 10 + .../@bitgo/foobar2/dist/src/index.js | 0 .../@bitgo/foobar2/dist/src/index.js.map | 3 + .../node_modules/@bitgo/foobar2/package.json | 16 ++ .../node_modules/@bitgo/foobar2/src/foobar.ts | 6 + .../node_modules/@bitgo/foobar2/src/index.ts | 1 + .../node_modules/@bitgo/foobar2/tsconfig.json | 7 + .../@bitgo/random-types2/dist/src/index.js | 0 .../random-types2/dist/src/index.js.map | 3 + .../@bitgo/random-types2/package.json | 16 ++ .../@bitgo/random-types2/src/index.ts | 1 + .../@bitgo/random-types2/src/randomType.ts | 6 + .../@bitgo/random-types2/tsconfig.json | 7 + .../test/projects/export-star/package.json | 11 + .../test/projects/import-path-error/index.ts | 5 + .../@bitgo/foobar3/dist/src/index.js | 0 .../@bitgo/foobar3/dist/src/index.js.map | 3 + .../node_modules/@bitgo/foobar3/package.json | 16 ++ .../node_modules/@bitgo/foobar3/src/foobar.ts | 6 + .../node_modules/@bitgo/foobar3/src/index.ts | 1 + .../node_modules/@bitgo/foobar3/tsconfig.json | 7 + .../projects/import-path-error/package.json | 11 + .../test/projects/import-star/index.ts | 10 + .../@bitgo/foobar4/dist/src/index.js | 0 .../@bitgo/foobar4/dist/src/index.js.map | 3 + .../node_modules/@bitgo/foobar4/package.json | 16 ++ .../node_modules/@bitgo/foobar4/src/foobar.ts | 6 + .../node_modules/@bitgo/foobar4/src/index.ts | 1 + .../node_modules/@bitgo/foobar4/tsconfig.json | 7 + .../@bitgo/random-types4/dist/src/index.js | 0 .../random-types4/dist/src/index.js.map | 3 + .../@bitgo/random-types4/package.json | 16 ++ .../@bitgo/random-types4/src/index.ts | 1 + .../@bitgo/random-types4/src/randomType.ts | 6 + .../@bitgo/random-types4/tsconfig.json | 7 + .../test/projects/import-star/package.json | 11 + .../test/projects/syntax-error/index.ts | 5 + .../@bitgo/foobar5/dist/src/index.js | 0 .../@bitgo/foobar5/dist/src/index.js.map | 3 + .../node_modules/@bitgo/foobar5/package.json | 16 ++ .../node_modules/@bitgo/foobar5/src/foobar.ts | 6 + .../node_modules/@bitgo/foobar5/src/index.ts | 1 + .../node_modules/@bitgo/foobar5/tsconfig.json | 7 + .../test/projects/syntax-error/package.json | 11 + packages/openapi-generator/tsconfig.json | 1 + 62 files changed, 584 insertions(+), 14 deletions(-) create mode 100644 packages/openapi-generator/test/externalModule.test.ts create mode 100644 packages/openapi-generator/test/projects/.gitignore create mode 100644 packages/openapi-generator/test/projects/export-declaration/index.ts create mode 100644 packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js create mode 100644 packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js.map create mode 100644 packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/package.json create mode 100644 packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/src/index.ts create mode 100644 packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/tsconfig.json create mode 100644 packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js create mode 100644 packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js.map create mode 100644 packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/package.json create mode 100644 packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/src/index.ts create mode 100644 packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/tsconfig.json create mode 100644 packages/openapi-generator/test/projects/export-declaration/package.json create mode 100644 packages/openapi-generator/test/projects/export-star/index.ts create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js.map create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/package.json create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/foobar.ts create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/index.ts create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/tsconfig.json create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js.map create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/package.json create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/index.ts create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/randomType.ts create mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/tsconfig.json create mode 100644 packages/openapi-generator/test/projects/export-star/package.json create mode 100644 packages/openapi-generator/test/projects/import-path-error/index.ts create mode 100644 packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js create mode 100644 packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js.map create mode 100644 packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/package.json create mode 100644 packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/foobar.ts create mode 100644 packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/index.ts create mode 100644 packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/tsconfig.json create mode 100644 packages/openapi-generator/test/projects/import-path-error/package.json create mode 100644 packages/openapi-generator/test/projects/import-star/index.ts create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js.map create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/package.json create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/foobar.ts create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/index.ts create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/tsconfig.json create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js.map create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/package.json create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/index.ts create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/randomType.ts create mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/tsconfig.json create mode 100644 packages/openapi-generator/test/projects/import-star/package.json create mode 100644 packages/openapi-generator/test/projects/syntax-error/index.ts create mode 100644 packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js create mode 100644 packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js.map create mode 100644 packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/package.json create mode 100644 packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/foobar.ts create mode 100644 packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/index.ts create mode 100644 packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/tsconfig.json create mode 100644 packages/openapi-generator/test/projects/syntax-error/package.json diff --git a/.prettierignore b/.prettierignore index d27bbf0b..84af0e71 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,4 @@ coverage/ dist/ flake.lock +tests/programs/* \ No newline at end of file diff --git a/package.json b/package.json index 58f25e68..2bfc34ab 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "homepage": "https://github.com/BitGo/api-ts#readme", "workspaces": [ - "packages/**" + "packages/*" ], "packageManager": "npm@8.5.0", "scripts": { diff --git a/packages/openapi-generator/src/project.ts b/packages/openapi-generator/src/project.ts index 819a2385..2d47ad40 100644 --- a/packages/openapi-generator/src/project.ts +++ b/packages/openapi-generator/src/project.ts @@ -33,10 +33,6 @@ export class Project { return this.files.hasOwnProperty(path); } - getSourceFile(): Record { - return this.files; - } - async parseEntryPoint(entryPoint: string): Promise> { const queue: string[] = [entryPoint]; let path: string | undefined; @@ -118,18 +114,15 @@ export class Project { resolve(basedir: string, path: string): E.Either { const BITGO_PREFIX = '@bitgo'; try { - // TODO: add support for non-strict mode (swc errors out when handling express as it is not in strict mode) - if (path.includes('express')) return E.right('express'); - let resolved = this.resolvePath(path, basedir); if (E.isLeft(resolved)) { // Could not resolve the path, try resolving in the types package resolved = this.resolvePath('@types/' + path, basedir); } - // Types package wasn't found, return an errord + // Types package wasn't found, return an error if (E.isLeft(resolved)) { - return E.left('Could not resolve ' + path); + return E.left('Could not resolve ' + path + ' from ' + basedir); } const result = resolved.right; @@ -138,6 +131,7 @@ export class Project { if (path.startsWith(BITGO_PREFIX)) { return this.findSourceFileFromPackage(result); } else { + // Else - find the declaration file and return it if it exists const dTsName = result.replace('.js', '.d.ts'); if (fs.existsSync(dTsName)) { @@ -160,10 +154,6 @@ export class Project { return this.knownImports[baseKey]?.[name]; } - getKnownImports() { - return this.knownImports; - } - getTypes() { return this.types; } diff --git a/packages/openapi-generator/test/externalModule.test.ts b/packages/openapi-generator/test/externalModule.test.ts new file mode 100644 index 00000000..e61071c9 --- /dev/null +++ b/packages/openapi-generator/test/externalModule.test.ts @@ -0,0 +1,219 @@ +import * as E from 'fp-ts/lib/Either'; +import assert from 'node:assert/strict'; +import test from 'node:test'; +import * as p from 'path'; + +import { parsePlainInitializer, Project, type Schema } from '../src'; +import { KNOWN_IMPORTS } from '../src/knownImports'; + +/** External library parsing test case + * + * @param description a description of the test case + * @param entryPoint relative path to the test case's index.tx + * @param expected a record of expected files (including ones in node_modules), with their expected parsed schemas + * @param expectedErrors optional record of expected parsing errors for each source file + */ +async function testCase( + description: string, + entryPoint: string, + expected: Record>, + expectedErrors: Record = {}, +) { + test(description, async () => { + const project = new Project({}, KNOWN_IMPORTS); + const entryPointPath = p.resolve(entryPoint); + const result = await project.parseEntryPoint(entryPointPath); + + if (E.isLeft(result)) { + } + + for (const path of Object.keys(expected)) { + const resolvedPath = p.resolve(path); + const sourceFile = project.get(resolvedPath); + + if (sourceFile === undefined) { + throw new Error(`Source file ${path} not found`); + } + + const actual: Record = {}; + const errors: string[] = []; + for (const symbol of sourceFile.symbols.declarations) { + if (symbol.init !== undefined) { + const result = parsePlainInitializer(project, sourceFile, symbol.init); + if (E.isLeft(result)) { + errors.push(result.left); + } else { + if (symbol.comment !== undefined) { + result.right.comment = symbol.comment; + } + actual[symbol.name] = result.right; + } + } + } + + assert.deepEqual(actual, expected[path]); + assert.deepEqual(errors, expectedErrors[path] ?? []); + } + + // If we are expecting errors in a file that wasn't parsed, raise that here + for (const path of Object.keys(expectedErrors)) { + const resolvedPath = p.resolve(path); + assert.notEqual( + project.get(resolvedPath), + undefined, + `Expected errors for ${path} but it wasn't parsed`, + ); + } + }); +} + +const FoobarObject: Schema = { + type: 'object', + properties: { + foo: { + type: 'string', + }, + bar: { + type: 'number', + }, + }, + required: ['foo', 'bar'], +}; + +const RandomTypeObject: Schema = { + type: 'object', + properties: { + random: { + type: 'string', + }, + type: { + type: 'number', + }, + }, + required: ['random', 'type'], +}; + +const FOO_Object = (testNum: number): Schema => ({ + type: 'object', + properties: { + foobar: { + location: '@bitgo/foobar' + testNum, + name: 'Foobar', + type: 'ref', + }, + }, + required: ['foobar'], +}); + +const RANDOM_Object = (testNum: number): Schema => ({ + type: 'object', + properties: { + random: { + location: '@bitgo/random-types' + testNum, + name: 'RandomType', + type: 'ref', + }, + }, + required: ['random'], +}); + +testCase( + 'type from correctly formatted external library with export declaration', + 'test/projects/export-declaration/index.ts', + { + 'test/projects/export-declaration/index.ts': { + FOO: FOO_Object(1), + RANDOM: RANDOM_Object(1), + }, + 'test/projects/export-declaration/node_modules/@bitgo/foobar1/src/index.ts': { + Foobar: FoobarObject, + }, + 'test/projects/export-declaration/node_modules/@bitgo/random-types1/src/index.ts': { + RandomType: RandomTypeObject, + }, + }, +); + +testCase( + 'type from correctly formatted external library with export star declaration', + 'test/projects/export-star/index.ts', + { + 'test/projects/export-star/index.ts': { + FOO: FOO_Object(2), + RANDOM: RANDOM_Object(2), + }, + 'test/projects/export-star/node_modules/@bitgo/foobar2/src/foobar.ts': { + Foobar: FoobarObject, + }, + 'test/projects/export-star/node_modules/@bitgo/random-types2/src/randomType.ts': { + RandomType: RandomTypeObject, + }, + }, +); + +testCase( + 'type from correctly formatted external library with export star declaration and import star declaration', + 'test/projects/import-star/index.ts', + { + 'test/projects/import-star/index.ts': { + FOO: FOO_Object(4), + RANDOM: RANDOM_Object(4), + }, + 'test/projects/import-star/node_modules/@bitgo/foobar4/src/foobar.ts': { + Foobar: FoobarObject, + }, + 'test/projects/import-star/node_modules/@bitgo/random-types4/src/randomType.ts': { + RandomType: RandomTypeObject, + }, + }, +); + +testCase( + 'type from external library with syntax errors', + 'test/projects/syntax-error/index.ts', + { + 'test/projects/syntax-error/index.ts': { + FOO: FOO_Object(5), + }, + 'test/projects/syntax-error/node_modules/@bitgo/foobar5/src/foobar.ts': {}, + }, + { + 'test/projects/syntax-error/node_modules/@bitgo/foobar5/src/foobar.ts': [ + 'Unknown identifier ttype', + ], + }, +); + +test('type from external library with import path error', async () => { + const entryPoint = 'test/projects/import-path-error/index.ts'; + const project = new Project({}, KNOWN_IMPORTS); + const entryPointPath = p.resolve(entryPoint); + const result = await project.parseEntryPoint(entryPointPath); + + const errorRegex = + /Could not resolve .* from .*\/api-ts\/packages\/openapi-generator\/test\/projects\/import-path-error\/node_modules\/@bitgo\/foobar3\/src/; + + assert(E.isLeft(result)); + assert(errorRegex.test(result.left)); +}); + +// testCase( +// 'type from external library with syntax errors', +// 'test/projects/bad-external-library/index.ts', +// { +// 'test/projects/bad-external-library/index.ts': { +// FOO: { +// type: 'object', +// properties: { +// foobar: { +// location: 'foobar', +// name: 'Foobar', +// type: 'ref', +// }, +// }, +// required: ['foobar'], +// }, +// }, +// }, +// {}, +// ); diff --git a/packages/openapi-generator/test/projects/.gitignore b/packages/openapi-generator/test/projects/.gitignore new file mode 100644 index 00000000..efdac649 --- /dev/null +++ b/packages/openapi-generator/test/projects/.gitignore @@ -0,0 +1,2 @@ +!node_modules +!dist \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-declaration/index.ts b/packages/openapi-generator/test/projects/export-declaration/index.ts new file mode 100644 index 00000000..998957c4 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-declaration/index.ts @@ -0,0 +1,10 @@ +import { Foobar } from '@bitgo/foobar1'; +import { RandomType } from '@bitgo/random-types1'; + +export const FOO = { + foobar: Foobar, +}; + +export const RANDOM = { + random: RandomType, +}; diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js.map b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js.map new file mode 100644 index 00000000..a84c1a88 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js.map @@ -0,0 +1,3 @@ +{ + "sources": ["../../src/index.ts"] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/package.json b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/package.json new file mode 100644 index 00000000..7cdf2f85 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/package.json @@ -0,0 +1,16 @@ +{ + "name": "foobar1", + "version": "0.0.1", + "main": "dist/src/index.js", + "types": "src/index.ts", + "files": [ + "dist/src/**/*", + "src/**/*" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/src/index.ts b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/src/index.ts new file mode 100644 index 00000000..2f9b87a5 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/src/index.ts @@ -0,0 +1,6 @@ +import * as t from 'io-ts' + +export const Foobar = t.type({ + foo: t.string, + bar: t.number, +}); \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/tsconfig.json b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/tsconfig.json new file mode 100644 index 00000000..6673fe4e --- /dev/null +++ b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "outDir": "dist" + }, + "references": [] + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js.map b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js.map new file mode 100644 index 00000000..a84c1a88 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js.map @@ -0,0 +1,3 @@ +{ + "sources": ["../../src/index.ts"] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/package.json b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/package.json new file mode 100644 index 00000000..88826e9e --- /dev/null +++ b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/package.json @@ -0,0 +1,16 @@ +{ + "name": "random-types1", + "version": "0.0.1", + "main": "dist/src/index.js", + "types": "src/index.ts", + "files": [ + "dist/src/**/*", + "src/**/*" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/src/index.ts b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/src/index.ts new file mode 100644 index 00000000..946e37fd --- /dev/null +++ b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/src/index.ts @@ -0,0 +1,6 @@ +import * as t from 'io-ts' + +export const RandomType = t.type({ + random: t.string, + type: t.number, +}); \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/tsconfig.json b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/tsconfig.json new file mode 100644 index 00000000..6673fe4e --- /dev/null +++ b/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "outDir": "dist" + }, + "references": [] + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-declaration/package.json b/packages/openapi-generator/test/projects/export-declaration/package.json new file mode 100644 index 00000000..b2d73a37 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-declaration/package.json @@ -0,0 +1,11 @@ +{ + "name": "export-declaration", + "version": "0.0.1", + "main": "index.ts", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" +} diff --git a/packages/openapi-generator/test/projects/export-star/index.ts b/packages/openapi-generator/test/projects/export-star/index.ts new file mode 100644 index 00000000..e8facaac --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/index.ts @@ -0,0 +1,10 @@ +import { Foobar } from '@bitgo/foobar2'; +import { RandomType } from '@bitgo/random-types2'; + +export const FOO = { + foobar: Foobar, +}; + +export const RANDOM = { + random: RandomType, +}; diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js.map b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js.map new file mode 100644 index 00000000..a84c1a88 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js.map @@ -0,0 +1,3 @@ +{ + "sources": ["../../src/index.ts"] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/package.json b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/package.json new file mode 100644 index 00000000..4f4bea28 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/package.json @@ -0,0 +1,16 @@ +{ + "name": "foobar2", + "version": "0.0.1", + "main": "dist/src/index.js", + "types": "src/index.ts", + "files": [ + "dist/src/**/*", + "src/**/*" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/foobar.ts b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/foobar.ts new file mode 100644 index 00000000..2f9b87a5 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/foobar.ts @@ -0,0 +1,6 @@ +import * as t from 'io-ts' + +export const Foobar = t.type({ + foo: t.string, + bar: t.number, +}); \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/index.ts b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/index.ts new file mode 100644 index 00000000..6a7fa2af --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/index.ts @@ -0,0 +1 @@ +export * from './foobar'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/tsconfig.json b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/tsconfig.json new file mode 100644 index 00000000..6673fe4e --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "outDir": "dist" + }, + "references": [] + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js.map b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js.map new file mode 100644 index 00000000..a84c1a88 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js.map @@ -0,0 +1,3 @@ +{ + "sources": ["../../src/index.ts"] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/package.json b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/package.json new file mode 100644 index 00000000..d825ae69 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/package.json @@ -0,0 +1,16 @@ +{ + "name": "random-types2", + "version": "0.0.1", + "main": "dist/src/index.js", + "types": "src/index.ts", + "files": [ + "dist/src/**/*", + "src/**/*" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/index.ts b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/index.ts new file mode 100644 index 00000000..6864c6a4 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/index.ts @@ -0,0 +1 @@ +export * from './randomType'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/randomType.ts b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/randomType.ts new file mode 100644 index 00000000..946e37fd --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/randomType.ts @@ -0,0 +1,6 @@ +import * as t from 'io-ts' + +export const RandomType = t.type({ + random: t.string, + type: t.number, +}); \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/tsconfig.json b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/tsconfig.json new file mode 100644 index 00000000..6673fe4e --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "outDir": "dist" + }, + "references": [] + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/package.json b/packages/openapi-generator/test/projects/export-star/package.json new file mode 100644 index 00000000..bcc80cf6 --- /dev/null +++ b/packages/openapi-generator/test/projects/export-star/package.json @@ -0,0 +1,11 @@ +{ + "name": "export-star", + "version": "0.0.1", + "main": "index.ts", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" +} diff --git a/packages/openapi-generator/test/projects/import-path-error/index.ts b/packages/openapi-generator/test/projects/import-path-error/index.ts new file mode 100644 index 00000000..9a73f99a --- /dev/null +++ b/packages/openapi-generator/test/projects/import-path-error/index.ts @@ -0,0 +1,5 @@ +import * as f from '@bitgo/foobar3'; + +export const FOO = { + foobar: f.Foobar, +}; diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js.map b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js.map new file mode 100644 index 00000000..a84c1a88 --- /dev/null +++ b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js.map @@ -0,0 +1,3 @@ +{ + "sources": ["../../src/index.ts"] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/package.json b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/package.json new file mode 100644 index 00000000..f4fa0295 --- /dev/null +++ b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/package.json @@ -0,0 +1,16 @@ +{ + "name": "foobar3", + "version": "0.0.1", + "main": "dist/src/index.js", + "types": "src/index.ts", + "files": [ + "dist/src/**/*", + "src/**/*" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/foobar.ts b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/foobar.ts new file mode 100644 index 00000000..ba9b1711 --- /dev/null +++ b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/foobar.ts @@ -0,0 +1,6 @@ +import * as t from 'io-tsg' + +export const Foobar = t.type({ + foo: t.string, + bar: t.number, +}); \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/index.ts b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/index.ts new file mode 100644 index 00000000..6a7fa2af --- /dev/null +++ b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/index.ts @@ -0,0 +1 @@ +export * from './foobar'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/tsconfig.json b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/tsconfig.json new file mode 100644 index 00000000..128699bc --- /dev/null +++ b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "outDir": "dist" + }, + "references": [] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-path-error/package.json b/packages/openapi-generator/test/projects/import-path-error/package.json new file mode 100644 index 00000000..a025ddcd --- /dev/null +++ b/packages/openapi-generator/test/projects/import-path-error/package.json @@ -0,0 +1,11 @@ +{ + "name": "import-path-error", + "version": "0.0.1", + "main": "index.ts", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" +} diff --git a/packages/openapi-generator/test/projects/import-star/index.ts b/packages/openapi-generator/test/projects/import-star/index.ts new file mode 100644 index 00000000..07db6e1b --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/index.ts @@ -0,0 +1,10 @@ +import * as f from '@bitgo/foobar4'; +import * as r from '@bitgo/random-types4'; + +export const FOO = { + foobar: f.Foobar, +}; + +export const RANDOM = { + random: r.RandomType, +}; diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js.map b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js.map new file mode 100644 index 00000000..a84c1a88 --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js.map @@ -0,0 +1,3 @@ +{ + "sources": ["../../src/index.ts"] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/package.json b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/package.json new file mode 100644 index 00000000..a74e6ae1 --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/package.json @@ -0,0 +1,16 @@ +{ + "name": "foobar4", + "version": "0.0.1", + "main": "dist/src/index.js", + "types": "src/index.ts", + "files": [ + "dist/src/**/*", + "src/**/*" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/foobar.ts b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/foobar.ts new file mode 100644 index 00000000..2f9b87a5 --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/foobar.ts @@ -0,0 +1,6 @@ +import * as t from 'io-ts' + +export const Foobar = t.type({ + foo: t.string, + bar: t.number, +}); \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/index.ts b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/index.ts new file mode 100644 index 00000000..6a7fa2af --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/index.ts @@ -0,0 +1 @@ +export * from './foobar'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/tsconfig.json b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/tsconfig.json new file mode 100644 index 00000000..128699bc --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "outDir": "dist" + }, + "references": [] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js.map b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js.map new file mode 100644 index 00000000..a84c1a88 --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js.map @@ -0,0 +1,3 @@ +{ + "sources": ["../../src/index.ts"] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/package.json b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/package.json new file mode 100644 index 00000000..d8d3ae81 --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/package.json @@ -0,0 +1,16 @@ +{ + "name": "random-types4", + "version": "0.0.1", + "main": "dist/src/index.js", + "types": "src/index.ts", + "files": [ + "dist/src/**/*", + "src/**/*" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/index.ts b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/index.ts new file mode 100644 index 00000000..6864c6a4 --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/index.ts @@ -0,0 +1 @@ +export * from './randomType'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/randomType.ts b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/randomType.ts new file mode 100644 index 00000000..946e37fd --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/randomType.ts @@ -0,0 +1,6 @@ +import * as t from 'io-ts' + +export const RandomType = t.type({ + random: t.string, + type: t.number, +}); \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/tsconfig.json b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/tsconfig.json new file mode 100644 index 00000000..6673fe4e --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "outDir": "dist" + }, + "references": [] + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/package.json b/packages/openapi-generator/test/projects/import-star/package.json new file mode 100644 index 00000000..202fe891 --- /dev/null +++ b/packages/openapi-generator/test/projects/import-star/package.json @@ -0,0 +1,11 @@ +{ + "name": "import-star", + "version": "0.0.1", + "main": "index.ts", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" +} diff --git a/packages/openapi-generator/test/projects/syntax-error/index.ts b/packages/openapi-generator/test/projects/syntax-error/index.ts new file mode 100644 index 00000000..4703e359 --- /dev/null +++ b/packages/openapi-generator/test/projects/syntax-error/index.ts @@ -0,0 +1,5 @@ +import * as f from '@bitgo/foobar5'; + +export const FOO = { + foobar: f.Foobar, +}; diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js.map b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js.map new file mode 100644 index 00000000..a84c1a88 --- /dev/null +++ b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js.map @@ -0,0 +1,3 @@ +{ + "sources": ["../../src/index.ts"] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/package.json b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/package.json new file mode 100644 index 00000000..07ab5586 --- /dev/null +++ b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/package.json @@ -0,0 +1,16 @@ +{ + "name": "foobar5", + "version": "0.0.1", + "main": "dist/src/index.js", + "types": "src/index.ts", + "files": [ + "dist/src/**/*", + "src/**/*" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" + } \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/foobar.ts b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/foobar.ts new file mode 100644 index 00000000..935f3328 --- /dev/null +++ b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/foobar.ts @@ -0,0 +1,6 @@ +import * as t from 'io-ts' + +export const Foobar = ttype({ + foo: t.string, + bar: t.number, +}); \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/index.ts b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/index.ts new file mode 100644 index 00000000..6a7fa2af --- /dev/null +++ b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/index.ts @@ -0,0 +1 @@ +export * from './foobar'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/tsconfig.json b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/tsconfig.json new file mode 100644 index 00000000..128699bc --- /dev/null +++ b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "outDir": "dist" + }, + "references": [] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/syntax-error/package.json b/packages/openapi-generator/test/projects/syntax-error/package.json new file mode 100644 index 00000000..22695b1c --- /dev/null +++ b/packages/openapi-generator/test/projects/syntax-error/package.json @@ -0,0 +1,11 @@ +{ + "name": "syntax-error", + "version": "0.0.1", + "main": "index.ts", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" +} diff --git a/packages/openapi-generator/tsconfig.json b/packages/openapi-generator/tsconfig.json index c1d8b5bb..734b316c 100644 --- a/packages/openapi-generator/tsconfig.json +++ b/packages/openapi-generator/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "../../tsconfig.json", "include": ["src/**/*.ts", "test/**/*.json", "test/**/*.ts"], + "exclude": ["test/projects/**/*"], "compilerOptions": { "outDir": "dist" }, From 8d508b31b4bdde74dee3feb56b9a0051b760360c Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Fri, 17 May 2024 09:15:02 -0400 Subject: [PATCH 09/19] refactor(openapi-generator): use unknown instead if any in project.js catch blocks --- packages/openapi-generator/src/project.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/openapi-generator/src/project.ts b/packages/openapi-generator/src/project.ts index 2d47ad40..96a8fe16 100644 --- a/packages/openapi-generator/src/project.ts +++ b/packages/openapi-generator/src/project.ts @@ -88,8 +88,8 @@ export class Project { }); return E.right(result); - } catch (e: any) { - if (typeof e === 'object' && e.hasOwnProperty('message')) { + } catch (e: unknown) { + if (e instanceof Error && e.message) { return E.left(e.message); } @@ -140,12 +140,12 @@ export class Project { return E.right(result); } - } catch (e: any) { - if (typeof e === 'object' && e.hasOwnProperty('message')) { + } catch (e: unknown) { + if (e instanceof Error && e.message) { return E.left(e.message); - } else { - return E.left(JSON.stringify(e)); } + + return E.left(JSON.stringify(e)); } } From e208234a045828e90a6c2fd5bd708d431595e615 Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Fri, 17 May 2024 10:58:41 -0400 Subject: [PATCH 10/19] refactor: merge test node_modules into one --- .prettierignore | 1 - package-lock.json | 2 +- packages/openapi-generator/package.json | 1 + .../test/externalModule.test.ts | 62 ++++++------------- .../projects/export-declaration/package.json | 11 ---- .../node_modules/@bitgo/foobar2/src/index.ts | 1 - .../@bitgo/random-types2/src/index.ts | 1 - .../test/projects/export-star/package.json | 11 ---- .../node_modules/@bitgo/foobar3/src/index.ts | 1 - .../projects/import-path-error/package.json | 11 ---- .../node_modules/@bitgo/foobar4/src/index.ts | 1 - .../@bitgo/random-types4/src/index.ts | 1 - .../test/projects/import-star/package.json | 11 ---- .../node_modules/@bitgo/foobar5/src/index.ts | 1 - .../test/projects/syntax-error/package.json | 11 ---- .../{projects => sample-types}/.gitignore | 0 .../exportDeclaration.ts} | 0 .../index.ts => sample-types/exportStar.ts} | 0 .../importPathError.ts} | 0 .../index.ts => sample-types/importStar.ts} | 0 .../@bitgo/foobar1/dist/src/index.js | 0 .../@bitgo/foobar1/dist/src/index.js.map | 0 .../node_modules/@bitgo/foobar1/package.json | 0 .../node_modules/@bitgo/foobar1/src/index.ts | 0 .../node_modules/@bitgo/foobar1/tsconfig.json | 0 .../@bitgo/foobar2}/dist/src/index.js | 0 .../@bitgo/foobar2}/dist/src/index.js.map | 0 .../node_modules/@bitgo/foobar2/package.json | 0 .../node_modules/@bitgo/foobar2/src/foobar.ts | 0 .../node_modules/@bitgo/foobar2/src/index.ts | 1 + .../@bitgo/foobar2}/tsconfig.json | 0 .../@bitgo/foobar3}/dist/src/index.js | 0 .../@bitgo/foobar3}/dist/src/index.js.map | 0 .../node_modules/@bitgo/foobar3/package.json | 0 .../node_modules/@bitgo/foobar3/src/foobar.ts | 0 .../node_modules/@bitgo/foobar3/src/index.ts | 1 + .../node_modules/@bitgo/foobar3/tsconfig.json | 0 .../@bitgo/foobar4}/dist/src/index.js | 0 .../@bitgo/foobar4}/dist/src/index.js.map | 0 .../node_modules/@bitgo/foobar4/package.json | 0 .../node_modules/@bitgo/foobar4/src/foobar.ts | 0 .../node_modules/@bitgo/foobar4/src/index.ts | 1 + .../node_modules/@bitgo/foobar4/tsconfig.json | 0 .../@bitgo/foobar5}/dist/src/index.js | 0 .../@bitgo/foobar5}/dist/src/index.js.map | 0 .../node_modules/@bitgo/foobar5/package.json | 0 .../node_modules/@bitgo/foobar5/src/foobar.ts | 0 .../node_modules/@bitgo/foobar5/src/index.ts | 1 + .../node_modules/@bitgo/foobar5/tsconfig.json | 0 .../@bitgo/random-types1}/dist/src/index.js | 0 .../random-types1}/dist/src/index.js.map | 0 .../@bitgo/random-types1/package.json | 0 .../@bitgo/random-types1/src/index.ts | 0 .../@bitgo/random-types1}/tsconfig.json | 0 .../@bitgo/random-types2}/dist/src/index.js | 0 .../random-types2}/dist/src/index.js.map | 0 .../@bitgo/random-types2/package.json | 0 .../@bitgo/random-types2/src/index.ts | 2 + .../@bitgo/random-types2/src/randomType.ts | 0 .../@bitgo/random-types2/tsconfig.json | 0 .../@bitgo/random-types4}/dist/src/index.js | 0 .../random-types4}/dist/src/index.js.map | 0 .../@bitgo/random-types4/package.json | 0 .../@bitgo/random-types4/src/index.ts | 1 + .../@bitgo/random-types4/src/randomType.ts | 0 .../@bitgo/random-types4/tsconfig.json | 0 .../index.ts => sample-types/syntaxError.ts} | 0 packages/openapi-generator/tsconfig.json | 2 +- 68 files changed, 29 insertions(+), 107 deletions(-) delete mode 100644 packages/openapi-generator/test/projects/export-declaration/package.json delete mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/index.ts delete mode 100644 packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/index.ts delete mode 100644 packages/openapi-generator/test/projects/export-star/package.json delete mode 100644 packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/index.ts delete mode 100644 packages/openapi-generator/test/projects/import-path-error/package.json delete mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/index.ts delete mode 100644 packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/index.ts delete mode 100644 packages/openapi-generator/test/projects/import-star/package.json delete mode 100644 packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/index.ts delete mode 100644 packages/openapi-generator/test/projects/syntax-error/package.json rename packages/openapi-generator/test/{projects => sample-types}/.gitignore (100%) rename packages/openapi-generator/test/{projects/export-declaration/index.ts => sample-types/exportDeclaration.ts} (100%) rename packages/openapi-generator/test/{projects/export-star/index.ts => sample-types/exportStar.ts} (100%) rename packages/openapi-generator/test/{projects/import-path-error/index.ts => sample-types/importPathError.ts} (100%) rename packages/openapi-generator/test/{projects/import-star/index.ts => sample-types/importStar.ts} (100%) rename packages/openapi-generator/test/{projects/export-declaration => sample-types}/node_modules/@bitgo/foobar1/dist/src/index.js (100%) rename packages/openapi-generator/test/{projects/export-declaration => sample-types}/node_modules/@bitgo/foobar1/dist/src/index.js.map (100%) rename packages/openapi-generator/test/{projects/export-declaration => sample-types}/node_modules/@bitgo/foobar1/package.json (100%) rename packages/openapi-generator/test/{projects/export-declaration => sample-types}/node_modules/@bitgo/foobar1/src/index.ts (100%) rename packages/openapi-generator/test/{projects/export-declaration => sample-types}/node_modules/@bitgo/foobar1/tsconfig.json (100%) rename packages/openapi-generator/test/{projects/export-declaration/node_modules/@bitgo/random-types1 => sample-types/node_modules/@bitgo/foobar2}/dist/src/index.js (100%) rename packages/openapi-generator/test/{projects/export-declaration/node_modules/@bitgo/random-types1 => sample-types/node_modules/@bitgo/foobar2}/dist/src/index.js.map (100%) rename packages/openapi-generator/test/{projects/export-star => sample-types}/node_modules/@bitgo/foobar2/package.json (100%) rename packages/openapi-generator/test/{projects/export-star => sample-types}/node_modules/@bitgo/foobar2/src/foobar.ts (100%) create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/src/index.ts rename packages/openapi-generator/test/{projects/export-declaration/node_modules/@bitgo/random-types1 => sample-types/node_modules/@bitgo/foobar2}/tsconfig.json (100%) rename packages/openapi-generator/test/{projects/export-star/node_modules/@bitgo/foobar2 => sample-types/node_modules/@bitgo/foobar3}/dist/src/index.js (100%) rename packages/openapi-generator/test/{projects/export-star/node_modules/@bitgo/foobar2 => sample-types/node_modules/@bitgo/foobar3}/dist/src/index.js.map (100%) rename packages/openapi-generator/test/{projects/import-path-error => sample-types}/node_modules/@bitgo/foobar3/package.json (100%) rename packages/openapi-generator/test/{projects/import-path-error => sample-types}/node_modules/@bitgo/foobar3/src/foobar.ts (100%) create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/src/index.ts rename packages/openapi-generator/test/{projects/import-path-error => sample-types}/node_modules/@bitgo/foobar3/tsconfig.json (100%) rename packages/openapi-generator/test/{projects/export-star/node_modules/@bitgo/random-types2 => sample-types/node_modules/@bitgo/foobar4}/dist/src/index.js (100%) rename packages/openapi-generator/test/{projects/export-star/node_modules/@bitgo/random-types2 => sample-types/node_modules/@bitgo/foobar4}/dist/src/index.js.map (100%) rename packages/openapi-generator/test/{projects/import-star => sample-types}/node_modules/@bitgo/foobar4/package.json (100%) rename packages/openapi-generator/test/{projects/import-star => sample-types}/node_modules/@bitgo/foobar4/src/foobar.ts (100%) create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/src/index.ts rename packages/openapi-generator/test/{projects/import-star => sample-types}/node_modules/@bitgo/foobar4/tsconfig.json (100%) rename packages/openapi-generator/test/{projects/import-path-error/node_modules/@bitgo/foobar3 => sample-types/node_modules/@bitgo/foobar5}/dist/src/index.js (100%) rename packages/openapi-generator/test/{projects/import-path-error/node_modules/@bitgo/foobar3 => sample-types/node_modules/@bitgo/foobar5}/dist/src/index.js.map (100%) rename packages/openapi-generator/test/{projects/syntax-error => sample-types}/node_modules/@bitgo/foobar5/package.json (100%) rename packages/openapi-generator/test/{projects/syntax-error => sample-types}/node_modules/@bitgo/foobar5/src/foobar.ts (100%) create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/src/index.ts rename packages/openapi-generator/test/{projects/syntax-error => sample-types}/node_modules/@bitgo/foobar5/tsconfig.json (100%) rename packages/openapi-generator/test/{projects/import-star/node_modules/@bitgo/foobar4 => sample-types/node_modules/@bitgo/random-types1}/dist/src/index.js (100%) rename packages/openapi-generator/test/{projects/import-star/node_modules/@bitgo/foobar4 => sample-types/node_modules/@bitgo/random-types1}/dist/src/index.js.map (100%) rename packages/openapi-generator/test/{projects/export-declaration => sample-types}/node_modules/@bitgo/random-types1/package.json (100%) rename packages/openapi-generator/test/{projects/export-declaration => sample-types}/node_modules/@bitgo/random-types1/src/index.ts (100%) rename packages/openapi-generator/test/{projects/export-star/node_modules/@bitgo/foobar2 => sample-types/node_modules/@bitgo/random-types1}/tsconfig.json (100%) rename packages/openapi-generator/test/{projects/import-star/node_modules/@bitgo/random-types4 => sample-types/node_modules/@bitgo/random-types2}/dist/src/index.js (100%) rename packages/openapi-generator/test/{projects/import-star/node_modules/@bitgo/random-types4 => sample-types/node_modules/@bitgo/random-types2}/dist/src/index.js.map (100%) rename packages/openapi-generator/test/{projects/export-star => sample-types}/node_modules/@bitgo/random-types2/package.json (100%) create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/src/index.ts rename packages/openapi-generator/test/{projects/export-star => sample-types}/node_modules/@bitgo/random-types2/src/randomType.ts (100%) rename packages/openapi-generator/test/{projects/export-star => sample-types}/node_modules/@bitgo/random-types2/tsconfig.json (100%) rename packages/openapi-generator/test/{projects/syntax-error/node_modules/@bitgo/foobar5 => sample-types/node_modules/@bitgo/random-types4}/dist/src/index.js (100%) rename packages/openapi-generator/test/{projects/syntax-error/node_modules/@bitgo/foobar5 => sample-types/node_modules/@bitgo/random-types4}/dist/src/index.js.map (100%) rename packages/openapi-generator/test/{projects/import-star => sample-types}/node_modules/@bitgo/random-types4/package.json (100%) create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/src/index.ts rename packages/openapi-generator/test/{projects/import-star => sample-types}/node_modules/@bitgo/random-types4/src/randomType.ts (100%) rename packages/openapi-generator/test/{projects/import-star => sample-types}/node_modules/@bitgo/random-types4/tsconfig.json (100%) rename packages/openapi-generator/test/{projects/syntax-error/index.ts => sample-types/syntaxError.ts} (100%) diff --git a/.prettierignore b/.prettierignore index 84af0e71..d27bbf0b 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,4 +1,3 @@ coverage/ dist/ flake.lock -tests/programs/* \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 6591aa4b..8f91fab6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "hasInstallScript": true, "license": "Apache-2.0", "workspaces": [ - "packages/**" + "packages/*" ], "devDependencies": { "@semantic-release-extras/github-comment-specific": "1.0.7", diff --git a/packages/openapi-generator/package.json b/packages/openapi-generator/package.json index b1528427..3f4acb8e 100644 --- a/packages/openapi-generator/package.json +++ b/packages/openapi-generator/package.json @@ -28,6 +28,7 @@ "io-ts": "2.1.3", "io-ts-types": "0.5.19", "openapi-types": "12.1.3", + "prettier": "^3.2.5", "resolve": "1.22.8" }, "devDependencies": { diff --git a/packages/openapi-generator/test/externalModule.test.ts b/packages/openapi-generator/test/externalModule.test.ts index e61071c9..4cac4e05 100644 --- a/packages/openapi-generator/test/externalModule.test.ts +++ b/packages/openapi-generator/test/externalModule.test.ts @@ -22,10 +22,7 @@ async function testCase( test(description, async () => { const project = new Project({}, KNOWN_IMPORTS); const entryPointPath = p.resolve(entryPoint); - const result = await project.parseEntryPoint(entryPointPath); - - if (E.isLeft(result)) { - } + await project.parseEntryPoint(entryPointPath); for (const path of Object.keys(expected)) { const resolvedPath = p.resolve(path); @@ -119,16 +116,16 @@ const RANDOM_Object = (testNum: number): Schema => ({ testCase( 'type from correctly formatted external library with export declaration', - 'test/projects/export-declaration/index.ts', + 'test/sample-types/exportDeclaration.ts', { - 'test/projects/export-declaration/index.ts': { + 'test/sample-types/exportDeclaration.ts': { FOO: FOO_Object(1), RANDOM: RANDOM_Object(1), }, - 'test/projects/export-declaration/node_modules/@bitgo/foobar1/src/index.ts': { + 'test/sample-types/node_modules/@bitgo/foobar1/src/index.ts': { Foobar: FoobarObject, }, - 'test/projects/export-declaration/node_modules/@bitgo/random-types1/src/index.ts': { + 'test/sample-types/node_modules/@bitgo/random-types1/src/index.ts': { RandomType: RandomTypeObject, }, }, @@ -136,16 +133,16 @@ testCase( testCase( 'type from correctly formatted external library with export star declaration', - 'test/projects/export-star/index.ts', + 'test/sample-types/exportStar.ts', { - 'test/projects/export-star/index.ts': { + 'test/sample-types/exportStar.ts': { FOO: FOO_Object(2), RANDOM: RANDOM_Object(2), }, - 'test/projects/export-star/node_modules/@bitgo/foobar2/src/foobar.ts': { + 'test/sample-types/node_modules/@bitgo/foobar2/src/foobar.ts': { Foobar: FoobarObject, }, - 'test/projects/export-star/node_modules/@bitgo/random-types2/src/randomType.ts': { + 'test/sample-types/node_modules/@bitgo/random-types2/src/randomType.ts': { RandomType: RandomTypeObject, }, }, @@ -153,16 +150,16 @@ testCase( testCase( 'type from correctly formatted external library with export star declaration and import star declaration', - 'test/projects/import-star/index.ts', + 'test/sample-types/importStar.ts', { - 'test/projects/import-star/index.ts': { + 'test/sample-types/importStar.ts': { FOO: FOO_Object(4), RANDOM: RANDOM_Object(4), }, - 'test/projects/import-star/node_modules/@bitgo/foobar4/src/foobar.ts': { + 'test/sample-types/node_modules/@bitgo/foobar4/src/foobar.ts': { Foobar: FoobarObject, }, - 'test/projects/import-star/node_modules/@bitgo/random-types4/src/randomType.ts': { + 'test/sample-types/node_modules/@bitgo/random-types4/src/randomType.ts': { RandomType: RandomTypeObject, }, }, @@ -170,50 +167,29 @@ testCase( testCase( 'type from external library with syntax errors', - 'test/projects/syntax-error/index.ts', + 'test/sample-types/syntaxError.ts', { - 'test/projects/syntax-error/index.ts': { + 'test/sample-types/syntaxError.ts': { FOO: FOO_Object(5), }, - 'test/projects/syntax-error/node_modules/@bitgo/foobar5/src/foobar.ts': {}, + 'test/sample-types/node_modules/@bitgo/foobar5/src/foobar.ts': {}, }, { - 'test/projects/syntax-error/node_modules/@bitgo/foobar5/src/foobar.ts': [ + 'test/sample-types/node_modules/@bitgo/foobar5/src/foobar.ts': [ 'Unknown identifier ttype', ], }, ); test('type from external library with import path error', async () => { - const entryPoint = 'test/projects/import-path-error/index.ts'; + const entryPoint = 'test/sample-types/importPathError.ts'; const project = new Project({}, KNOWN_IMPORTS); const entryPointPath = p.resolve(entryPoint); const result = await project.parseEntryPoint(entryPointPath); const errorRegex = - /Could not resolve .* from .*\/api-ts\/packages\/openapi-generator\/test\/projects\/import-path-error\/node_modules\/@bitgo\/foobar3\/src/; + /Could not resolve io-tsg from .*\/test\/sample-types\/node_modules\/@bitgo\/foobar3\/src/; assert(E.isLeft(result)); assert(errorRegex.test(result.left)); }); - -// testCase( -// 'type from external library with syntax errors', -// 'test/projects/bad-external-library/index.ts', -// { -// 'test/projects/bad-external-library/index.ts': { -// FOO: { -// type: 'object', -// properties: { -// foobar: { -// location: 'foobar', -// name: 'Foobar', -// type: 'ref', -// }, -// }, -// required: ['foobar'], -// }, -// }, -// }, -// {}, -// ); diff --git a/packages/openapi-generator/test/projects/export-declaration/package.json b/packages/openapi-generator/test/projects/export-declaration/package.json deleted file mode 100644 index b2d73a37..00000000 --- a/packages/openapi-generator/test/projects/export-declaration/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "export-declaration", - "version": "0.0.1", - "main": "index.ts", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "description": "" -} diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/index.ts b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/index.ts deleted file mode 100644 index 6a7fa2af..00000000 --- a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './foobar'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/index.ts b/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/index.ts deleted file mode 100644 index 6864c6a4..00000000 --- a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './randomType'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/export-star/package.json b/packages/openapi-generator/test/projects/export-star/package.json deleted file mode 100644 index bcc80cf6..00000000 --- a/packages/openapi-generator/test/projects/export-star/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "export-star", - "version": "0.0.1", - "main": "index.ts", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "description": "" -} diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/index.ts b/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/index.ts deleted file mode 100644 index 6a7fa2af..00000000 --- a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './foobar'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-path-error/package.json b/packages/openapi-generator/test/projects/import-path-error/package.json deleted file mode 100644 index a025ddcd..00000000 --- a/packages/openapi-generator/test/projects/import-path-error/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "import-path-error", - "version": "0.0.1", - "main": "index.ts", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "description": "" -} diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/index.ts b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/index.ts deleted file mode 100644 index 6a7fa2af..00000000 --- a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './foobar'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/index.ts b/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/index.ts deleted file mode 100644 index 6864c6a4..00000000 --- a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './randomType'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/import-star/package.json b/packages/openapi-generator/test/projects/import-star/package.json deleted file mode 100644 index 202fe891..00000000 --- a/packages/openapi-generator/test/projects/import-star/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "import-star", - "version": "0.0.1", - "main": "index.ts", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "description": "" -} diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/index.ts b/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/index.ts deleted file mode 100644 index 6a7fa2af..00000000 --- a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './foobar'; \ No newline at end of file diff --git a/packages/openapi-generator/test/projects/syntax-error/package.json b/packages/openapi-generator/test/projects/syntax-error/package.json deleted file mode 100644 index 22695b1c..00000000 --- a/packages/openapi-generator/test/projects/syntax-error/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "syntax-error", - "version": "0.0.1", - "main": "index.ts", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "description": "" -} diff --git a/packages/openapi-generator/test/projects/.gitignore b/packages/openapi-generator/test/sample-types/.gitignore similarity index 100% rename from packages/openapi-generator/test/projects/.gitignore rename to packages/openapi-generator/test/sample-types/.gitignore diff --git a/packages/openapi-generator/test/projects/export-declaration/index.ts b/packages/openapi-generator/test/sample-types/exportDeclaration.ts similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/index.ts rename to packages/openapi-generator/test/sample-types/exportDeclaration.ts diff --git a/packages/openapi-generator/test/projects/export-star/index.ts b/packages/openapi-generator/test/sample-types/exportStar.ts similarity index 100% rename from packages/openapi-generator/test/projects/export-star/index.ts rename to packages/openapi-generator/test/sample-types/exportStar.ts diff --git a/packages/openapi-generator/test/projects/import-path-error/index.ts b/packages/openapi-generator/test/sample-types/importPathError.ts similarity index 100% rename from packages/openapi-generator/test/projects/import-path-error/index.ts rename to packages/openapi-generator/test/sample-types/importPathError.ts diff --git a/packages/openapi-generator/test/projects/import-star/index.ts b/packages/openapi-generator/test/sample-types/importStar.ts similarity index 100% rename from packages/openapi-generator/test/projects/import-star/index.ts rename to packages/openapi-generator/test/sample-types/importStar.ts diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar1/dist/src/index.js similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar1/dist/src/index.js diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js.map b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar1/dist/src/index.js.map similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/dist/src/index.js.map rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar1/dist/src/index.js.map diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/package.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar1/package.json similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/package.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar1/package.json diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar1/src/index.ts similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/src/index.ts rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar1/src/index.ts diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/tsconfig.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar1/tsconfig.json similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/foobar1/tsconfig.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar1/tsconfig.json diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/dist/src/index.js similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/dist/src/index.js diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js.map b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/dist/src/index.js.map similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/dist/src/index.js.map rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/dist/src/index.js.map diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/package.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/package.json similarity index 100% rename from packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/package.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/package.json diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/foobar.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/src/foobar.ts similarity index 100% rename from packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/src/foobar.ts rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/src/foobar.ts diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/src/index.ts new file mode 100644 index 00000000..8c0467ec --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/src/index.ts @@ -0,0 +1 @@ +export * from './foobar'; diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/tsconfig.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/tsconfig.json similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/tsconfig.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar2/tsconfig.json diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/dist/src/index.js similarity index 100% rename from packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/dist/src/index.js diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js.map b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/dist/src/index.js.map similarity index 100% rename from packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/dist/src/index.js.map rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/dist/src/index.js.map diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/package.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/package.json similarity index 100% rename from packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/package.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/package.json diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/foobar.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/src/foobar.ts similarity index 100% rename from packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/src/foobar.ts rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/src/foobar.ts diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/src/index.ts new file mode 100644 index 00000000..8c0467ec --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/src/index.ts @@ -0,0 +1 @@ +export * from './foobar'; diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/tsconfig.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/tsconfig.json similarity index 100% rename from packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/tsconfig.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/tsconfig.json diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/dist/src/index.js similarity index 100% rename from packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/dist/src/index.js diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js.map b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/dist/src/index.js.map similarity index 100% rename from packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/dist/src/index.js.map rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/dist/src/index.js.map diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/package.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/package.json similarity index 100% rename from packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/package.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/package.json diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/foobar.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/src/foobar.ts similarity index 100% rename from packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/src/foobar.ts rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/src/foobar.ts diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/src/index.ts new file mode 100644 index 00000000..8c0467ec --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/src/index.ts @@ -0,0 +1 @@ +export * from './foobar'; diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/tsconfig.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/tsconfig.json similarity index 100% rename from packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/tsconfig.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar4/tsconfig.json diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/dist/src/index.js similarity index 100% rename from packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/dist/src/index.js diff --git a/packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js.map b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/dist/src/index.js.map similarity index 100% rename from packages/openapi-generator/test/projects/import-path-error/node_modules/@bitgo/foobar3/dist/src/index.js.map rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/dist/src/index.js.map diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/package.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/package.json similarity index 100% rename from packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/package.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/package.json diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/foobar.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/src/foobar.ts similarity index 100% rename from packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/src/foobar.ts rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/src/foobar.ts diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/src/index.ts new file mode 100644 index 00000000..8c0467ec --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/src/index.ts @@ -0,0 +1 @@ +export * from './foobar'; diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/tsconfig.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/tsconfig.json similarity index 100% rename from packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/tsconfig.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar5/tsconfig.json diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types1/dist/src/index.js similarity index 100% rename from packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types1/dist/src/index.js diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js.map b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types1/dist/src/index.js.map similarity index 100% rename from packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/foobar4/dist/src/index.js.map rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types1/dist/src/index.js.map diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/package.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types1/package.json similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/package.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types1/package.json diff --git a/packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types1/src/index.ts similarity index 100% rename from packages/openapi-generator/test/projects/export-declaration/node_modules/@bitgo/random-types1/src/index.ts rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types1/src/index.ts diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/tsconfig.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types1/tsconfig.json similarity index 100% rename from packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/foobar2/tsconfig.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types1/tsconfig.json diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/dist/src/index.js similarity index 100% rename from packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/dist/src/index.js diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js.map b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/dist/src/index.js.map similarity index 100% rename from packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/dist/src/index.js.map rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/dist/src/index.js.map diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/package.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/package.json similarity index 100% rename from packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/package.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/package.json diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/src/index.ts new file mode 100644 index 00000000..586e0257 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/src/index.ts @@ -0,0 +1,2 @@ +export * from './randomType'; + diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/randomType.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/src/randomType.ts similarity index 100% rename from packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/src/randomType.ts rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/src/randomType.ts diff --git a/packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/tsconfig.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/tsconfig.json similarity index 100% rename from packages/openapi-generator/test/projects/export-star/node_modules/@bitgo/random-types2/tsconfig.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types2/tsconfig.json diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/dist/src/index.js similarity index 100% rename from packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/dist/src/index.js diff --git a/packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js.map b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/dist/src/index.js.map similarity index 100% rename from packages/openapi-generator/test/projects/syntax-error/node_modules/@bitgo/foobar5/dist/src/index.js.map rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/dist/src/index.js.map diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/package.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/package.json similarity index 100% rename from packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/package.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/package.json diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/src/index.ts new file mode 100644 index 00000000..ed5c4aec --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/src/index.ts @@ -0,0 +1 @@ +export * from './randomType'; diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/randomType.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/src/randomType.ts similarity index 100% rename from packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/src/randomType.ts rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/src/randomType.ts diff --git a/packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/tsconfig.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/tsconfig.json similarity index 100% rename from packages/openapi-generator/test/projects/import-star/node_modules/@bitgo/random-types4/tsconfig.json rename to packages/openapi-generator/test/sample-types/node_modules/@bitgo/random-types4/tsconfig.json diff --git a/packages/openapi-generator/test/projects/syntax-error/index.ts b/packages/openapi-generator/test/sample-types/syntaxError.ts similarity index 100% rename from packages/openapi-generator/test/projects/syntax-error/index.ts rename to packages/openapi-generator/test/sample-types/syntaxError.ts diff --git a/packages/openapi-generator/tsconfig.json b/packages/openapi-generator/tsconfig.json index 734b316c..b05a10f9 100644 --- a/packages/openapi-generator/tsconfig.json +++ b/packages/openapi-generator/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "include": ["src/**/*.ts", "test/**/*.json", "test/**/*.ts"], - "exclude": ["test/projects/**/*"], + // "exclude": ["test/sample-types/**/*"], "compilerOptions": { "outDir": "dist" }, From 96c911e4e2d541bb6edc8742819446e9d956650b Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Fri, 17 May 2024 11:09:10 -0400 Subject: [PATCH 11/19] refactor: assert error message when file cannot be parsed --- packages/openapi-generator/src/sourceFile.ts | 2 +- packages/openapi-generator/test/project.test.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/openapi-generator/src/sourceFile.ts b/packages/openapi-generator/src/sourceFile.ts index bd12baba..3b375c16 100644 --- a/packages/openapi-generator/src/sourceFile.ts +++ b/packages/openapi-generator/src/sourceFile.ts @@ -38,7 +38,7 @@ export async function parseSource( span: module.span, }; } catch (e: unknown) { - console.error('Error parsing source file: ', path, e); + console.error(`Error parsing source file: ${path}`, e); return undefined; } } diff --git a/packages/openapi-generator/test/project.test.ts b/packages/openapi-generator/test/project.test.ts index 02ef8cdd..60bebb2a 100644 --- a/packages/openapi-generator/test/project.test.ts +++ b/packages/openapi-generator/test/project.test.ts @@ -107,7 +107,9 @@ test('non-strict files are ignored and logged to stderr', async () => { console.error = (...args) => { errorCalled = true; - originalConsoleError(...args); + console.error = originalConsoleError; + const errorRegex = /Error parsing source file: \/index.ts/; + assert(errorRegex.test(args[0])); }; const project = new TestProject({ '/index.ts': NON_STRICT_MODE_SRC }, {}); @@ -116,6 +118,4 @@ test('non-strict files are ignored and logged to stderr', async () => { assert.strictEqual(sourceFile, undefined); assert.strictEqual(errorCalled, true, new Error('console.error was not called')); - - console.error = originalConsoleError; }); From c2b02b49c6f971290f07d4acf5dfdd52e0c4ea95 Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Fri, 17 May 2024 11:18:09 -0400 Subject: [PATCH 12/19] fix: remove prettier from `package.json` --- packages/openapi-generator/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/openapi-generator/package.json b/packages/openapi-generator/package.json index 3f4acb8e..b1528427 100644 --- a/packages/openapi-generator/package.json +++ b/packages/openapi-generator/package.json @@ -28,7 +28,6 @@ "io-ts": "2.1.3", "io-ts-types": "0.5.19", "openapi-types": "12.1.3", - "prettier": "^3.2.5", "resolve": "1.22.8" }, "devDependencies": { From 6aea62aed7a6333e77f9f1e63f4d99ed30403e69 Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Fri, 17 May 2024 11:20:38 -0400 Subject: [PATCH 13/19] fix: exclude `sample-types` type package from build --- packages/openapi-generator/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-generator/tsconfig.json b/packages/openapi-generator/tsconfig.json index b05a10f9..42beba8f 100644 --- a/packages/openapi-generator/tsconfig.json +++ b/packages/openapi-generator/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "include": ["src/**/*.ts", "test/**/*.json", "test/**/*.ts"], - // "exclude": ["test/sample-types/**/*"], + "exclude": ["test/sample-types/**/*"], "compilerOptions": { "outDir": "dist" }, From 3ffdd120b75ec4bc1e286e35fb1958d49244536b Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Fri, 17 May 2024 15:35:05 -0400 Subject: [PATCH 14/19] test: add e2e tests for api spec generation from imported types --- .prettierignore | 1 + .../openapi-generator/test/.prettierignore | 1 + .../test/externalModule.test.ts | 34 ++- .../test/externalModuleApiSpec.test.ts | 273 ++++++++++++++++++ .../test/sample-types/apiSpec.ts | 28 ++ .../test/sample-types/apiSpecWithEnum.ts | 15 + .../test/sample-types/exportPathError.ts | 5 + .../@bitgo/foobar6/dist/src/index.js | 0 .../@bitgo/foobar6/dist/src/index.js.map | 3 + .../node_modules/@bitgo/foobar6/package.json | 16 + .../node_modules/@bitgo/foobar6/src/foobar.ts | 6 + .../node_modules/@bitgo/foobar6/src/index.ts | 2 + .../node_modules/@bitgo/foobar6/tsconfig.json | 7 + .../@bitgo/test-types/dist/src/index.js | 0 .../@bitgo/test-types/dist/src/index.js.map | 3 + .../@bitgo/test-types/package.json | 16 + .../@bitgo/test-types/src/index.ts | 24 ++ .../@bitgo/test-types/tsconfig.json | 7 + 18 files changed, 429 insertions(+), 12 deletions(-) create mode 100644 packages/openapi-generator/test/.prettierignore create mode 100644 packages/openapi-generator/test/externalModuleApiSpec.test.ts create mode 100644 packages/openapi-generator/test/sample-types/apiSpec.ts create mode 100644 packages/openapi-generator/test/sample-types/apiSpecWithEnum.ts create mode 100644 packages/openapi-generator/test/sample-types/exportPathError.ts create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/dist/src/index.js create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/dist/src/index.js.map create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/package.json create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/foobar.ts create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/index.ts create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/tsconfig.json create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/dist/src/index.js create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/dist/src/index.js.map create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/package.json create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/src/index.ts create mode 100644 packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/tsconfig.json diff --git a/.prettierignore b/.prettierignore index d27bbf0b..dd536776 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,4 @@ coverage/ dist/ flake.lock +packages/openapi-generator/test/ diff --git a/packages/openapi-generator/test/.prettierignore b/packages/openapi-generator/test/.prettierignore new file mode 100644 index 00000000..cb24d602 --- /dev/null +++ b/packages/openapi-generator/test/.prettierignore @@ -0,0 +1 @@ +sample-types/ \ No newline at end of file diff --git a/packages/openapi-generator/test/externalModule.test.ts b/packages/openapi-generator/test/externalModule.test.ts index 4cac4e05..31ee1893 100644 --- a/packages/openapi-generator/test/externalModule.test.ts +++ b/packages/openapi-generator/test/externalModule.test.ts @@ -18,11 +18,18 @@ async function testCase( entryPoint: string, expected: Record>, expectedErrors: Record = {}, + parseErrorRegex: RegExp | undefined = undefined, ) { test(description, async () => { const project = new Project({}, KNOWN_IMPORTS); const entryPointPath = p.resolve(entryPoint); - await project.parseEntryPoint(entryPointPath); + const parsed = await project.parseEntryPoint(entryPointPath); + + if (parseErrorRegex !== undefined) { + assert(E.isLeft(parsed)); + assert(parseErrorRegex.test(parsed.left)); + return; + } for (const path of Object.keys(expected)) { const resolvedPath = p.resolve(path); @@ -181,15 +188,18 @@ testCase( }, ); -test('type from external library with import path error', async () => { - const entryPoint = 'test/sample-types/importPathError.ts'; - const project = new Project({}, KNOWN_IMPORTS); - const entryPointPath = p.resolve(entryPoint); - const result = await project.parseEntryPoint(entryPointPath); - - const errorRegex = - /Could not resolve io-tsg from .*\/test\/sample-types\/node_modules\/@bitgo\/foobar3\/src/; +testCase( + 'type from external library with import path error', + 'test/sample-types/importPathError.ts', + {}, + {}, + /Could not resolve io-tsg from .*\/test\/sample-types\/node_modules\/@bitgo\/foobar3\/src/, +); - assert(E.isLeft(result)); - assert(errorRegex.test(result.left)); -}); +testCase( + 'type from external library with export path error', + 'test/sample-types/exportPathError.ts', + {}, + {}, + /Could not resolve .\/foobart from .*\/test\/sample-types\/node_modules\/@bitgo\/foobar6\/src/, +); diff --git a/packages/openapi-generator/test/externalModuleApiSpec.test.ts b/packages/openapi-generator/test/externalModuleApiSpec.test.ts new file mode 100644 index 00000000..ec0cd12a --- /dev/null +++ b/packages/openapi-generator/test/externalModuleApiSpec.test.ts @@ -0,0 +1,273 @@ +import assert from 'assert'; +import test from 'node:test'; +import { version } from 'typescript'; +import { + Project, + Route, + parseApiSpec, + Schema, + getRefs, + parseCodecInitializer, + convertRoutesToOpenAPI, +} from '../src'; +import { KNOWN_IMPORTS } from '../src/knownImports'; +import { findSymbolInitializer } from '../src/resolveInit'; +import * as p from 'path'; +import * as E from 'fp-ts/Either'; + +/** External library parsing and api spec generation test case + * + * + * @param description a description of the test case + * @param entryPoint the entrypoint of the api spec + * @param expected an open api spec object + * @param expectedErrors opetional record of expected parsing errors + */ +async function testCase( + description: string, + entryPoint: string, + expected: Record, + expectedErrors: string[] = [], +) { + test(description, async () => { + const project = new Project({}, KNOWN_IMPORTS); + const entryPointPath = p.resolve(entryPoint); + await project.parseEntryPoint(entryPointPath); + + const sourceFile = project.get(entryPointPath); + + if (sourceFile === undefined) { + throw new Error(`could not find source file ${entryPoint}`); + } + + const actual: Record = {}; + const errors: string[] = []; + + for (const symbol of sourceFile.symbols.declarations) { + if (symbol.init !== undefined) { + if (symbol.init.type !== 'CallExpression') { + continue; + } else if ( + symbol.init.callee.type !== 'MemberExpression' || + symbol.init.callee.property.type !== 'Identifier' || + symbol.init.callee.property.value !== 'apiSpec' + ) { + continue; + } else if (symbol.init.arguments.length !== 1) { + continue; + } + const arg = symbol.init.arguments[0]!; + if (arg.expression.type !== 'ObjectExpression') { + continue; + } + const result = parseApiSpec(project, sourceFile, arg.expression); + if (E.isLeft(result)) { + errors.push(result.left); + } else { + actual[symbol.name] = result.right; + } + } + } + + const apiSpec = Object.values(actual).flatMap((routes) => routes); + + const components: Record = {}; + const queue: Schema[] = apiSpec.flatMap((route) => { + return [ + ...route.parameters.map((p) => p.schema), + ...(route.body !== undefined ? [route.body] : []), + ...Object.values(route.response), + ]; + }); + let schema: Schema | undefined; + while (((schema = queue.pop()), schema !== undefined)) { + const refs = getRefs(schema, project.getTypes()); + for (const ref of refs) { + if (components[ref.name] !== undefined) { + continue; + } + const sourceFile = project.get(ref.location); + if (sourceFile === undefined) { + console.error(`Could not find '${ref.name}' from '${ref.location}'`); + process.exit(1); + } + + const initE = findSymbolInitializer(project, sourceFile, ref.name); + if (E.isLeft(initE)) { + console.error( + `Could not find symbol '${ref.name}' in '${ref.location}': ${initE.left}`, + ); + process.exit(1); + } + const [newSourceFile, init] = initE.right; + + const codecE = parseCodecInitializer(project, newSourceFile, init); + if (E.isLeft(codecE)) { + console.error( + `Could not parse codec '${ref.name}' in '${ref.location}': ${codecE.left}`, + ); + process.exit(1); + } + components[ref.name] = codecE.right; + queue.push(codecE.right); + } + } + + const name = description; + + const openapi = convertRoutesToOpenAPI( + { + title: name, + version, + description, + }, + [], + apiSpec, + components, + ); + + assert.deepEqual(errors, expectedErrors); + assert.deepEqual(openapi, expected); + }); +} + +testCase( + 'simple api spec with imported types', + 'test/sample-types/apiSpec.ts', + { + openapi: '3.0.3', + info: { + title: 'simple api spec with imported types', + version: '4.7.4', + description: 'simple api spec with imported types', + }, + paths: { + '/test': { + post: { + parameters: [], + requestBody: { + content: { + 'application/json': { + schema: { + type: 'object', + properties: { + path1: { + type: 'string', + }, + path2: { + type: 'number', + }, + path3: { + type: 'boolean', + }, + path4: { + type: 'string', + enum: ['literal'], + }, + }, + required: ['path1', 'path2', 'path3', 'path4'], + }, + }, + }, + }, + responses: { + '200': { + description: 'OK', + content: { + 'application/json': { + schema: { + type: 'string', + }, + }, + }, + }, + }, + }, + get: { + parameters: [], + responses: { + '200': { + description: 'OK', + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/SampleGetResponse', + }, + }, + }, + }, + }, + }, + }, + }, + components: { + schemas: { + SampleGetResponse: { + title: 'SampleGetResponse', + type: 'object', + properties: { + response1: { + type: 'string', + }, + response2: { + type: 'object', + properties: { + nested1: { + type: 'number', + }, + nested2: { + type: 'boolean', + }, + }, + required: ['nested1', 'nested2'], + }, + }, + required: ['response1', 'response2'], + }, + }, + }, + }, + [], +); + +testCase( + 'simple api spec with exported enum', + 'test/sample-types/apiSpecWithEnum.ts', + { + openapi: '3.0.3', + info: { + title: 'simple api spec with exported enum', + version: '4.7.4', + description: 'simple api spec with exported enum', + }, + paths: { + '/test': { + get: { + parameters: [], + responses: { + '200': { + description: 'OK', + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/SampleEnumType', + }, + }, + }, + }, + }, + }, + }, + }, + components: { + schemas: { + SampleEnumType: { + title: 'SampleEnumType', + type: 'string', + enum: ['Value1', 'Value2'], + }, + }, + }, + }, + [], +); diff --git a/packages/openapi-generator/test/sample-types/apiSpec.ts b/packages/openapi-generator/test/sample-types/apiSpec.ts new file mode 100644 index 00000000..420e3738 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/apiSpec.ts @@ -0,0 +1,28 @@ +import { SamplePostRequest, SampleGetResponse } from '@bitgo/test-types'; +import * as t from 'io-ts'; +import * as h from '@api-ts/io-ts-http'; + +export const test = h.apiSpec({ + 'api.post.test': { + post: h.httpRoute({ + path: '/test', + method: 'POST', + request: h.httpRequest({ + body: SamplePostRequest, + }), + response: { + 200: t.string, + }, + }), + }, + 'api.get.test': { + get: h.httpRoute({ + path: '/test', + method: 'GET', + request: h.httpRequest({}), + response: { + 200: SampleGetResponse, + }, + }), + }, +}); diff --git a/packages/openapi-generator/test/sample-types/apiSpecWithEnum.ts b/packages/openapi-generator/test/sample-types/apiSpecWithEnum.ts new file mode 100644 index 00000000..ad1897aa --- /dev/null +++ b/packages/openapi-generator/test/sample-types/apiSpecWithEnum.ts @@ -0,0 +1,15 @@ +import { SampleEnumType } from '@bitgo/test-types'; +import * as h from '@api-ts/io-ts-http'; + +export const enumTest = h.apiSpec({ + 'api.get.test': { + get: h.httpRoute({ + path: '/test', + method: 'GET', + request: h.httpRequest({}), + response: { + 200: SampleEnumType, + }, + }), + }, +}); diff --git a/packages/openapi-generator/test/sample-types/exportPathError.ts b/packages/openapi-generator/test/sample-types/exportPathError.ts new file mode 100644 index 00000000..78ccfcd6 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/exportPathError.ts @@ -0,0 +1,5 @@ +import * as f from '@bitgo/foobar6'; + +export const FOO = { + foobar: f.foobar, +}; diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/dist/src/index.js b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/dist/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/dist/src/index.js.map b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/dist/src/index.js.map new file mode 100644 index 00000000..a84c1a88 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/dist/src/index.js.map @@ -0,0 +1,3 @@ +{ + "sources": ["../../src/index.ts"] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/package.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/package.json new file mode 100644 index 00000000..dac77093 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/package.json @@ -0,0 +1,16 @@ +{ + "name": "foobar6", + "version": "0.0.1", + "main": "dist/src/index.js", + "types": "src/index.ts", + "files": [ + "dist/src/**/*", + "src/**/*" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" + } \ No newline at end of file diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/foobar.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/foobar.ts new file mode 100644 index 00000000..2f9b87a5 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/foobar.ts @@ -0,0 +1,6 @@ +import * as t from 'io-ts' + +export const Foobar = t.type({ + foo: t.string, + bar: t.number, +}); \ No newline at end of file diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/index.ts new file mode 100644 index 00000000..fb503117 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/index.ts @@ -0,0 +1,2 @@ +export * from './foobart'; +export const foobar = 'foobar'; \ No newline at end of file diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/tsconfig.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/tsconfig.json new file mode 100644 index 00000000..128699bc --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "outDir": "dist" + }, + "references": [] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/dist/src/index.js b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/dist/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/dist/src/index.js.map b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/dist/src/index.js.map new file mode 100644 index 00000000..a84c1a88 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/dist/src/index.js.map @@ -0,0 +1,3 @@ +{ + "sources": ["../../src/index.ts"] +} \ No newline at end of file diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/package.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/package.json new file mode 100644 index 00000000..e5df2e18 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/package.json @@ -0,0 +1,16 @@ +{ + "name": "test-types", + "version": "0.0.1", + "main": "dist/src/index.js", + "types": "src/index.ts", + "files": [ + "dist/src/**/*", + "src/**/*" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "" + } \ No newline at end of file diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/src/index.ts new file mode 100644 index 00000000..8aa55324 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/src/index.ts @@ -0,0 +1,24 @@ +import * as t from 'io-ts'; + +export const SamplePostRequest = { + path1: t.string, + path2: t.number, + path3: t.boolean, + path4: t.literal('literal'), +}; + +export const SampleGetResponse = t.type({ + response1: t.string, + response2: t.type({ + nested1: t.number, + nested2: t.boolean, + }) +}); + +export enum SampleEnum { + Value1 = 'value1', + Value2 = 'value2', +} + +export const SampleEnumType = t.keyof(SampleEnum, "SampleEnum"); +export type SampleEnumType = t.TypeOf; \ No newline at end of file diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/tsconfig.json b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/tsconfig.json new file mode 100644 index 00000000..6673fe4e --- /dev/null +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "outDir": "dist" + }, + "references": [] + } \ No newline at end of file From eae36324b610df88bd81625b43019f6e13aaa469 Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Fri, 17 May 2024 15:41:33 -0400 Subject: [PATCH 15/19] test: add comments to ensure intentional errors in test node_modules aren't fixed --- .../test/sample-types/node_modules/@bitgo/foobar3/src/foobar.ts | 2 +- .../test/sample-types/node_modules/@bitgo/foobar6/src/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/src/foobar.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/src/foobar.ts index ba9b1711..bfa05275 100644 --- a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/src/foobar.ts +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar3/src/foobar.ts @@ -1,4 +1,4 @@ -import * as t from 'io-tsg' +import * as t from 'io-tsg' // This type is intentional, don't fix export const Foobar = t.type({ foo: t.string, diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/index.ts index fb503117..bce948dd 100644 --- a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/index.ts +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/foobar6/src/index.ts @@ -1,2 +1,2 @@ -export * from './foobart'; +export * from './foobart'; // This typo is intentional, don't fix export const foobar = 'foobar'; \ No newline at end of file From 8dc41648566779f6e9db9e176b03c9376f51ec0a Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Fri, 17 May 2024 16:10:41 -0400 Subject: [PATCH 16/19] fix: add `union` type test to `externalModuleApiSpec.test.ts` --- .../test/externalModuleApiSpec.test.ts | 48 +++++++++++++++++++ .../test/sample-types/apiSpecWithUnion.ts | 15 ++++++ .../@bitgo/test-types/src/index.ts | 4 +- 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 packages/openapi-generator/test/sample-types/apiSpecWithUnion.ts diff --git a/packages/openapi-generator/test/externalModuleApiSpec.test.ts b/packages/openapi-generator/test/externalModuleApiSpec.test.ts index ec0cd12a..35048dae 100644 --- a/packages/openapi-generator/test/externalModuleApiSpec.test.ts +++ b/packages/openapi-generator/test/externalModuleApiSpec.test.ts @@ -271,3 +271,51 @@ testCase( }, [], ); + +testCase( + 'simple api spec with exported union type', + 'test/sample-types/apiSpecWithUnion.ts', + { + openapi: "3.0.3", + info: { + title: "simple api spec with exported union type", + version: "4.7.4", + description: "simple api spec with exported union type" + }, + paths: { + "/test": { + get: { + parameters: [], + responses: { + 200: { + description: "OK", + content: { + 'application/json': { + schema: { + $ref: "#/components/schemas/SampleUnion" + } + } + } + } + } + } + } + }, + components: { + schemas: { + SampleUnion: { + title: "SampleUnion", + oneOf: [ + { + type: "string" + }, + { + type: "number" + } + ] + } + } + } + }, + [] +) diff --git a/packages/openapi-generator/test/sample-types/apiSpecWithUnion.ts b/packages/openapi-generator/test/sample-types/apiSpecWithUnion.ts new file mode 100644 index 00000000..46985559 --- /dev/null +++ b/packages/openapi-generator/test/sample-types/apiSpecWithUnion.ts @@ -0,0 +1,15 @@ +import { SampleUnion } from '@bitgo/test-types'; +import * as h from '@api-ts/io-ts-http'; + +export const enumTest = h.apiSpec({ + 'api.get.test': { + get: h.httpRoute({ + path: '/test', + method: 'GET', + request: h.httpRequest({}), + response: { + 200: SampleUnion, + }, + }), + }, +}); diff --git a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/src/index.ts b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/src/index.ts index 8aa55324..9bff5aa3 100644 --- a/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/src/index.ts +++ b/packages/openapi-generator/test/sample-types/node_modules/@bitgo/test-types/src/index.ts @@ -21,4 +21,6 @@ export enum SampleEnum { } export const SampleEnumType = t.keyof(SampleEnum, "SampleEnum"); -export type SampleEnumType = t.TypeOf; \ No newline at end of file +export type SampleEnumType = t.TypeOf; + +export const SampleUnion = t.union([t.string, t.number]); From de2c6c6b8870bb918d00207a6ed8587efed64004 Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Fri, 17 May 2024 16:30:21 -0400 Subject: [PATCH 17/19] test: add error to array instead of exiting test process DX-389 --- .../test/externalModuleApiSpec.test.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/openapi-generator/test/externalModuleApiSpec.test.ts b/packages/openapi-generator/test/externalModuleApiSpec.test.ts index 35048dae..05c37e64 100644 --- a/packages/openapi-generator/test/externalModuleApiSpec.test.ts +++ b/packages/openapi-generator/test/externalModuleApiSpec.test.ts @@ -88,25 +88,25 @@ async function testCase( } const sourceFile = project.get(ref.location); if (sourceFile === undefined) { - console.error(`Could not find '${ref.name}' from '${ref.location}'`); - process.exit(1); + errors.push(`Could not find '${ref.name}' from '${ref.location}'`); + break; } const initE = findSymbolInitializer(project, sourceFile, ref.name); if (E.isLeft(initE)) { - console.error( + errors.push( `Could not find symbol '${ref.name}' in '${ref.location}': ${initE.left}`, ); - process.exit(1); + break; } const [newSourceFile, init] = initE.right; const codecE = parseCodecInitializer(project, newSourceFile, init); if (E.isLeft(codecE)) { - console.error( - `Could not parse codec '${ref.name}' in '${ref.location}': ${codecE.left}`, + errors.push( + `Could not parse codec '${ref.name}' in '${ref.location}': ${codecE.left}`, ); - process.exit(1); + break; } components[ref.name] = codecE.right; queue.push(codecE.right); From c02b6e2c4d18ddb63d112f790e3379c35c97084d Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Tue, 21 May 2024 11:38:22 -0400 Subject: [PATCH 18/19] feat(openapi-generator): make node_module resolving more generic by removing @bitgo prefix --- packages/openapi-generator/src/project.ts | 96 +++++++++---------- packages/openapi-generator/src/resolveInit.ts | 8 +- .../test/externalModule.test.ts | 14 +-- 3 files changed, 53 insertions(+), 65 deletions(-) diff --git a/packages/openapi-generator/src/project.ts b/packages/openapi-generator/src/project.ts index 96a8fe16..9aa98cda 100644 --- a/packages/openapi-generator/src/project.ts +++ b/packages/openapi-generator/src/project.ts @@ -54,12 +54,23 @@ export class Project { this.add(path, sourceFile); for (const sym of Object.values(sourceFile.symbols.imports)) { - const filePath = p.dirname(path); - const absImportPathE = this.resolve(filePath, sym.from); - if (E.isLeft(absImportPathE)) { - return absImportPathE; - } else if (!this.has(absImportPathE.right)) { - queue.push(absImportPathE.right); + if (!sym.from.startsWith('.')) { + // If we are not resolving a relative path, we need to resolve the entry point + const baseDir = p.dirname(sourceFile.path); + let entryPoint = this.resolveEntryPoint(baseDir, sym.from); + if (E.isLeft(entryPoint)) { + continue; + } else if (!this.has(entryPoint.right)) { + queue.push(entryPoint.right); + } + } else { + const filePath = p.dirname(path); + const absImportPathE = this.resolve(filePath, sym.from); + if (E.isLeft(absImportPathE)) { + return absImportPathE; + } else if (!this.has(absImportPathE.right)) { + queue.push(absImportPathE.right); + } } } for (const starExport of sourceFile.symbols.exportStarFiles) { @@ -80,66 +91,45 @@ export class Project { return await readFile(filename, 'utf8'); } - private resolvePath(path: string, basedir: string): E.Either { + resolveEntryPoint(basedir: string, library: string): E.Either { try { - const result = resolve.sync(path, { + const packageJson = resolve.sync(`${library}/package.json`, { basedir, - extensions: ['.ts', '.js', '.d.ts'], + extensions: ['.json'], }); + const packageInfo = JSON.parse(fs.readFileSync(packageJson, 'utf8')); - return E.right(result); - } catch (e: unknown) { - if (e instanceof Error && e.message) { - return E.left(e.message); + let typesEntryPoint = ''; + + if (packageInfo['types']) { + typesEntryPoint = packageInfo['types']; } - return E.left(JSON.stringify(e)); - } - } + if (packageInfo['typings']) { + typesEntryPoint = packageInfo['typings']; + } - private findSourceFileFromPackage(path: string): E.Either { - const mapName = path.replace('.js', '.js.map'); + if (!typesEntryPoint) { + return E.left(`Could not find types entry point for ${library}`); + } - if (fs.existsSync(mapName)) { - const mapJson = JSON.parse(fs.readFileSync(mapName, 'utf8')); - const dirName = p.dirname(path); - const source = mapJson.sources[0]; - const response = resolve.sync(source, { basedir: dirName }); - return E.right(response); + const entryPoint = resolve.sync(`${library}/${typesEntryPoint}`, { + basedir, + extensions: ['.ts', '.js'], + }); + return E.right(entryPoint); + } catch (err) { + return E.left(`Could not resolve entry point for ${library}: ${err}`); } - - return E.left('Map file not found for ' + path); } resolve(basedir: string, path: string): E.Either { - const BITGO_PREFIX = '@bitgo'; try { - let resolved = this.resolvePath(path, basedir); - if (E.isLeft(resolved)) { - // Could not resolve the path, try resolving in the types package - resolved = this.resolvePath('@types/' + path, basedir); - } - - // Types package wasn't found, return an error - if (E.isLeft(resolved)) { - return E.left('Could not resolve ' + path + ' from ' + basedir); - } - - const result = resolved.right; - - // If we are parsing an internal type package, we want to return the path to the source TS file - if (path.startsWith(BITGO_PREFIX)) { - return this.findSourceFileFromPackage(result); - } else { - // Else - find the declaration file and return it if it exists - const dTsName = result.replace('.js', '.d.ts'); - - if (fs.existsSync(dTsName)) { - return E.right(dTsName); - } - - return E.right(result); - } + const result = resolve.sync(path, { + basedir, + extensions: ['.ts', '.js'], + }); + return E.right(result); } catch (e: unknown) { if (e instanceof Error && e.message) { return E.left(e.message); diff --git a/packages/openapi-generator/src/resolveInit.ts b/packages/openapi-generator/src/resolveInit.ts index 243d4d8f..6c254b22 100644 --- a/packages/openapi-generator/src/resolveInit.ts +++ b/packages/openapi-generator/src/resolveInit.ts @@ -13,7 +13,13 @@ function resolveImportPath( sourceFile: SourceFile, path: string, ): E.Either { - const importPathE = project.resolve(dirname(sourceFile.path), path); + let importPathE; + if (path.startsWith('.')) { + importPathE = project.resolve(dirname(sourceFile.path), path); + } else { + importPathE = project.resolveEntryPoint(dirname(sourceFile.path), path); + } + if (E.isLeft(importPathE)) { return importPathE; } diff --git a/packages/openapi-generator/test/externalModule.test.ts b/packages/openapi-generator/test/externalModule.test.ts index 31ee1893..b7831c42 100644 --- a/packages/openapi-generator/test/externalModule.test.ts +++ b/packages/openapi-generator/test/externalModule.test.ts @@ -5,6 +5,7 @@ import * as p from 'path'; import { parsePlainInitializer, Project, type Schema } from '../src'; import { KNOWN_IMPORTS } from '../src/knownImports'; +// import { resolve } from 'node:path'; /** External library parsing test case * @@ -18,23 +19,16 @@ async function testCase( entryPoint: string, expected: Record>, expectedErrors: Record = {}, - parseErrorRegex: RegExp | undefined = undefined, ) { test(description, async () => { const project = new Project({}, KNOWN_IMPORTS); const entryPointPath = p.resolve(entryPoint); - const parsed = await project.parseEntryPoint(entryPointPath); - - if (parseErrorRegex !== undefined) { - assert(E.isLeft(parsed)); - assert(parseErrorRegex.test(parsed.left)); - return; - } + await project.parseEntryPoint(entryPointPath); for (const path of Object.keys(expected)) { const resolvedPath = p.resolve(path); const sourceFile = project.get(resolvedPath); - + if (sourceFile === undefined) { throw new Error(`Source file ${path} not found`); } @@ -193,7 +187,6 @@ testCase( 'test/sample-types/importPathError.ts', {}, {}, - /Could not resolve io-tsg from .*\/test\/sample-types\/node_modules\/@bitgo\/foobar3\/src/, ); testCase( @@ -201,5 +194,4 @@ testCase( 'test/sample-types/exportPathError.ts', {}, {}, - /Could not resolve .\/foobart from .*\/test\/sample-types\/node_modules\/@bitgo\/foobar6\/src/, ); From b97074eddc5d5cd8f546d184420f5c607664547b Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Wed, 22 May 2024 19:33:49 -0400 Subject: [PATCH 19/19] refactor(openapi-generator): remove unneeded files and package bumps --- packages/openapi-generator/package.json | 2 +- packages/openapi-generator/test/.prettierignore | 1 - packages/openapi-generator/test/externalModule.test.ts | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 packages/openapi-generator/test/.prettierignore diff --git a/packages/openapi-generator/package.json b/packages/openapi-generator/package.json index b1528427..2889cdb1 100644 --- a/packages/openapi-generator/package.json +++ b/packages/openapi-generator/package.json @@ -35,7 +35,7 @@ "@types/resolve": "1.20.6", "c8": "9.1.0", "memfs": "4.9.2", - "typescript": "^4.7.4" + "typescript": "4.7.4" }, "optionalDependencies": { "@swc/core-linux-x64-gnu": "1.5.7", diff --git a/packages/openapi-generator/test/.prettierignore b/packages/openapi-generator/test/.prettierignore deleted file mode 100644 index cb24d602..00000000 --- a/packages/openapi-generator/test/.prettierignore +++ /dev/null @@ -1 +0,0 @@ -sample-types/ \ No newline at end of file diff --git a/packages/openapi-generator/test/externalModule.test.ts b/packages/openapi-generator/test/externalModule.test.ts index b7831c42..38efa569 100644 --- a/packages/openapi-generator/test/externalModule.test.ts +++ b/packages/openapi-generator/test/externalModule.test.ts @@ -5,7 +5,6 @@ import * as p from 'path'; import { parsePlainInitializer, Project, type Schema } from '../src'; import { KNOWN_IMPORTS } from '../src/knownImports'; -// import { resolve } from 'node:path'; /** External library parsing test case *