From 97af45fd65fc76d74b391dc6d2b432469865ce83 Mon Sep 17 00:00:00 2001 From: Mohsen Azimi Date: Thu, 8 Dec 2016 06:37:46 -0800 Subject: [PATCH] Add babel-code-frame (#13102) * Add babel-code-frame * Add babel-code-frame * index * Follow naming convention * tests * ref fix * Address issues raised in code review * CR fixes * make options... optional * Update test to not assert --- babel-code-frame/babel-code-frame-tests.ts | 18 +++++++++++ babel-code-frame/index.d.ts | 36 ++++++++++++++++++++++ babel-code-frame/tsconfig.json | 21 +++++++++++++ babel-code-frame/tslint.json | 3 ++ 4 files changed, 78 insertions(+) create mode 100644 babel-code-frame/babel-code-frame-tests.ts create mode 100644 babel-code-frame/index.d.ts create mode 100644 babel-code-frame/tsconfig.json create mode 100644 babel-code-frame/tslint.json diff --git a/babel-code-frame/babel-code-frame-tests.ts b/babel-code-frame/babel-code-frame-tests.ts new file mode 100644 index 0000000000..875af43d3a --- /dev/null +++ b/babel-code-frame/babel-code-frame-tests.ts @@ -0,0 +1,18 @@ +import codeFrame from "babel-code-frame"; + +describe('babel-code-frame', () => { + it('frames given code', () => { + const code = ` + const number = 1; + var string = 'foo'; + + function print(name: string) { + console.log(string + name); + } + `; + + codeFrame(code, 5, 22); + codeFrame(code, 5, 22, { forceColor: true }); + codeFrame(code, 2, 2, { highlightCode: true }); + }); +}); diff --git a/babel-code-frame/index.d.ts b/babel-code-frame/index.d.ts new file mode 100644 index 0000000000..935f39ea4e --- /dev/null +++ b/babel-code-frame/index.d.ts @@ -0,0 +1,36 @@ +// Type definitions for babel-code-frame 6.16 +// Project: https://github.com/babel/babel/tree/master/packages +// Definitions by: Mohsen Azimi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface BabelCodeFrameOptions { + /** Syntax highlight the code as JavaScript for terminals. default: false*/ + highlightCode?: boolean; + /** The number of lines to show above the error. default: 2 */ + linesBelow?: number; + /** The number of lines to show below the error. default: 3 */ + linesAbove?: number; + /** + * Forcibly syntax highlight the code as JavaScript (for non-terminals); + * overrides highlightCode. + * default: false + */ + forceColor?: boolean; +} + +/** + * Generate errors that contain a code frame that point to source locations. + * + * @param rawLines Raw lines to frame + * @param lineNumber Line number (1 indexed) + * @param colNumber Column number + * @param options Additional options + * + * @returns Framed code + */ +export default function babelCodeFrame( + rawLines: string, + lineNumber: number, + colNumber: number, + options?: BabelCodeFrameOptions +): string; diff --git a/babel-code-frame/tsconfig.json b/babel-code-frame/tsconfig.json new file mode 100644 index 0000000000..25631aac91 --- /dev/null +++ b/babel-code-frame/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [ + "mocha" + ], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "babel-code-frame-tests.ts" + ] +} diff --git a/babel-code-frame/tslint.json b/babel-code-frame/tslint.json new file mode 100644 index 0000000000..ec365f164b --- /dev/null +++ b/babel-code-frame/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../tslint.json" +}