From 639271aed95fa579623f385bade4939a9c70e959 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 16 May 2023 15:55:08 +0200 Subject: [PATCH] Fix table alignment related rules for `micromark` changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../remark-lint-table-cell-padding/index.js | 106 ++++++++---------- .../remark-lint-table-cell-padding/readme.md | 18 +-- .../remark-lint-table-pipe-alignment/index.js | 20 +++- .../readme.md | 16 +++ test.js | 4 +- 5 files changed, 89 insertions(+), 75 deletions(-) diff --git a/packages/remark-lint-table-cell-padding/index.js b/packages/remark-lint-table-cell-padding/index.js index 600c2c9..e6456c9 100644 --- a/packages/remark-lint-table-cell-padding/index.js +++ b/packages/remark-lint-table-cell-padding/index.js @@ -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]) + ) } } ) diff --git a/packages/remark-lint-table-cell-padding/readme.md b/packages/remark-lint-table-cell-padding/readme.md index add4ba6..75ff26b 100644 --- a/packages/remark-lint-table-cell-padding/readme.md +++ b/packages/remark-lint-table-cell-padding/readme.md @@ -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` @@ -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` diff --git a/packages/remark-lint-table-pipe-alignment/index.js b/packages/remark-lint-table-pipe-alignment/index.js index 5d420e3..2c6f10f 100644 --- a/packages/remark-lint-table-pipe-alignment/index.js +++ b/packages/remark-lint-table-pipe-alignment/index.js @@ -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' || diff --git a/packages/remark-lint-table-pipe-alignment/readme.md b/packages/remark-lint-table-pipe-alignment/readme.md index 8b29180..004ebad 100644 --- a/packages/remark-lint-table-pipe-alignment/readme.md +++ b/packages/remark-lint-table-pipe-alignment/readme.md @@ -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 diff --git a/test.js b/test.js index fc6a3d9..268ed7d 100644 --- a/test.js +++ b/test.js @@ -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) {