Skip to content

Commit 37b75fb

Browse files
committed
Update tsconfig.json
1 parent 1a84670 commit 37b75fb

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

lib/index.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @typedef {number|string} Mode
77
* @typedef {BufferEncoding|{encoding?: null|BufferEncoding, flag?: string}} ReadOptions
8-
* @typedef {BufferEncoding|{encoding?: null|BufferEncoding, mode: Mode?, flag?: string}} WriteOptions
8+
* @typedef {BufferEncoding|{encoding?: null|undefined|BufferEncoding, mode?: Mode | undefined, flag?: string | undefined}} WriteOptions
99
*
1010
* @typedef {URL|Value} Path
1111
* Path of the file.
@@ -17,7 +17,7 @@
1717
/**
1818
* @callback Callback
1919
* @param {NodeJS.ErrnoException|null} error
20-
* @param {VFile|null} file
20+
* @param {VFile | null | undefined} file
2121
*/
2222

2323
import fs from 'fs'
@@ -84,7 +84,7 @@ export const read =
8484
* Create a virtual file and read it in, asynchronously.
8585
*
8686
* @param {Compatible} description
87-
* @param {ReadOptions} [options]
87+
* @param {ReadOptions | null} [options]
8888
* @param {Callback} [callback]
8989
*/
9090
function (description, options, callback) {
@@ -105,12 +105,13 @@ export const read =
105105
* @param {VFile} result
106106
*/
107107
function resolve(result) {
108+
// @ts-expect-error: `callback` always defined.
108109
callback(null, result)
109110
}
110111

111112
/**
112-
* @param {(x: VFile) => void} resolve
113-
* @param {(x: Error, y?: VFile) => void} reject
113+
* @param {(error: VFile) => void} resolve
114+
* @param {(error: NodeJS.ErrnoException, file?: VFile | undefined) => void} reject
114115
*/
115116
function executor(resolve, reject) {
116117
/** @type {string} */
@@ -119,13 +120,14 @@ export const read =
119120
try {
120121
fp = path.resolve(file.cwd, file.path)
121122
} catch (error) {
122-
return reject(error)
123+
const exception = /** @type {NodeJS.ErrnoException} */ (error)
124+
return reject(exception)
123125
}
124126

125127
fs.readFile(fp, options, done)
126128

127129
/**
128-
* @param {Error} error
130+
* @param {NodeJS.ErrnoException | null} error
129131
* @param {Value} result
130132
*/
131133
function done(error, result) {
@@ -175,12 +177,13 @@ export const write =
175177
* @param {VFile} result
176178
*/
177179
function resolve(result) {
180+
// @ts-expect-error: `callback` always defined.
178181
callback(null, result)
179182
}
180183

181184
/**
182-
* @param {(x: VFile) => void} resolve
183-
* @param {(x: Error, y?: VFile) => void} reject
185+
* @param {(error: VFile) => void} resolve
186+
* @param {(error: NodeJS.ErrnoException, file: VFile | null) => void} reject
184187
*/
185188
function executor(resolve, reject) {
186189
/** @type {string} */
@@ -189,17 +192,18 @@ export const write =
189192
try {
190193
fp = path.resolve(file.cwd, file.path)
191194
} catch (error) {
192-
return reject(error)
195+
const exception = /** @type {NodeJS.ErrnoException} */ (error)
196+
return reject(exception, null)
193197
}
194198

195-
fs.writeFile(fp, file.value || '', options, done)
199+
fs.writeFile(fp, file.value || '', options || null, done)
196200

197201
/**
198-
* @param {Error} error
202+
* @param {NodeJS.ErrnoException | null} error
199203
*/
200204
function done(error) {
201205
if (error) {
202-
reject(error)
206+
reject(error, null)
203207
} else {
204208
resolve(file)
205209
}
@@ -209,15 +213,15 @@ export const write =
209213
)
210214

211215
/**
212-
* @param {Compatible} value
216+
* @param {Compatible | undefined} value
213217
* @returns {value is VFile}
214218
*/
215219
function looksLikeAVFile(value) {
216-
return (
220+
return Boolean(
217221
value &&
218-
typeof value === 'object' &&
219-
'message' in value &&
220-
'messages' in value
222+
typeof value === 'object' &&
223+
'message' in value &&
224+
'messages' in value
221225
)
222226
}
223227

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,17 @@
4444
"prettier": "^2.0.0",
4545
"remark-cli": "^11.0.0",
4646
"remark-preset-wooorm": "^9.0.0",
47-
"rimraf": "^3.0.0",
4847
"tape": "^5.0.0",
4948
"type-coverage": "^2.0.0",
5049
"typescript": "^4.0.0",
5150
"xo": "^0.53.0"
5251
},
5352
"scripts": {
5453
"prepack": "npm run build && npm run format",
55-
"build": "rimraf \"{lib/**,}*.d.ts\" && tsc && type-coverage",
54+
"build": "tsc --build --clean && tsc --build && type-coverage",
5655
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
57-
"test-api": "node test.js",
58-
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
56+
"test-api": "node --conditions development test.js",
57+
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
5958
"test": "npm run build && npm run format && npm run test-coverage"
6059
},
6160
"prettier": {

test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert/strict'
12
import {Buffer} from 'node:buffer'
23
import fs from 'node:fs'
34
import path from 'node:path'
@@ -75,7 +76,7 @@ test('toVFile.readSync', (t) => {
7576

7677
t.test('should fail without path', (t) => {
7778
t.throws(() => {
78-
// @ts-ignore runtime.
79+
// @ts-expect-error runtime.
7980
toVFile.readSync()
8081
}, /path/i)
8182

@@ -146,6 +147,7 @@ test('toVFile.read', (t) => {
146147
t.test('should pass an error without path', (t) => {
147148
t.plan(1)
148149

150+
// @ts-expect-error: not a path.
149151
toVFile.read(null, (error) => {
150152
t.ok(/path/i.test(String(error)))
151153
})
@@ -156,6 +158,7 @@ test('toVFile.read', (t) => {
156158

157159
toVFile.read('readme.md', (error, file) => {
158160
t.ifErr(error)
161+
assert(file, 'expected file')
159162
t.equal(file.path, 'readme.md')
160163
t.ok(buffer(file.value))
161164
t.equal(file.toString(), fixture)
@@ -182,6 +185,7 @@ test('toVFile.read', (t) => {
182185

183186
toVFile.read('readme.md', 'utf8', (error, file) => {
184187
t.ifErr(error)
188+
assert(file, 'expected file')
185189
t.equal(file.path, 'readme.md')
186190
t.equal(typeof file.value, 'string')
187191
t.equal(file.toString(), fixture)
@@ -207,6 +211,7 @@ test('toVFile.read', (t) => {
207211
t.plan(3)
208212

209213
toVFile.read('missing.md', 'utf8', (error, file) => {
214+
assert(error, 'expected error')
210215
t.equal(file, undefined)
211216
t.ok(error instanceof Error)
212217
t.ok(/ENOENT/.test(error.message))
@@ -236,7 +241,7 @@ test('toVFile.writeSync', (t) => {
236241

237242
t.test('should fail without path', (t) => {
238243
t.throws(() => {
239-
// @ts-ignore runtime.
244+
// @ts-expect-error runtime.
240245
toVFile.writeSync()
241246
}, /path/i)
242247

@@ -296,6 +301,7 @@ test('toVFile.write', (t) => {
296301
t.test('should pass an error without path', (t) => {
297302
t.plan(1)
298303

304+
// @ts-expect-error: missing path.
299305
toVFile.write(null, (error) => {
300306
t.ok(/path/i.test(String(error)))
301307
})
@@ -308,6 +314,7 @@ test('toVFile.write', (t) => {
308314

309315
toVFile.write(file, (error, result) => {
310316
t.ifErr(error)
317+
assert(result, 'expected result')
311318
t.equal(result.path, filePath)
312319
t.equal(fs.readFileSync(filePath, 'utf8'), 'bäz')
313320
})
@@ -320,6 +327,7 @@ test('toVFile.write', (t) => {
320327

321328
toVFile.write(file, (error, result) => {
322329
t.ifErr(error)
330+
assert(result, 'expected result')
323331
t.equal(result.path, filePath)
324332
t.equal(fs.readFileSync(filePath, 'utf8'), 'qüx')
325333
})
@@ -346,6 +354,7 @@ test('toVFile.write', (t) => {
346354

347355
toVFile.write(file, 'hex', (error, result) => {
348356
t.ifErr(error)
357+
assert(result, 'expected result')
349358
t.equal(result.path, filePath)
350359
t.equal(fs.readFileSync(filePath, 'utf8'), 'bär')
351360
})
@@ -373,6 +382,7 @@ test('toVFile.write', (t) => {
373382

374383
fs.unlinkSync(filePath)
375384

385+
assert(result, 'expected result')
376386
t.ifErr(error)
377387
t.equal(result.path, filePath)
378388
t.equal(doc, '')
@@ -401,6 +411,7 @@ test('toVFile.write', (t) => {
401411
t.plan(1)
402412

403413
toVFile.write(invalidFilePath, (error) => {
414+
assert(error, 'expected error')
404415
t.ok(/ENOENT/.test(error.message))
405416
})
406417
})

tsconfig.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
2-
"include": ["*.js", "lib/**/*.js"],
2+
"include": ["**/*.js"],
3+
"exclude": ["coverage/", "node_modules/"],
34
"compilerOptions": {
4-
"target": "ES2020",
5-
"lib": ["ES2020"],
6-
"module": "ES2020",
7-
"moduleResolution": "node",
8-
"allowJs": true,
95
"checkJs": true,
106
"declaration": true,
117
"emitDeclarationOnly": true,
12-
"allowSyntheticDefaultImports": true,
13-
"skipLibCheck": true
8+
"exactOptionalPropertyTypes": true,
9+
"forceConsistentCasingInFileNames": true,
10+
"lib": ["es2020"],
11+
"module": "node16",
12+
"newLine": "lf",
13+
"skipLibCheck": true,
14+
"strict": true,
15+
"target": "es2020"
1416
}
1517
}

0 commit comments

Comments
 (0)