Skip to content

Commit 639271a

Browse files
committed
Fix table alignment related rules for micromark changes
Related-to: micromark#146. Recently, an underlying package used throughout remark received some great changes, improving performance significantly, and fixing several (uncommon) bugs: [`[email protected]`](https://github.com/micromark/micromark-extension-gfm-table/releases/tag/1.0.6). Unfortunately, that release changed *which* cell owns the pipes compared to before. Old behavior was that the first column would receive two pipes, and later ones the final pipe. New behavior is for the columns to receive one pipe, and the last column two. I think the new behavior makes slightly more sense, but it’s a bit vague, and as the behavior is unspecified, and the release is very tough to roll back, I instead fixed the two packages that use the position info here. If you’re getting these updated packages, you’re likely also getting last week’s `micromark-extension-gfm-table`, and it’ll all work.
1 parent 04cffec commit 639271a

File tree

5 files changed

+88
-74
lines changed

5 files changed

+88
-74
lines changed

packages/remark-lint-table-cell-padding/index.js

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@
6666
* 3:9: Cell should be padded
6767
* 7:2: Cell should be padded
6868
* 7:17: Cell should be padded
69-
* 13:9: Cell should be padded with 1 space, not 2
70-
* 13:20: Cell should be padded with 1 space, not 2
71-
* 13:21: Cell should be padded with 1 space, not 2
72-
* 13:29: Cell should be padded with 1 space, not 2
73-
* 13:30: Cell should be padded with 1 space, not 2
69+
* 13:7: Cell should be padded with 1 space, not 2
70+
* 13:18: Cell should be padded with 1 space, not 2
71+
* 13:23: Cell should be padded with 1 space, not 2
72+
* 13:27: Cell should be padded with 1 space, not 2
73+
* 13:32: Cell should be padded with 1 space, not 2
7474
*
7575
* @example
7676
* {"name": "ok.md", "config": "compact", "gfm": true}
@@ -93,9 +93,9 @@
9393
* @example
9494
* {"name": "not-ok.md", "label": "output", "config": "compact", "gfm": true}
9595
*
96-
* 3:2: Cell should be compact
97-
* 3:11: Cell should be compact
98-
* 7:16: Cell should be compact
96+
* 3:5: Cell should be compact
97+
* 3:12: Cell should be compact
98+
* 7:15: Cell should be compact
9999
*
100100
* @example
101101
* {"name": "ok-padded.md", "config": "consistent", "gfm": true}
@@ -149,7 +149,7 @@
149149
* @example
150150
* {"name": "not-ok-compact.md", "label": "output", "config": "consistent", "gfm": true}
151151
*
152-
* 7:16: Cell should be compact
152+
* 7:15: Cell should be compact
153153
*
154154
* @example
155155
* {"name": "not-ok.md", "label": "output", "config": "💩", "positionless": true, "gfm": true}
@@ -255,38 +255,38 @@ const remarkLintTableCellPadding = lintRule(
255255
while (++column < row.children.length) {
256256
const cell = row.children[column]
257257

258-
if (cell.children.length > 0) {
259-
const cellStart = pointStart(cell).offset
260-
const cellEnd = pointEnd(cell).offset
261-
const contentStart = pointStart(cell.children[0]).offset
262-
const contentEnd = pointEnd(
263-
cell.children[cell.children.length - 1]
264-
).offset
258+
const cellStart = pointStart(cell).offset
259+
const cellEnd = pointEnd(cell).offset
260+
const contentStart = pointStart(cell.children[0]).offset
261+
const contentEnd = pointEnd(
262+
cell.children[cell.children.length - 1]
263+
).offset
265264

266-
if (
267-
typeof cellStart !== 'number' ||
268-
typeof cellEnd !== 'number' ||
269-
typeof contentStart !== 'number' ||
270-
typeof contentEnd !== 'number'
271-
) {
272-
continue
273-
}
265+
if (
266+
typeof cellStart !== 'number' ||
267+
typeof cellEnd !== 'number' ||
268+
typeof contentStart !== 'number' ||
269+
typeof contentEnd !== 'number'
270+
) {
271+
continue
272+
}
274273

275-
entries.push({
276-
node: cell,
277-
start: contentStart - cellStart - (column ? 0 : 1),
278-
end: cellEnd - contentEnd - 1,
279-
column
280-
})
274+
entries.push({
275+
node: cell,
276+
start: contentStart - cellStart - 1,
277+
end:
278+
cellEnd -
279+
contentEnd -
280+
(column === row.children.length - 1 ? 1 : 0),
281+
column
282+
})
281283

282-
// Detect max space per column.
283-
sizes[column] = Math.max(
284-
// More cells could exist than the align row for generated tables.
285-
/* c8 ignore next */
286-
sizes[column] || 0,
287-
contentEnd - contentStart
288-
)
289-
}
284+
// Detect max space per column.
285+
sizes[column] = Math.max(
286+
/* c8 ignore next */
287+
sizes[column] || 0,
288+
contentEnd - contentStart
289+
)
290290
}
291291
}
292292

@@ -346,28 +346,12 @@ const remarkLintTableCellPadding = lintRule(
346346
}
347347
}
348348

349-
/** @type {Point} */
350-
let point
351-
352-
if (side === 'start') {
353-
point = pointStart(cell)
354-
if (!column) {
355-
point.column++
356-
357-
if (typeof point.offset === 'number') {
358-
point.offset++
359-
}
360-
}
361-
} else {
362-
point = pointEnd(cell)
363-
point.column--
364-
365-
if (typeof point.offset === 'number') {
366-
point.offset--
367-
}
368-
}
369-
370-
file.message(reason, point)
349+
file.message(
350+
reason,
351+
side === 'start'
352+
? pointStart(cell.children[0])
353+
: pointEnd(cell.children[cell.children.length - 1])
354+
)
371355
}
372356
}
373357
)

packages/remark-lint-table-cell-padding/readme.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ Too much padding isn’t good either:
199199
3:9: Cell should be padded
200200
7:2: Cell should be padded
201201
7:17: Cell should be padded
202-
13:9: Cell should be padded with 1 space, not 2
203-
13:20: Cell should be padded with 1 space, not 2
204-
13:21: Cell should be padded with 1 space, not 2
205-
13:29: Cell should be padded with 1 space, not 2
206-
13:30: Cell should be padded with 1 space, not 2
202+
13:7: Cell should be padded with 1 space, not 2
203+
13:18: Cell should be padded with 1 space, not 2
204+
13:23: Cell should be padded with 1 space, not 2
205+
13:27: Cell should be padded with 1 space, not 2
206+
13:32: Cell should be padded with 1 space, not 2
207207
```
208208

209209
##### `empty.md`
@@ -290,9 +290,9 @@ When configured with `'compact'`.
290290
###### Out
291291

292292
```text
293-
3:2: Cell should be compact
294-
3:11: Cell should be compact
295-
7:16: Cell should be compact
293+
3:5: Cell should be compact
294+
3:12: Cell should be compact
295+
7:15: Cell should be compact
296296
```
297297

298298
##### `ok-padded.md`
@@ -384,7 +384,7 @@ When configured with `'consistent'`.
384384
###### Out
385385

386386
```text
387-
7:16: Cell should be compact
387+
7:15: Cell should be compact
388388
```
389389

390390
##### `not-ok.md`

packages/remark-lint-table-pipe-alignment/index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
*
5555
* 3:9-3:10: Misaligned table fence
5656
* 3:17-3:18: Misaligned table fence
57+
*
58+
* @example
59+
* {"name": "ok-empty-cells.md", "gfm": true}
60+
*
61+
* | | B | |
62+
* |-| ----- | - |
63+
* | | Bravo | |
64+
*
5765
*/
5866

5967
/**
@@ -87,8 +95,16 @@ const remarkLintTablePipeAlignment = lintRule(
8795
const cell = row.children[column]
8896
const nextColumn = column + 1
8997
const next = row.children[nextColumn]
90-
const initial = cell ? pointEnd(cell).offset : pointStart(row).offset
91-
const final = next ? pointStart(next).offset : pointEnd(row).offset
98+
const initial = cell
99+
? cell.children.length === 0
100+
? pointStart(cell).offset
101+
: pointEnd(cell.children[cell.children.length - 1]).offset
102+
: pointStart(row).offset
103+
const final = next
104+
? next.children.length === 0
105+
? pointEnd(next).offset
106+
: pointStart(next.children[0]).offset
107+
: pointEnd(row).offset
92108

93109
if (
94110
typeof initial !== 'number' ||

packages/remark-lint-table-pipe-alignment/readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,22 @@ No messages.
184184
3:17-3:18: Misaligned table fence
185185
```
186186

187+
##### `ok-empty-cells.md`
188+
189+
###### In
190+
191+
> 👉 **Note**: this example uses GFM ([`remark-gfm`][gfm]).
192+
193+
```markdown
194+
| | B | |
195+
|-| ----- | - |
196+
| | Bravo | |
197+
```
198+
199+
###### Out
200+
201+
No messages.
202+
187203
## Compatibility
188204

189205
Projects maintained by the unified collective are compatible with all maintained

test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,8 @@ test('rules', async (t) => {
288288
const info = rule(base)
289289
const href = url.pathToFileURL(base).href + '/index.js'
290290

291-
// type-coverage:ignore-next-line
291+
/** @type {{default: Plugin}} */
292292
const pluginMod = await import(href)
293-
/** @type {Plugin} */
294-
// type-coverage:ignore-next-line
295293
const fn = pluginMod.default
296294

297295
if (Object.keys(info.tests).length === 0) {

0 commit comments

Comments
 (0)