Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle better error messages #470

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

## [2.2.0] - 2022-05-12

### Added

- Better error messages in the case of a syntax error.

## [2.1.0] - 2022-04-16

### Added
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prettier/plugin-xml",
"version": "2.1.0",
"version": "2.2.0",
"description": "prettier plugin for XML",
"main": "dist/plugin.js",
"scripts": {
Expand All @@ -24,15 +24,15 @@
"prettier": ">=2.4.0"
},
"devDependencies": {
"@types/jest": "^27.0.0",
"@types/jest": "^27.5.1",
"@types/node": "^17.0.22",
"@types/prettier": "^2.3.0",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"eslint": "^8.5.0",
"eslint-config-prettier": "^8.0.0",
"jest": "^27.0.1",
"ts-jest": "^27.0.2",
"jest": "^28.1.0",
"ts-jest": "^28.0.2",
"ts-node": "^10.0.0",
"typescript": "^4.3.2"
},
Expand Down
35 changes: 33 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import { parse as xmlToolsParse } from "@xml-tools/parser";
import type { Parser } from "./types";

type LocatedError = Error & {
loc: {
start: { line: number; column: number };
end: { line: number; column: number };
};
};

const parser: Parser = {
parse(text) {
const { lexErrors, parseErrors, cst } = xmlToolsParse(text);

if (lexErrors.length > 0 || parseErrors.length > 0) {
throw Error("Error parsing XML");
// If there are any lexical errors, throw the first of them as an error.
if (lexErrors.length > 0) {
const lexError = lexErrors[0];
const error = new Error(lexError.message) as LocatedError;

error.loc = {
start: { line: lexError.line, column: lexError.column },
end: { line: lexError.line, column: lexError.column + lexError.length }
};

throw error;
}

// If there are any parse errors, throw the first of them as an error.
if (parseErrors.length > 0) {
const parseError = parseErrors[0];
const error = new Error(parseError.message) as LocatedError;

const { token } = parseError;
error.loc = {
start: { line: token.startLine!, column: token.startColumn! },
end: { line: token.endLine!, column: token.endColumn! }
};

throw error;
}

// Otherwise return the CST.
return cst;
},
astFormat: "xml",
Expand Down
Loading