Skip to content

Commit 125d379

Browse files
committed
refactor: Ensure package types are built & validated
1 parent c35ef18 commit 125d379

8 files changed

+23
-49
lines changed

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
"version": "3.3.1",
44
"description": "Transforms module resolution paths using TypeScript path mapping and/or custom paths",
55
"main": "dist/index.js",
6-
"types": "types/index.d.ts",
6+
"types": "dist/index.d.ts",
77
"scripts": {
88
"compile": "tsc",
9-
"build": "yarn run clean && yarn run compile",
9+
"build": "yarn run clean && yarn run compile && yarn run check:built-types",
1010
"test": "jest",
1111
"release": "standard-version",
1212
"--------------": "",
1313
"format": "prettier --write \"{src,test}/**/{*.js,!(*.d).ts}\"",
14-
"clean": "rimraf dist **/*.tsbuildinfo",
15-
"clean:all": "yarn run clean && rimraf node_modules **/node_modules test/.yarn-cache",
14+
"clean": "npx rimraf dist **/*.tsbuildinfo",
15+
"clean:all": "yarn run clean && npx rimraf node_modules **/node_modules test/.yarn-cache",
1616
"reset": "yarn run clean:all && yarn install && yarn build",
17+
"check:built-types": "tsc dist/index.d.ts --esModuleInterop",
1718
"-------------- ": "",
18-
"prebuild": "rimraf dist",
19+
"prebuild": "npx rimraf dist",
1920
"install:tests": "cd test && yarn install && cd projects/extras && yarn install",
2021
"prepare": "yarn run install:tests"
2122
},

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import transformer from "./transformer";
22
export default transformer;
33

4-
export { TsTransformPathsConfig } from "./types";
4+
export { TsTransformPathsConfig, TransformerExtras } from "./types";
55
export { register } from "./register";

src/transformer.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
import {} from "ts-expose-internals";
33
import path from "path";
44
import ts from "typescript";
5-
import { cast } from "./utils";
6-
import { TsTransformPathsConfig, TsTransformPathsContext, TypeScriptThree, VisitorContext } from "./types";
5+
import { TransformerExtras, TsTransformPathsConfig, TsTransformPathsContext, VisitorContext } from "./types";
76
import { nodeVisitor } from "./visitor";
87
import { createHarmonyFactory } from "./utils/harmony-factory";
98
import { Minimatch } from "minimatch";
109
import { createSyntheticEmitHost, getTsNodeRegistrationProperties } from "./utils/ts-helpers";
11-
import { TransformerExtras } from "ts-patch";
10+
1211

1312
/* ****************************************************************************************************************** */
1413
// region: Helpers
@@ -109,7 +108,6 @@ export default function transformer(
109108
emitHost,
110109
isTranspileOnly,
111110
isTsNode,
112-
tsThreeInstance: cast<TypeScriptThree>(tsInstance),
113111
excludeMatchers: config.exclude?.map((globPattern) => new Minimatch(globPattern, { matchBase: true })),
114112
outputFileNamesCache: new Map(),
115113
// Get paths patterns appropriate for TS compiler version

src/utils/harmony-factory.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import TS, {
88
NamedExportBindings,
99
NamedImportBindings,
1010
} from "typescript";
11+
import { cast } from "./general-utils";
12+
import * as TsThree from '../declarations/typescript3';
1113
import { TsTransformPathsContext } from "../types";
1214
import { downSampleTsTypes } from "./ts-type-conversion";
1315

@@ -27,7 +29,8 @@ export interface HarmonyFactory extends TS.NodeFactory {}
2729
* Creates a node factory compatible with TS v3+
2830
*/
2931
export function createHarmonyFactory(context: TsTransformPathsContext): HarmonyFactory {
30-
const { tsThreeInstance } = context;
32+
const { tsInstance } = context;
33+
const tsThreeInstance = cast<typeof TsThree>(tsInstance);
3134

3235
return new Proxy(context.tsFactory ?? context.tsInstance, {
3336
get(target, prop) {

src/utils/resolve-module-name.ts

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ function getReturnPath(ctx: GetReturnPathContext) {
8888

8989
/**
9090
* Resolve a module name
91+
* @internal — Uses internal TS type
9192
*/
9293
export function resolveModuleName(
9394
context: VisitorContext,

src/utils/ts-type-conversion.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ type TypeMapping = [
2828
type TsType = Exclude<TypeMapping[number][0], undefined>;
2929
type TsThreeType = Exclude<TypeMapping[number][1], undefined>;
3030

31-
export type TsTypeConversion<Tuple extends [...unknown[]]> = {
31+
type TsTypeConversion<Tuple extends [...unknown[]]> = {
3232
[i in keyof Tuple]: Tuple[i] extends any[] ? TsTypeConversion<Tuple[i]> : DownSampleTsType<Tuple[i]>;
3333
} & { length: Tuple["length"] };
3434

3535
type DownSampleTsType<T> = T extends TsType ? Extract<TypeMapping[number], [T, any]>[1] : T;
3636

37-
export type UpSampleTsTypes<Tuple extends [...unknown[]]> = {
37+
type UpSampleTsTypes<Tuple extends [...unknown[]]> = {
3838
[i in keyof Tuple]: Tuple[i] extends any[] ? UpSampleTsTypes<Tuple[i]> : UpSampleTsType<Tuple[i]>;
3939
} & { length: Tuple["length"] };
4040

@@ -48,27 +48,31 @@ type UpSampleTsType<T> = T extends TsThreeType ? Extract<TypeMapping[number], [a
4848

4949
/**
5050
* Convert TS4 to TS3 types
51-
*/
51+
* @internal
52+
*/
5253
export function downSampleTsTypes<T extends [...unknown[]]>(...args: T): TsTypeConversion<T> {
5354
return args as TsTypeConversion<T>;
5455
}
5556

5657
/**
5758
* Convert TS4 to TS3 type
59+
* @internal
5860
*/
5961
export function downSampleTsType<T>(v: T): DownSampleTsType<T> {
6062
return v as DownSampleTsType<T>;
6163
}
6264

6365
/**
6466
* Convert TS3 to TS4 types
67+
* @internal
6568
*/
6669
export function upSampleTsTypes<T extends [...unknown[]]>(...args: T): UpSampleTsTypes<T> {
6770
return args as UpSampleTsTypes<T>;
6871
}
6972

7073
/**
7174
* Convert TS3 to TS4 type
75+
* @internal
7276
*/
7377
export function upSampleTsType<T extends TsThreeType>(v: T): UpSampleTsType<T> {
7478
return v as UpSampleTsType<T>;

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"compilerOptions": {
77
"rootDir": "src",
88
"outDir": "dist",
9-
"declaration": true
9+
"declaration": true,
10+
"stripInternal": true
1011
}
1112
}

types/index.d.ts

-34
This file was deleted.

0 commit comments

Comments
 (0)