Skip to content

Commit

Permalink
add configuration test
Browse files Browse the repository at this point in the history
  • Loading branch information
svenliebig committed Aug 25, 2022
1 parent 3c1381d commit f6317d3
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 24 deletions.
58 changes: 35 additions & 23 deletions src/models/Import.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@
import { existsSync } from "fs";
import { parse, resolve } from "path";
import { L } from "../utils/logger";
import { existsSync } from "fs"
import { parse, resolve } from "path"
import { L } from "../utils/logger"

export class Import {
constructor(
/** the path of the file that contains the import declaration. */
private source: string,
/** the relative path to the target file of the import. */
private from: string,
public from: string,
/** the named exports (`import { named } from "./from"`). */
private named: Array<string>,
/** the default exports (`import default from "./from"`). */
private default_: string | null
) {}

public resolve() {
L.d(`<resolve>`, this.toString());
L.d(`<resolve>`, this.toString())

const { dir } = parse(this.source);
const pathToFile = resolve(dir, this.from);
const tsPath = `${pathToFile}.ts`;
const tsxPath = `${pathToFile}.tsx`;
const { dir } = parse(this.source)
const pathToFile = resolve(dir, this.from)
const tsPath = `${pathToFile}.ts`
const tsxPath = `${pathToFile}.tsx`

L.d(`<resolve>`, `trying if exists: ${tsPath}`);
L.d(`<resolve>`, `trying if exists: ${tsPath}`)

if (existsSync(tsPath)) {
L.d(`<resolve>`, `"${tsPath}" path exists`);
return tsPath;
L.d(`<resolve>`, `"${tsPath}" path exists`)
return tsPath
}

if (existsSync(tsxPath)) {
L.d(`<resolve>`, `"${tsPath}" path exists`);
return tsxPath;
L.d(`<resolve>`, `"${tsPath}" path exists`)
return tsxPath
}

// TODO node_modules
// TODO @types

throw new Error(
`Could not resolve import ${this.toString()} from "${this.from}".`
);
throw new UnresolvedImportError(this)
}

/**
* Checks if the {@link Import} contains a specific identifier.
*
*
* For example: `import Jon, { Doe } from "./helloworld"`
*
*
* Would result in:
*
*
* ```ts
* containsIdentifier("Jon") // true
* containsIdentifier("Doe") // true
Expand All @@ -60,8 +58,22 @@ export class Import {
}

public toString() {
return `import ${this.default_ ? `${this.default_} ` : ""} ${
this.named.length > 0 ? `{ ${this.named.join(", ")} }` : ""
} from "${this.from}"`;
return `import ${this.default_ ? `${this.default_} ` : ""}${this.named.length > 0 ? `{ ${this.named.join(", ")} }` : ""} from "${this.from}"`
}
}

export class UnresolvedImportError extends Error {
constructor(imp: Import) {
super()
this.message = this.createMessage(imp)
}

private createMessage(imp: Import): string {
return [
`Could not resolve ${imp.toString()} from "${imp.from}".`,
"",
"It is possible to suppress this error when the 'breakOnUnresolvedImport' options is set to 'false'.",
"The type will resolve to unknown then.",
].join("\n")
}
}
5 changes: 5 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { ArrayType } from "./ArrayType"
export { ArrayTypeDeclaration } from "./ArrayTypeDeclaration"
export { BooleanType } from "./BooleanType"
export { EnumType } from "./EnumType"
export { EnumTypeDeclaration } from "./EnumTypeDeclaration"
8 changes: 7 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ export class Parser {
return declaration
}

if (resolvedType instanceof TypeReference && this.config.doNotResolve.includes(resolvedType.identifier)) {
declaration.type = resolvedType
return declaration
}

if (resolvedType instanceof UnionType) {
// TODO remove the typescript node from the TypeLiteral class to prevent this
declaration.type = resolvedType
Expand All @@ -336,7 +341,8 @@ export class Parser {
return declaration
}

if (resolvedType instanceof TypeReference && this.config.doNotResolve.includes(resolvedType.identifier)) {
if (resolvedType instanceof UnknownType) {
// TODO remove the typescript node from the TypeLiteral class to prevent this
declaration.type = resolvedType
return declaration
}
Expand Down
21 changes: 21 additions & 0 deletions tests/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import test from "ava"
import { resolve } from "path"
import { UnresolvedImportError } from "../src/models/Import"
import { UnknownType } from "../src/models/UnknownType"
import { Parser } from "../src/parser"

test("breakOnUnresolvedImports: true", (t) => {
const parser = new Parser(resolve(__dirname, "fixtures", "configurationTestTypes.ts"), { breakOnUnresolvedImports: true })
t.throws(
() => {
parser.resolve("UnresolvedImport")
},
{ instanceOf: UnresolvedImportError }
)
})

test("breakOnUnresolvedImports: false, should resolve to unknown", (t) => {
const parser = new Parser(resolve(__dirname, "fixtures", "configurationTestTypes.ts"), { breakOnUnresolvedImports: false })
const declaration = parser.resolve("UnresolvedImport")
t.assert(declaration.type instanceof UnknownType)
})
3 changes: 3 additions & 0 deletions tests/fixtures/configurationTestTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { UnResolved } from "does-not-exist"

type UnresolvedImport = UnResolved
8 changes: 8 additions & 0 deletions tests/stringTypeDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ test("should parse the ExportedBasicString StringTypeDeclaration", (t) => {
t.assert(declaration instanceof StringTypeDeclaration)
t.assert(declaration?.type instanceof StringType)
})

// toString

test("toString of BasicString", (t) => {
const parser = new Parser(resolve(__dirname, "fixtures", "stringType.ts"))
const declaration = parser.getDeclaration("BasicString")
t.is(declaration?.toString(), "type BasicString = string")
})

0 comments on commit f6317d3

Please sign in to comment.