Skip to content

Commit a67e739

Browse files
committed
refactor: use strictest typescript configuration
1 parent 3ee03d1 commit a67e739

13 files changed

+53
-71
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"devDependencies": {
6363
"@eslint/js": "^9.8.0",
6464
"@tsconfig/node18": "^18.2.4",
65+
"@tsconfig/strictest": "^2.0.5",
6566
"@types/eslint": "^9",
6667
"@types/minimatch": "^5.1.2",
6768
"@types/node": "^22.2.0",

src/harmony/versions/four-seven.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
4949
case "updateImportDeclaration":
5050
return function (
5151
node: ImportDeclaration,
52-
modifiers: readonly Modifier[] | undefined,
52+
_modifiers: readonly Modifier[] | undefined,
5353
importClause: ImportClause | undefined,
5454
moduleSpecifier: Expression,
5555
assertClause: AssertClause | undefined,
@@ -73,7 +73,7 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
7373
case "updateExportDeclaration":
7474
return function (
7575
node: ExportDeclaration,
76-
modifiers: readonly Modifier[] | undefined,
76+
_modifiers: readonly Modifier[] | undefined,
7777
isTypeOnly: boolean,
7878
exportClause: NamedExportBindings | undefined,
7979
moduleSpecifier: Expression | undefined,
@@ -99,7 +99,7 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
9999
case "updateModuleDeclaration":
100100
return function (
101101
node: ModuleDeclaration,
102-
modifiers: readonly Modifier[] | undefined,
102+
_modifiers: readonly Modifier[] | undefined,
103103
name: ModuleName,
104104
body: ModuleBody | undefined,
105105
) {

src/harmony/versions/three-eight.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
7070
case "updateImportClause":
7171
return function (
7272
node: ImportClause,
73-
isTypeOnly: boolean,
73+
_isTypeOnly: boolean,
7474
name: Identifier | undefined,
7575
namedBindings: NamedImportBindings | undefined,
7676
) {
@@ -80,7 +80,7 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
8080
case "updateImportDeclaration":
8181
return function (
8282
node: ImportDeclaration,
83-
modifiers: readonly Modifier[] | undefined,
83+
_modifiers: readonly Modifier[] | undefined,
8484
importClause: ImportClause | undefined,
8585
moduleSpecifier: Expression,
8686
) {
@@ -97,8 +97,8 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
9797
case "updateExportDeclaration":
9898
return function (
9999
node: ExportDeclaration,
100-
modifiers: readonly Modifier[] | undefined,
101-
isTypeOnly: boolean,
100+
_modifiers: readonly Modifier[] | undefined,
101+
_isTypeOnly: boolean,
102102
exportClause: NamedExportBindings | undefined,
103103
moduleSpecifier: Expression | undefined,
104104
) {
@@ -115,7 +115,7 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
115115
case "updateModuleDeclaration":
116116
return function (
117117
node: ModuleDeclaration,
118-
modifiers: readonly Modifier[] | undefined,
118+
_modifiers: readonly Modifier[] | undefined,
119119
name: ModuleName,
120120
body: ModuleBody | undefined,
121121
) {
@@ -127,7 +127,7 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
127127
return function (
128128
node: ImportTypeNode,
129129
argument: TypeNode,
130-
assertions: ImportTypeAssertionContainer | undefined,
130+
_assertions: ImportTypeAssertionContainer | undefined,
131131
qualifier: EntityName | undefined,
132132
typeArguments: readonly TypeNode[] | undefined,
133133
isTypeOf?: boolean,

src/index.ts

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

4-
export { TsTransformPathsConfig } from "./types";
4+
export type { TsTransformPathsConfig } from "./types";
55
export { register } from "./register";
66
export { nxTransformerPlugin } from "./plugins";

src/register.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type TSNode from "ts-node";
1+
import TSNode from "ts-node";
22
import type { REGISTER_INSTANCE } from "ts-node";
33
import ts from "typescript";
44
import transformer from "./transformer";
@@ -7,11 +7,18 @@ import transformer from "./transformer";
77
// region: Helpers
88
/* ****************************************************************************************************************** */
99

10+
type PluginExtended = ts.PluginImport & {
11+
transform?: string;
12+
after?: boolean;
13+
before?: boolean;
14+
afterDeclarations?: boolean;
15+
};
16+
1017
function getProjectTransformerConfig(pcl: ts.ParsedCommandLine) {
11-
const plugins = pcl.options.plugins as Record<string, string>[] | undefined;
18+
const plugins = pcl.options.plugins as PluginExtended[];
1219
if (!plugins) return;
1320

14-
const res: { afterDeclarations?: Record<string, string>; before?: Record<string, string> } = {};
21+
const res: { afterDeclarations?: PluginExtended; before?: PluginExtended } = {};
1522
for (const plugin of plugins) {
1623
if (plugin.transform === "typescript-transform-paths" && !plugin.after)
1724
res[plugin.afterDeclarations ? "afterDeclarations" : "before"] = plugin;
@@ -22,8 +29,8 @@ function getProjectTransformerConfig(pcl: ts.ParsedCommandLine) {
2229

2330
function getTransformers(
2431
program?: ts.Program,
25-
beforeConfig?: Record<string, string>,
26-
afterDeclarationsConfig?: Record<string, string>,
32+
beforeConfig?: PluginExtended,
33+
afterDeclarationsConfig?: PluginExtended,
2734
): ts.CustomTransformers {
2835
return {
2936
...(beforeConfig && { before: [transformer(program, beforeConfig)] }),

src/transformer.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import { Minimatch } from "minimatch";
77
import { createSyntheticEmitHost, getTsNodeRegistrationProperties } from "./utils/ts-helpers";
88
import { TransformerExtras } from "ts-patch";
99

10-
/* ****************************************************************************************************************** */
11-
// region: Helpers
12-
/* ****************************************************************************************************************** */
13-
1410
function getTsProperties(args: Parameters<typeof transformer>) {
1511
let fileNames: readonly string[] | undefined;
1612
let compilerOptions: CompilerOptions;
@@ -69,12 +65,6 @@ function getTsProperties(args: Parameters<typeof transformer>) {
6965
return { tsInstance, compilerOptions, fileNames, runMode, tsNodeState };
7066
}
7167

72-
// endregion
73-
74-
/* ****************************************************************************************************************** */
75-
// region: Transformer
76-
/* ****************************************************************************************************************** */
77-
7868
export default function transformer(
7969
program?: ts.Program,
8070
pluginConfig?: TsTransformPathsConfig,
@@ -114,6 +104,8 @@ export default function transformer(
114104
const { tryParsePatterns } = tsInstance;
115105
const [tsVersionMajor, tsVersionMinor] = tsInstance.versionMajorMinor.split(".").map((v) => +v);
116106

107+
if (!tsVersionMajor || !tsVersionMinor) throw new Error("Expected version to be parsed");
108+
117109
const tsTransformPathsContext: TsTransformPathsContext = {
118110
compilerOptions,
119111
config,
@@ -154,5 +146,3 @@ export default function transformer(
154146
};
155147
};
156148
}
157-
158-
// endregion

src/utils/elide-import-export.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export function elideImportOrExportDeclaration(
131131
}
132132

133133
const allowEmpty =
134-
!!compilerOptions.verbatimModuleSyntax ||
134+
!!compilerOptions["verbatimModuleSyntax"] ||
135135
(!!node.moduleSpecifier &&
136136
(compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve ||
137137
compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Error));
@@ -187,7 +187,7 @@ export function elideImportOrExportDeclaration(
187187
} else {
188188
// Elide named imports if all of its import specifiers are elided.
189189
const allowEmpty =
190-
compilerOptions.verbatimModuleSyntax ||
190+
compilerOptions["verbatimModuleSyntax"] ||
191191
(compilerOptions.preserveValueImports &&
192192
(compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve ||
193193
compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Error));
@@ -234,14 +234,14 @@ export function elideImportOrExportDeclaration(
234234
*/
235235
function visitExportSpecifier(node: ExportSpecifier): VisitResult<ExportSpecifier> {
236236
// Elide an export specifier if it does not reference a value.
237-
return !node.isTypeOnly && (compilerOptions.verbatimModuleSyntax || resolver.isValueAliasDeclaration(node))
237+
return !node.isTypeOnly && (compilerOptions["verbatimModuleSyntax"] || resolver.isValueAliasDeclaration(node))
238238
? node
239239
: undefined;
240240
}
241241

242242
function shouldEmitAliasDeclaration(node: Node): boolean {
243243
return (
244-
!!compilerOptions.verbatimModuleSyntax ||
244+
!!compilerOptions["verbatimModuleSyntax"] ||
245245
isInJSFile(node) ||
246246
(compilerOptions.preserveValueImports
247247
? resolver.isValueAliasDeclaration(node)

src/utils/general-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import path from "path";
55
* General Utilities & Helpers
66
* ****************************************************************************************************************** */
77

8-
export const isURL = (s: string): boolean => !!s && (!!url.parse(s).host || !!url.parse(s).hostname);
8+
export const isURL = (s: string | undefined): s is string => !!s && (!!url.parse(s).host || !!url.parse(s).hostname);
99

1010
export const isBaseDir = (baseDir: string, testDir: string): boolean => {
1111
const relative = path.relative(baseDir, testDir);

src/utils/resolve-module-name.ts

+2-20
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import { removeFileExtension, removeSuffix, ResolvedModuleFull, SourceFile } fro
55
import { getOutputDirForSourceFile } from "./ts-helpers";
66
import { getRelativePath } from "./get-relative-path";
77

8-
/* ****************************************************************************************************************** */
9-
// region: Types
10-
/* ****************************************************************************************************************** */
11-
128
export interface ResolvedModule {
139
/** Absolute path to resolved module */
1410
resolvedPath: string | undefined;
@@ -25,12 +21,6 @@ enum IndexType {
2521
ImplicitPackage,
2622
}
2723

28-
// endregion
29-
30-
/* ****************************************************************************************************************** */
31-
// region: Helpers
32-
/* ****************************************************************************************************************** */
33-
3424
function getPathDetail(moduleName: string, resolvedModule: ResolvedModuleFull) {
3525
const resolvedFileName = resolvedModule.originalPath ?? resolvedModule.resolvedFileName;
3626
const implicitPackageIndex = resolvedModule.packageId?.subModuleName;
@@ -105,12 +95,6 @@ function getResolvedSourceFile(context: VisitorContext, fileName: string): Sourc
10595
return tsInstance.createSourceFile(fileName, ``, tsInstance.ScriptTarget.ESNext, /* setParentNodes */ false);
10696
}
10797

108-
// endregion
109-
110-
/* ****************************************************************************************************************** */
111-
// region: Utils
112-
/* ****************************************************************************************************************** */
113-
11498
/** Resolve a module name */
11599
export function resolveModuleName(context: VisitorContext, moduleName: string): ResolvedModule | undefined {
116100
const { tsInstance, compilerOptions, sourceFile, config, rootDirs } = context;
@@ -126,10 +110,10 @@ export function resolveModuleName(context: VisitorContext, moduleName: string):
126110
// Handle non-resolvable module
127111
if (!resolvedModule) {
128112
const maybeURL = failedLookupLocations[0];
129-
if (!isURL(maybeURL)) return void 0;
113+
if (!isURL(maybeURL)) return undefined;
130114
return {
131115
isURL: true,
132-
resolvedPath: void 0,
116+
resolvedPath: undefined,
133117
outputPath: maybeURL,
134118
};
135119
}
@@ -172,5 +156,3 @@ export function resolveModuleName(context: VisitorContext, moduleName: string):
172156

173157
return { isURL: false, outputPath, resolvedPath: resolvedFileName };
174158
}
175-
176-
// endregion

src/utils/resolve-path-update-node.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { isURL, maybeAddRelativeLocalPrefix } from "./general-utils";
44
import { isModulePathsMatch } from "./ts-helpers";
55
import { resolveModuleName } from "./resolve-module-name";
66

7-
/* ****************************************************************************************************************** */
8-
// region: Node Updater Utility
9-
/* ****************************************************************************************************************** */
10-
117
/** Gets proper path and calls updaterFn to get the new node if it should be updated */
128
export function resolvePathAndUpdateNode(
139
context: VisitorContext,
@@ -69,7 +65,8 @@ export function resolvePathAndUpdateNode(
6965
const trivia = targetNode.getFullText(sourceFile).slice(0, targetNode.getLeadingTriviaWidth(sourceFile));
7066
const regex = /^\s*\/\/\/?\s*@(transform-path|no-transform-path)(?:[^\S\r\n](.+?))?$/gim;
7167

72-
for (let match = regex.exec(trivia); match; match = regex.exec(trivia)) commentTags.set(match[1], match[2]);
68+
for (let match = regex.exec(trivia); match; match = regex.exec(trivia))
69+
if (match[1]) commentTags.set(match[1], match[2]);
7370
} catch {}
7471
}
7572

@@ -83,7 +80,7 @@ export function resolvePathAndUpdateNode(
8380

8481
function findTag(expected: string): boolean | string | undefined {
8582
if (commentTags.has(expected)) return commentTags.get(expected) || true;
86-
if (!jsDocTags?.length) return void 0;
83+
if (!jsDocTags?.length) return undefined;
8784

8885
for (const tag of jsDocTags) {
8986
const tagName = tag.tagName.text.toLowerCase();
@@ -103,8 +100,8 @@ export function resolvePathAndUpdateNode(
103100
: void 0;
104101
}
105102
}
103+
104+
return undefined;
106105
}
107106
}
108107
}
109-
110-
// endregion

src/visitor.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@ import ts from "typescript";
22
import { VisitorContext } from "./types";
33
import { elideImportOrExportDeclaration, resolvePathAndUpdateNode } from "./utils";
44

5-
/* ****************************************************************************************************************** *
6-
* Helpers
7-
* ****************************************************************************************************************** */
8-
95
const isAsyncImport = ({ tsInstance }: VisitorContext, node: ts.Node): node is ts.CallExpression =>
106
tsInstance.isCallExpression(node) &&
117
node.expression.kind === tsInstance.SyntaxKind.ImportKeyword &&
8+
!!node.arguments[0] &&
129
tsInstance.isStringLiteral(node.arguments[0]) &&
1310
node.arguments.length === 1;
1411

1512
const isRequire = ({ tsInstance }: VisitorContext, node: ts.Node): node is ts.CallExpression =>
1613
tsInstance.isCallExpression(node) &&
1714
tsInstance.isIdentifier(node.expression) &&
1815
node.expression.text === "require" &&
16+
!!node.arguments[0] &&
1917
tsInstance.isStringLiteral(node.arguments[0]) &&
2018
node.arguments.length === 1;
2119

22-
/* ****************************************************************************************************************** *
23-
* Node Visitor
24-
* ****************************************************************************************************************** */
25-
2620
/** Visit and replace nodes with module specifiers */
2721
export function nodeVisitor(this: VisitorContext, node: ts.Node): ts.Node | undefined {
2822
const { factory, tsInstance, transformationContext } = this;
@@ -40,7 +34,8 @@ export function nodeVisitor(this: VisitorContext, node: ts.Node): ts.Node | unde
4034

4135
/* Handle comments */
4236
const textNode = node.arguments[0];
43-
const commentRanges = tsInstance.getLeadingCommentRanges(textNode.getFullText(), 0) || [];
37+
if (!textNode) throw new Error("Expected textNode");
38+
const commentRanges = tsInstance.getLeadingCommentRanges(textNode.getFullText(), 0) ?? [];
4439

4540
for (const range of commentRanges) {
4641
const { kind, pos, end, hasTrailingNewLine } = range;

tsconfig.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
2-
"extends": "@tsconfig/node18",
2+
"extends": ["@tsconfig/strictest", "@tsconfig/node18"],
33
"include": ["src"],
44
"exclude": ["src/declarations"],
55
"compilerOptions": {
66
"rootDir": "src",
77
"outDir": "dist",
88
"declaration": true,
9-
"sourceMap": true
9+
"sourceMap": true,
10+
// less strict
11+
"exactOptionalPropertyTypes": false
1012
}
1113
}

yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ __metadata:
217217
languageName: node
218218
linkType: hard
219219

220+
"@tsconfig/strictest@npm:^2.0.5":
221+
version: 2.0.5
222+
resolution: "@tsconfig/strictest@npm:2.0.5"
223+
checksum: 10c0/cfc86da2d57f7b4b0827701b132c37a4974284e5c40649656c0e474866dfd8a69f57c6718230d8a8139967e2a95438586b8224c13ab0ff9d3a43eda771c50cc4
224+
languageName: node
225+
linkType: hard
226+
220227
"@types/debug@npm:^4.0.0":
221228
version: 4.1.12
222229
resolution: "@types/debug@npm:4.1.12"
@@ -3019,6 +3026,7 @@ __metadata:
30193026
dependencies:
30203027
"@eslint/js": "npm:^9.8.0"
30213028
"@tsconfig/node18": "npm:^18.2.4"
3029+
"@tsconfig/strictest": "npm:^2.0.5"
30223030
"@types/eslint": "npm:^9"
30233031
"@types/minimatch": "npm:^5.1.2"
30243032
"@types/node": "npm:^22.2.0"

0 commit comments

Comments
 (0)