Skip to content

Commit 5a457bf

Browse files
committed
rename loc to start, include character info in locations
1 parent 07bad96 commit 5a457bf

File tree

151 files changed

+787
-489
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+787
-489
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"glob": "^7.1.1",
5959
"is-reference": "^1.1.0",
6060
"jsdom": "^11.6.1",
61-
"locate-character": "^2.0.0",
61+
"locate-character": "^2.0.5",
6262
"magic-string": "^0.22.3",
6363
"mocha": "^3.2.0",
6464
"nightmare": "^2.10.0",

src/css/Stylesheet.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -392,20 +392,22 @@ export default class Stylesheet {
392392
const handler = (selector: Selector) => {
393393
const pos = selector.node.start;
394394

395-
if (!locator) locator = getLocator(this.source);
396-
const { line, column } = locator(pos);
395+
if (!locator) locator = getLocator(this.source, { offsetLine: 1 });
396+
const start = locator(pos);
397+
const end = locator(selector.node.end);
397398

398-
const frame = getCodeFrame(this.source, line, column);
399+
const frame = getCodeFrame(this.source, start.line - 1, start.column);
399400
const message = `Unused CSS selector`;
400401

401402
onwarn({
402403
code: `css-unused-selector`,
403404
message,
404405
frame,
405-
loc: { line: line + 1, column },
406+
start,
407+
end,
406408
pos,
407409
filename: this.filename,
408-
toString: () => `${message} (${line + 1}:${column})\n${frame}`,
410+
toString: () => `${message} (${start.line}:${start.column})\n${frame}`,
409411
});
410412
};
411413

src/generators/Generator.ts

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { getLocator } from 'locate-character';
55
import Stats from '../Stats';
66
import deindent from '../utils/deindent';
77
import CodeBuilder from '../utils/CodeBuilder';
8-
import getCodeFrame from '../utils/getCodeFrame';
98
import flattenReference from '../utils/flattenReference';
109
import reservedNames from '../utils/reservedNames';
1110
import namespaces from '../utils/namespaces';

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ function normalizeOptions(options: CompileOptions): CompileOptions {
2323
}
2424

2525
function defaultOnwarn(warning: Warning) {
26-
if (warning.loc) {
26+
if (warning.start) {
2727
console.warn(
28-
`(${warning.loc.line}:${warning.loc.column}) – ${warning.message}`
28+
`(${warning.start.line}:${warning.start.column}) – ${warning.message}`
2929
); // eslint-disable-line no-console
3030
} else {
3131
console.warn(warning.message); // eslint-disable-line no-console

src/interfaces.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface Parsed {
2828
}
2929

3030
export interface Warning {
31-
loc?: { line: number; column: number; pos?: number };
31+
start?: { line: number; column: number; pos?: number };
3232
end?: { line: number; column: number; };
3333
pos?: number;
3434
code: string;

src/parse/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { locate, Location } from 'locate-character';
33
import fragment from './state/fragment';
44
import { whitespace } from '../utils/patterns';
55
import { trimStart, trimEnd } from '../utils/trim';
6-
import getCodeFrame from '../utils/getCodeFrame';
76
import reservedNames from '../utils/reservedNames';
87
import fullCharCodeAt from '../utils/fullCharCodeAt';
98
import hash from '../utils/hash';

src/utils/error.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import getCodeFrame from '../utils/getCodeFrame';
33

44
class CompileError extends Error {
55
code: string;
6-
loc: { line: number, column: number };
6+
start: { line: number, column: number };
77
end: { line: number, column: number };
88
pos: number;
99
filename: string;
1010
frame: string;
1111

1212
toString() {
13-
return `${this.message} (${this.loc.line}:${this.loc.column})\n${this.frame}`;
13+
return `${this.message} (${this.start.line}:${this.start.column})\n${this.frame}`;
1414
}
1515
}
1616

@@ -25,16 +25,16 @@ export default function error(message: string, props: {
2525
const error = new CompileError(message);
2626
error.name = props.name;
2727

28-
const start = locate(props.source, props.start);
29-
const end = locate(props.source, props.end || props.start);
28+
const start = locate(props.source, props.start, { offsetLine: 1 });
29+
const end = locate(props.source, props.end || props.start, { offsetLine: 1 });
3030

3131
error.code = props.code;
32-
error.loc = { line: start.line + 1, column: start.column };
33-
error.end = { line: end.line + 1, column: end.column };
32+
error.start = start;
33+
error.end = end;
3434
error.pos = props.start;
3535
error.filename = props.filename;
3636

37-
error.frame = getCodeFrame(props.source, start.line, start.column);
37+
error.frame = getCodeFrame(props.source, start.line - 1, start.column);
3838

3939
throw error;
4040
}

src/validate/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ export class Validator {
7373
}
7474

7575
warn(pos: { start: number, end: number }, { code, message }: { code: string, message: string }) {
76-
if (!this.locator) this.locator = getLocator(this.source);
76+
if (!this.locator) this.locator = getLocator(this.source, { offsetLine: 1 });
7777
const start = this.locator(pos.start);
7878
const end = this.locator(pos.end);
7979

80-
const frame = getCodeFrame(this.source, start.line, start.column);
80+
const frame = getCodeFrame(this.source, start.line - 1, start.column);
8181

8282
this.stats.warn({
8383
code,
8484
message,
8585
frame,
86-
loc: { line: start.line + 1, column: start.column },
87-
end: { line: end.line + 1, column: end.column },
86+
start,
87+
end,
8888
pos: pos.start,
8989
filename: this.filename,
9090
toString: () => `${message} (${start.line + 1}:${start.column})\n${frame}`,

test/css/samples/empty-class/_config.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ export default {
33
filename: "SvelteComponent.html",
44
code: `css-unused-selector`,
55
message: "Unused CSS selector",
6-
loc: {
6+
start: {
77
line: 4,
8-
column: 1
8+
column: 1,
9+
character: 31
10+
},
11+
end: {
12+
line: 4,
13+
column: 3,
14+
character: 33
915
},
1016
pos: 31,
1117
frame: `

test/css/samples/omit-scoping-attribute-descendant/_config.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ export default {
22
warnings: [{
33
code: `css-unused-selector`,
44
message: 'Unused CSS selector',
5-
loc: {
5+
start: {
66
line: 8,
7-
column: 1
7+
column: 1,
8+
character: 74
9+
},
10+
end: {
11+
line: 8,
12+
column: 8,
13+
character: 81
814
},
915
pos: 74,
1016
frame: `

test/css/samples/refs-qualified/_config.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ export default {
66
warnings: [{
77
code: `css-unused-selector`,
88
message: 'Unused CSS selector',
9-
loc: {
9+
start: {
1010
column: 1,
11-
line: 12
11+
line: 12,
12+
character: 169
13+
},
14+
end: {
15+
column: 20,
16+
line: 12,
17+
character: 188
1218
},
1319
pos: 169,
1420
frame: `

test/css/samples/refs/_config.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ export default {
22
warnings: [{
33
code: `css-unused-selector`,
44
message: 'Unused CSS selector',
5-
loc: {
5+
start: {
66
column: 1,
7-
line: 14
7+
line: 14,
8+
character: 120
9+
},
10+
end: {
11+
column: 6,
12+
line: 14,
13+
character: 125
814
},
915
pos: 120,
1016
frame: `

test/css/samples/unused-selector-leading/_config.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ export default {
44
filename: "SvelteComponent.html",
55
code: `css-unused-selector`,
66
message: "Unused CSS selector",
7-
loc: {
7+
start: {
88
line: 4,
9-
column: 1
9+
column: 1,
10+
character: 34
11+
},
12+
end: {
13+
line: 4,
14+
column: 5,
15+
character: 38
1016
},
1117
pos: 34,
1218
frame: `
@@ -22,9 +28,15 @@ export default {
2228
filename: "SvelteComponent.html",
2329
code: `css-unused-selector`,
2430
message: "Unused CSS selector",
25-
loc: {
31+
start: {
32+
line: 4,
33+
column: 13,
34+
character: 46
35+
},
36+
end: {
2637
line: 4,
27-
column: 13
38+
column: 17,
39+
character: 50
2840
},
2941
pos: 46,
3042
frame: `

test/css/samples/unused-selector-ternary/_config.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ export default {
77
filename: "SvelteComponent.html",
88
code: `css-unused-selector`,
99
message: "Unused CSS selector",
10-
loc: {
10+
start: {
1111
line: 12,
12-
column: 1
12+
column: 1,
13+
character: 123
14+
},
15+
end: {
16+
line: 12,
17+
column: 13,
18+
character: 135
1319
},
1420
pos: 123,
1521
frame: `

test/css/samples/unused-selector/_config.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ export default {
33
filename: "SvelteComponent.html",
44
code: `css-unused-selector`,
55
message: "Unused CSS selector",
6-
loc: {
6+
start: {
77
line: 8,
8-
column: 1
8+
column: 1,
9+
character: 60
10+
},
11+
end: {
12+
line: 8,
13+
column: 5,
14+
character: 64
915
},
1016
pos: 60,
1117
frame: `

test/js/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe("js", () => {
7171
expectedBundle.trim().replace(/^[ \t]+$/gm, "")
7272
);
7373
}).catch(err => {
74-
if (err.loc) console.error(err.loc);
74+
if (err.start) console.error(err.start);
7575
throw err;
7676
});
7777
});

test/parser/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ describe('parse', () => {
3737
try {
3838
assert.equal(err.code, expectedError.code);
3939
assert.equal(err.message, expectedError.message);
40-
assert.deepEqual(err.loc, expectedError.loc);
40+
assert.deepEqual(err.start, expectedError.start);
4141
assert.equal(err.pos, expectedError.pos);
42-
assert.equal(err.toString().split('\n')[0], `${expectedError.message} (${expectedError.loc.line}:${expectedError.loc.column})`);
42+
assert.equal(err.toString().split('\n')[0], `${expectedError.message} (${expectedError.start.line}:${expectedError.start.column})`);
4343
} catch (err2) {
4444
const e = err2.code === 'MODULE_NOT_FOUND' ? err : err2;
4545
throw e;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"code": "duplicate-attribute",
33
"message": "Attributes need to be unique",
4-
"loc": {
4+
"start": {
55
"line": 1,
6-
"column": 17
6+
"column": 17,
7+
"character": 17
78
},
89
"pos": 17
910
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"code": "binding-disabled",
33
"message": "Two-way binding is disabled",
4-
"loc": {
4+
"start": {
55
"line": 1,
6-
"column": 7
6+
"column": 7,
7+
"character": 7
78
},
89
"pos": 7
910
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"code": "invalid-directive-value",
33
"message": "directive values should not be wrapped — use 'foo', not '{foo}'",
4-
"loc": {
4+
"start": {
55
"line": 1,
6-
"column": 19
6+
"column": 19,
7+
"character": 19
78
},
89
"pos": 19
910
}

test/parser/samples/error-binding-rvalue/error.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
"code": "invalid-directive-value",
33
"message": "Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)",
44
"pos": 19,
5-
"loc": {
5+
"start": {
66
"line": 1,
7-
"column": 19
7+
"column": 19,
8+
"character": 19
89
}
910
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"code": "unexpected-eof",
33
"message": "comment was left open, expected -->",
4-
"loc": {
4+
"start": {
55
"line": 1,
6-
"column": 24
6+
"column": 24,
7+
"character": 24
78
},
89
"pos": 24
910
}
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"code": "css-syntax-error",
33
"message": "LeftCurlyBracket is expected",
4-
"loc": {
4+
"start": {
55
"line": 2,
6-
"column": 16
6+
"column": 16,
7+
"character": 24
78
},
89
"pos": 24
910
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"code": "invalid-directive-value",
33
"message": "Expected a method call",
4-
"loc": {
4+
"start": {
55
"line": 1,
6-
"column": 15
6+
"column": 15,
7+
"character": 15
78
},
89
"pos": 15
910
}

0 commit comments

Comments
 (0)