Skip to content

Commit 0b5fd4c

Browse files
committedAug 18, 2024
refactor: add prettier-plugin-jsdoc
1 parent 6f0a280 commit 0b5fd4c

14 files changed

+425
-90
lines changed
 

‎package.json

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"eslint": "9.x",
7474
"globals": "^15.9.0",
7575
"prettier": "^3.3.3",
76+
"prettier-plugin-jsdoc": "^1.3.0",
7677
"ts-patch": "^3.2.1",
7778
"typescript": "^5.5.4",
7879
"typescript-eslint": "^8.0.1"
@@ -83,5 +84,10 @@
8384
"dependencies": {
8485
"minimatch": "^10.0.1"
8586
},
87+
"prettier": {
88+
"plugins": [
89+
"prettier-plugin-jsdoc"
90+
]
91+
},
8692
"packageManager": "yarn@4.4.0+sha512.91d93b445d9284e7ed52931369bc89a663414e5582d00eea45c67ddc459a2582919eece27c412d6ffd1bd0793ff35399381cb229326b961798ce4f4cc60ddfdb"
8793
}

‎src/harmony/harmony-factory.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ export interface HarmonyFactory extends TS.NodeFactory {}
1414
// region: Utilities
1515
/* ****************************************************************************************************************** */
1616

17-
/**
18-
* Creates a node factory compatible with TS v3+
19-
*/
17+
/** Creates a node factory compatible with TS v3+ */
2018
export function createHarmonyFactory(context: TsTransformPathsContext): HarmonyFactory {
2119
return new Proxy(context.tsFactory ?? context.tsInstance, {
2220
get(target, prop) {

‎src/harmony/versions/four-seven.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* Changes after this point: https://github.com/microsoft/TypeScript/wiki/API-Breaking-Changes#typescript-48
3-
*/
1+
/** Changes after this point: https://github.com/microsoft/TypeScript/wiki/API-Breaking-Changes#typescript-48 */
42
import type {
53
default as TsCurrentModule,
64
AssertClause,

‎src/harmony/versions/three-eight.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* Changes after this point: https://github.com/microsoft/TypeScript/wiki/API-Breaking-Changes#typescript-40
3-
*/
1+
/** Changes after this point: https://github.com/microsoft/TypeScript/wiki/API-Breaking-Changes#typescript-40 */
42
import type {
53
default as TsCurrentModule,
64
EntityName,

‎src/transformer.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ export default function transformer(
7979
program?: ts.Program,
8080
pluginConfig?: TsTransformPathsConfig,
8181
transformerExtras?: TransformerExtras,
82-
/**
83-
* Supply if manually transforming with compiler API via 'transformNodes' / 'transformModule'
84-
*/
82+
/** Supply if manually transforming with compiler API via 'transformNodes' / 'transformModule' */
8583
manualTransformOptions?: {
8684
compilerOptions?: ts.CompilerOptions;
8785
fileNames?: string[];

‎src/types.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ export interface TsTransformPathsConfig extends PluginConfig {
2929
/* ****************************************************************************************************************** */
3030

3131
export interface TsTransformPathsContext {
32-
/**
33-
* TS Instance passed from ts-patch / ttypescript
34-
*/
32+
/** TS Instance passed from ts-patch / ttypescript */
3533
readonly tsInstance: typeof ts;
3634
readonly tsVersionMajor: number;
3735
readonly tsVersionMinor: number;

‎src/utils/elide-import-export.ts

+21-25
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
/**
2-
* ----------------------------------------------------------------------------
32
* UPDATE:
43
*
54
* TODO - In next major version, we can remove this file entirely due to TS PR 57223
65
* https://github.com/microsoft/TypeScript/pull/57223
7-
* ----------------------------------------------------------------------------
86
*
9-
* This file and its contents are due to an issue in TypeScript (affecting *at least* up to 4.1) which causes type
7+
* This file and its contents are due to an issue in TypeScript (affecting _at least_ up to 4.1) which causes type
108
* elision to break during emit for nodes which have been transformed. Specifically, if the 'original' property is set,
119
* elision functionality no longer works.
1210
*
13-
* This results in module specifiers for types being output in import/export declarations in the compiled *JS files*
11+
* This results in module specifiers for types being output in import/export declarations in the compiled _JS files_
1412
*
1513
* The logic herein compensates for that issue by recreating type elision separately so that the transformer can update
1614
* the clause with the properly elided information
1715
*
1816
* Issues:
19-
* @see https://github.com/LeDDGroup/typescript-transform-paths/issues/184
20-
* @see https://github.com/microsoft/TypeScript/issues/40603
21-
* @see https://github.com/microsoft/TypeScript/issues/31446
17+
*
18+
* - See https://github.com/LeDDGroup/typescript-transform-paths/issues/184
19+
* - See https://github.com/microsoft/TypeScript/issues/40603
20+
* - See https://github.com/microsoft/TypeScript/issues/31446
2221
*
2322
* @example
24-
* // a.ts
25-
* export type A = string
26-
* export const B = 2
23+
* // a.ts
24+
* export type A = string;
25+
* export const B = 2;
2726
*
28-
* // b.ts
29-
* import { A, B } from './b'
30-
* export { A } from './b'
27+
* // b.ts
28+
* import { A, B } from "./b";
29+
* export { A } from "./b";
3130
*
32-
* // Expected output for b.js
33-
* import { B } from './b'
31+
* // Expected output for b.js
32+
* import { B } from "./b";
3433
*
35-
* // Actual output for b.js
36-
* import { A, B } from './b'
37-
* export { A } from './b'
34+
* // Actual output for b.js
35+
* import { A, B } from "./b";
36+
* export { A } from "./b";
3837
*/
3938
import { ImportOrExportDeclaration, VisitorContext } from "../types";
4039
import {
@@ -60,10 +59,10 @@ import {
6059
/* ****************************************************************************************************************** */
6160

6261
/**
63-
* Get import / export clause for node (replicates TS elision behaviour for js files)
64-
* See notes in get-import-export-clause.ts header for why this is necessary
62+
* Get import / export clause for node (replicates TS elision behaviour for js files) See notes in
63+
* get-import-export-clause.ts header for why this is necessary
6564
*
66-
* @returns import or export clause or undefined if it entire declaration should be elided
65+
* @returns Import or export clause or undefined if it entire declaration should be elided
6766
*/
6867
export function elideImportOrExportDeclaration<T extends ImportOrExportDeclaration>(
6968
context: VisitorContext,
@@ -208,10 +207,7 @@ export function elideImportOrExportDeclaration(
208207
return !node.isTypeOnly && shouldEmitAliasDeclaration(node) ? node : undefined;
209208
}
210209

211-
/**
212-
* Visits named exports, eliding it if it does not contain an export specifier that
213-
* resolves to a value.
214-
*/
210+
/** Visits named exports, eliding it if it does not contain an export specifier that resolves to a value. */
215211
function visitNamedExports(node: NamedExports, allowEmpty: boolean): VisitResult<NamedExports> | undefined {
216212
// Elide the named exports if all of its export specifiers were elided.
217213
const elements = visitNodes(node.elements, <Visitor>visitExportSpecifier, isExportSpecifier);

‎src/utils/resolve-module-name.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,11 @@ import { getRelativePath } from "./get-relative-path";
1010
/* ****************************************************************************************************************** */
1111

1212
export interface ResolvedModule {
13-
/**
14-
* Absolute path to resolved module
15-
*/
13+
/** Absolute path to resolved module */
1614
resolvedPath: string | undefined;
17-
/**
18-
* Output path
19-
*/
15+
/** Output path */
2016
outputPath: string;
21-
/**
22-
* Resolved to URL
23-
*/
17+
/** Resolved to URL */
2418
isURL: boolean;
2519
}
2620

@@ -117,9 +111,7 @@ function getResolvedSourceFile(context: VisitorContext, fileName: string): Sourc
117111
// region: Utils
118112
/* ****************************************************************************************************************** */
119113

120-
/**
121-
* Resolve a module name
122-
*/
114+
/** Resolve a module name */
123115
export function resolveModuleName(context: VisitorContext, moduleName: string): ResolvedModule | undefined {
124116
const { tsInstance, compilerOptions, sourceFile, config, rootDirs } = context;
125117

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import { resolveModuleName } from "./resolve-module-name";
88
// region: Node Updater Utility
99
/* ****************************************************************************************************************** */
1010

11-
/**
12-
* Gets proper path and calls updaterFn to get the new node if it should be updated
13-
*/
11+
/** Gets proper path and calls updaterFn to get the new node if it should be updated */
1412
export function resolvePathAndUpdateNode(
1513
context: VisitorContext,
1614
node: ts.Node,

‎src/utils/ts-helpers.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import type { REGISTER_INSTANCE } from "ts-node";
77
// region: TS Helpers
88
/* ****************************************************************************************************************** */
99

10-
/**
11-
* Determine output file path for source file
12-
*/
10+
/** Determine output file path for source file */
1311
export function getOutputDirForSourceFile(context: VisitorContext, sourceFile: SourceFile): string {
1412
const {
1513
tsInstance,
@@ -45,9 +43,7 @@ export function getOutputDirForSourceFile(context: VisitorContext, sourceFile: S
4543
return tsInstance.normalizePath(res);
4644
}
4745

48-
/**
49-
* Determine if moduleName matches config in paths
50-
*/
46+
/** Determine if moduleName matches config in paths */
5147
export function isModulePathsMatch(context: VisitorContext, moduleName: string): boolean {
5248
const {
5349
pathsPatterns,
@@ -56,9 +52,7 @@ export function isModulePathsMatch(context: VisitorContext, moduleName: string):
5652
return !!(pathsPatterns && matchPatternOrExact(pathsPatterns as readonly string[], moduleName));
5753
}
5854

59-
/**
60-
* Create barebones EmitHost (for no-Program transform)
61-
*/
55+
/** Create barebones EmitHost (for no-Program transform) */
6256
export function createSyntheticEmitHost(
6357
compilerOptions: ts.CompilerOptions,
6458
tsInstance: typeof ts,
@@ -77,9 +71,7 @@ export function createSyntheticEmitHost(
7771
} as unknown as ts.EmitHost;
7872
}
7973

80-
/**
81-
* Get ts-node register info
82-
*/
74+
/** Get ts-node register info */
8375
export function getTsNodeRegistrationProperties(tsInstance: typeof ts) {
8476
let tsNodeSymbol: typeof REGISTER_INSTANCE;
8577
try {

‎src/visitor.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ const isRequire = ({ tsInstance }: VisitorContext, node: ts.Node): node is ts.Ca
2323
* Node Visitor
2424
* ****************************************************************************************************************** */
2525

26-
/**
27-
* Visit and replace nodes with module specifiers
28-
*/
26+
/** Visit and replace nodes with module specifiers */
2927
export function nodeVisitor(this: VisitorContext, node: ts.Node): ts.Node | undefined {
3028
const { factory, tsInstance, transformationContext } = this;
3129

3230
/**
3331
* Update require / import functions
3432
*
35-
* require('module')
36-
* import('module')
33+
* @example
34+
* require("module");
35+
* import("module");
3736
*/
3837
if (isRequire(this, node) || isAsyncImport(this, node))
3938
return resolvePathAndUpdateNode(this, node, (<ts.StringLiteral>node.arguments[0]).text, (p) => {
@@ -67,7 +66,8 @@ export function nodeVisitor(this: VisitorContext, node: ts.Node): ts.Node | unde
6766
/**
6867
* Update ExternalModuleReference
6968
*
70-
* import foo = require("foo");
69+
* @example
70+
* import foo = require("foo");
7171
*/
7272
if (tsInstance.isExternalModuleReference(node) && tsInstance.isStringLiteral(node.expression))
7373
return resolvePathAndUpdateNode(this, node, node.expression.text, (p) =>
@@ -77,8 +77,9 @@ export function nodeVisitor(this: VisitorContext, node: ts.Node): ts.Node | unde
7777
/**
7878
* Update ImportTypeNode
7979
*
80-
* typeof import("./bar");
81-
* import ("package").MyType;
80+
* @example
81+
* typeof import("./bar");
82+
* import("package").MyType;
8283
*/
8384
if (tsInstance.isImportTypeNode(node)) {
8485
const argument = node.argument as ts.LiteralTypeNode;
@@ -104,7 +105,8 @@ export function nodeVisitor(this: VisitorContext, node: ts.Node): ts.Node | unde
104105
/**
105106
* Update ImportDeclaration
106107
*
107-
* import ... 'module';
108+
* @example
109+
* import ... 'module';
108110
*/
109111
if (tsInstance.isImportDeclaration(node) && node.moduleSpecifier && tsInstance.isStringLiteral(node.moduleSpecifier))
110112
return resolvePathAndUpdateNode(this, node, node.moduleSpecifier.text, (p) => {
@@ -123,7 +125,8 @@ export function nodeVisitor(this: VisitorContext, node: ts.Node): ts.Node | unde
123125
/**
124126
* Update ExportDeclaration
125127
*
126-
* export ... 'module';
128+
* @example
129+
* export ... 'module';
127130
*/
128131
if (tsInstance.isExportDeclaration(node) && node.moduleSpecifier && tsInstance.isStringLiteral(node.moduleSpecifier))
129132
return resolvePathAndUpdateNode(this, node, node.moduleSpecifier.text, (p) => {
@@ -146,9 +149,7 @@ export function nodeVisitor(this: VisitorContext, node: ts.Node): ts.Node | unde
146149
);
147150
});
148151

149-
/**
150-
* Update module augmentation
151-
*/
152+
/** Update module augmentation */
152153
if (tsInstance.isModuleDeclaration(node) && tsInstance.isStringLiteral(node.name))
153154
return resolvePathAndUpdateNode(this, node, node.name.text, (p) =>
154155
factory.updateModuleDeclaration(node, node.modifiers, p, node.body),

‎test/projects/specific/src/tags.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
* JSDoc
33
* ****************************************************************************************************************** */
44

5-
/**
6-
* @no-transform-path
7-
*/
5+
/** @no-transform-path */
86
import * as skipTransform1 from "#root/index";
97

108
/**

‎test/utils/helpers.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ function createReadFile(
6363
// region: Utilities
6464
/* ****************************************************************************************************************** */
6565

66-
/**
67-
* Create TS Program with faux files and options
68-
*/
66+
/** Create TS Program with faux files and options */
6967
export function createTsProgram(
7068
opt: CreateTsProgramOptions,
7169
transformerPath: string = config.transformerPath,
@@ -148,9 +146,7 @@ export function createTsSolutionBuilder(
148146
});
149147
}
150148

151-
/**
152-
* Get emitted files for program
153-
*/
149+
/** Get emitted files for program */
154150
export function getEmitResultFromProgram(program: ts.Program): EmittedFiles {
155151
const outputFiles: EmittedFiles = {};
156152
program.emit(undefined, createWriteFile(outputFiles));

0 commit comments

Comments
 (0)