diff --git a/package.json b/package.json index ae3df5b31b..89229e3130 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "glob": "^7.1.1", "is-reference": "^1.1.0", "jsdom": "^11.6.1", - "locate-character": "^2.0.0", + "locate-character": "^2.0.5", "magic-string": "^0.22.3", "mocha": "^3.2.0", "nightmare": "^2.10.0", diff --git a/src/css/Stylesheet.ts b/src/css/Stylesheet.ts index 3f5262fcc1..4a3630f300 100644 --- a/src/css/Stylesheet.ts +++ b/src/css/Stylesheet.ts @@ -392,20 +392,22 @@ export default class Stylesheet { const handler = (selector: Selector) => { const pos = selector.node.start; - if (!locator) locator = getLocator(this.source); - const { line, column } = locator(pos); + if (!locator) locator = getLocator(this.source, { offsetLine: 1 }); + const start = locator(pos); + const end = locator(selector.node.end); - const frame = getCodeFrame(this.source, line, column); + const frame = getCodeFrame(this.source, start.line - 1, start.column); const message = `Unused CSS selector`; onwarn({ code: `css-unused-selector`, message, frame, - loc: { line: line + 1, column }, + start, + end, pos, filename: this.filename, - toString: () => `${message} (${line + 1}:${column})\n${frame}`, + toString: () => `${message} (${start.line}:${start.column})\n${frame}`, }); }; diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 714945fba2..5bbf2e17f1 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -5,7 +5,6 @@ import { getLocator } from 'locate-character'; import Stats from '../Stats'; import deindent from '../utils/deindent'; import CodeBuilder from '../utils/CodeBuilder'; -import getCodeFrame from '../utils/getCodeFrame'; import flattenReference from '../utils/flattenReference'; import reservedNames from '../utils/reservedNames'; import namespaces from '../utils/namespaces'; diff --git a/src/index.ts b/src/index.ts index 875202118b..6178b1e45f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,9 +23,9 @@ function normalizeOptions(options: CompileOptions): CompileOptions { } function defaultOnwarn(warning: Warning) { - if (warning.loc) { + if (warning.start) { console.warn( - `(${warning.loc.line}:${warning.loc.column}) – ${warning.message}` + `(${warning.start.line}:${warning.start.column}) – ${warning.message}` ); // eslint-disable-line no-console } else { console.warn(warning.message); // eslint-disable-line no-console diff --git a/src/interfaces.ts b/src/interfaces.ts index 6ed7e31dde..c49ea0494f 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -28,7 +28,7 @@ export interface Parsed { } export interface Warning { - loc?: { line: number; column: number; pos?: number }; + start?: { line: number; column: number; pos?: number }; end?: { line: number; column: number; }; pos?: number; code: string; diff --git a/src/parse/index.ts b/src/parse/index.ts index f3d724c847..1ea91e040b 100644 --- a/src/parse/index.ts +++ b/src/parse/index.ts @@ -3,7 +3,6 @@ import { locate, Location } from 'locate-character'; import fragment from './state/fragment'; import { whitespace } from '../utils/patterns'; import { trimStart, trimEnd } from '../utils/trim'; -import getCodeFrame from '../utils/getCodeFrame'; import reservedNames from '../utils/reservedNames'; import fullCharCodeAt from '../utils/fullCharCodeAt'; import hash from '../utils/hash'; diff --git a/src/utils/error.ts b/src/utils/error.ts index 35daba2c80..acaab37117 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -3,14 +3,14 @@ import getCodeFrame from '../utils/getCodeFrame'; class CompileError extends Error { code: string; - loc: { line: number, column: number }; + start: { line: number, column: number }; end: { line: number, column: number }; pos: number; filename: string; frame: string; toString() { - return `${this.message} (${this.loc.line}:${this.loc.column})\n${this.frame}`; + return `${this.message} (${this.start.line}:${this.start.column})\n${this.frame}`; } } @@ -25,16 +25,16 @@ export default function error(message: string, props: { const error = new CompileError(message); error.name = props.name; - const start = locate(props.source, props.start); - const end = locate(props.source, props.end || props.start); + const start = locate(props.source, props.start, { offsetLine: 1 }); + const end = locate(props.source, props.end || props.start, { offsetLine: 1 }); error.code = props.code; - error.loc = { line: start.line + 1, column: start.column }; - error.end = { line: end.line + 1, column: end.column }; + error.start = start; + error.end = end; error.pos = props.start; error.filename = props.filename; - error.frame = getCodeFrame(props.source, start.line, start.column); + error.frame = getCodeFrame(props.source, start.line - 1, start.column); throw error; } \ No newline at end of file diff --git a/src/validate/index.ts b/src/validate/index.ts index 9734124d26..b79a93a0ce 100644 --- a/src/validate/index.ts +++ b/src/validate/index.ts @@ -73,18 +73,18 @@ export class Validator { } warn(pos: { start: number, end: number }, { code, message }: { code: string, message: string }) { - if (!this.locator) this.locator = getLocator(this.source); + if (!this.locator) this.locator = getLocator(this.source, { offsetLine: 1 }); const start = this.locator(pos.start); const end = this.locator(pos.end); - const frame = getCodeFrame(this.source, start.line, start.column); + const frame = getCodeFrame(this.source, start.line - 1, start.column); this.stats.warn({ code, message, frame, - loc: { line: start.line + 1, column: start.column }, - end: { line: end.line + 1, column: end.column }, + start, + end, pos: pos.start, filename: this.filename, toString: () => `${message} (${start.line + 1}:${start.column})\n${frame}`, diff --git a/test/css/samples/empty-class/_config.js b/test/css/samples/empty-class/_config.js index 5ffb0321bb..fb1565c7bc 100644 --- a/test/css/samples/empty-class/_config.js +++ b/test/css/samples/empty-class/_config.js @@ -3,9 +3,15 @@ export default { filename: "SvelteComponent.html", code: `css-unused-selector`, message: "Unused CSS selector", - loc: { + start: { line: 4, - column: 1 + column: 1, + character: 31 + }, + end: { + line: 4, + column: 3, + character: 33 }, pos: 31, frame: ` diff --git a/test/css/samples/omit-scoping-attribute-descendant/_config.js b/test/css/samples/omit-scoping-attribute-descendant/_config.js index 1fb3055ae1..a4aaec7c19 100644 --- a/test/css/samples/omit-scoping-attribute-descendant/_config.js +++ b/test/css/samples/omit-scoping-attribute-descendant/_config.js @@ -2,9 +2,15 @@ export default { warnings: [{ code: `css-unused-selector`, message: 'Unused CSS selector', - loc: { + start: { line: 8, - column: 1 + column: 1, + character: 74 + }, + end: { + line: 8, + column: 8, + character: 81 }, pos: 74, frame: ` diff --git a/test/css/samples/refs-qualified/_config.js b/test/css/samples/refs-qualified/_config.js index 9f75d743cc..dbc3ac2b4e 100644 --- a/test/css/samples/refs-qualified/_config.js +++ b/test/css/samples/refs-qualified/_config.js @@ -6,9 +6,15 @@ export default { warnings: [{ code: `css-unused-selector`, message: 'Unused CSS selector', - loc: { + start: { column: 1, - line: 12 + line: 12, + character: 169 + }, + end: { + column: 20, + line: 12, + character: 188 }, pos: 169, frame: ` diff --git a/test/css/samples/refs/_config.js b/test/css/samples/refs/_config.js index 4c684d76e5..76252a8a5f 100644 --- a/test/css/samples/refs/_config.js +++ b/test/css/samples/refs/_config.js @@ -2,9 +2,15 @@ export default { warnings: [{ code: `css-unused-selector`, message: 'Unused CSS selector', - loc: { + start: { column: 1, - line: 14 + line: 14, + character: 120 + }, + end: { + column: 6, + line: 14, + character: 125 }, pos: 120, frame: ` diff --git a/test/css/samples/unused-selector-leading/_config.js b/test/css/samples/unused-selector-leading/_config.js index ec245fe80a..c01e978405 100644 --- a/test/css/samples/unused-selector-leading/_config.js +++ b/test/css/samples/unused-selector-leading/_config.js @@ -4,9 +4,15 @@ export default { filename: "SvelteComponent.html", code: `css-unused-selector`, message: "Unused CSS selector", - loc: { + start: { line: 4, - column: 1 + column: 1, + character: 34 + }, + end: { + line: 4, + column: 5, + character: 38 }, pos: 34, frame: ` @@ -22,9 +28,15 @@ export default { filename: "SvelteComponent.html", code: `css-unused-selector`, message: "Unused CSS selector", - loc: { + start: { + line: 4, + column: 13, + character: 46 + }, + end: { line: 4, - column: 13 + column: 17, + character: 50 }, pos: 46, frame: ` diff --git a/test/css/samples/unused-selector-ternary/_config.js b/test/css/samples/unused-selector-ternary/_config.js index f4f7826ffc..f99ad90601 100644 --- a/test/css/samples/unused-selector-ternary/_config.js +++ b/test/css/samples/unused-selector-ternary/_config.js @@ -7,9 +7,15 @@ export default { filename: "SvelteComponent.html", code: `css-unused-selector`, message: "Unused CSS selector", - loc: { + start: { line: 12, - column: 1 + column: 1, + character: 123 + }, + end: { + line: 12, + column: 13, + character: 135 }, pos: 123, frame: ` diff --git a/test/css/samples/unused-selector/_config.js b/test/css/samples/unused-selector/_config.js index 23f4f75771..5f6abbc08b 100644 --- a/test/css/samples/unused-selector/_config.js +++ b/test/css/samples/unused-selector/_config.js @@ -3,9 +3,15 @@ export default { filename: "SvelteComponent.html", code: `css-unused-selector`, message: "Unused CSS selector", - loc: { + start: { line: 8, - column: 1 + column: 1, + character: 60 + }, + end: { + line: 8, + column: 5, + character: 64 }, pos: 60, frame: ` diff --git a/test/js/index.js b/test/js/index.js index 1d6e499679..8c6104fa37 100644 --- a/test/js/index.js +++ b/test/js/index.js @@ -71,7 +71,7 @@ describe("js", () => { expectedBundle.trim().replace(/^[ \t]+$/gm, "") ); }).catch(err => { - if (err.loc) console.error(err.loc); + if (err.start) console.error(err.start); throw err; }); }); diff --git a/test/parser/index.js b/test/parser/index.js index f2acd631d9..7a87fc51b1 100644 --- a/test/parser/index.js +++ b/test/parser/index.js @@ -37,9 +37,9 @@ describe('parse', () => { try { assert.equal(err.code, expectedError.code); assert.equal(err.message, expectedError.message); - assert.deepEqual(err.loc, expectedError.loc); + assert.deepEqual(err.start, expectedError.start); assert.equal(err.pos, expectedError.pos); - assert.equal(err.toString().split('\n')[0], `${expectedError.message} (${expectedError.loc.line}:${expectedError.loc.column})`); + assert.equal(err.toString().split('\n')[0], `${expectedError.message} (${expectedError.start.line}:${expectedError.start.column})`); } catch (err2) { const e = err2.code === 'MODULE_NOT_FOUND' ? err : err2; throw e; diff --git a/test/parser/samples/attribute-unique-error/error.json b/test/parser/samples/attribute-unique-error/error.json index 737157b246..dd14572149 100644 --- a/test/parser/samples/attribute-unique-error/error.json +++ b/test/parser/samples/attribute-unique-error/error.json @@ -1,9 +1,10 @@ { "code": "duplicate-attribute", "message": "Attributes need to be unique", - "loc": { + "start": { "line": 1, - "column": 17 + "column": 17, + "character": 17 }, "pos": 17 } diff --git a/test/parser/samples/error-binding-disabled/error.json b/test/parser/samples/error-binding-disabled/error.json index 71953c4176..63f01e7056 100644 --- a/test/parser/samples/error-binding-disabled/error.json +++ b/test/parser/samples/error-binding-disabled/error.json @@ -1,9 +1,10 @@ { "code": "binding-disabled", "message": "Two-way binding is disabled", - "loc": { + "start": { "line": 1, - "column": 7 + "column": 7, + "character": 7 }, "pos": 7 } \ No newline at end of file diff --git a/test/parser/samples/error-binding-mustaches/error.json b/test/parser/samples/error-binding-mustaches/error.json index 4d23927086..b2a5c1c564 100644 --- a/test/parser/samples/error-binding-mustaches/error.json +++ b/test/parser/samples/error-binding-mustaches/error.json @@ -1,9 +1,10 @@ { "code": "invalid-directive-value", "message": "directive values should not be wrapped — use 'foo', not '{foo}'", - "loc": { + "start": { "line": 1, - "column": 19 + "column": 19, + "character": 19 }, "pos": 19 } \ No newline at end of file diff --git a/test/parser/samples/error-binding-rvalue/error.json b/test/parser/samples/error-binding-rvalue/error.json index b774b8a29f..2d081afac5 100644 --- a/test/parser/samples/error-binding-rvalue/error.json +++ b/test/parser/samples/error-binding-rvalue/error.json @@ -2,8 +2,9 @@ "code": "invalid-directive-value", "message": "Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)", "pos": 19, - "loc": { + "start": { "line": 1, - "column": 19 + "column": 19, + "character": 19 } } \ No newline at end of file diff --git a/test/parser/samples/error-comment-unclosed/error.json b/test/parser/samples/error-comment-unclosed/error.json index d83ecbdc09..8e355fb821 100644 --- a/test/parser/samples/error-comment-unclosed/error.json +++ b/test/parser/samples/error-comment-unclosed/error.json @@ -1,9 +1,10 @@ { "code": "unexpected-eof", "message": "comment was left open, expected -->", - "loc": { + "start": { "line": 1, - "column": 24 + "column": 24, + "character": 24 }, "pos": 24 } diff --git a/test/parser/samples/error-css/error.json b/test/parser/samples/error-css/error.json index db6703b3d0..90f7a091d2 100644 --- a/test/parser/samples/error-css/error.json +++ b/test/parser/samples/error-css/error.json @@ -1,9 +1,10 @@ { "code": "css-syntax-error", "message": "LeftCurlyBracket is expected", - "loc": { + "start": { "line": 2, - "column": 16 + "column": 16, + "character": 24 }, "pos": 24 } diff --git a/test/parser/samples/error-event-handler/error.json b/test/parser/samples/error-event-handler/error.json index 43be6e3951..8ccfa1c881 100644 --- a/test/parser/samples/error-event-handler/error.json +++ b/test/parser/samples/error-event-handler/error.json @@ -1,9 +1,10 @@ { "code": "invalid-directive-value", "message": "Expected a method call", - "loc": { + "start": { "line": 1, - "column": 15 + "column": 15, + "character": 15 }, "pos": 15 } \ No newline at end of file diff --git a/test/parser/samples/error-illegal-expression/error.json b/test/parser/samples/error-illegal-expression/error.json index fdce2f1fb9..d8b48a786e 100644 --- a/test/parser/samples/error-illegal-expression/error.json +++ b/test/parser/samples/error-illegal-expression/error.json @@ -1,9 +1,10 @@ { "code": "parse-error", "message": "Assigning to rvalue", - "loc": { + "start": { "line": 1, - "column": 1 + "column": 1, + "character": 1 }, "pos": 1 } diff --git a/test/parser/samples/error-multiple-styles/error.json b/test/parser/samples/error-multiple-styles/error.json index 937a37a1f3..1c460c5bad 100644 --- a/test/parser/samples/error-multiple-styles/error.json +++ b/test/parser/samples/error-multiple-styles/error.json @@ -1,9 +1,10 @@ { "code": "duplicate-style", "message": "You can only have one top-level