generated from unjs/template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(loc): add loc helpers ; improve proxyBlock ; fix block issue
- Loading branch information
Showing
7 changed files
with
158 additions
and
21 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
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,64 @@ | ||
export function countLines(source: string) { return (source.match(/\n/g) || []).length } | ||
export function lastLineLength(source: string) { return source.substring(source.lastIndexOf('\n')).length } | ||
|
||
export function createSourceLocation(source: string) { | ||
const endLine = countLines(source) | ||
const endColumn = lastLineLength(source) | ||
|
||
return { | ||
start: { | ||
offset: 0, | ||
line: 0, | ||
column: 0, | ||
}, | ||
end: { | ||
offset: source.length, | ||
line: endLine, | ||
column: endColumn, | ||
}, | ||
source, | ||
} | ||
} | ||
|
||
export function findAllSourceLocations(source: string, search: string) { | ||
const locations = [] | ||
let startOffset = 0 | ||
|
||
while (startOffset < source.length) { | ||
const index = source.indexOf(search, startOffset) | ||
if (index === -1) { | ||
break | ||
} | ||
|
||
const endOffset = index + search.length | ||
|
||
// calculate line and column for start position | ||
const preStartStr = source.substring(0, index) | ||
const startLine = (preStartStr.match(/\n/g) || []).length | ||
const startColumn = preStartStr.substring(preStartStr.lastIndexOf('\n')).length | ||
|
||
// calculate line and column for end position | ||
const preEndStr = source.substring(0, endOffset) | ||
const endLine = (preEndStr.match(/\n/g) || []).length | ||
const endColumn = preEndStr.substring(preEndStr.lastIndexOf('\n')).length | ||
|
||
locations.push({ | ||
start: { | ||
offset: index, | ||
line: startLine, | ||
column: startColumn, | ||
}, | ||
end: { | ||
offset: endOffset, | ||
line: endLine, | ||
column: endColumn, | ||
}, | ||
source: search, | ||
}) | ||
|
||
// move startOffset to after the found occurrence | ||
startOffset = endOffset | ||
} | ||
|
||
return locations.length > 0 ? locations : null | ||
} |
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
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
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,43 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { countLines, createSourceLocation, findAllSourceLocations, lastLineLength } from '../src' | ||
import { completeComponent, script } from './utils' | ||
|
||
describe('LOC Helpers', () => { | ||
it('Can create SourceLocation', () => { | ||
const source = '<script setup>let test: string</script>' | ||
|
||
const loc = createSourceLocation(source) | ||
|
||
expect(loc).toStrictEqual({ | ||
start: { | ||
offset: 0, | ||
line: 0, | ||
column: 0, | ||
}, | ||
end: { | ||
offset: source.length, | ||
column: source.length, | ||
line: 0, | ||
}, | ||
source, | ||
}) | ||
}) | ||
|
||
it('Can find source locations', () => { | ||
const locations = findAllSourceLocations(completeComponent, script) | ||
|
||
expect(locations[0]).toStrictEqual({ | ||
source: script, | ||
start: { | ||
offset: 3, | ||
line: 1, | ||
column: 3, | ||
}, | ||
end: { | ||
offset: 3 + script.length, | ||
line: 1 + countLines(script), | ||
column: 3 + lastLineLength(script), | ||
}, | ||
}) | ||
}) | ||
}) |
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,12 @@ | ||
export const script = '<script>let baseScript: string</script>' | ||
export const scriptSetup = '<script setup>let scriptSetup: string</script>' | ||
export const template = '<template><div>Hello World!</div></template>' | ||
export const style = '<style>div { color: blue; }</style>' | ||
export const styleScoped = '<style scoped>.scoped { color: blue; }</style>' | ||
export const completeComponent = ` | ||
${script} | ||
${scriptSetup} | ||
${template} | ||
${style} | ||
${styleScoped} | ||
` |
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