Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: prettier/plugin-xml
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.1.0
Choose a base ref
...
head repository: prettier/plugin-xml
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.2.0
Choose a head ref
Loading
Showing with 729 additions and 1,025 deletions.
  1. +1 −0 .npmignore
  2. +8 −1 CHANGELOG.md
  3. +4 −4 package.json
  4. +33 −2 src/parser.ts
  5. +683 −1,018 yarn.lock
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -2,3 +2,4 @@
bin/
src/
test/
test.xml
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
@@ -185,7 +191,8 @@ and it will maintain your formatting.

- Initial release 🎉

[unreleased]: https://github.com/prettier/plugin-xml/compare/v2.1.0...HEAD
[unreleased]: https://github.com/prettier/plugin-xml/compare/v2.2.0...HEAD
[2.2.0]: https://github.com/prettier/plugin-xml/compare/v2.1.0...v2.2.0
[2.1.0]: https://github.com/prettier/plugin-xml/compare/v2.0.1...v2.1.0
[2.0.1]: https://github.com/prettier/plugin-xml/compare/v2.0.0...v2.0.1
[2.0.0]: https://github.com/prettier/plugin-xml/compare/v1.2.0...v2.0.0
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": {
@@ -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"
},
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",
Loading