Skip to content

Commit 6d1ca1e

Browse files
committed
Add strict to tsconfig.json
1 parent 433309d commit 6d1ca1e

File tree

3 files changed

+150
-49
lines changed

3 files changed

+150
-49
lines changed

index.js

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import {toString} from 'nlcst-to-string'
3131
import {pointStart, pointEnd} from 'unist-util-position'
3232
import {location} from 'vfile-location'
3333

34+
const defaultIgnore = ['table', 'tableRow', 'tableCell']
35+
const defaultSource = ['inlineCode']
36+
3437
/**
3538
* Transform a `tree` in mdast to nlcst.
3639
*
@@ -65,26 +68,25 @@ export function toNlcst(tree, file, Parser, options = {}) {
6568

6669
const parser = 'parse' in Parser ? Parser : new Parser()
6770

71+
const result = one(
72+
{
73+
doc: String(file),
74+
place: location(file),
75+
parser,
76+
ignore: options.ignore
77+
? defaultIgnore.concat(options.ignore)
78+
: defaultIgnore,
79+
source: options.source
80+
? defaultSource.concat(options.source)
81+
: defaultSource
82+
},
83+
// @ts-expect-error assume mdast node.
84+
tree
85+
)
86+
6887
// Transform mdast into nlcst tokens, and pass these into `parser.parse` to
6988
// insert sentences, paragraphs where needed.
70-
return parser.parse(
71-
one(
72-
{
73-
doc: String(file),
74-
place: location(file),
75-
parser,
76-
ignore: [].concat(
77-
'table',
78-
'tableRow',
79-
'tableCell',
80-
options.ignore || []
81-
),
82-
source: [].concat('inlineCode', options.source || [])
83-
},
84-
// @ts-ignore assume mdast node.
85-
tree
86-
)
87-
)
89+
return parser.parse(result || [])
8890
}
8991

9092
/**
@@ -94,13 +96,10 @@ export function toNlcst(tree, file, Parser, options = {}) {
9496
* @returns {Array.<Node>|undefined}
9597
*/
9698
function one(config, node) {
97-
/** @type {number} */
98-
let start
99+
const start = node.position ? node.position.start.offset : undefined
99100

100101
if (!config.ignore.includes(node.type)) {
101-
start = node.position.start.offset
102-
103-
if (config.source.includes(node.type)) {
102+
if (config.source.includes(node.type) && start && node.position) {
104103
return patch(
105104
config,
106105
[
@@ -113,12 +112,15 @@ function one(config, node) {
113112
}
114113

115114
if ('children' in node) {
116-
// @ts-ignore looks like a parent.
117115
return all(config, node)
118116
}
119117

120-
if (node.type === 'image' || node.type === 'imageReference') {
121-
return patch(config, config.parser.tokenize(node.alt), start + 2)
118+
if ((node.type === 'image' || node.type === 'imageReference') && node.alt) {
119+
return patch(
120+
config,
121+
config.parser.tokenize(node.alt),
122+
typeof start === 'number' ? start + 2 : undefined
123+
)
122124
}
123125

124126
if (node.type === 'break') {
@@ -141,12 +143,12 @@ function all(config, parent) {
141143
let index = -1
142144
/** @type {Array.<Node>} */
143145
const results = []
144-
/** @type {Point} */
146+
/** @type {Point|undefined} */
145147
let end
146148

147149
while (++index < parent.children.length) {
148150
/** @type {Content} */
149-
// @ts-ignore Assume `parent` is an mdast parent.
151+
// @ts-expect-error Assume `parent` is an mdast parent.
150152
const child = parent.children[index]
151153
const start = pointStart(child)
152154

@@ -178,7 +180,7 @@ function all(config, parent) {
178180
* @template {Array.<Node>} T
179181
* @param {Context} config
180182
* @param {T} nodes
181-
* @param {number} offset
183+
* @param {number|undefined} offset
182184
* @returns {T}
183185
*/
184186
function patch(config, nodes, offset) {
@@ -189,16 +191,20 @@ function patch(config, nodes, offset) {
189191
const node = nodes[index]
190192

191193
if ('children' in node) {
192-
// @ts-ignore looks like a parent.
194+
// @ts-expect-error looks like a parent.
193195
patch(config, node.children, start)
194196
}
195197

196-
const end = start + toString(node).length
198+
const end =
199+
typeof start === 'number' ? start + toString(node).length : undefined
197200

198-
node.position = {
199-
start: config.place.toPoint(start),
200-
end: config.place.toPoint(end)
201-
}
201+
node.position =
202+
start !== undefined && end !== undefined
203+
? {
204+
start: config.place.toPoint(start),
205+
end: config.place.toPoint(end)
206+
}
207+
: undefined
202208

203209
start = end
204210
}

test/index.js

Lines changed: 107 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* @typedef {import('unist').Node} Node
33
* @typedef {import('unist').Literal<string>} Literal
4+
* @typedef {import('mdast').Root} Root
5+
* @typedef {import('mdast').InlineCode} InlineCode
46
* @typedef {import('vfile').VFile} VFile
57
*/
68

@@ -11,16 +13,19 @@ import remark from 'remark'
1113
import gfm from 'remark-gfm'
1214
import frontmatter from 'remark-frontmatter'
1315
import {toVFile as vfile} from 'to-vfile'
16+
// @ts-expect-error: to do type.
1417
import {ParseLatin} from 'parse-latin'
18+
// @ts-expect-error: to do type.
1519
import {ParseDutch} from 'parse-dutch'
20+
// @ts-expect-error: to do type.
1621
import {ParseEnglish} from 'parse-english'
1722
import {isHidden} from 'is-hidden'
1823
import {toNlcst} from '../index.js'
1924

2025
test('mdast-util-to-nlcst', (t) => {
2126
t.throws(
2227
() => {
23-
// @ts-ignore runtime.
28+
// @ts-expect-error runtime.
2429
toNlcst()
2530
},
2631
/mdast-util-to-nlcst expected node/,
@@ -29,7 +34,7 @@ test('mdast-util-to-nlcst', (t) => {
2934

3035
t.throws(
3136
() => {
32-
// @ts-ignore runtime.
37+
// @ts-expect-error runtime.
3338
toNlcst({})
3439
},
3540
/mdast-util-to-nlcst expected node/,
@@ -38,7 +43,7 @@ test('mdast-util-to-nlcst', (t) => {
3843

3944
t.throws(
4045
() => {
41-
// @ts-ignore runtime.
46+
// @ts-expect-error runtime.
4247
toNlcst({type: 'foo'})
4348
},
4449
/mdast-util-to-nlcst expected file/,
@@ -47,7 +52,7 @@ test('mdast-util-to-nlcst', (t) => {
4752

4853
t.throws(
4954
() => {
50-
// @ts-ignore runtime.
55+
// @ts-expect-error runtime.
5156
toNlcst({type: 'foo'})
5257
},
5358
/mdast-util-to-nlcst expected file/,
@@ -56,7 +61,7 @@ test('mdast-util-to-nlcst', (t) => {
5661

5762
t.throws(
5863
() => {
59-
// @ts-ignore runtime.
64+
// @ts-expect-error runtime.
6065
toNlcst({type: 'text', value: 'foo'}, {foo: 'bar'})
6166
},
6267
/mdast-util-to-nlcst expected file/,
@@ -65,8 +70,11 @@ test('mdast-util-to-nlcst', (t) => {
6570

6671
t.throws(
6772
() => {
68-
// @ts-ignore runtime.
69-
toNlcst({type: 'text', value: 'foo'}, vfile({contents: 'foo'}))
73+
// @ts-expect-error runtime.
74+
toNlcst(
75+
/** @type {Literal} */ ({type: 'text', value: 'foo'}),
76+
vfile({contents: 'foo'})
77+
)
7078
},
7179
/mdast-util-to-nlcst expected parser/,
7280
'should fail without parser'
@@ -114,7 +122,7 @@ test('mdast-util-to-nlcst', (t) => {
114122
{
115123
type: 'text',
116124
value: 'foo',
117-
// @ts-ignore runtime.
125+
// @ts-expect-error runtime.
118126
position: {start: {}, end: {}}
119127
},
120128
vfile(),
@@ -125,6 +133,91 @@ test('mdast-util-to-nlcst', (t) => {
125133
'should fail when not given positional information (#2)'
126134
)
127135

136+
t.deepEqual(
137+
toNlcst(
138+
/** @type {Root} */ ({
139+
type: 'root',
140+
children: [{type: 'text', value: 'foo'}],
141+
position: {start: {line: 1, column: 1}, end: {line: 1, column: 4}}
142+
}),
143+
vfile(),
144+
ParseLatin
145+
),
146+
{
147+
type: 'RootNode',
148+
children: [
149+
{
150+
type: 'ParagraphNode',
151+
children: [
152+
{
153+
type: 'SentenceNode',
154+
children: [
155+
{
156+
type: 'WordNode',
157+
children: [
158+
{type: 'TextNode', value: 'foo', position: undefined}
159+
],
160+
position: undefined
161+
}
162+
]
163+
}
164+
]
165+
}
166+
]
167+
},
168+
'should handle a node in the tree w/o positional information'
169+
)
170+
171+
t.deepEqual(
172+
toNlcst(
173+
/** @type {Root} */ ({
174+
type: 'root',
175+
children: [{type: 'image', alt: 'a'}],
176+
position: {start: {line: 1, column: 1}, end: {line: 1, column: 4}}
177+
}),
178+
vfile(),
179+
ParseLatin
180+
),
181+
{
182+
type: 'RootNode',
183+
children: [
184+
{
185+
type: 'ParagraphNode',
186+
children: [
187+
{
188+
type: 'SentenceNode',
189+
children: [
190+
{
191+
type: 'WordNode',
192+
children: [
193+
{type: 'TextNode', value: 'a', position: undefined}
194+
],
195+
position: undefined
196+
}
197+
]
198+
}
199+
]
200+
}
201+
]
202+
},
203+
'should handle an image in the tree w/o positional information'
204+
)
205+
206+
t.deepEqual(
207+
toNlcst(
208+
/** @type {InlineCode} */ ({
209+
type: 'inlineCode',
210+
value: 'a',
211+
position: {start: {line: 1, column: 1}, end: {line: 1, column: 4}}
212+
}),
213+
vfile(),
214+
ParseLatin,
215+
{ignore: ['inlineCode']}
216+
),
217+
{type: 'RootNode', children: []},
218+
'should handle an image in the tree w/o positional information'
219+
)
220+
128221
t.end()
129222
})
130223

@@ -140,7 +233,7 @@ test('Fixtures', (t) => {
140233
let expected
141234
/** @type {Node} */
142235
let mdast
143-
/** @type {Object.<string, unknown>} */
236+
/** @type {Object.<string, unknown>|undefined} */
144237
let options
145238

146239
while (++index < files.length) {
@@ -161,10 +254,11 @@ test('Fixtures', (t) => {
161254
options = undefined
162255
}
163256

164-
mdast = remark()
165-
.use(options && options.useRemarkGfm ? gfm : undefined)
166-
.use(options && options.useRemarkFrontmatter ? frontmatter : undefined)
167-
.parse(input)
257+
const processor = remark()
258+
if (options && options.useRemarkGfm) processor.use(gfm)
259+
if (options && options.useRemarkFrontmatter) processor.use(frontmatter)
260+
261+
mdast = processor.parse(input)
168262

169263
t.deepEqual(
170264
toNlcst(mdast, input, ParseLatin, options),

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"declaration": true,
1111
"emitDeclarationOnly": true,
1212
"allowSyntheticDefaultImports": true,
13-
"skipLibCheck": true
13+
"skipLibCheck": true,
14+
"strict": true
1415
}
1516
}

0 commit comments

Comments
 (0)