Skip to content

Commit

Permalink
fix(twoslash-vue): update @vue/language-core to 2.0.22 (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk authored Jun 22, 2024
1 parent 1ecd42c commit fd6136c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/twoslash-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"typescript": "*"
},
"dependencies": {
"@vue/language-core": "^2.0.21",
"@vue/language-core": "^2.0.22",
"twoslash": "workspace:*",
"twoslash-protocol": "workspace:*"
}
Expand Down
20 changes: 10 additions & 10 deletions packages/twoslash-vue/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Language, VueCompilerOptions } from '@vue/language-core'
import { FileMap, SourceMap, createLanguage, createVueLanguagePlugin, resolveVueCompilerOptions } from '@vue/language-core'
import { FileMap, createLanguage, createVueLanguagePlugin, defaultMapperFactory, resolveVueCompilerOptions } from '@vue/language-core'
import type { CompilerOptions } from 'typescript'
import ts from 'typescript'
import type {
Expand Down Expand Up @@ -142,10 +142,10 @@ export function createTwoslasher(createOptions: CreateTwoslashVueOptions = {}):
const compiled = fileCompiled.snapshot.getText(0, fileCompiled.snapshot.getLength())
.replace(/(?=export const __VLS_globalTypesStart)/, '// ---cut-after---\n')

const map = new SourceMap(fileCompiled.mappings)
const map = defaultMapperFactory(fileCompiled.mappings)

function getLastGeneratedOffset(pos: number) {
const offsets = [...map.getGeneratedOffsets(pos)]
const offsets = [...map.toGeneratedLocation(pos)]
if (!offsets.length)
return undefined
return offsets[offsets.length - 1]?.[0]
Expand All @@ -171,12 +171,12 @@ export function createTwoslasher(createOptions: CreateTwoslashVueOptions = {}):
positionCompletions: sourceMeta.positionCompletions
.map(p => getLastGeneratedOffset(p)!),
positionQueries: sourceMeta.positionQueries
.map(p => get(map.getGeneratedOffsets(p), 0)?.[0])
.map(p => get(map.toGeneratedLocation(p), 0)?.[0])
.filter(isNotNull),
positionHighlights: sourceMeta.positionHighlights
.map(([start, end]) => [
get(map.getGeneratedOffsets(start), 0)?.[0],
get(map.getGeneratedOffsets(end), 0)?.[0],
get(map.toGeneratedLocation(start), 0)?.[0],
get(map.toGeneratedLocation(end), 0)?.[0],
])
.filter((x): x is [number, number] => x[0] != null && x[1] != null),
})
Expand All @@ -189,11 +189,11 @@ export function createTwoslasher(createOptions: CreateTwoslashVueOptions = {}):
.map((q) => {
if ('text' in q && q.text === 'any')
return undefined
const startMap = get(map.getSourceOffsets(q.start), 0)
const startMap = get(map.toSourceLocation(q.start), 0)
if (!startMap)
return undefined
const start = startMap[0]
let end = get(map.getSourceOffsets(q.start + q.length), 0)?.[0]
let end = get(map.toSourceLocation(q.start + q.length), 0)?.[0]
if (end == null && startMap[1].sourceOffsets[0] === startMap[0])
end = startMap[1].sourceOffsets[1]
if (end == null || start < 0 || end < 0 || start > end)
Expand All @@ -211,8 +211,8 @@ export function createTwoslasher(createOptions: CreateTwoslashVueOptions = {}):
...sourceMeta.removals,
...result.meta.removals
.map((r) => {
const start = get(map.getSourceOffsets(r[0]), 0)?.[0] ?? code.match(/(?<=<script[\s\S]*>\s)/)?.index
const end = get(map.getSourceOffsets(r[1]), 0)?.[0]
const start = get(map.toSourceLocation(r[0]), 0)?.[0] ?? code.match(/(?<=<script[\s\S]*>\s)/)?.index
const end = get(map.toSourceLocation(r[1]), 0)?.[0]
if (start == null || end == null || start < 0 || end < 0 || start >= end)
return undefined
return [start, end] as Range
Expand Down
29 changes: 14 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd6136c

Please sign in to comment.