From 64212253be209a871bd2efda7814e6882d0d5e97 Mon Sep 17 00:00:00 2001 From: azu Date: Sun, 23 May 2021 13:04:31 +0900 Subject: [PATCH 1/2] fix: sort the children explicitly --- package-lock.json | 20 +- src/parse.js | 9 +- test/__snapshots__/snapshots.test.js.snap | 364 ++++++++++++++++++++-- test/snapshot_fixtures/list.adoc | 5 + test/snapshots.test.js | 2 + 5 files changed, 376 insertions(+), 24 deletions(-) create mode 100644 test/snapshot_fixtures/list.adoc diff --git a/package-lock.json b/package-lock.json index 70ff29f..20aee8d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -303,6 +303,7 @@ "dev": true, "optional": true, "requires": { + "bindings": "^1.5.0", "nan": "^2.12.1" } }, @@ -2629,7 +2630,7 @@ }, "table": { "version": "3.8.3", - "resolved": "https://registry.npmjs.org/table/-/table-3.8.3.tgz", + "resolved": "http://registry.npmjs.org/table/-/table-3.8.3.tgz", "integrity": "sha1-K7xULw/amGGnVdOUf+/Ys/UThV8=", "dev": true, "requires": { @@ -3270,6 +3271,16 @@ "dev": true, "optional": true }, + "bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "dev": true, + "optional": true, + "requires": { + "file-uri-to-path": "1.0.0" + } + }, "bluebird": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", @@ -4431,6 +4442,13 @@ "object-assign": "^4.0.1" } }, + "file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "dev": true, + "optional": true + }, "find-cache-dir": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-2.1.0.tgz", diff --git a/src/parse.js b/src/parse.js index a48b60a..13471b5 100644 --- a/src/parse.js +++ b/src/parse.js @@ -300,9 +300,14 @@ class Converter { } locAndRangeFrom(children) { + // In ListItem does not sort the loc in sometimes + // sort ascending order explicitly + const sortedChildren = children.slice().sort((childA, childB) => { + return childA.range[0] > childB.range[0] ? 1 : -1 + }); const loc = { - start: children[0].loc.start, - end: children[children.length - 1].loc.end + start: sortedChildren[0].loc.start, + end: sortedChildren[sortedChildren.length - 1].loc.end }; const range = this.locationToRange(loc); return { loc, range }; diff --git a/test/__snapshots__/snapshots.test.js.snap b/test/__snapshots__/snapshots.test.js.snap index 79caf28..71afbbd 100644 --- a/test/__snapshots__/snapshots.test.js.snap +++ b/test/__snapshots__/snapshots.test.js.snap @@ -1946,8 +1946,8 @@ of AsciiDoc labeled lists.", ], "loc": Object { "end": Object { - "column": 42, - "line": 124, + "column": 22, + "line": 126, }, "start": Object { "column": 0, @@ -1956,7 +1956,7 @@ of AsciiDoc labeled lists.", }, "range": Array [ 3313, - 3373, + 3397, ], "raw": "", "type": "List", @@ -1964,8 +1964,8 @@ of AsciiDoc labeled lists.", ], "loc": Object { "end": Object { - "column": 42, - "line": 124, + "column": 22, + "line": 126, }, "start": Object { "column": 0, @@ -1974,7 +1974,7 @@ of AsciiDoc labeled lists.", }, "range": Array [ 0, - 3373, + 3397, ], "raw": "// NOTE: This is the article example from the AsciiDoc Python project The Article Title @@ -4036,17 +4036,17 @@ and continue typing on the next line.", ], "loc": Object { "end": Object { - "column": 13, - "line": 92, + "column": 11, + "line": 96, }, "start": Object { - "column": 2, - "line": 96, + "column": 8, + "line": 92, }, }, "range": Array [ - 2917, - 2842, + 2837, + 2926, ], "raw": "", "type": "List", @@ -4054,17 +4054,17 @@ and continue typing on the next line.", ], "loc": Object { "end": Object { - "column": 13, - "line": 92, + "column": 26, + "line": 95, }, "start": Object { - "column": 2, - "line": 95, + "column": 8, + "line": 92, }, }, "range": Array [ - 2890, - 2842, + 2837, + 2914, ], "raw": "", "type": "ListItem", @@ -4072,8 +4072,8 @@ and continue typing on the next line.", ], "loc": Object { "end": Object { - "column": 13, - "line": 92, + "column": 20, + "line": 94, }, "start": Object { "column": 2, @@ -4082,7 +4082,7 @@ and continue typing on the next line.", }, "range": Array [ 2831, - 2842, + 2887, ], "raw": "", "type": "List", @@ -6599,6 +6599,328 @@ No additional settings are needed in order for this to work on GitHub.", } `; +exports[`Snapshot testing Test Snapshot:list.adoc 1`] = ` +Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "loc": Object { + "end": Object { + "column": 3, + "line": 1, + }, + "start": Object { + "column": 2, + "line": 1, + }, + }, + "range": Array [ + 2, + 3, + ], + "raw": "A", + "type": "Str", + "value": "A", + }, + ], + "loc": Object { + "end": Object { + "column": 3, + "line": 1, + }, + "start": Object { + "column": 2, + "line": 1, + }, + }, + "range": Array [ + 2, + 3, + ], + "raw": "A", + "type": "Paragraph", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 7, + 16, + ], + "raw": "A**Fail**", + "type": "Str", + "value": "A**Fail**", + }, + ], + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 7, + 16, + ], + "raw": "A**Fail**", + "type": "Paragraph", + }, + ], + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 7, + 16, + ], + "raw": "", + "type": "ListItem", + }, + ], + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 7, + 16, + ], + "raw": "", + "type": "List", + }, + ], + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 2, + "line": 1, + }, + }, + "range": Array [ + 2, + 16, + ], + "raw": "", + "type": "ListItem", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "loc": Object { + "end": Object { + "column": 3, + "line": 3, + }, + "start": Object { + "column": 2, + "line": 3, + }, + }, + "range": Array [ + 19, + 20, + ], + "raw": "B", + "type": "Str", + "value": "B", + }, + ], + "loc": Object { + "end": Object { + "column": 3, + "line": 3, + }, + "start": Object { + "column": 2, + "line": 3, + }, + }, + "range": Array [ + 19, + 20, + ], + "raw": "B", + "type": "Paragraph", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 7, + 16, + ], + "raw": "A**Fail**", + "type": "Str", + "value": "A**Fail**", + }, + ], + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 7, + 16, + ], + "raw": "A**Fail**", + "type": "Paragraph", + }, + ], + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 7, + 16, + ], + "raw": "", + "type": "ListItem", + }, + ], + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 7, + 16, + ], + "raw": "", + "type": "List", + }, + ], + "loc": Object { + "end": Object { + "column": 3, + "line": 3, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 7, + 20, + ], + "raw": "", + "type": "ListItem", + }, + ], + "loc": Object { + "end": Object { + "column": 3, + "line": 3, + }, + "start": Object { + "column": 2, + "line": 1, + }, + }, + "range": Array [ + 2, + 20, + ], + "raw": "", + "type": "List", + }, + ], + "loc": Object { + "end": Object { + "column": 3, + "line": 3, + }, + "start": Object { + "column": 2, + "line": 1, + }, + }, + "range": Array [ + 2, + 20, + ], + "raw": "* A +** A**Fail** +* B +** A**Fail**", + "type": "Document", +} +`; + exports[`Snapshot testing Test Snapshot:runtime.adoc 1`] = ` Object { "children": Array [ diff --git a/test/snapshot_fixtures/list.adoc b/test/snapshot_fixtures/list.adoc new file mode 100644 index 0000000..e2573fa --- /dev/null +++ b/test/snapshot_fixtures/list.adoc @@ -0,0 +1,5 @@ +* A +** A**Fail** +* B +** A**Fail** + diff --git a/test/snapshots.test.js b/test/snapshots.test.js index 2d7b4ee..25c62fe 100644 --- a/test/snapshots.test.js +++ b/test/snapshots.test.js @@ -1,6 +1,7 @@ const fs = require("fs"); const path = require("path"); import parse from "../src/parse"; +import { test as testAST } from "@textlint/ast-tester"; const fixturesDir = path.join(__dirname, "snapshot_fixtures"); describe("Snapshot testing", () => { @@ -10,6 +11,7 @@ describe("Snapshot testing", () => { const fixtureFileName = path.join(fixturesDir, caseName); const actualContent = fs.readFileSync(fixtureFileName, "utf-8"); const actual = parse(actualContent); + testAST(actual); expect(actual).toMatchSnapshot(); }); }); From b7d215e71fcb5255ae2bdcd958c51a36dad17971 Mon Sep 17 00:00:00 2001 From: azu Date: Sun, 23 May 2021 13:25:20 +0900 Subject: [PATCH 2/2] fix: sort children on table cell fix #13 --- src/parse.js | 27 ++- test/__snapshots__/snapshots.test.js.snap | 242 ++++++++++++++++++++++ test/snapshot_fixtures/table.adoc | 4 + 3 files changed, 264 insertions(+), 9 deletions(-) create mode 100644 test/snapshot_fixtures/table.adoc diff --git a/src/parse.js b/src/parse.js index 13471b5..cdeca77 100644 --- a/src/parse.js +++ b/src/parse.js @@ -3,6 +3,11 @@ const asciidoctor = require("asciidoctor.js")(); +function sortChildrenByRange(children){ + return children.slice().sort((childA, childB) => { + return childA.range[0] > childB.range[0] ? 1 : -1 + }); +} class Converter { convert(text) { const doc = asciidoctor.load(text, { sourcemap: true }); @@ -239,15 +244,16 @@ class Converter { if (children.length === 0) { return []; } + const sortedChildren = sortChildrenByRange(children); const loc = { - start: children[0].loc.start, - end: children[children.length - 1].loc.end + start: sortedChildren[0].loc.start, + end: sortedChildren[sortedChildren.length - 1].loc.end }; const range = this.locationToRange(loc); return [ { type: "TableRow", - children, + children:sortedChildren, loc, range, raw: "" @@ -266,15 +272,20 @@ class Converter { if (children.length === 0) { return []; } + // In ListItem does not sort the loc in sometimes + // sort ascending order explicitly + const sortedChildren = children.slice().sort((childA, childB) => { + return childA.range[0] > childB.range[0] ? 1 : -1 + }); const loc = { - start: children[0].loc.start, - end: children[children.length - 1].loc.end + start: sortedChildren[0].loc.start, + end: sortedChildren[sortedChildren.length - 1].loc.end }; const range = this.locationToRange(loc); return [ { type: "Table", - children, + children: sortedChildren, loc, range, raw: "" @@ -302,9 +313,7 @@ class Converter { locAndRangeFrom(children) { // In ListItem does not sort the loc in sometimes // sort ascending order explicitly - const sortedChildren = children.slice().sort((childA, childB) => { - return childA.range[0] > childB.range[0] ? 1 : -1 - }); + const sortedChildren = sortChildrenByRange(children); const loc = { start: sortedChildren[0].loc.start, end: sortedChildren[sortedChildren.length - 1].loc.end diff --git a/test/__snapshots__/snapshots.test.js.snap b/test/__snapshots__/snapshots.test.js.snap index 71afbbd..904bcda 100644 --- a/test/__snapshots__/snapshots.test.js.snap +++ b/test/__snapshots__/snapshots.test.js.snap @@ -13713,3 +13713,245 @@ The ID of this section is examples-2 and resolves to <>. "type": "Document", } `; + +exports[`Snapshot testing Test Snapshot:table.adoc 1`] = ` +Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "loc": Object { + "end": Object { + "column": 2, + "line": 2, + }, + "start": Object { + "column": 1, + "line": 2, + }, + }, + "range": Array [ + 6, + 7, + ], + "raw": "A", + "type": "Str", + "value": "A", + }, + ], + "loc": Object { + "end": Object { + "column": 2, + "line": 2, + }, + "start": Object { + "column": 1, + "line": 2, + }, + }, + "range": Array [ + 6, + 7, + ], + "raw": "A", + "type": "TableCell", + }, + Object { + "children": Array [ + Object { + "loc": Object { + "end": Object { + "column": 17, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 8, + 22, + ], + "raw": "identical text", + "type": "Str", + "value": "identical text", + }, + ], + "loc": Object { + "end": Object { + "column": 17, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 8, + 22, + ], + "raw": "identical text", + "type": "TableCell", + }, + ], + "loc": Object { + "end": Object { + "column": 17, + "line": 2, + }, + "start": Object { + "column": 1, + "line": 2, + }, + }, + "range": Array [ + 6, + 22, + ], + "raw": "", + "type": "TableRow", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "loc": Object { + "end": Object { + "column": 17, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 8, + 22, + ], + "raw": "identical text", + "type": "Str", + "value": "identical text", + }, + ], + "loc": Object { + "end": Object { + "column": 17, + "line": 2, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 8, + 22, + ], + "raw": "identical text", + "type": "TableCell", + }, + Object { + "children": Array [ + Object { + "loc": Object { + "end": Object { + "column": 2, + "line": 3, + }, + "start": Object { + "column": 1, + "line": 3, + }, + }, + "range": Array [ + 24, + 25, + ], + "raw": "C", + "type": "Str", + "value": "C", + }, + ], + "loc": Object { + "end": Object { + "column": 2, + "line": 3, + }, + "start": Object { + "column": 1, + "line": 3, + }, + }, + "range": Array [ + 24, + 25, + ], + "raw": "C", + "type": "TableCell", + }, + ], + "loc": Object { + "end": Object { + "column": 2, + "line": 3, + }, + "start": Object { + "column": 3, + "line": 2, + }, + }, + "range": Array [ + 8, + 25, + ], + "raw": "", + "type": "TableRow", + }, + ], + "loc": Object { + "end": Object { + "column": 2, + "line": 3, + }, + "start": Object { + "column": 1, + "line": 2, + }, + }, + "range": Array [ + 6, + 25, + ], + "raw": "", + "type": "Table", + }, + ], + "loc": Object { + "end": Object { + "column": 2, + "line": 3, + }, + "start": Object { + "column": 1, + "line": 2, + }, + }, + "range": Array [ + 6, + 25, + ], + "raw": "|=== +|A|identical text +|C|identical text +|===", + "type": "Document", +} +`; diff --git a/test/snapshot_fixtures/table.adoc b/test/snapshot_fixtures/table.adoc new file mode 100644 index 0000000..ad6b0cf --- /dev/null +++ b/test/snapshot_fixtures/table.adoc @@ -0,0 +1,4 @@ +|=== +|A|identical text +|C|identical text +|===