Skip to content

Commit

Permalink
add an option to use type keyword instead of an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rikukissa committed Jul 8, 2021
1 parent b5048a4 commit c4490f2
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 38 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ From 1.4.0 forward also Promises are supported. All other values (functions etc.

## Extension Settings

| Setting | Type | Default | Description |
| ------------------------------- | ----------- | ------- | ------------------------------------------------------------------------------- |
| typehole.runtime.autoInstall | boolean | true | Install Typehole runtime package automatically when the first typehole is added |
| typehole.runtime.projectPath | string | | Project directory where Typehole runtime should be installed |
| typehole.runtime.packageManager | npm \| yarn | npm | Package manager to be used when installing the runtime |
| typehole.runtime.extensionPort | number | 17341 | HTTP port for HTTP extension to listen for incoming samples |
| Setting | Type | Default | Description |
| ------------------------------- | ----------------- | --------- | ------------------------------------------------------------------------------- |
| typehole.runtime.autoInstall | boolean | true | Install Typehole runtime package automatically when the first typehole is added |
| typehole.runtime.projectPath | string | | Project directory where Typehole runtime should be installed |
| typehole.runtime.packageManager | npm \| yarn | npm | Package manager to be used when installing the runtime |
| typehole.runtime.extensionPort | number | 17341 | HTTP port for HTTP extension to listen for incoming samples |
| typehole.typeOrInterface | interface \| type | interface | Keyword to be used for generated types |

## Runtime

Expand Down Expand Up @@ -134,6 +135,12 @@ configure({

## Release Notes

## [1.7.0] - 2021-07-08

### Added

- New option "typehole.typeOrInterface" added for using `type` keyword instead of `interface`. All thanks to @akafaneh 🎉

## [1.6.3] - 2021-06-20

### Fixed
Expand Down
26 changes: 10 additions & 16 deletions packages/extension/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publisher": "rikurouvila",
"description": "🧪 Take samples of runtime values and turn them into type definitions automatically",
"repository": "https://github.com/rikukissa/typehole",
"version": "1.6.3",
"version": "1.8.0",
"private": true,
"icon": "images/logo.png",
"galleryBanner": {
Expand Down Expand Up @@ -32,6 +32,16 @@
"description": "Installs Typehole runtime package automatically when the first typehole is added",
"scope": "window"
},
"typehole.typeOrInterface": {
"type": "string",
"enum": [
"type",
"interface"
],
"default": "interface",
"description": "Keyword to be used for generated types",
"scope": "window"
},
"typehole.runtime.extensionPort": {
"type": "number",
"default": 17341,
Expand Down Expand Up @@ -99,7 +109,7 @@
},
"dependencies": {
"@phenomnomnominal/tsquery": "^4.1.1",
"@riku/json-to-ts": "^2.0.0",
"@riku/json-to-ts": "^2.1.0",
"@types/esquery": "^1.0.1",
"@types/npm": "^2.0.31",
"esquery": "^1.4.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/extension/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export function getConfiguration(
extensionPort: configuration.get(
"typehole.runtime.extensionPort"
) as number,
typeOrInterface: configuration.get("typehole.typeOrInterface") as
| "interface"
| "type",
autoInstall: configuration.get("typehole.runtime.autoInstall") as boolean,
projectPath: configuration.get("typehole.runtime.projectPath") as string,
packageManager: configuration.get(
Expand Down
9 changes: 8 additions & 1 deletion packages/extension/src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import f from "fastify";

import * as ts from "typescript";
import * as vscode from "vscode";
import { getConfiguration } from "./config";

import { getEditorRange, getProjectRoot } from "./editor/utils";
import { error, log } from "./logger";
Expand Down Expand Up @@ -37,8 +38,14 @@ function createServer() {
const body = request.body as any;
log(body.id, "-", "New sample", JSON.stringify(request.body), "received");

const editor = vscode.window.activeTextEditor;
const document = editor?.document;
const config = getConfiguration("", document);

const samples = addSample(body.id, body.sample);
const typeString = samplesToType(samples);
const typeString = samplesToType(samples, {
useTypeAlias: config.typeOrInterface === "type",
});

try {
await onTypeExtracted(body.id, typeString);
Expand Down
6 changes: 6 additions & 0 deletions packages/extension/src/transforms/samplesToType/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ test("generates types and interfaces from samples", () => {
`type TypeholeRoot = (boolean | TypeholeRootWrapper2 | null | number);
interface TypeholeRootWrapper2 {
a: number;
}`
);
expect(samplesToType([1, { a: 2 }, true, null], { useTypeAlias: true })).toBe(
`type TypeholeRoot = (boolean | TypeholeRootWrapper2 | null | number);
type TypeholeRootWrapper2 = {
a: number;
}`
);
expect(samplesToType([{ a: 2 }])).toBe(
Expand Down
15 changes: 11 additions & 4 deletions packages/extension/src/transforms/samplesToType/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import json2ts from "@riku/json-to-ts";
import { Options } from "@riku/json-to-ts/build/src/model";

export function samplesToType(samples: any[]): string {
export function samplesToType(
samples: any[],
jsonToTSOptions?: Options
): string {
let wrapperType = null;
let samplesWithoutWrapperTypes = [];
for (const sample of samples) {
Expand All @@ -13,9 +17,12 @@ export function samplesToType(samples: any[]): string {
}

// eslint-disable-next-line @typescript-eslint/naming-convention
const types = json2ts({
__typeholeRootWrapper__: samplesWithoutWrapperTypes,
}).join("\n");
const types = json2ts(
{
__typeholeRootWrapper__: samplesWithoutWrapperTypes,
},
jsonToTSOptions
).join("\n");

let root = types
.match(/__typeholeRootWrapper__:\s(.+)/)![1]
Expand Down
10 changes: 2 additions & 8 deletions packages/runtime/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.6.3",
"version": "1.7.0",
"name": "typehole",
"repository": "rikukissa/typehole",
"description": "Turn runtime types into static typescript types automatically",
Expand Down

0 comments on commit c4490f2

Please sign in to comment.