-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reuse codeFrame helper in logger and deduplicate code (#350)
* refactor: reuse codeFrame helper in logger and deduplicate code * chore: revert unnecessary change * chore: correct linting * chore: add missing changeset * test: update snapshots * fix: reuse already computed pos for ts location start * fix: apply changes from code review * fix: rename locationToBabelLocation to lineColLocToBabelLoc
- Loading branch information
Showing
5 changed files
with
76 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'vite-plugin-checker': patch | ||
--- | ||
|
||
refactor: reuse codeFrame helper in logger and deduplicate code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import type { SourceLocation } from '@babel/code-frame' | ||
|
||
import { tsLocationToBabelLocation } from '../src/codeFrame' | ||
import { lineColLocToBabelLoc, tsLikeLocToBabelLoc } from '../src/codeFrame' | ||
|
||
describe('code frame', () => { | ||
it('should add 1 offset to TS location', () => { | ||
const babelLoc = tsLocationToBabelLocation({ | ||
const babelLoc = tsLikeLocToBabelLoc({ | ||
start: { line: 1, character: 2 }, | ||
end: { line: 3, character: 4 }, | ||
}) | ||
|
||
expect(babelLoc).toEqual({ | ||
start: { line: 2, column: 3 }, | ||
end: { line: 4, column: 5 }, | ||
} as SourceLocation) | ||
}) | ||
}) | ||
|
||
it('transform location without offset', () => { | ||
const babelLoc = lineColLocToBabelLoc({ | ||
line: 1, | ||
column: 2, | ||
endLine: 3, | ||
endColumn: 4, | ||
}) | ||
|
||
expect(babelLoc).toEqual({ | ||
start: { line: 1, column: 2 }, | ||
end: { line: 3, column: 4 }, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,39 @@ | ||
import os from 'node:os' | ||
import type ts from 'typescript' | ||
|
||
import { type SourceLocation, codeFrameColumns } from '@babel/code-frame' | ||
|
||
export function createFrame({ | ||
source, | ||
location, | ||
}: { | ||
source: string // file source code | ||
location: SourceLocation | ||
}) { | ||
const frame = codeFrameColumns(source, location, { | ||
highlightCode: true, | ||
/** | ||
* Create a code frame from source code and location | ||
* @param source source code | ||
* @param location babel compatible location to highlight | ||
*/ | ||
export function createFrame(source: string, location: SourceLocation): string { | ||
return codeFrameColumns(source, location, { | ||
// worker tty did not fork parent process stdout, let's make a workaround | ||
forceColor: true, | ||
}) | ||
.split('\n') | ||
.map((line) => ` ${line}`) | ||
.join(os.EOL) | ||
|
||
return frame | ||
} | ||
|
||
export function tsLocationToBabelLocation( | ||
tsLoc: Record<'start' | 'end', ts.LineAndCharacter /** 0-based */> | ||
export function tsLikeLocToBabelLoc( | ||
tsLoc: Record<'start' | 'end', { line: number; character: number } /** 0-based */> | ||
): SourceLocation { | ||
return { | ||
start: { line: tsLoc.start.line + 1, column: tsLoc.start.character + 1 }, | ||
end: { line: tsLoc.end.line + 1, column: tsLoc.end.character + 1 }, | ||
} | ||
} | ||
|
||
export function lineColLocToBabelLoc(d: { | ||
line: number | ||
column: number | ||
endLine?: number | ||
endColumn?: number | ||
}): SourceLocation { | ||
return { | ||
start: { line: d.line, column: d.column }, | ||
end: { line: d.endLine || 0, column: d.endColumn }, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 16 additions & 16 deletions
32
playground/vls-vue2/__tests__/__snapshots__/test.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters