feature: vor und nachteile

This commit is contained in:
Hikyu
2026-05-31 13:54:31 +02:00
parent 552a947a1c
commit e3dad17446
23270 changed files with 3024794 additions and 200 deletions
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024-PRESENT Anthony Fu <https://github.com/antfu>
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.
+19
View File
@@ -0,0 +1,19 @@
# twoslash-vue
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
Extended Twoslash for Vue SFC support.
[📚 Documentation](https://twoslash.netlify.app/packages/vue)
## License
[MIT](./LICENSE) License © 2023-PRESENT [Anthony Fu](https://github.com/antfu)
<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/v/twoslash-vue?style=flat&colorA=161514&colorB=EAB836
[npm-version-href]: https://npmjs.com/package/twoslash-vue
[npm-downloads-src]: https://img.shields.io/npm/dm/twoslash-vue?style=flat&colorA=161514&colorB=E66041
[npm-downloads-href]: https://npmjs.com/package/twoslash-vue
+183
View File
@@ -0,0 +1,183 @@
'use strict';
const languageCore = require('@vue/language-core');
const twoslash = require('twoslash');
const twoslashProtocol = require('twoslash-protocol');
const ts = require('typescript');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
const ts__default = /*#__PURE__*/_interopDefaultCompat(ts);
function createTwoslasher(createOptions = {}) {
const twoslasherBase = twoslash.createTwoslasher(createOptions);
const cache = twoslasherBase.getCacheMap();
const tsOptionDeclarations = ts__default.optionDeclarations;
function getVueLanguage(compilerOptions, vueCompilerOptions) {
if (!cache)
return getLanguage();
const key = `vue:${twoslash.getObjectHash([compilerOptions, vueCompilerOptions])}`;
if (!cache.has(key)) {
const env = getLanguage();
cache.set(key, env);
return env;
}
return cache.get(key);
function getLanguage() {
const resolvedVueOptions = languageCore.resolveVueCompilerOptions(vueCompilerOptions);
resolvedVueOptions.__setupedGlobalTypes = languageCore.setupGlobalTypes(ts__default.sys.getCurrentDirectory(), resolvedVueOptions, ts__default.sys);
const vueLanguagePlugin = languageCore.createVueLanguagePlugin(ts__default, twoslash.defaultCompilerOptions, resolvedVueOptions, (id) => id);
return languageCore.createLanguage(
[vueLanguagePlugin],
new languageCore.FileMap(ts__default.sys.useCaseSensitiveFileNames),
() => {
}
);
}
}
function twoslasher(code, extension, options = {}) {
if (extension !== "vue")
return twoslasherBase(code, extension, options);
const vueCompilerOptions = {
...createOptions.vueCompilerOptions,
...options.vueCompilerOptions
};
const compilerOptions = {
...twoslash.defaultCompilerOptions,
...options.compilerOptions
};
const handbookOptions = {
...twoslash.defaultHandbookOptions,
noErrorsCutted: true,
...options.handbookOptions
};
const sourceMeta = {
removals: [],
positionCompletions: [],
positionQueries: [],
positionHighlights: [],
flagNotations: []
};
const {
customTags = createOptions.customTags || []
} = options;
const pc = twoslashProtocol.createPositionConverter(code);
twoslash.findQueryMarkers(code, sourceMeta, pc);
const flagNotations = twoslash.findFlagNotations(code, customTags, tsOptionDeclarations);
for (const flag of flagNotations) {
switch (flag.type) {
case "unknown":
continue;
case "compilerOptions":
compilerOptions[flag.name] = flag.value;
break;
case "handbookOptions":
handbookOptions[flag.name] = flag.value;
break;
}
sourceMeta.removals.push([flag.start, flag.end]);
}
let strippedCode = code;
for (const [start, end] of sourceMeta.removals) {
strippedCode = strippedCode.slice(0, start) + strippedCode.slice(start, end).replace(/\S/g, " ") + strippedCode.slice(end);
}
const lang = getVueLanguage(compilerOptions, vueCompilerOptions);
const sourceScript = lang.scripts.set("index.vue", ts__default.ScriptSnapshot.fromString(strippedCode));
const fileCompiled = get(sourceScript.generated.embeddedCodes.values(), 2);
const compiled = fileCompiled.snapshot.getText(0, fileCompiled.snapshot.getLength());
const map = languageCore.defaultMapperFactory(fileCompiled.mappings);
function getLastGeneratedOffset(pos) {
const offsets = [...map.toGeneratedLocation(pos)];
if (!offsets.length)
return void 0;
return offsets[offsets.length - 1]?.[0];
}
const result = twoslasherBase(compiled, "tsx", {
...options,
compilerOptions: {
jsx: 1,
jsxImportSource: "vue",
noImplicitAny: false,
...compilerOptions
},
handbookOptions: {
...handbookOptions,
keepNotations: true
},
shouldGetHoverInfo(id) {
return !id.startsWith("__VLS");
},
positionCompletions: sourceMeta.positionCompletions.map((p) => getLastGeneratedOffset(p)),
positionQueries: sourceMeta.positionQueries.map((p) => get(map.toGeneratedLocation(p), 0)?.[0]).filter(isNotNull),
positionHighlights: sourceMeta.positionHighlights.map(([start, end]) => [
get(map.toGeneratedLocation(start), 0)?.[0],
get(map.toGeneratedLocation(end), 0)?.[0]
]).filter((x) => x[0] != null && x[1] != null)
});
if (createOptions.debugShowGeneratedCode)
return result;
const mappedNodes = result.nodes.map((q) => {
if ("text" in q && q.text === "any")
return void 0;
const startMap = get(map.toSourceLocation(q.start), 0);
if (!startMap)
return void 0;
const start = startMap[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)
return void 0;
return Object.assign(q, {
...q,
target: code.slice(start, end),
start: startMap[0],
length: end - start
});
}).filter(isNotNull);
const mappedRemovals = [
...sourceMeta.removals,
...result.meta.removals.map((r) => {
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 void 0;
return [start, end];
}).filter(isNotNull)
];
if (!options.handbookOptions?.keepNotations) {
const removed = twoslashProtocol.removeCodeRanges(code, mappedRemovals, mappedNodes);
result.code = removed.code;
result.meta.removals = removed.removals;
result.nodes = twoslashProtocol.resolveNodePositions(removed.nodes, result.code);
} else {
result.meta.removals = mappedRemovals;
}
result.nodes = result.nodes.filter((n, idx) => {
const next = result.nodes[idx + 1];
if (!next)
return true;
if (next.type === n.type && next.start === n.start)
return false;
return true;
});
result.meta.extension = "vue";
return result;
}
twoslasher.getCacheMap = twoslasherBase.getCacheMap;
return twoslasher;
}
const createTwoslasherVue = createTwoslasher;
function isNotNull(x) {
return x != null;
}
function get(iterator, index) {
for (const item of iterator) {
if (index-- === 0)
return item;
}
return void 0;
}
exports.createTwoslasher = createTwoslasher;
exports.createTwoslasherVue = createTwoslasherVue;
+29
View File
@@ -0,0 +1,29 @@
import { VueCompilerOptions } from '@vue/language-core';
import { CreateTwoslashOptions, TwoslashExecuteOptions, TwoslashInstance } from 'twoslash';
interface VueSpecificOptions {
/**
* Vue Compiler options
*/
vueCompilerOptions?: Partial<VueCompilerOptions>;
}
interface CreateTwoslashVueOptions extends CreateTwoslashOptions, VueSpecificOptions {
/**
* Render the generated code in the output instead of the Vue file
*
* @default false
*/
debugShowGeneratedCode?: boolean;
}
interface TwoslashVueExecuteOptions extends TwoslashExecuteOptions, VueSpecificOptions {
}
/**
* Create a twoslasher instance that add additional support for Vue SFC.
*/
declare function createTwoslasher(createOptions?: CreateTwoslashVueOptions): TwoslashInstance;
/**
* @deprecated Use `createTwoslasher` instead.
*/
declare const createTwoslasherVue: typeof createTwoslasher;
export { type CreateTwoslashVueOptions, type TwoslashVueExecuteOptions, type VueSpecificOptions, createTwoslasher, createTwoslasherVue };
+29
View File
@@ -0,0 +1,29 @@
import { VueCompilerOptions } from '@vue/language-core';
import { CreateTwoslashOptions, TwoslashExecuteOptions, TwoslashInstance } from 'twoslash';
interface VueSpecificOptions {
/**
* Vue Compiler options
*/
vueCompilerOptions?: Partial<VueCompilerOptions>;
}
interface CreateTwoslashVueOptions extends CreateTwoslashOptions, VueSpecificOptions {
/**
* Render the generated code in the output instead of the Vue file
*
* @default false
*/
debugShowGeneratedCode?: boolean;
}
interface TwoslashVueExecuteOptions extends TwoslashExecuteOptions, VueSpecificOptions {
}
/**
* Create a twoslasher instance that add additional support for Vue SFC.
*/
declare function createTwoslasher(createOptions?: CreateTwoslashVueOptions): TwoslashInstance;
/**
* @deprecated Use `createTwoslasher` instead.
*/
declare const createTwoslasherVue: typeof createTwoslasher;
export { type CreateTwoslashVueOptions, type TwoslashVueExecuteOptions, type VueSpecificOptions, createTwoslasher, createTwoslasherVue };
+29
View File
@@ -0,0 +1,29 @@
import { VueCompilerOptions } from '@vue/language-core';
import { CreateTwoslashOptions, TwoslashExecuteOptions, TwoslashInstance } from 'twoslash';
interface VueSpecificOptions {
/**
* Vue Compiler options
*/
vueCompilerOptions?: Partial<VueCompilerOptions>;
}
interface CreateTwoslashVueOptions extends CreateTwoslashOptions, VueSpecificOptions {
/**
* Render the generated code in the output instead of the Vue file
*
* @default false
*/
debugShowGeneratedCode?: boolean;
}
interface TwoslashVueExecuteOptions extends TwoslashExecuteOptions, VueSpecificOptions {
}
/**
* Create a twoslasher instance that add additional support for Vue SFC.
*/
declare function createTwoslasher(createOptions?: CreateTwoslashVueOptions): TwoslashInstance;
/**
* @deprecated Use `createTwoslasher` instead.
*/
declare const createTwoslasherVue: typeof createTwoslasher;
export { type CreateTwoslashVueOptions, type TwoslashVueExecuteOptions, type VueSpecificOptions, createTwoslasher, createTwoslasherVue };
+176
View File
@@ -0,0 +1,176 @@
import { defaultMapperFactory, resolveVueCompilerOptions, setupGlobalTypes, createVueLanguagePlugin, createLanguage, FileMap } from '@vue/language-core';
import { createTwoslasher as createTwoslasher$1, defaultCompilerOptions, defaultHandbookOptions, findQueryMarkers, findFlagNotations, getObjectHash } from 'twoslash';
import { createPositionConverter, removeCodeRanges, resolveNodePositions } from 'twoslash-protocol';
import ts from 'typescript';
function createTwoslasher(createOptions = {}) {
const twoslasherBase = createTwoslasher$1(createOptions);
const cache = twoslasherBase.getCacheMap();
const tsOptionDeclarations = ts.optionDeclarations;
function getVueLanguage(compilerOptions, vueCompilerOptions) {
if (!cache)
return getLanguage();
const key = `vue:${getObjectHash([compilerOptions, vueCompilerOptions])}`;
if (!cache.has(key)) {
const env = getLanguage();
cache.set(key, env);
return env;
}
return cache.get(key);
function getLanguage() {
const resolvedVueOptions = resolveVueCompilerOptions(vueCompilerOptions);
resolvedVueOptions.__setupedGlobalTypes = setupGlobalTypes(ts.sys.getCurrentDirectory(), resolvedVueOptions, ts.sys);
const vueLanguagePlugin = createVueLanguagePlugin(ts, defaultCompilerOptions, resolvedVueOptions, (id) => id);
return createLanguage(
[vueLanguagePlugin],
new FileMap(ts.sys.useCaseSensitiveFileNames),
() => {
}
);
}
}
function twoslasher(code, extension, options = {}) {
if (extension !== "vue")
return twoslasherBase(code, extension, options);
const vueCompilerOptions = {
...createOptions.vueCompilerOptions,
...options.vueCompilerOptions
};
const compilerOptions = {
...defaultCompilerOptions,
...options.compilerOptions
};
const handbookOptions = {
...defaultHandbookOptions,
noErrorsCutted: true,
...options.handbookOptions
};
const sourceMeta = {
removals: [],
positionCompletions: [],
positionQueries: [],
positionHighlights: [],
flagNotations: []
};
const {
customTags = createOptions.customTags || []
} = options;
const pc = createPositionConverter(code);
findQueryMarkers(code, sourceMeta, pc);
const flagNotations = findFlagNotations(code, customTags, tsOptionDeclarations);
for (const flag of flagNotations) {
switch (flag.type) {
case "unknown":
continue;
case "compilerOptions":
compilerOptions[flag.name] = flag.value;
break;
case "handbookOptions":
handbookOptions[flag.name] = flag.value;
break;
}
sourceMeta.removals.push([flag.start, flag.end]);
}
let strippedCode = code;
for (const [start, end] of sourceMeta.removals) {
strippedCode = strippedCode.slice(0, start) + strippedCode.slice(start, end).replace(/\S/g, " ") + strippedCode.slice(end);
}
const lang = getVueLanguage(compilerOptions, vueCompilerOptions);
const sourceScript = lang.scripts.set("index.vue", ts.ScriptSnapshot.fromString(strippedCode));
const fileCompiled = get(sourceScript.generated.embeddedCodes.values(), 2);
const compiled = fileCompiled.snapshot.getText(0, fileCompiled.snapshot.getLength());
const map = defaultMapperFactory(fileCompiled.mappings);
function getLastGeneratedOffset(pos) {
const offsets = [...map.toGeneratedLocation(pos)];
if (!offsets.length)
return void 0;
return offsets[offsets.length - 1]?.[0];
}
const result = twoslasherBase(compiled, "tsx", {
...options,
compilerOptions: {
jsx: 1,
jsxImportSource: "vue",
noImplicitAny: false,
...compilerOptions
},
handbookOptions: {
...handbookOptions,
keepNotations: true
},
shouldGetHoverInfo(id) {
return !id.startsWith("__VLS");
},
positionCompletions: sourceMeta.positionCompletions.map((p) => getLastGeneratedOffset(p)),
positionQueries: sourceMeta.positionQueries.map((p) => get(map.toGeneratedLocation(p), 0)?.[0]).filter(isNotNull),
positionHighlights: sourceMeta.positionHighlights.map(([start, end]) => [
get(map.toGeneratedLocation(start), 0)?.[0],
get(map.toGeneratedLocation(end), 0)?.[0]
]).filter((x) => x[0] != null && x[1] != null)
});
if (createOptions.debugShowGeneratedCode)
return result;
const mappedNodes = result.nodes.map((q) => {
if ("text" in q && q.text === "any")
return void 0;
const startMap = get(map.toSourceLocation(q.start), 0);
if (!startMap)
return void 0;
const start = startMap[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)
return void 0;
return Object.assign(q, {
...q,
target: code.slice(start, end),
start: startMap[0],
length: end - start
});
}).filter(isNotNull);
const mappedRemovals = [
...sourceMeta.removals,
...result.meta.removals.map((r) => {
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 void 0;
return [start, end];
}).filter(isNotNull)
];
if (!options.handbookOptions?.keepNotations) {
const removed = removeCodeRanges(code, mappedRemovals, mappedNodes);
result.code = removed.code;
result.meta.removals = removed.removals;
result.nodes = resolveNodePositions(removed.nodes, result.code);
} else {
result.meta.removals = mappedRemovals;
}
result.nodes = result.nodes.filter((n, idx) => {
const next = result.nodes[idx + 1];
if (!next)
return true;
if (next.type === n.type && next.start === n.start)
return false;
return true;
});
result.meta.extension = "vue";
return result;
}
twoslasher.getCacheMap = twoslasherBase.getCacheMap;
return twoslasher;
}
const createTwoslasherVue = createTwoslasher;
function isNotNull(x) {
return x != null;
}
function get(iterator, index) {
for (const item of iterator) {
if (index-- === 0)
return item;
}
return void 0;
}
export { createTwoslasher, createTwoslasherVue };
+56
View File
@@ -0,0 +1,56 @@
{
"name": "twoslash-vue",
"type": "module",
"version": "0.2.12",
"description": "Extended Twoslash for Vue SFC support",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
"funding": "https://github.com/sponsors/antfu",
"homepage": "https://github.com/twoslashes/twoslash#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/twoslashes/twoslash.git",
"directory": "packages/twoslash-vue"
},
"bugs": "https://github.com/twoslashes/twoslash/issues",
"keywords": [
"twoslash",
"vue",
"shiki"
],
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/*",
"./dist/index.d.ts"
]
}
},
"files": [
"dist"
],
"peerDependencies": {
"typescript": "*"
},
"dependencies": {
"@vue/language-core": "~2.1.6",
"twoslash": "0.2.12",
"twoslash-protocol": "0.2.12"
},
"scripts": {
"build": "unbuild",
"dev": "unbuild --stub",
"start": "esno src/index.ts"
}
}