diff --git a/lib/sort.js b/lib/sort.js index bd5b072bf..ef3519143 100644 --- a/lib/sort.js +++ b/lib/sort.js @@ -75,8 +75,23 @@ module.exports = function sortDocs(comments, options) { return fixed.concat(unfixed); }; +function compare(a, b) { + return a.localeCompare(b, undefined, {caseFirst: 'upper'}); +} + function compareCommentsByName(a, b) { - return a.name.localeCompare(b.name, undefined, {caseFirst: 'upper'}); + var rv; + if (a.memberof) { + if (b.memberof) { + rv = compare(a.memberof, b.memberof); + } else { + rv = compare(a.memberof, b.name); + } + } else if (b.memberof) { + rv = compare(a.name, b.memberof); + } + + return rv || compare(a.name, b.name); } function compareCommentsBySourceLocation(a, b) { diff --git a/test/fixture/sort-order-alpha.input.js b/test/fixture/sort-order-alpha.input.js index af5bd7d8a..4ef2be662 100644 --- a/test/fixture/sort-order-alpha.input.js +++ b/test/fixture/sort-order-alpha.input.js @@ -7,25 +7,25 @@ function b() {} function a() {} /** */ -class A { +class C { /** */ - y() {} + b() {} /** */ - Y() {} + B() {} /** */ - x() {} + a() {} /** */ - X() {} + A() {} } /** */ -class B { +class D { /** */ - y() {} + b() {} /** */ - Y() {} + B() {} /** */ - x() {} + a() {} /** */ - X() {} + A() {} } diff --git a/test/fixture/sort-order-alpha.output.json b/test/fixture/sort-order-alpha.output.json index 6c7503af2..9d9257b5c 100644 --- a/test/fixture/sort-order-alpha.output.json +++ b/test/fixture/sort-order-alpha.output.json @@ -1,4 +1,84 @@ [ + { + "description": "", + "tags": [], + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 6 + } + }, + "context": { + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 15 + } + } + }, + "errors": [], + "name": "a", + "kind": "function", + "members": { + "instance": [], + "static": [] + }, + "path": [ + { + "name": "a", + "kind": "function" + } + ], + "namespace": "a" + }, + { + "description": "", + "tags": [], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 6 + } + }, + "context": { + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 15 + } + } + }, + "errors": [], + "name": "b", + "kind": "function", + "members": { + "instance": [], + "static": [] + }, + "path": [ + { + "name": "b", + "kind": "function" + } + ], + "namespace": "b" + }, { "description": "", "tags": [], @@ -25,7 +105,7 @@ } }, "errors": [], - "name": "A", + "name": "C", "kind": "class", "members": { "instance": [ @@ -55,9 +135,9 @@ } }, "errors": [], - "name": "X", + "name": "A", "kind": "function", - "memberof": "A", + "memberof": "C", "scope": "instance", "members": { "instance": [], @@ -65,16 +145,16 @@ }, "path": [ { - "name": "A", + "name": "C", "kind": "class" }, { - "name": "X", + "name": "A", "kind": "function", "scope": "instance" } ], - "namespace": "A#X" + "namespace": "C#A" }, { "description": "", @@ -102,9 +182,9 @@ } }, "errors": [], - "name": "x", + "name": "a", "kind": "function", - "memberof": "A", + "memberof": "C", "scope": "instance", "members": { "instance": [], @@ -112,16 +192,16 @@ }, "path": [ { - "name": "A", + "name": "C", "kind": "class" }, { - "name": "x", + "name": "a", "kind": "function", "scope": "instance" } ], - "namespace": "A#x" + "namespace": "C#a" }, { "description": "", @@ -149,9 +229,9 @@ } }, "errors": [], - "name": "Y", + "name": "B", "kind": "function", - "memberof": "A", + "memberof": "C", "scope": "instance", "members": { "instance": [], @@ -159,16 +239,16 @@ }, "path": [ { - "name": "A", + "name": "C", "kind": "class" }, { - "name": "Y", + "name": "B", "kind": "function", "scope": "instance" } ], - "namespace": "A#Y" + "namespace": "C#B" }, { "description": "", @@ -196,9 +276,9 @@ } }, "errors": [], - "name": "y", + "name": "b", "kind": "function", - "memberof": "A", + "memberof": "C", "scope": "instance", "members": { "instance": [], @@ -206,16 +286,16 @@ }, "path": [ { - "name": "A", + "name": "C", "kind": "class" }, { - "name": "y", + "name": "b", "kind": "function", "scope": "instance" } ], - "namespace": "A#y" + "namespace": "C#b" } ], "static": [], @@ -223,51 +303,11 @@ }, "path": [ { - "name": "A", + "name": "C", "kind": "class" } ], - "namespace": "A" - }, - { - "description": "", - "tags": [], - "loc": { - "start": { - "line": 6, - "column": 0 - }, - "end": { - "line": 6, - "column": 6 - } - }, - "context": { - "loc": { - "start": { - "line": 7, - "column": 0 - }, - "end": { - "line": 7, - "column": 15 - } - } - }, - "errors": [], - "name": "a", - "kind": "function", - "members": { - "instance": [], - "static": [] - }, - "path": [ - { - "name": "a", - "kind": "function" - } - ], - "namespace": "a" + "namespace": "C" }, { "description": "", @@ -295,7 +335,7 @@ } }, "errors": [], - "name": "B", + "name": "D", "kind": "class", "members": { "instance": [ @@ -325,9 +365,9 @@ } }, "errors": [], - "name": "X", + "name": "A", "kind": "function", - "memberof": "B", + "memberof": "D", "scope": "instance", "members": { "instance": [], @@ -335,16 +375,16 @@ }, "path": [ { - "name": "B", + "name": "D", "kind": "class" }, { - "name": "X", + "name": "A", "kind": "function", "scope": "instance" } ], - "namespace": "B#X" + "namespace": "D#A" }, { "description": "", @@ -372,9 +412,9 @@ } }, "errors": [], - "name": "x", + "name": "a", "kind": "function", - "memberof": "B", + "memberof": "D", "scope": "instance", "members": { "instance": [], @@ -382,16 +422,16 @@ }, "path": [ { - "name": "B", + "name": "D", "kind": "class" }, { - "name": "x", + "name": "a", "kind": "function", "scope": "instance" } ], - "namespace": "B#x" + "namespace": "D#a" }, { "description": "", @@ -419,9 +459,9 @@ } }, "errors": [], - "name": "Y", + "name": "B", "kind": "function", - "memberof": "B", + "memberof": "D", "scope": "instance", "members": { "instance": [], @@ -429,16 +469,16 @@ }, "path": [ { - "name": "B", + "name": "D", "kind": "class" }, { - "name": "Y", + "name": "B", "kind": "function", "scope": "instance" } ], - "namespace": "B#Y" + "namespace": "D#B" }, { "description": "", @@ -466,9 +506,9 @@ } }, "errors": [], - "name": "y", + "name": "b", "kind": "function", - "memberof": "B", + "memberof": "D", "scope": "instance", "members": { "instance": [], @@ -476,16 +516,16 @@ }, "path": [ { - "name": "B", + "name": "D", "kind": "class" }, { - "name": "y", + "name": "b", "kind": "function", "scope": "instance" } ], - "namespace": "B#y" + "namespace": "D#b" } ], "static": [], @@ -493,50 +533,10 @@ }, "path": [ { - "name": "B", + "name": "D", "kind": "class" } ], - "namespace": "B" - }, - { - "description": "", - "tags": [], - "loc": { - "start": { - "line": 3, - "column": 0 - }, - "end": { - "line": 3, - "column": 6 - } - }, - "context": { - "loc": { - "start": { - "line": 4, - "column": 0 - }, - "end": { - "line": 4, - "column": 15 - } - } - }, - "errors": [], - "name": "b", - "kind": "function", - "members": { - "instance": [], - "static": [] - }, - "path": [ - { - "name": "b", - "kind": "function" - } - ], - "namespace": "b" + "namespace": "D" } ] \ No newline at end of file diff --git a/test/fixture/sort-order-alpha.output.md b/test/fixture/sort-order-alpha.output.md index 8e08ed171..52350ab59 100644 --- a/test/fixture/sort-order-alpha.output.md +++ b/test/fixture/sort-order-alpha.output.md @@ -1,25 +1,25 @@ -# A +# a -## X +# b -## x +# C -## Y +## A -## y +## a -# a +## B -# B +## b -## X +# D -## x +## A -## Y +## a -## y +## B -# b +## b diff --git a/test/fixture/sort-order-alpha.output.md.json b/test/fixture/sort-order-alpha.output.md.json index ab2ef2dc6..fe0781da4 100644 --- a/test/fixture/sort-order-alpha.output.md.json +++ b/test/fixture/sort-order-alpha.output.md.json @@ -11,27 +11,27 @@ "children": [ { "type": "text", - "value": "A" + "value": "a" } ] }, { - "depth": 2, + "depth": 1, "type": "heading", "children": [ { "type": "text", - "value": "X" + "value": "b" } ] }, { - "depth": 2, + "depth": 1, "type": "heading", "children": [ { "type": "text", - "value": "x" + "value": "C" } ] }, @@ -41,7 +41,7 @@ "children": [ { "type": "text", - "value": "Y" + "value": "A" } ] }, @@ -51,37 +51,37 @@ "children": [ { "type": "text", - "value": "y" + "value": "a" } ] }, { - "depth": 1, + "depth": 2, "type": "heading", "children": [ { "type": "text", - "value": "a" + "value": "B" } ] }, { - "depth": 1, + "depth": 2, "type": "heading", "children": [ { "type": "text", - "value": "B" + "value": "b" } ] }, { - "depth": 2, + "depth": 1, "type": "heading", "children": [ { "type": "text", - "value": "X" + "value": "D" } ] }, @@ -91,7 +91,7 @@ "children": [ { "type": "text", - "value": "x" + "value": "A" } ] }, @@ -101,7 +101,7 @@ "children": [ { "type": "text", - "value": "Y" + "value": "a" } ] }, @@ -111,12 +111,12 @@ "children": [ { "type": "text", - "value": "y" + "value": "B" } ] }, { - "depth": 1, + "depth": 2, "type": "heading", "children": [ {