Skip to content

Commit 524ab52

Browse files
committed
Update for TS7
I am testing Typescript 7's JS support, which I've largely rewritten during the switch to Go. The new code processes tags by converting them into synthetic nodes just after parsing their host node. For unified, that means that `@template` tags can't be shared between multiple `@overload` tags like before. This PR also has to convert function declarations with `@type` on them to variables initialised with a function expression. TS7 doesn't currently support this feature on function declarations--but both unified and svelte use this feature a lot, so I think we'll need to add it before TS7's JS features are ready. At that point the shuffling of functions in the test files won't be needed anymore. In addition, microsoft/typescript-go#1444 makes it possible to have rest parameters in `@overload`, and is needed for this change to work in TS7, so it won't compile with the nightly until that PR is merged. Because TS7 is quite a way off, I don't know whether you'll want to take this PR. I created it to see how hard it would be to update popular JS code that uses TS for checking.
1 parent 45d8fa5 commit 524ab52

File tree

3 files changed

+55
-54
lines changed

3 files changed

+55
-54
lines changed

lib/index.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,6 @@ export class Processor extends CallableInstance {
538538
* processor.data() // => {charlie: 'delta'}
539539
* ```
540540
*
541-
* @template {keyof Data} Key
542541
*
543542
* @overload
544543
* @returns {Data}
@@ -547,15 +546,18 @@ export class Processor extends CallableInstance {
547546
* @param {Data} dataset
548547
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
549548
*
549+
* @template {keyof Data} Key
550550
* @overload
551551
* @param {Key} key
552552
* @returns {Data[Key]}
553553
*
554+
* @template {keyof Data} Key
554555
* @overload
555556
* @param {Key} key
556557
* @param {Data[Key]} value
557558
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
558559
*
560+
* @template {keyof Data} Key
559561
* @param {Data | Key} [key]
560562
* Key to get or set, or entire dataset to set, or nothing to get the
561563
* entire dataset (optional).
@@ -805,6 +807,14 @@ export class Processor extends CallableInstance {
805807
let complete = false
806808
/** @type {VFileWithOutput<CompileResult> | undefined} */
807809
let result
810+
/**
811+
* @type {ProcessCallback<VFileWithOutput<CompileResult>>}
812+
*/
813+
const realDone = (error, file) => {
814+
complete = true
815+
bail(error)
816+
result = file
817+
}
808818

809819
this.freeze()
810820
assertParser('processSync', this.parser || this.Parser)
@@ -815,15 +825,6 @@ export class Processor extends CallableInstance {
815825
assert(result, 'we either bailed on an error or have a tree')
816826

817827
return result
818-
819-
/**
820-
* @type {ProcessCallback<VFileWithOutput<CompileResult>>}
821-
*/
822-
function realDone(error, file) {
823-
complete = true
824-
bail(error)
825-
result = file
826-
}
827828
}
828829

829830
/**
@@ -940,21 +941,21 @@ export class Processor extends CallableInstance {
940941
let complete = false
941942
/** @type {(TailTree extends undefined ? Node : TailTree) | undefined} */
942943
let result
943-
944-
this.run(tree, file, realDone)
945-
946-
assertDone('runSync', 'run', complete)
947-
assert(result, 'we either bailed on an error or have a tree')
948-
return result
949-
950944
/**
951945
* @type {RunCallback<TailTree extends undefined ? Node : TailTree>}
952946
*/
953-
function realDone(error, tree) {
947+
const realDone = (error, tree) => {
954948
bail(error)
955949
result = tree
956950
complete = true
957951
}
952+
953+
this.run(tree, file, realDone)
954+
955+
assertDone('runSync', 'run', complete)
956+
assert(result, 'we either bailed on an error or have a tree')
957+
return result
958+
958959
}
959960

960961
/**
@@ -1029,10 +1030,6 @@ export class Processor extends CallableInstance {
10291030
* .use({settings: {position: false}})
10301031
* ```
10311032
*
1032-
* @template {Array<unknown>} [Parameters=[]]
1033-
* @template {Node | string | undefined} [Input=undefined]
1034-
* @template [Output=Input]
1035-
*
10361033
* @overload
10371034
* @param {Preset | null | undefined} [preset]
10381035
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
@@ -1041,6 +1038,9 @@ export class Processor extends CallableInstance {
10411038
* @param {PluggableList} list
10421039
* @returns {Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>}
10431040
*
1041+
* @template {Array<unknown>} [Parameters=[]]
1042+
* @template {Node | string | undefined} [Input=undefined]
1043+
* @template [Output=Input]
10441044
* @overload
10451045
* @param {Plugin<Parameters, Input, Output>} plugin
10461046
* @param {...(Parameters | [boolean])} parameters

test/freeze.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ import test from 'node:test'
88
import {unified} from 'unified'
99
import {simpleCompiler, simpleParser} from './util/simple.js'
1010

11+
// `this` in JS is buggy in TS.
12+
/**
13+
* @type {Plugin<[], string, Node>}
14+
*/
15+
const parse = function () {
16+
this.parser = simpleParser
17+
}
18+
19+
// `this` in JS is buggy in TS.
20+
/**
21+
* @type {Plugin<[], Node, string>}
22+
*/
23+
const compile = function () {
24+
this.compiler = simpleCompiler
25+
}
26+
1127
test('`freeze`', async function (t) {
1228
const frozen = unified().use(parse).use(compile).freeze()
1329

@@ -219,19 +235,3 @@ test('`freeze`', async function (t) {
219235
})
220236
})
221237
})
222-
223-
// `this` in JS is buggy in TS.
224-
/**
225-
* @type {Plugin<[], string, Node>}
226-
*/
227-
function parse() {
228-
this.parser = simpleParser
229-
}
230-
231-
// `this` in JS is buggy in TS.
232-
/**
233-
* @type {Plugin<[], Node, string>}
234-
*/
235-
function compile() {
236-
this.compiler = simpleCompiler
237-
}

test/process-sync.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ import test from 'node:test'
88
import {unified} from 'unified'
99
import {simpleCompiler, simpleParser} from './util/simple.js'
1010

11+
12+
// `this` in JS is buggy in TS.
13+
/**
14+
* @type {Plugin<[], string, Node>}
15+
*/
16+
const parse = function () {
17+
this.parser = simpleParser
18+
}
19+
20+
// `this` in JS is buggy in TS.
21+
/**
22+
* @type {Plugin<[], Node, string>}
23+
*/
24+
const compile = function () {
25+
this.compiler = simpleCompiler
26+
}
27+
1128
test('`processSync`', async function (t) {
1229
await t.test('should throw w/o `parser`', async function () {
1330
assert.throws(function () {
@@ -63,19 +80,3 @@ test('`processSync`', async function (t) {
6380
}
6481
)
6582
})
66-
67-
// `this` in JS is buggy in TS.
68-
/**
69-
* @type {Plugin<[], string, Node>}
70-
*/
71-
function parse() {
72-
this.parser = simpleParser
73-
}
74-
75-
// `this` in JS is buggy in TS.
76-
/**
77-
* @type {Plugin<[], Node, string>}
78-
*/
79-
function compile() {
80-
this.compiler = simpleCompiler
81-
}

0 commit comments

Comments
 (0)