Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: twoslash-svelte #57

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
4a8b006
Initial efforts
Hugos68 Oct 29, 2024
673d20b
Fix readme
Hugos68 Oct 30, 2024
6d3580d
fix lockfile
Hugos68 Oct 30, 2024
cd7b60f
remove comment
Hugos68 Oct 30, 2024
e2a4d11
It works!
Hugos68 Oct 30, 2024
4d99dea
Fixed generateSourceMap
Hugos68 Oct 30, 2024
2a77d4f
Added doc page
Hugos68 Oct 30, 2024
dd44bc9
stuff works
Hugos68 Oct 30, 2024
b44820e
Yay it works
Hugos68 Oct 30, 2024
b80ada6
Fix stuff
Hugos68 Oct 30, 2024
ebca1ce
doc issues
Hugos68 Oct 30, 2024
3337866
Added TODO
Hugos68 Oct 30, 2024
708c755
docs now run
Hugos68 Oct 30, 2024
6453298
Moved package version to pnpm catalog
Hugos68 Oct 30, 2024
bb809da
fix lockfile
Hugos68 Oct 30, 2024
9eaafc1
Hopefully fix lockfile stuff
Hugos68 Oct 30, 2024
4f92d04
test
Hugos68 Oct 30, 2024
6f44a60
hopefully finally fix shit
Hugos68 Oct 30, 2024
cdd8046
Added note
Hugos68 Oct 30, 2024
5cc0a20
Better example
Hugos68 Oct 31, 2024
f089df5
Fix type issue
Hugos68 Oct 31, 2024
16a963f
Added query marker to example
Hugos68 Oct 31, 2024
dfba87f
added back conflict removal
Hugos68 Oct 31, 2024
9ae8fd5
Added svelte2tsx as peer dep
Hugos68 Oct 31, 2024
92678eb
cleanup
Hugos68 Nov 1, 2024
70097d0
Cleanup
Hugos68 Nov 1, 2024
747b686
Added fixture tests for Svelte
Hugos68 Nov 1, 2024
1c9bc12
perhaps fixed lockfile
Hugos68 Nov 1, 2024
928d984
idk
Hugos68 Nov 1, 2024
f1eb24f
fix lockfile
Hugos68 Nov 1, 2024
2916976
Added cut block tests
Hugos68 Nov 1, 2024
f209418
cleanup
Hugos68 Nov 1, 2024
3029210
Removed windows line endings in fixtures
Hugos68 Nov 1, 2024
2b1a472
Remove unnecessary dev dependency
Hugos68 Nov 1, 2024
a39eabc
Remove redundant dep (use positionConverter instead)
Hugos68 Nov 1, 2024
1e13320
remove space
Hugos68 Nov 2, 2024
31efdc9
Fixed Vue test
Hugos68 Nov 2, 2024
955639a
Revert "Fixed Vue test"
Hugos68 Nov 2, 2024
be58cda
Simpler tests
Hugos68 Nov 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import antfu from '@antfu/eslint-config'
import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
import { bundledThemes } from 'shiki'
import { createTwoslasher as createTwoslasherESLint } from 'twoslash-eslint'
import { createTwoslasher as createTwoslasherSvelte } from 'twoslash-svelte'
import { defineConfig } from 'vitepress'
import { version } from '../../package.json'
import vite from './vite.config'
Expand All @@ -24,6 +25,7 @@ const REFERENCES: DefaultTheme.NavItemWithLink[] = [

const INTEGRATIONS: DefaultTheme.NavItemWithLink[] = [
{ text: 'Vue Language Support', link: '/packages/vue' },
{ text: 'Svelte Language Support', link: '/packages/svelte' },
{ text: 'ESLint TwoSlash', link: '/packages/eslint' },
{ text: 'CDN Usage', link: '/packages/cdn' },
]
Expand Down Expand Up @@ -63,6 +65,10 @@ export default defineConfig({
}) as any,
explicitTrigger: /\beslint-check\b/,
}),
transformerTwoslash({
langs: ['svelte'],
twoslasher: createTwoslasherSvelte(),
}),
{
// Render custom themes with codeblocks
name: 'twoslash:inline-theme',
Expand Down
2 changes: 2 additions & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"floating-vue": "catalog:",
"fuse.js": "catalog:",
"sass": "catalog:",
"svelte": "catalog:",
"svelte2tsx": "catalog:",
"unocss": "catalog:",
"vitepress": "catalog:",
"vue": "catalog:"
Expand Down
75 changes: 75 additions & 0 deletions docs/packages/svelte.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Twoslash Svelte

This package added the support for Twoslash to handle Svelte files.

For example:

```svelte twoslash
<script>
import { onMount } from 'svelte'
// ^?

// Reactive state.
let count = $state(0)

// Functions that mutate state and trigger updates.
function increment() {
count++
}

// Lifecycle hooks.
onMount(() => {
console.log(`The initial count is ${count}.`)
})
</script>

<button onclick={increment}>
Count is: {count}
</button>
```

## Installation

::: code-group

```bash [npm]
npm i -D twoslash-svelte svelte2tsx
```
```bash [pnpm]
pnpm i -D twoslash-svelte svelte2tsx
```
```bash [yarn]
yarn add -D twoslash-svelte svelte2tsx
```
```bash [bun]
bun add -D twoslash-svelte svelte2tsx
```

:::

::: info Required Types

`twoslash-svelte` requires `svelte2tsx` to be installed so that required declaration files are present during compilation of the Svelte code. Without `svelte2tsx` installed you might see errors like: `Cannot find name 'svelteHTML'.`

:::

## Usage

```ts twoslash
// @noErrors
import { createTwoslasher } from 'twoslash' // [!code --]
import { createTwoslasher } from 'twoslash-svelte' // [!code ++]

const code = `
<script>
const msg = 'Hello Svelte!'
</script>

<div>
<h1>{msg}</h1>
</div>
`

const twoslasher = createTwoslasher()
const result = twoslasher(code, 'svelte') // [!code hl]
```
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@
"react": "catalog:",
"shiki": "catalog:",
"simple-git-hooks": "catalog:",
"svelte": "catalog:",
"svelte2tsx": "catalog:",
"tslib": "catalog:",
"twoslash": "workspace:*",
"twoslash-cdn": "workspace:*",
"twoslash-eslint": "workspace:*",
"twoslash-svelte": "workspace:*",
"twoslash-vue": "workspace:*",
"typescript": "catalog:",
"unbuild": "catalog:",
Expand Down
21 changes: 21 additions & 0 deletions packages/twoslash-svelte/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024-PRESENT Hugo Korte <https://github.com/Hugos68>

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 changes: 19 additions & 0 deletions packages/twoslash-svelte/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# twoslash-svelte

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]

Extended Twoslash for Svelte support.

[📚 Documentation](https://twoslash.netlify.app/packages/svelte)

## License

[MIT](./LICENSE) License © 2024-PRESENT [Hugos68](https://github.com/Hugos68)

<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/twoslash-svelte?style=flat&colorA=161514&colorB=EAB836
[npm-version-href]: https://npmjs.com/package/twoslash-svelte
[npm-downloads-src]: https://img.shields.io/npm/dm/twoslash-svelte?style=flat&colorA=161514&colorB=E66041
[npm-downloads-href]: https://npmjs.com/package/twoslash-svelte
12 changes: 12 additions & 0 deletions packages/twoslash-svelte/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
entries: [
'src/index',
],
declaration: true,
clean: true,
rollup: {
emitCJS: true,
},
})
58 changes: 58 additions & 0 deletions packages/twoslash-svelte/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "twoslash-svelte",
"type": "module",
"version": "0.2.12",
"description": "Extended Twoslash for Svelte support",
"author": "Hugo Korte <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/twoslashes/twoslash#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/twoslashes/twoslash.git",
"directory": "packages/twoslash-svelte"
},
"bugs": "https://github.com/twoslashes/twoslash/issues",
"keywords": [
"twoslash",
"svelte",
"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"
],
"scripts": {
"build": "unbuild",
"dev": "unbuild --stub",
"prepublishOnly": "nr build",
"start": "esno src/index.ts"
},
"peerDependencies": {
"svelte2tsx": "*",
"typescript": "*"
},
"dependencies": {
"@jridgewell/sourcemap-codec": "catalog:",
"@volar/language-core": "catalog:",
"twoslash": "workspace:*",
"twoslash-protocol": "workspace:*"
}
}
Loading