Skip to content

Commit 5b47228

Browse files
authored
Merge pull request webpack#4095 from ts-webpack/fix/compareLocations
fix: remove unreasonable comparelocation logic and update test cases
2 parents 59cdb15 + 462b329 commit 5b47228

2 files changed

Lines changed: 24 additions & 40 deletions

File tree

‎lib/compareLocations.js‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
"use strict";
56
module.exports = function compareLocations(a, b) {
67
if(typeof a === "string") {
78
if(typeof b === "string") {
@@ -17,14 +18,14 @@ module.exports = function compareLocations(a, b) {
1718
if(typeof b === "string") {
1819
return -1;
1920
} else if(typeof b === "object") {
20-
var aa = a.start ? a.start : a;
21-
var bb = b.start ? b.start : b;
22-
if(aa.line < bb.line) return -1;
23-
if(aa.line > bb.line) return 1;
24-
if(aa.column < bb.column) return -1;
25-
if(aa.column > bb.column) return 1;
26-
if(aa.index < bb.index) return -1;
27-
if(aa.index > bb.index) return 1;
21+
if(a.start && b.start) {
22+
const ap = a.start;
23+
const bp = b.start;
24+
if(ap.line < bp.line) return -1;
25+
if(ap.line > bp.line) return 1;
26+
if(ap.column < bp.column) return -1;
27+
if(ap.column > bp.column) return 1;
28+
}
2829
if(a.index < b.index) return -1;
2930
if(a.index > b.index) return 1;
3031
return 0;

‎test/compareLocations.test.js‎

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
const should = require("should");
44
const compareLocations = require("../lib/compareLocations");
5-
const createLocation = function(overides) {
5+
const createPosition = function(overides) {
66
return Object.assign({
77
line: 10,
8-
column: 5,
9-
index: 3
8+
column: 5
109
}, overides);
1110
};
1211

12+
const createLocation = function(start, end, index) {
13+
return {
14+
start: createPosition(start),
15+
end: createPosition(end),
16+
index: index || 3
17+
};
18+
};
19+
1320
describe("compareLocations", () => {
1421
describe("string location comparison", () => {
1522
it("returns -1 when the first string comes before the second string", () =>
@@ -35,8 +42,9 @@ describe("compareLocations", () => {
3542
});
3643
});
3744

38-
it("returns -1 when the first location line number comes before the second location line number", () =>
39-
compareLocations(a, b).should.be.exactly(-1));
45+
it("returns -1 when the first location line number comes before the second location line number", () => {
46+
return compareLocations(a, b).should.be.exactly(-1)
47+
});
4048

4149
it("returns 1 when the first location line number comes after the second location line number", () =>
4250
compareLocations(b, a).should.be.exactly(1));
@@ -61,12 +69,8 @@ describe("compareLocations", () => {
6169

6270
describe("location index number", () => {
6371
beforeEach(() => {
64-
a = createLocation({
65-
index: 10
66-
});
67-
b = createLocation({
68-
index: 20
69-
});
72+
a = createLocation(null, null, 10);
73+
b = createLocation(null, null, 20);
7074
});
7175

7276
it("returns -1 when the first location index number comes before the second location index number", () =>
@@ -86,27 +90,6 @@ describe("compareLocations", () => {
8690
compareLocations(a, b).should.be.exactly(0);
8791
});
8892
});
89-
90-
describe("start location set", () => {
91-
beforeEach(() => {
92-
a = {
93-
start: createLocation({
94-
line: 10
95-
})
96-
};
97-
b = {
98-
start: createLocation({
99-
line: 20
100-
})
101-
};
102-
});
103-
104-
it("returns -1 when the first location line number comes before the second location line number", () =>
105-
compareLocations(a, b).should.be.exactly(-1));
106-
107-
it("returns 1 when the first location line number comes after the second location line number", () =>
108-
compareLocations(b, a).should.be.exactly(1));
109-
});
11093
});
11194

11295
describe("string and object location comparison", () => {

0 commit comments

Comments
 (0)