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:
[`micromark-extension-gfm-table@1.0.6`](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.
This commit is contained in:
Titus Wormer 2023-05-16 15:55:08 +02:00
parent 04cffeca1e
commit 639271aed9
No known key found for this signature in database
GPG Key ID: E6E581152ED04E2E
5 changed files with 89 additions and 75 deletions

View File

@ -66,11 +66,11 @@
* 3:9: Cell should be padded
* 7:2: Cell should be padded
* 7:17: Cell should be padded
* 13:9: Cell should be padded with 1 space, not 2
* 13:20: Cell should be padded with 1 space, not 2
* 13:21: Cell should be padded with 1 space, not 2
* 13:29: Cell should be padded with 1 space, not 2
* 13:30: Cell should be padded with 1 space, not 2
* 13:7: Cell should be padded with 1 space, not 2
* 13:18: Cell should be padded with 1 space, not 2
* 13:23: Cell should be padded with 1 space, not 2
* 13:27: Cell should be padded with 1 space, not 2
* 13:32: Cell should be padded with 1 space, not 2
*
* @example
* {"name": "ok.md", "config": "compact", "gfm": true}
@ -93,9 +93,9 @@
* @example
* {"name": "not-ok.md", "label": "output", "config": "compact", "gfm": true}
*
* 3:2: Cell should be compact
* 3:11: Cell should be compact
* 7:16: Cell should be compact
* 3:5: Cell should be compact
* 3:12: Cell should be compact
* 7:15: Cell should be compact
*
* @example
* {"name": "ok-padded.md", "config": "consistent", "gfm": true}
@ -149,7 +149,7 @@
* @example
* {"name": "not-ok-compact.md", "label": "output", "config": "consistent", "gfm": true}
*
* 7:16: Cell should be compact
* 7:15: Cell should be compact
*
* @example
* {"name": "not-ok.md", "label": "output", "config": "💩", "positionless": true, "gfm": true}
@ -255,38 +255,38 @@ const remarkLintTableCellPadding = lintRule(
while (++column < row.children.length) {
const cell = row.children[column]
if (cell.children.length > 0) {
const cellStart = pointStart(cell).offset
const cellEnd = pointEnd(cell).offset
const contentStart = pointStart(cell.children[0]).offset
const contentEnd = pointEnd(
cell.children[cell.children.length - 1]
).offset
const cellStart = pointStart(cell).offset
const cellEnd = pointEnd(cell).offset
const contentStart = pointStart(cell.children[0]).offset
const contentEnd = pointEnd(
cell.children[cell.children.length - 1]
).offset
if (
typeof cellStart !== 'number' ||
typeof cellEnd !== 'number' ||
typeof contentStart !== 'number' ||
typeof contentEnd !== 'number'
) {
continue
}
entries.push({
node: cell,
start: contentStart - cellStart - (column ? 0 : 1),
end: cellEnd - contentEnd - 1,
column
})
// Detect max space per column.
sizes[column] = Math.max(
// More cells could exist than the align row for generated tables.
/* c8 ignore next */
sizes[column] || 0,
contentEnd - contentStart
)
if (
typeof cellStart !== 'number' ||
typeof cellEnd !== 'number' ||
typeof contentStart !== 'number' ||
typeof contentEnd !== 'number'
) {
continue
}
entries.push({
node: cell,
start: contentStart - cellStart - 1,
end:
cellEnd -
contentEnd -
(column === row.children.length - 1 ? 1 : 0),
column
})
// Detect max space per column.
sizes[column] = Math.max(
/* c8 ignore next */
sizes[column] || 0,
contentEnd - contentStart
)
}
}
@ -346,28 +346,12 @@ const remarkLintTableCellPadding = lintRule(
}
}
/** @type {Point} */
let point
if (side === 'start') {
point = pointStart(cell)
if (!column) {
point.column++
if (typeof point.offset === 'number') {
point.offset++
}
}
} else {
point = pointEnd(cell)
point.column--
if (typeof point.offset === 'number') {
point.offset--
}
}
file.message(reason, point)
file.message(
reason,
side === 'start'
? pointStart(cell.children[0])
: pointEnd(cell.children[cell.children.length - 1])
)
}
}
)

View File

@ -199,11 +199,11 @@ Too much padding isnt good either:
3:9: Cell should be padded
7:2: Cell should be padded
7:17: Cell should be padded
13:9: Cell should be padded with 1 space, not 2
13:20: Cell should be padded with 1 space, not 2
13:21: Cell should be padded with 1 space, not 2
13:29: Cell should be padded with 1 space, not 2
13:30: Cell should be padded with 1 space, not 2
13:7: Cell should be padded with 1 space, not 2
13:18: Cell should be padded with 1 space, not 2
13:23: Cell should be padded with 1 space, not 2
13:27: Cell should be padded with 1 space, not 2
13:32: Cell should be padded with 1 space, not 2
```
##### `empty.md`
@ -290,9 +290,9 @@ When configured with `'compact'`.
###### Out
```text
3:2: Cell should be compact
3:11: Cell should be compact
7:16: Cell should be compact
3:5: Cell should be compact
3:12: Cell should be compact
7:15: Cell should be compact
```
##### `ok-padded.md`
@ -384,7 +384,7 @@ When configured with `'consistent'`.
###### Out
```text
7:16: Cell should be compact
7:15: Cell should be compact
```
##### `not-ok.md`

View File

@ -54,6 +54,14 @@
*
* 3:9-3:10: Misaligned table fence
* 3:17-3:18: Misaligned table fence
*
* @example
* {"name": "ok-empty-cells.md", "gfm": true}
*
* | | B | |
* |-| ----- | - |
* | | Bravo | |
*
*/
/**
@ -87,8 +95,16 @@ const remarkLintTablePipeAlignment = lintRule(
const cell = row.children[column]
const nextColumn = column + 1
const next = row.children[nextColumn]
const initial = cell ? pointEnd(cell).offset : pointStart(row).offset
const final = next ? pointStart(next).offset : pointEnd(row).offset
const initial = cell
? cell.children.length === 0
? pointStart(cell).offset
: pointEnd(cell.children[cell.children.length - 1]).offset
: pointStart(row).offset
const final = next
? next.children.length === 0
? pointEnd(next).offset
: pointStart(next.children[0]).offset
: pointEnd(row).offset
if (
typeof initial !== 'number' ||

View File

@ -184,6 +184,22 @@ No messages.
3:17-3:18: Misaligned table fence
```
##### `ok-empty-cells.md`
###### In
> 👉 **Note**: this example uses GFM ([`remark-gfm`][gfm]).
```markdown
| | B | |
|-| ----- | - |
| | Bravo | |
```
###### Out
No messages.
## Compatibility
Projects maintained by the unified collective are compatible with all maintained

View File

@ -288,10 +288,8 @@ test('rules', async (t) => {
const info = rule(base)
const href = url.pathToFileURL(base).href + '/index.js'
// type-coverage:ignore-next-line
/** @type {{default: Plugin}} */
const pluginMod = await import(href)
/** @type {Plugin} */
// type-coverage:ignore-next-line
const fn = pluginMod.default
if (Object.keys(info.tests).length === 0) {