Skip to content

Commit 8b36b2a

Browse files
committed
Check rollup types, add jsdoc-plugin-typescript
I began to add a larger test using Vite and this plugin, and the HandlebarsPrecompiler() function produced type check errors. So it seemed prudent to actually use Rollup types instead of simulating them and instructing users to disable checking. At the same time, I learned I didn't need to disable type checking for my vitest.config.js files after all. Some of the specific changes include: - Using `...(configDefaults.coverage.exclude || [])` as appropriate to avoid type errors due to configDefaults.coverage.exclude potentially being undefined. - Adding 'rollup' as a devDependency and adding `import("rollup").Type` as appropriate in JSDoc `@type` directives. - Adding 'jsdoc-plugin-typescript' to the build to handle the TypeScript `import().Type` directives. This ended up pulling in the 'es-abstract' module, which blew up the pnpm-lock.yaml file. If I get an itch, I'll implement my own plugin one day and replace it. - Removing redundant types from lib/types.js. This included removing PrecompilerOptions, which I realized I was already visible from the 'handlebars' module. - Adding "DOM" to the "lib" field of jsconfig.json. - Added a JSDoc type cast to main.test.js to eliminate type errors. This is safe because we're testing our own object that is known to have certain methods defined.
1 parent 82145e2 commit 8b36b2a

10 files changed

+575
-90
lines changed

index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,27 @@
3838

3939
import PluginImpl, { PLUGIN_NAME } from './lib/index.js'
4040
// eslint-disable-next-line no-unused-vars
41-
import { PluginOptions, Transform } from './lib/types.js'
41+
import { PluginOptions } from './lib/types.js'
4242

4343
/**
4444
* A Rollup plugin object for precompiling Handlebars templates.
4545
* @module rollup-plugin-handlebars-precompiler
4646
*/
4747

48-
/**
49-
* @typedef {object} RollupPlugin
50-
* @property {string} name - plugin name
51-
* @property {Function} resolveId - resolves the plugin's own import ID
52-
* @property {Function} load - emits the plugin's helper module code
53-
* @property {Function} transform - emits JavaScript code compiled from
54-
* Handlebars templates
55-
* @see https://rollupjs.org/plugin-development/
56-
*/
57-
5848
/**
5949
* Returns a Rollup plugin object for precompiling Handlebars templates.
6050
* @function default
6151
* @param {PluginOptions} options - plugin configuration options
62-
* @returns {RollupPlugin} - the configured plugin object
52+
* @returns {import("rollup").Plugin} - the configured plugin object
53+
* @see https://rollupjs.org/plugin-development/
6354
*/
6455
export default function HandlebarsPrecompiler(options) {
6556
const p = new PluginImpl(options)
6657
return {
6758
name: PLUGIN_NAME,
6859

6960
/**
61+
* @type {import("rollup").ResolveIdHook}
7062
* @param {string} id - import identifier to resolve
7163
* @returns {(string | undefined)} - the plugin ID if id matches it
7264
* @see https://rollupjs.org/plugin-development/#resolveid
@@ -76,6 +68,7 @@ export default function HandlebarsPrecompiler(options) {
7668
},
7769

7870
/**
71+
* @type {import("rollup").LoadHook}
7972
* @param {string} id - import identifier to load
8073
* @returns {(string | undefined)} - the plugin helper module if id matches
8174
* @see https://rollupjs.org/plugin-development/#load
@@ -84,7 +77,14 @@ export default function HandlebarsPrecompiler(options) {
8477
return p.shouldEmitHelpersModule(id) ? p.helpersModule() : undefined
8578
},
8679

87-
/** @type {Transform} */
80+
/**
81+
* @type {import("rollup").TransformHook}
82+
* @param {string} code - potential Handlebars template to precompile
83+
* @param {string} id - import identifier of possible Handlebars template
84+
* @returns {(Partial<import("rollup").SourceDescription> | undefined)} -
85+
* the precompiled Handlebars template, if id identifies a template file
86+
* @see https://rollupjs.org/plugin-development/#transform
87+
*/
8888
transform: function (code, id) {
8989
return p.isTemplate(id) ? p.compile(code, id) : undefined
9090
}

jsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"compilerOptions": {
33
"checkJs": true,
44
"lib": [
5-
"ES2022"
5+
"ES2022",
6+
"DOM"
67
],
78
"module": "node16",
89
"target": "es2020",

jsdoc.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
{
2-
"plugins": [ "plugins/markdown" ],
2+
"plugins": [
3+
"plugins/markdown",
4+
"jsdoc-plugin-typescript"
5+
],
36
"recurseDepth": 10,
47
"source": {
58
"includePattern": ".+\\.js$",
69
"exclude": ["node_modules"]
710
},
11+
"typescript": {
12+
"moduleRoot": "."
13+
},
814
"opts": {
915
"destination": "jsdoc",
1016
"recurse": true,

lib/index.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
* obtain one at https://mozilla.org/MPL/2.0/.
3737
*/
3838

39+
/**
40+
* @module rollup-plugin-handlebars-precompiler/lib
41+
*/
42+
3943
import collectPartials from './partials.js'
40-
import {
41-
// eslint-disable-next-line no-unused-vars
42-
Compiled, PartialName, PartialPath, PluginOptions, SourceMap, Transform
43-
} from './types.js'
44+
// eslint-disable-next-line no-unused-vars
45+
import { PartialName, PartialPath, PluginOptions } from './types.js'
4446
import { createFilter } from '@rollup/pluginutils'
4547
import Handlebars from 'handlebars'
4648

@@ -78,7 +80,8 @@ const IMPORT_HELPERS = `import Render from '${PLUGIN_ID}'`
7880
* @param {number} numLinesBeforeTmpl - number of empty lines to add to the
7981
* beginning of the source mappings to account for the generated code before
8082
* the precompiled template
81-
* @returns {SourceMap} - potentially modified Handlebars source map
83+
* @returns {import("rollup").SourceMapInput} - potentially modified Handlebars
84+
* source map
8285
*/
8386

8487
/**
@@ -156,7 +159,18 @@ export default class PluginImpl {
156159
*/
157160
isTemplate(id) { return this.#isTemplate(id) }
158161

159-
/** @type {Transform} */
162+
/**
163+
* @typedef {object} Compiled
164+
* @property {string} code - the precompiled Handlebars template code
165+
* @property {string} map - the Handlebars source map as a JSON string
166+
*/
167+
168+
/**
169+
* @param {string} code - Handlebars template source to precompile
170+
* @param {string} id - file path used to import the Handlebars template
171+
* @returns {Partial<import("rollup").SourceDescription>} - JavaScript
172+
* precompiled from a Handlebars template
173+
*/
160174
compile(code, id) {
161175
const opts = this.#compilerOpts(id)
162176
const ast = Handlebars.parse(code, opts)

lib/partials.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
* obtain one at https://mozilla.org/MPL/2.0/.
3737
*/
3838

39+
/**
40+
* @module rollup-plugin-handlebars-precompiler/partials
41+
*/
42+
3943
import Handlebars from 'handlebars'
4044

4145
/**

lib/types.js

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
* obtain one at https://mozilla.org/MPL/2.0/.
3737
*/
3838

39+
/**
40+
* @module rollup-plugin-handlebars-precompiler/types
41+
*/
42+
3943
/**
4044
* @callback PartialName
4145
* @param {string} id - import identifier of a Handlebars partial template
@@ -53,16 +57,6 @@ export let PartialName
5357
/** @type {PartialPath} */
5458
export let PartialPath
5559

56-
/**
57-
* An approximation of Handlebars's PrecompileOptions
58-
* @typedef {object} PrecompileOptions
59-
* @property {string} [srcName] - input file path used to generate source map;
60-
* should not be set, and will be deleted if present
61-
* @property {string} [destName] - destination file path used to generate source
62-
* map; should not be set, and will be deleted if present
63-
* @see https://handlebarsjs.com/api-reference/compilation.html
64-
*/
65-
6660
/**
6761
* @typedef {object} PluginOptions
6862
* @property {string[]} [helpers] - an array of file paths to modules containing
@@ -77,45 +71,11 @@ export let PartialPath
7771
* name into the name used to apply the partial in other templates
7872
* @property {PartialPath} [partialPath] - function to transform a partial's
7973
* name and that of the module importing it into its import path
80-
* @property {PrecompileOptions} [compiler] - compiler options passed through to
81-
* Handlebars.parse() and Handlebars.precompile()
74+
* @property {PrecompileOptions} [compiler] - Handlebars compiler options passed
75+
* through to Handlebars.parse() and Handlebars.precompile()
8276
* @property {boolean} [sourcemap] - disables source map generation when false
8377
* @property {boolean} [sourceMap] - disables source map generation when false
78+
* @see https://handlebarsjs.com/api-reference/compilation.html
8479
*/
8580
/** @type {PluginOptions} */
8681
export let PluginOptions
87-
88-
/**
89-
* @typedef {object} Compiled
90-
* @property {string} code - the precompiled Handlebars template code
91-
* @property {string} map - the Handlebars source map as a JSON string
92-
*/
93-
/** @type {Compiled} */
94-
export let Compiled
95-
96-
/**
97-
* @typedef {object} SourceMap - a source map for transformed source code
98-
* @property {string} mappings - encoded mapping data
99-
* @see https://sourcemaps.info/spec.html
100-
*/
101-
/** @type {SourceMap} */
102-
export let SourceMap
103-
104-
/**
105-
* @typedef {object} TransformResult - result from RollupPlugin.transform()
106-
* @property {string} code - the transformed source code
107-
* @property {SourceMap} map - the source map for the transformed source code
108-
*/
109-
/** @type {TransformResult} */
110-
export let TransformResult
111-
112-
/**
113-
* @callback Transform
114-
* @param {string} code - source code to potentially transform
115-
* @param {string} id - import ID of source file
116-
* @returns {(TransformResult | undefined)} - JavaScript precompiled from a
117-
* Handlebars template, if id matches the configured template filter
118-
* @see https://rollupjs.org/plugin-development/#transform
119-
*/
120-
/** @type {Transform} */
121-
export let Transform

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
"eslint-plugin-vitest": "^0.3.20",
4444
"jsdoc": "^4.0.2",
4545
"jsdoc-cli-wrapper": "^1.0.4",
46+
"jsdoc-plugin-typescript": "^2.2.1",
47+
"rollup": "^4.9.4",
4648
"typescript": "^5.3.3",
4749
"vitest": "^1.1.3"
4850
},

0 commit comments

Comments
 (0)