Skip to content

Commit

Permalink
Fix table alignment related rules for micromark changes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wooorm committed May 16, 2023
1 parent 04cffec commit 639271a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 74 deletions.
104 changes: 44 additions & 60 deletions packages/remark-lint-table-cell-padding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
}
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
})
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(
// More cells could exist than the align row for generated tables.
/* c8 ignore next */
sizes[column] || 0,
contentEnd - contentStart
)
}
// Detect max space per column.
sizes[column] = Math.max(
/* c8 ignore next */
sizes[column] || 0,
contentEnd - contentStart
)
}
}

Expand Down Expand Up @@ -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])
)
}
}
)
Expand Down
18 changes: 9 additions & 9 deletions packages/remark-lint-table-cell-padding/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ Too much padding isn’t 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`
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down
20 changes: 18 additions & 2 deletions packages/remark-lint-table-pipe-alignment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |
*
*/

/**
Expand Down Expand Up @@ -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' ||
Expand Down
16 changes: 16 additions & 0 deletions packages/remark-lint-table-pipe-alignment/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 639271a

Please sign in to comment.