From 8b62595fe1202f8bbeb0ba7903327231b3efdab0 Mon Sep 17 00:00:00 2001 From: Ika Date: Mon, 29 Oct 2018 00:00:58 +0800 Subject: [PATCH] fix: report syntax error instead of throw (#272) --- package.json | 1 + src/prettierRule.ts | 43 +++++++++++++++++---- tests/prettier/default/syntax-error.ts.lint | 2 + yarn.lock | 4 ++ 4 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 tests/prettier/default/syntax-error.ts.lint diff --git a/package.json b/package.json index 0f92dd27..e0c0b450 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "dependencies": { "eslint-plugin-prettier": "^2.2.0", + "lines-and-columns": "^1.1.6", "tslib": "^1.7.1" }, "devDependencies": { diff --git a/src/prettierRule.ts b/src/prettierRule.ts index 8306067b..448f4482 100644 --- a/src/prettierRule.ts +++ b/src/prettierRule.ts @@ -1,4 +1,5 @@ import * as utils from 'eslint-plugin-prettier'; +import LineAndColumn from 'lines-and-columns'; import * as path from 'path'; import * as prettier from 'prettier'; import * as tslint from 'tslint'; @@ -60,19 +61,45 @@ class Walker extends tslint.AbstractWalker { } const source = sourceFile.getFullText(); - const formatted = prettier.format(source, { - parser: 'typescript', - ...options, - }); - if (source === formatted) { - return; - } + try { + const formatted = prettier.format(source, { + parser: 'typescript', + ...options, + }); + + if (source === formatted) { + return; + } - reportDifferences(this, source, formatted); + reportDifferences(this, source, formatted); + } catch (e) { + // istanbul ignore else + if (e.loc) { + reportSyntaxError(this, source, e); + } else { + throw e; + } + } } } +function reportSyntaxError( + walkContext: tslint.WalkContext, + source: string, + error: { message: string; loc: { start: { line: number; column: number } } }, +) { + const locator = new LineAndColumn(source); + const offset = locator.indexForLocation({ + column: error.loc.start.column - 1, + line: error.loc.start.line - 1, + })!; + const message = error.message + .split('\n')[0] + .replace(/\s*\(\d+:\d+\)\s*$/, ''); + walkContext.addFailureAt(offset, 1, `SyntaxError: ${message}`); +} + function reportDifferences( walkContext: tslint.WalkContext, source: string, diff --git a/tests/prettier/default/syntax-error.ts.lint b/tests/prettier/default/syntax-error.ts.lint new file mode 100644 index 00000000..935454d9 --- /dev/null +++ b/tests/prettier/default/syntax-error.ts.lint @@ -0,0 +1,2 @@ +hello world + ~ [SyntaxError: ';' expected.] diff --git a/yarn.lock b/yarn.lock index 0a2e7839..744b3542 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1294,6 +1294,10 @@ lcid@^1.0.0: dependencies: invert-kv "^1.0.0" +lines-and-columns@^1.1.6: + version "1.1.6" + resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00" + load-json-file@^1.0.0: version "1.1.0" resolved "http://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz#956905708d58b4bab4c2261b04f59f31c99374c0"