larry babby and threejs for glsl

This commit is contained in:
Sam
2024-06-24 21:24:00 +12:00
parent 87d5dc634d
commit 907ebae4c0
6474 changed files with 1279596 additions and 8 deletions

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017-present Devon Govett
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,38 @@
{
"name": "@parcel/codeframe",
"version": "2.12.0",
"description": "Blazing fast, zero configuration web application bundler",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/parcel"
},
"repository": {
"type": "git",
"url": "https://github.com/parcel-bundler/parcel.git"
},
"main": "lib/codeframe.js",
"source": "src/codeframe.js",
"engines": {
"node": ">= 12.0.0"
},
"targets": {
"main": {
"includeNodeModules": {
"chalk": false
}
}
},
"dependencies": {
"chalk": "^4.1.0"
},
"devDependencies": {
"emphasize": "^4.2.0",
"slice-ansi": "^4.0.0",
"string-width": "^4.2.0"
},
"gitHead": "2059029ee91e5f03a273b0954d3e629d7375f986"
}

View File

@@ -0,0 +1,302 @@
// @flow
import type {DiagnosticCodeHighlight} from '@parcel/diagnostic';
import chalk from 'chalk';
import emphasize from 'emphasize';
import stringWidth from 'string-width';
import sliceAnsi from 'slice-ansi';
type CodeFramePadding = {|
before: number,
after: number,
|};
type CodeFrameOptionsInput = $Shape<CodeFrameOptions>;
type CodeFrameOptions = {|
useColor: boolean,
syntaxHighlighting: boolean,
maxLines: number,
padding: CodeFramePadding,
terminalWidth: number,
language?: string,
|};
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
const TAB_REPLACE_REGEX = /\t/g;
const TAB_REPLACEMENT = ' ';
const DEFAULT_TERMINAL_WIDTH = 80;
const highlightSyntax = (txt: string, lang?: string): string => {
if (lang) {
try {
return emphasize.highlight(lang, txt).value;
} catch (e) {
// fallback for unknown languages...
}
}
return emphasize.highlightAuto(txt).value;
};
export default function codeFrame(
code: string,
highlights: Array<DiagnosticCodeHighlight>,
inputOpts: CodeFrameOptionsInput = {},
): string {
if (highlights.length < 1) return '';
let opts: CodeFrameOptions = {
useColor: !!inputOpts.useColor,
syntaxHighlighting: !!inputOpts.syntaxHighlighting,
language: inputOpts.language,
maxLines: inputOpts.maxLines ?? 12,
terminalWidth: inputOpts.terminalWidth || DEFAULT_TERMINAL_WIDTH,
padding: inputOpts.padding || {
before: 1,
after: 2,
},
};
// Highlights messages and prefixes when colors are enabled
const highlighter = (s: string, bold?: boolean) => {
if (opts.useColor) {
let redString = chalk.red(s);
return bold ? chalk.bold(redString) : redString;
}
return s;
};
// Prefix lines with the line number
const lineNumberPrefixer = (params: {|
lineNumber?: string,
lineNumberLength: number,
isHighlighted: boolean,
|}) => {
let {lineNumber, lineNumberLength, isHighlighted} = params;
return `${isHighlighted ? highlighter('>') : ' '} ${
lineNumber
? lineNumber.padStart(lineNumberLength, ' ')
: ' '.repeat(lineNumberLength)
} | `;
};
// Make columns/lines start at 1
let originalHighlights = highlights;
highlights = highlights.map(h => {
return {
start: {
column: h.start.column - 1,
line: h.start.line - 1,
},
end: {
column: h.end.column - 1,
line: h.end.line - 1,
},
message: h.message,
};
});
// Find first and last highlight
let firstHighlight =
highlights.length > 1
? highlights.sort((a, b) => a.start.line - b.start.line)[0]
: highlights[0];
let lastHighlight =
highlights.length > 1
? highlights.sort((a, b) => b.end.line - a.end.line)[0]
: highlights[0];
// Calculate first and last line index of codeframe
let startLine = firstHighlight.start.line - opts.padding.before;
startLine = startLine < 0 ? 0 : startLine;
let endLineIndex = lastHighlight.end.line + opts.padding.after;
let tail;
if (endLineIndex - startLine > opts.maxLines) {
let maxLine = startLine + opts.maxLines - 1;
highlights = highlights.filter(h => h.start.line < maxLine);
lastHighlight = highlights[0];
endLineIndex = Math.min(
maxLine,
lastHighlight.end.line + opts.padding.after,
);
tail = originalHighlights.filter(h => h.start.line > endLineIndex);
}
let lineNumberLength = (endLineIndex + 1).toString(10).length;
// Split input into lines and highlight syntax
let lines = code.split(NEWLINE);
let syntaxHighlightedLines = (
opts.syntaxHighlighting ? highlightSyntax(code, opts.language) : code
)
.replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT)
.split(NEWLINE);
// Loop over all lines and create codeframe
let resultLines = [];
for (
let currentLineIndex = startLine;
currentLineIndex < syntaxHighlightedLines.length;
currentLineIndex++
) {
if (currentLineIndex > endLineIndex) break;
if (currentLineIndex > syntaxHighlightedLines.length - 1) break;
// Find highlights that need to get rendered on the current line
let lineHighlights = highlights
.filter(
highlight =>
highlight.start.line <= currentLineIndex &&
highlight.end.line >= currentLineIndex,
)
.sort(
(a, b) =>
(a.start.line < currentLineIndex ? 0 : a.start.column) -
(b.start.line < currentLineIndex ? 0 : b.start.column),
);
// Check if this line has a full line highlight
let isWholeLine =
lineHighlights.length &&
!!lineHighlights.find(
h => h.start.line < currentLineIndex && h.end.line > currentLineIndex,
);
let lineLengthLimit =
opts.terminalWidth > lineNumberLength + 7
? opts.terminalWidth - (lineNumberLength + 5)
: 10;
// Split the line into line parts that will fit the provided terminal width
let colOffset = 0;
let lineEndCol = lineLengthLimit;
let syntaxHighlightedLine = syntaxHighlightedLines[currentLineIndex];
if (stringWidth(syntaxHighlightedLine) > lineLengthLimit) {
if (lineHighlights.length > 0) {
if (lineHighlights[0].start.line === currentLineIndex) {
colOffset = lineHighlights[0].start.column - 5;
} else if (lineHighlights[0].end.line === currentLineIndex) {
colOffset = lineHighlights[0].end.column - 5;
}
}
colOffset = colOffset > 0 ? colOffset : 0;
lineEndCol = colOffset + lineLengthLimit;
syntaxHighlightedLine = sliceAnsi(
syntaxHighlightedLine,
colOffset,
lineEndCol,
);
}
// Write the syntax highlighted line part
resultLines.push(
lineNumberPrefixer({
lineNumber: (currentLineIndex + 1).toString(10),
lineNumberLength,
isHighlighted: lineHighlights.length > 0,
}) + syntaxHighlightedLine,
);
let lineWidth = stringWidth(syntaxHighlightedLine);
let highlightLine = '';
if (isWholeLine) {
highlightLine = highlighter('^'.repeat(lineWidth));
} else if (lineHighlights.length > 0) {
let lastCol = 0;
let highlight = null;
let highlightHasEnded = false;
for (
let highlightIndex = 0;
highlightIndex < lineHighlights.length;
highlightIndex++
) {
// Set highlight to current highlight
highlight = lineHighlights[highlightIndex];
highlightHasEnded = false;
// Calculate the startColumn and get the real width by doing a substring of the original
// line and replacing tabs with our tab replacement to support tab handling
let startCol = 0;
if (
highlight.start.line === currentLineIndex &&
highlight.start.column > colOffset
) {
startCol = lines[currentLineIndex]
.substring(colOffset, highlight.start.column)
.replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT).length;
}
// Calculate the endColumn and get the real width by doing a substring of the original
// line and replacing tabs with our tab replacement to support tab handling
let endCol = lineWidth - 1;
if (highlight.end.line === currentLineIndex) {
endCol = lines[currentLineIndex]
.substring(colOffset, highlight.end.column)
.replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT).length;
// If the endCol is too big for this line part, trim it so we can handle it in the next one
if (endCol > lineWidth) {
endCol = lineWidth - 1;
}
highlightHasEnded = true;
}
// If endcol is smaller than lastCol it overlaps with another highlight and is no longer visible, we can skip those
if (endCol >= lastCol) {
let characters = endCol - startCol + 1;
if (startCol > lastCol) {
// startCol is before lastCol, so add spaces as padding before the highlight indicators
highlightLine += ' '.repeat(startCol - lastCol);
} else if (lastCol > startCol) {
// If last column is larger than the start, there's overlap in highlights
// This line adjusts the characters count to ensure we don't add too many characters
characters += startCol - lastCol;
}
// Don't crash (and swallow the original message) if the diagnostic is malformed (end is before start).
characters = Math.max(1, characters);
// Append the highlight indicators
highlightLine += highlighter('^'.repeat(characters));
// Set the lastCol equal to character count between start of line part and highlight end-column
lastCol = endCol + 1;
}
// There's no point in processing more highlights if we reached the end of the line
if (endCol >= lineEndCol - 1) {
break;
}
}
// Append the highlight message if the current highlights ends on this line part
if (highlight && highlight.message && highlightHasEnded) {
highlightLine += ' ' + highlighter(highlight.message, true);
}
}
if (highlightLine) {
resultLines.push(
lineNumberPrefixer({
lineNumberLength,
isHighlighted: true,
}) + highlightLine,
);
}
}
let result = resultLines.join('\n');
if (tail && tail.length > 0) {
result += '\n\n' + codeFrame(code, tail, inputOpts);
}
return result;
}

View File

@@ -0,0 +1,822 @@
import assert from 'assert';
import {readFileSync} from 'fs';
import {join as joinPath} from 'path';
import codeframe from '../src/codeframe';
const LINE_END = '\n';
describe('codeframe', () => {
it('should create a codeframe', () => {
let codeframeString = codeframe(
'hello world',
[
{
start: {
column: 1,
line: 1,
},
end: {
column: 1,
line: 1,
},
},
{
start: {
column: 3,
line: 1,
},
end: {
column: 5,
line: 1,
},
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hello world');
assert.equal(lines[1], '> | ^ ^^^');
});
it('should create a codeframe with multiple lines', () => {
let codeframeString = codeframe(
'hello world\nEnjoy this nice codeframe',
[
{
start: {
column: 1,
line: 1,
},
end: {
column: 1,
line: 1,
},
},
{
start: {
column: 7,
line: 1,
},
end: {
column: 10,
line: 2,
},
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hello world');
assert.equal(lines[1], '> | ^ ^^^^^');
assert.equal(lines[2], '> 2 | Enjoy this nice codeframe');
assert.equal(lines[3], '> | ^^^^^^^^^^');
});
it('should handle unordered overlapping highlights properly', () => {
let codeframeString = codeframe(
'hello world\nEnjoy this nice codeframe',
[
{
start: {
column: 1,
line: 1,
},
end: {
column: 1,
line: 1,
},
},
{
start: {
column: 7,
line: 1,
},
end: {
column: 10,
line: 2,
},
},
{
start: {
column: 4,
line: 2,
},
end: {
column: 7,
line: 2,
},
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hello world');
assert.equal(lines[1], '> | ^ ^^^^^');
assert.equal(lines[2], '> 2 | Enjoy this nice codeframe');
assert.equal(lines[3], '> | ^^^^^^^^^^');
});
it('should handle partial overlapping highlights properly', () => {
let codeframeString = codeframe(
'hello world\nEnjoy this nice codeframe',
[
{
start: {
column: 1,
line: 1,
},
end: {
column: 1,
line: 1,
},
},
{
start: {
column: 7,
line: 1,
},
end: {
column: 10,
line: 2,
},
},
{
start: {
column: 4,
line: 2,
},
end: {
column: 12,
line: 2,
},
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hello world');
assert.equal(lines[1], '> | ^ ^^^^^');
assert.equal(lines[2], '> 2 | Enjoy this nice codeframe');
assert.equal(lines[3], '> | ^^^^^^^^^^^^');
});
it('should be able to render inline messages', () => {
let codeframeString = codeframe(
'hello world\nEnjoy this nice codeframe',
[
{
start: {
column: 1,
line: 1,
},
end: {
column: 6,
line: 1,
},
message: 'test',
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hello world');
assert.equal(lines[1], '> | ^^^^^^ test');
assert.equal(lines[2], ' 2 | Enjoy this nice codeframe');
});
it('should only render last inline message of a column', () => {
let codeframeString = codeframe(
'hello world\nEnjoy this nice codeframe',
[
{
start: {
column: 1,
line: 1,
},
end: {
column: 3,
line: 1,
},
message: 'test',
},
{
start: {
column: 1,
line: 1,
},
end: {
column: 6,
line: 1,
},
message: 'this should be printed',
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hello world');
assert.equal(lines[1], '> | ^^^^^^ this should be printed');
assert.equal(lines[2], ' 2 | Enjoy this nice codeframe');
});
it('should only render last inline message of a column with space', () => {
let codeframeString = codeframe(
'hello world\nEnjoy this nice codeframe',
[
{
start: {
column: 1,
line: 1,
},
end: {
column: 1,
line: 1,
},
message: 'test',
},
{
start: {
column: 3,
line: 1,
},
end: {
column: 7,
line: 1,
},
message: 'this should be printed',
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hello world');
assert.equal(lines[1], '> | ^ ^^^^^ this should be printed');
assert.equal(lines[2], ' 2 | Enjoy this nice codeframe');
});
it('should only render last inline message of a column with multiple lines and space', () => {
let codeframeString = codeframe(
'hello world\nEnjoy this nice codeframe\nThis is another line',
[
{
start: {
column: 1,
line: 1,
},
end: {
column: 1,
line: 1,
},
message: 'test',
},
{
start: {
column: 3,
line: 1,
},
end: {
column: 7,
line: 1,
},
message: 'this should be printed',
},
{
start: {
column: 3,
line: 2,
},
end: {
column: 7,
line: 3,
},
message: 'message line 2',
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hello world');
assert.equal(lines[1], '> | ^ ^^^^^ this should be printed');
assert.equal(lines[2], '> 2 | Enjoy this nice codeframe');
assert.equal(lines[3], '> | ^^^^^^^^^^^^^^^^^^^^^^^');
assert.equal(lines[4], '> 3 | This is another line');
assert.equal(lines[5], '> | ^^^^^^^ message line 2');
});
it('should only render last inline message of a column with multiple lines and space', () => {
let codeframeString = codeframe(
'hello world\nEnjoy this nice codeframe\nThis is another line',
[
{
start: {
column: 1,
line: 1,
},
end: {
column: 1,
line: 1,
},
message: 'test',
},
{
start: {
column: 3,
line: 1,
},
end: {
column: 7,
line: 1,
},
message: 'this should be printed',
},
{
start: {
column: 3,
line: 2,
},
end: {
column: 7,
line: 3,
},
message: 'message line 2',
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hello world');
assert.equal(lines[1], '> | ^ ^^^^^ this should be printed');
assert.equal(lines[2], '> 2 | Enjoy this nice codeframe');
assert.equal(lines[3], '> | ^^^^^^^^^^^^^^^^^^^^^^^');
assert.equal(lines[4], '> 3 | This is another line');
assert.equal(lines[5], '> | ^^^^^^^ message line 2');
});
it('should properly use padding', () => {
let codeframeString = codeframe(
'test\n'.repeat(100),
[
{
start: {
column: 2,
line: 5,
},
end: {
column: 2,
line: 5,
},
message: 'test',
},
],
{
useColor: false,
padding: {
before: 2,
after: 4,
},
},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines.length, 8);
assert.equal(lines[0], ' 3 | test');
assert.equal(lines[2], '> 5 | test');
assert.equal(lines[3], '> | ^ test');
assert.equal(lines[7], ' 9 | test');
});
it('should properly pad numbers for large files', () => {
let codeframeString = codeframe('test\n'.repeat(1000), [
{
start: {
column: 2,
line: 99,
},
end: {
column: 2,
line: 99,
},
message: 'test',
},
{
start: {
column: 2,
line: 100,
},
end: {
column: 2,
line: 100,
},
message: 'test 2',
},
]);
let lines = codeframeString.split(LINE_END);
assert.equal(lines.length, 7);
assert.equal(lines[0], ' 98 | test');
assert.equal(lines[1], '> 99 | test');
assert.equal(lines[2], '> | ^ test');
assert.equal(lines[3], '> 100 | test');
assert.equal(lines[4], '> | ^ test 2');
assert.equal(lines[5], ' 101 | test');
assert.equal(lines[6], ' 102 | test');
});
it('should properly pad numbers for short files', () => {
let codeframeString = codeframe('test\n'.repeat(1000), [
{
start: {
column: 2,
line: 7,
},
end: {
column: 2,
line: 7,
},
message: 'test',
},
{
start: {
column: 2,
line: 12,
},
end: {
column: 2,
line: 12,
},
message: 'test',
},
]);
let lines = codeframeString.split(LINE_END);
assert.equal(lines.length, 11);
assert.equal(lines[0], ' 6 | test');
assert.equal(lines[4], ' 9 | test');
assert.equal(lines[5], ' 10 | test');
assert.equal(lines[6], ' 11 | test');
assert.equal(lines[10], ' 14 | test');
});
it('should properly use maxLines', () => {
let line = 'test '.repeat(100);
let codeframeString = codeframe(
`${line}\n`.repeat(100),
[
{
start: {
column: 2,
line: 5,
},
end: {
column: 2,
line: 5,
},
message: 'test',
},
{
start: {
column: 2,
line: 12,
},
end: {
column: 2,
line: 20,
},
message: 'test',
},
],
{
useColor: false,
maxLines: 10,
terminalWidth: 5,
},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines.length, 13);
assert.equal(lines[0], ' 4 | test test ');
assert.equal(lines[7], ' 10 | test test ');
assert.equal(lines[11], '> 13 | test test ');
assert.equal(lines[12], '> | ^^^^^^^^^^');
});
it('should be able to handle tabs', () => {
let codeframeString = codeframe(
'hel\tlo wor\tld\nEnjoy thi\ts nice cod\teframe',
[
{
start: {
column: 5,
line: 1,
},
end: {
column: 8,
line: 1,
},
message: 'test',
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hel lo wor ld');
assert.equal(lines[1], '> | ^^^^ test');
assert.equal(lines[2], ' 2 | Enjoy thi s nice cod eframe');
});
it('should be able to handle tabs with multiple highlights', () => {
let codeframeString = codeframe(
'hel\tlo wor\tld\nEnjoy thi\ts nice cod\teframe',
[
{
start: {
column: 3,
line: 1,
},
end: {
column: 5,
line: 1,
},
message: 'test',
},
{
start: {
column: 7,
line: 1,
},
end: {
column: 8,
line: 1,
},
message: 'test',
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hel lo wor ld');
assert.equal(lines[1], '> | ^^^^ ^^ test');
assert.equal(lines[2], ' 2 | Enjoy thi s nice cod eframe');
});
it('multiline highlights with tabs', () => {
let codeframeString = codeframe(
'hel\tlo wor\tld\nEnjoy thi\ts nice cod\teframe\ntest',
[
{
start: {
column: 3,
line: 1,
},
end: {
column: 2,
line: 3,
},
message: 'test',
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hel lo wor ld');
assert.equal(lines[1], '> | ^^^^^^^^^^^^^');
assert.equal(lines[2], '> 2 | Enjoy thi s nice cod eframe');
assert.equal(lines[3], '> | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^');
assert.equal(lines[4], '> 3 | test');
assert.equal(lines[5], '> | ^^ test');
});
it('Should truncate long lines and print message', () => {
let originalLine = 'hello world '.repeat(1000);
let codeframeString = codeframe(
originalLine,
[
{
start: {
column: 1000,
line: 1,
},
end: {
column: 1200,
line: 1,
},
message: 'This is a message',
},
],
{useColor: false, terminalWidth: 25},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines.length, 2);
assert.equal(lines[0], '> 1 | d hello world hello');
assert.equal(lines[1], '> | ^^^^^^^^^^^^^^ This is a message');
});
it('Truncation across multiple lines', () => {
let originalLine =
'hello world '.repeat(100) + '\n' + 'new line '.repeat(100);
let codeframeString = codeframe(
originalLine,
[
{
start: {
column: 15,
line: 1,
},
end: {
column: 400,
line: 1,
},
message: 'This is the first line',
},
{
start: {
column: 2,
line: 2,
},
end: {
column: 100,
line: 2,
},
message: 'This is the second line',
},
],
{useColor: false, terminalWidth: 25},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines.length, 4);
assert.equal(lines[0], '> 1 | ld hello world hell');
assert.equal(lines[1], '> | ^^^^^^^^^^^^^^ This is the first line');
assert.equal(lines[2], '> 2 | new line new line n');
assert.equal(lines[3], '> | ^^^^^^^^^^^^^^^^^^ This is the second line');
});
it('Truncation across various types and positions of highlights', () => {
let originalLine =
'hello world '.repeat(100) + '\n' + 'new line '.repeat(100);
let codeframeString = codeframe(
originalLine,
[
{
start: {
column: 2,
line: 1,
},
end: {
column: 5,
line: 1,
},
},
{
start: {
column: 6,
line: 1,
},
end: {
column: 10,
line: 1,
},
message: 'I have a message',
},
{
start: {
column: 15,
line: 1,
},
end: {
column: 25,
line: 1,
},
message: 'I also have a message',
},
{
start: {
column: 2,
line: 2,
},
end: {
column: 5,
line: 2,
},
message: 'This is the second line',
},
],
{useColor: false, terminalWidth: 25},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines.length, 4);
assert.equal(lines[0], '> 1 | hello world hello w');
assert.equal(lines[1], '> | ^^^^^^^^^ ^^^^^ I also have a message');
assert.equal(lines[2], '> 2 | new line new line n');
assert.equal(lines[3], '> | ^^^^ This is the second line');
});
it('Multi-line highlight w/ truncation', () => {
let originalLine =
'hello world '.repeat(100) + '\n' + 'new line '.repeat(100);
let codeframeString = codeframe(
originalLine,
[
{
start: {
column: 2,
line: 1,
},
end: {
column: 151,
line: 2,
},
message: 'I have a message',
},
],
{useColor: false, terminalWidth: 25},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines.length, 4);
assert.equal(lines[0], '> 1 | hello world hello w');
assert.equal(lines[1], '> | ^^^^^^^^^^^^^^^^^^');
assert.equal(lines[2], '> 2 | ew line new line ne');
assert.equal(lines[3], '> | ^^^^^^ I have a message');
});
it('Should pad properly, T-650', () => {
let fileContent = readFileSync(
joinPath(__dirname, './fixtures/a.js'),
'utf8',
);
let codeframeString = codeframe(
fileContent,
[
{
start: {
line: 8,
column: 10,
},
end: {
line: 8,
column: 48,
},
},
],
{
useColor: false,
syntaxHighlighting: false,
language: 'js',
terminalWidth: 100,
},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines.length, 5);
assert.equal(lines[0], ` 7 | import Tooltip from '../tooltip';`);
assert.equal(
lines[1],
`> 8 | import VisuallyHidden from '../visually-hidden';`,
);
assert.equal(
lines[2],
'> | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^',
);
assert.equal(lines[3], ' 9 | ');
assert.equal(lines[4], ' 10 | /**');
});
it('should still generate a codeframe when end is before start', () => {
let codeframeString = codeframe(
'hello world',
[
{
start: {
column: 5,
line: 1,
},
end: {
column: 1,
line: 1,
},
},
],
{useColor: false},
);
let lines = codeframeString.split(LINE_END);
assert.equal(lines[0], '> 1 | hello world');
assert.equal(lines[1], '> | ^');
});
});

View File

@@ -0,0 +1,13 @@
import test from 'test';
import component from './component';
/**
* This is a comment
*/
import Tooltip from '../tooltip';
import VisuallyHidden from '../visually-hidden';
/**
* This is another comment
*/
import {Label} from './label';