@parcel/codeframe 2.0.0-beta.1 → 2.0.0-nightly.1002

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@parcel/codeframe",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-nightly.1002+5530a6ef",
4
4
  "description": "Blazing fast, zero configuration web application bundler",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
+ "funding": {
10
+ "type": "opencollective",
11
+ "url": "https://opencollective.com/parcel"
12
+ },
9
13
  "repository": {
10
14
  "type": "git",
11
15
  "url": "https://github.com/parcel-bundler/parcel.git"
@@ -13,16 +17,23 @@
13
17
  "main": "lib/codeframe.js",
14
18
  "source": "src/codeframe.js",
15
19
  "engines": {
16
- "node": ">= 10.0.0"
20
+ "node": ">= 12.0.0"
21
+ },
22
+ "targets": {
23
+ "main": {
24
+ "includeNodeModules": {
25
+ "chalk": false
26
+ }
27
+ }
17
28
  },
18
29
  "dependencies": {
19
- "chalk": "^2.4.2",
20
- "emphasize": "^2.1.0",
21
- "slice-ansi": "^4.0.0",
22
- "string-width": "^4.2.0"
30
+ "chalk": "^4.1.0"
23
31
  },
24
32
  "devDependencies": {
33
+ "emphasize": "^4.2.0",
34
+ "slice-ansi": "^4.0.0",
35
+ "string-width": "^4.2.0",
25
36
  "strip-ansi": "^6.0.0"
26
37
  },
27
- "gitHead": "74335525be92e23bac4ed1bf30595443cfb238e3"
38
+ "gitHead": "5530a6eff8b619873353baeb0457ae4ec591e9fa"
28
39
  }
package/src/codeframe.js CHANGED
@@ -71,15 +71,15 @@ export default function codeFrame(
71
71
  // Prefix lines with the line number
72
72
  const lineNumberPrefixer = (params: {|
73
73
  lineNumber?: string,
74
- endLine: string,
74
+ lineNumberLength: number,
75
75
  isHighlighted: boolean,
76
76
  |}) => {
77
- let {lineNumber, endLine, isHighlighted} = params;
77
+ let {lineNumber, lineNumberLength, isHighlighted} = params;
78
78
 
79
79
  return `${isHighlighted ? highlighter('>') : ' '} ${
80
80
  lineNumber
81
- ? lineNumber.padEnd(endLine.length, ' ')
82
- : ' '.repeat(endLine.length)
81
+ ? lineNumber.padStart(lineNumberLength, ' ')
82
+ : ' '.repeat(lineNumberLength)
83
83
  } | `;
84
84
  };
85
85
 
@@ -111,18 +111,18 @@ export default function codeFrame(
111
111
  // Calculate first and last line index of codeframe
112
112
  let startLine = firstHighlight.start.line - opts.padding.before;
113
113
  startLine = startLine < 0 ? 0 : startLine;
114
- let endLine = lastHighlight.end.line + opts.padding.after;
115
- endLine =
116
- endLine - startLine > opts.maxLines
114
+ let endLineIndex = lastHighlight.end.line + opts.padding.after;
115
+ endLineIndex =
116
+ endLineIndex - startLine > opts.maxLines
117
117
  ? startLine + opts.maxLines - 1
118
- : endLine;
119
- let endLineString = endLine.toString(10);
118
+ : endLineIndex;
119
+
120
+ let lineNumberLength = (endLineIndex + 1).toString(10).length;
120
121
 
121
122
  // Split input into lines and highlight syntax
122
123
  let lines = code.split(NEWLINE);
123
- let syntaxHighlightedLines = (opts.syntaxHighlighting
124
- ? highlightSyntax(code, opts.language)
125
- : code
124
+ let syntaxHighlightedLines = (
125
+ opts.syntaxHighlighting ? highlightSyntax(code, opts.language) : code
126
126
  )
127
127
  .replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT)
128
128
  .split(NEWLINE);
@@ -134,7 +134,7 @@ export default function codeFrame(
134
134
  currentLineIndex < syntaxHighlightedLines.length;
135
135
  currentLineIndex++
136
136
  ) {
137
- if (currentLineIndex > endLine) break;
137
+ if (currentLineIndex > endLineIndex) break;
138
138
  if (currentLineIndex > syntaxHighlightedLines.length - 1) break;
139
139
 
140
140
  // Find highlights that need to get rendered on the current line
@@ -158,8 +158,8 @@ export default function codeFrame(
158
158
  );
159
159
 
160
160
  let lineLengthLimit =
161
- opts.terminalWidth > endLineString.length + 7
162
- ? opts.terminalWidth - (endLineString.length + 5)
161
+ opts.terminalWidth > lineNumberLength + 7
162
+ ? opts.terminalWidth - (lineNumberLength + 5)
163
163
  : 10;
164
164
 
165
165
  // Split the line into line parts that will fit the provided terminal width
@@ -189,7 +189,7 @@ export default function codeFrame(
189
189
  resultLines.push(
190
190
  lineNumberPrefixer({
191
191
  lineNumber: (currentLineIndex + 1).toString(10),
192
- endLine: endLineString,
192
+ lineNumberLength,
193
193
  isHighlighted: lineHighlights.length > 0,
194
194
  }) + syntaxHighlightedLine,
195
195
  );
@@ -274,7 +274,7 @@ export default function codeFrame(
274
274
  if (highlightLine) {
275
275
  resultLines.push(
276
276
  lineNumberPrefixer({
277
- endLine: endLineString,
277
+ lineNumberLength,
278
278
  isHighlighted: true,
279
279
  }) + highlightLine,
280
280
  );
@@ -1,4 +1,6 @@
1
1
  import assert from 'assert';
2
+ import {readFileSync} from 'fs';
3
+ import {join as joinPath} from 'path';
2
4
 
3
5
  import codeframe from '../src/codeframe';
4
6
 
@@ -393,7 +395,7 @@ describe('codeframe', () => {
393
395
  assert.equal(lines[7], ' 9 | test');
394
396
  });
395
397
 
396
- it('should properly pad numbers', () => {
398
+ it('should properly pad numbers for large files', () => {
397
399
  let codeframeString = codeframe('test\n'.repeat(1000), [
398
400
  {
399
401
  start: {
@@ -415,17 +417,22 @@ describe('codeframe', () => {
415
417
  column: 2,
416
418
  line: 100,
417
419
  },
418
- message: 'test',
420
+ message: 'test 2',
419
421
  },
420
422
  ]);
421
423
 
422
424
  let lines = codeframeString.split(LINE_END);
423
425
  assert.equal(lines.length, 7);
424
- assert.equal(lines[0], ' 98 | test');
426
+ assert.equal(lines[0], ' 98 | test');
427
+ assert.equal(lines[1], '> 99 | test');
428
+ assert.equal(lines[2], '> | ^ test');
429
+ assert.equal(lines[3], '> 100 | test');
430
+ assert.equal(lines[4], '> | ^ test 2');
431
+ assert.equal(lines[5], ' 101 | test');
425
432
  assert.equal(lines[6], ' 102 | test');
426
433
  });
427
434
 
428
- it('should properly pad numbers', () => {
435
+ it('should properly pad numbers for short files', () => {
429
436
  let codeframeString = codeframe('test\n'.repeat(1000), [
430
437
  {
431
438
  start: {
@@ -453,13 +460,17 @@ describe('codeframe', () => {
453
460
 
454
461
  let lines = codeframeString.split(LINE_END);
455
462
  assert.equal(lines.length, 11);
456
- assert.equal(lines[0], ' 6 | test');
463
+ assert.equal(lines[0], ' 6 | test');
464
+ assert.equal(lines[4], ' 9 | test');
465
+ assert.equal(lines[5], ' 10 | test');
466
+ assert.equal(lines[6], ' 11 | test');
457
467
  assert.equal(lines[10], ' 14 | test');
458
468
  });
459
469
 
460
470
  it('should properly use maxLines', () => {
471
+ let line = 'test '.repeat(100);
461
472
  let codeframeString = codeframe(
462
- 'test\n'.repeat(100),
473
+ `${line}\n`.repeat(100),
463
474
  [
464
475
  {
465
476
  start: {
@@ -487,14 +498,16 @@ describe('codeframe', () => {
487
498
  {
488
499
  useColor: false,
489
500
  maxLines: 10,
501
+ terminalWidth: 5,
490
502
  },
491
503
  );
492
504
 
493
505
  let lines = codeframeString.split(LINE_END);
494
506
  assert.equal(lines.length, 13);
495
- assert.equal(lines[0], ' 4 | test');
496
- assert.equal(lines[11], '> 13 | test');
497
- assert.equal(lines[12], '> | ^^^^');
507
+ assert.equal(lines[0], ' 4 | test test ');
508
+ assert.equal(lines[7], ' 10 | test test ');
509
+ assert.equal(lines[11], '> 13 | test test ');
510
+ assert.equal(lines[12], '> | ^^^^^^^^^^');
498
511
  });
499
512
 
500
513
  it('should be able to handle tabs', () => {
@@ -741,4 +754,46 @@ describe('codeframe', () => {
741
754
  assert.equal(lines[2], '> 2 | ew line new line ne');
742
755
  assert.equal(lines[3], '> | ^^^^^^ I have a message');
743
756
  });
757
+
758
+ it('Should pad properly, T-650', () => {
759
+ let fileContent = readFileSync(
760
+ joinPath(__dirname, './fixtures/a.js'),
761
+ 'utf8',
762
+ );
763
+ let codeframeString = codeframe(
764
+ fileContent,
765
+ [
766
+ {
767
+ start: {
768
+ line: 8,
769
+ column: 10,
770
+ },
771
+ end: {
772
+ line: 8,
773
+ column: 48,
774
+ },
775
+ },
776
+ ],
777
+ {
778
+ useColor: false,
779
+ syntaxHighlighting: false,
780
+ language: 'js',
781
+ terminalWidth: 100,
782
+ },
783
+ );
784
+
785
+ let lines = codeframeString.split(LINE_END);
786
+ assert.equal(lines.length, 5);
787
+ assert.equal(lines[0], ` 7 | import Tooltip from '../tooltip';`);
788
+ assert.equal(
789
+ lines[1],
790
+ `> 8 | import VisuallyHidden from '../visually-hidden';`,
791
+ );
792
+ assert.equal(
793
+ lines[2],
794
+ '> | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^',
795
+ );
796
+ assert.equal(lines[3], ' 9 | ');
797
+ assert.equal(lines[4], ' 10 | /**');
798
+ });
744
799
  });
@@ -0,0 +1,13 @@
1
+ import test from 'test';
2
+ import component from './component';
3
+
4
+ /**
5
+ * This is a comment
6
+ */
7
+ import Tooltip from '../tooltip';
8
+ import VisuallyHidden from '../visually-hidden';
9
+
10
+ /**
11
+ * This is another comment
12
+ */
13
+ import {Label} from './label';