Skip to content

Commit 704b164

Browse files
committed
Add babel-code-frame and syntax highlighting in error message
1 parent f4dacc5 commit 704b164

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"art": "^0.10.1",
99
"async": "^1.5.0",
1010
"babel-cli": "^6.6.5",
11+
"babel-code-frame": "^6.26.0",
1112
"babel-core": "^6.0.0",
1213
"babel-eslint": "^7.1.0",
1314
"babel-jest": "^21.3.0-beta.4",

scripts/rollup/build.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const syncReactNativeCS = require('./sync').syncReactNativeCS;
2525
const Packaging = require('./packaging');
2626
const Header = require('./header');
2727
const closure = require('rollup-plugin-closure-compiler-js');
28+
const codeFrame = require('babel-code-frame');
2829

2930
const UMD_DEV = Bundles.bundleTypes.UMD_DEV;
3031
const UMD_PROD = Bundles.bundleTypes.UMD_PROD;
@@ -605,7 +606,15 @@ function createBundle(bundle, bundleType) {
605606
`\x1b[31m-- ${error.code}${error.plugin ? ` (${error.plugin})` : ''} --`
606607
);
607608
console.error(error.message);
608-
console.error(error.frame);
609+
610+
const {file, line, column} = error.loc;
611+
const rawLines = fs.readFileSync(file, 'utf-8');
612+
// column + 1 is required due to rollup counting column start position from 0
613+
// whereas babel-code-frame counts from 1
614+
const frame = codeFrame(rawLines, line, column + 1, {
615+
highlightCode: true,
616+
});
617+
console.error(frame);
609618
} else {
610619
console.error(error);
611620
}

yarn.lock

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ [email protected], babel-code-frame@^6.16.0, babel-code-frame@^6.22.0:
284284
esutils "^2.0.2"
285285
js-tokens "^3.0.0"
286286

287+
babel-code-frame@^6.26.0:
288+
version "6.26.0"
289+
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
290+
dependencies:
291+
chalk "^1.1.3"
292+
esutils "^2.0.2"
293+
js-tokens "^3.0.2"
294+
287295
babel-core@6, babel-core@^6.0.0, babel-core@^6.24.1:
288296
version "6.24.1"
289297
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.24.1.tgz#8c428564dce1e1f41fb337ec34f4c3b022b5ad83"
@@ -2961,7 +2969,7 @@ [email protected]:
29612969
version "1.0.1"
29622970
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-1.0.1.tgz#cc435a5c8b94ad15acb7983140fc80182c89aeae"
29632971

2964-
js-tokens@^3.0.0:
2972+
js-tokens@^3.0.0, js-tokens@^3.0.2:
29652973
version "3.0.2"
29662974
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
29672975

0 commit comments

Comments
 (0)