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
This commit is contained in:
Mohsen Azimi
2016-12-08 06:37:46 -08:00
committed by Andy
parent 5895abee7e
commit 97af45fd65
4 changed files with 78 additions and 0 deletions
@@ -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 });
});
});
+36
View File
@@ -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 <https://github.com/mohsen1>
// 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;
+21
View File
@@ -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"
]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "../tslint.json"
}