yhtml5-test
Version:
A test framework for front-end projects
88 lines (75 loc) • 2.46 kB
JavaScript
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import CodeBlock from '../components/CodeBlock';
import { applyStyles } from '../utils/dom/css';
import { absolutifyCaret } from '../utils/dom/absolutifyCaret';
import { primaryErrorStyle, secondaryErrorStyle } from '../styles';
import generateAnsiHTML from '../utils/generateAnsiHTML';
import codeFrame from 'babel-code-frame';
// Exact type workaround for spread operator.
// See: https://github.com/facebook/flow/issues/2405
function StackFrameCodeBlock(props) {
var lines = props.lines,
lineNum = props.lineNum,
columnNum = props.columnNum,
contextSize = props.contextSize,
main = props.main;
var sourceCode = [];
var whiteSpace = Infinity;
lines.forEach(function (e) {
var text = e.content;
var m = text.match(/^\s*/);
if (text === '') {
return;
}
if (m && m[0]) {
whiteSpace = Math.min(whiteSpace, m[0].length);
} else {
whiteSpace = 0;
}
});
lines.forEach(function (e) {
var text = e.content;
var line = e.lineNumber;
if (isFinite(whiteSpace)) {
text = text.substring(whiteSpace);
}
sourceCode[line - 1] = text;
});
var ansiHighlight = codeFrame(sourceCode.join('\n'), lineNum, columnNum == null ? 0 : columnNum - (isFinite(whiteSpace) ? whiteSpace : 0), {
forceColor: true,
linesAbove: contextSize,
linesBelow: contextSize
});
var htmlHighlight = generateAnsiHTML(ansiHighlight);
var code = document.createElement('code');
code.innerHTML = htmlHighlight;
absolutifyCaret(code);
var ccn = code.childNodes;
// eslint-disable-next-line
oLoop: for (var index = 0; index < ccn.length; ++index) {
var node = ccn[index];
var ccn2 = node.childNodes;
for (var index2 = 0; index2 < ccn2.length; ++index2) {
var lineNode = ccn2[index2];
var text = lineNode.innerText;
if (text == null) {
continue;
}
if (text.indexOf(' ' + lineNum + ' |') === -1) {
continue;
}
// $FlowFixMe
applyStyles(node, main ? primaryErrorStyle : secondaryErrorStyle);
// eslint-disable-next-line
break oLoop;
}
}
return React.createElement(CodeBlock, { main: main, codeHTML: code.innerHTML });
}
export default StackFrameCodeBlock;