-
-
Notifications
You must be signed in to change notification settings - Fork 23
Add support for store access type infomation #192
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
import type { Context } from "../../context"; | ||
|
||
/** | ||
* Append store type declarations. | ||
* Append TypeScript code like | ||
* `declare let $foo: Parameters<Parameters<(typeof foo)["subscribe"]>[0]>[0];` | ||
* to define the type information for like $foo variable. | ||
*/ | ||
export function appendDeclareStoreTypes(ctx: Context): void { | ||
const vcode = ctx.sourceCode.scripts.vcode; | ||
const extractStoreRe = /\$[\p{ID_Start}$_][\p{ID_Continue}$\u200c\u200d]*/giu; | ||
let m; | ||
const maybeStores = new Set<string>(); | ||
while ((m = extractStoreRe.exec(vcode))) { | ||
const storeName = m[0]; | ||
const originalName = storeName.slice(1); | ||
maybeStores.add(originalName); | ||
} | ||
|
||
ctx.scriptLet.appendDeclareMaybeStores(maybeStores); | ||
} |
This file contains hidden or 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
7 changes: 7 additions & 0 deletions
7
tests/fixtures/integrations/type-info-tests/i18n-input.svelte
This file contains hidden or 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,7 @@ | ||
<script lang="ts"> | ||
import { _ } from './svelte-i18n'; | ||
</script> | ||
|
||
<main> | ||
<input name={$_('test')}> | ||
</main> |
This file contains hidden or 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 @@ | ||
[] |
This file contains hidden or 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,24 @@ | ||
/* eslint eslint-comments/require-description: 0, @typescript-eslint/explicit-module-boundary-types: 0 */ | ||
import type { Linter } from "eslint"; | ||
import { BASIC_PARSER_OPTIONS } from "../../../src/parser/test-utils"; | ||
import { rules } from "@typescript-eslint/eslint-plugin"; | ||
export function setupLinter(linter: Linter) { | ||
linter.defineRule( | ||
"@typescript-eslint/no-unsafe-call", | ||
rules["no-unsafe-call"] as never | ||
); | ||
} | ||
|
||
export function getConfig() { | ||
return { | ||
parser: "svelte-eslint-parser", | ||
parserOptions: BASIC_PARSER_OPTIONS, | ||
rules: { | ||
"@typescript-eslint/no-unsafe-call": "error", | ||
}, | ||
env: { | ||
browser: true, | ||
es2021: true, | ||
}, | ||
}; | ||
} |
This file contains hidden or 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,6 @@ | ||
import type { Readable } from "svelte/store"; | ||
|
||
declare type MessageFormatter = (id: string) => string; | ||
declare const $format: Readable<MessageFormatter>; | ||
|
||
export { $format as _ }; |
This file contains hidden or 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,7 @@ | ||
<script lang="ts"> | ||
import { _ } from './i18n-test-svelte-i18n'; | ||
</script> | ||
|
||
<main> | ||
<input name={$_('test')}> | ||
</main> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if there is already an identifier named
$nm
or it's already declared by the user manually?This could be uncommon, but before this PR this could happen if the user want to workaround this issue, then this PR would break that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked the edge case and it didn't seem to be a problem.
https://github.com/ota-meshi/svelte-eslint-parser/blob/store-type/tests/fixtures/parser/ast/ts-store01-type-output.svelte
Also, the svelte compiler doesn't seem to allow you to declare variables that start with $ yourself.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
script
withlang="ts"
+ user defineddeclare const $n: xxx
works the same?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test case to check that edge case. That doesn't seem to be a problem either 😉.
https://github.com/ota-meshi/svelte-eslint-parser/blob/store-type/tests/fixtures/parser/ast/ts-store02-type-output.svelte
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that is a bit of surprise to me
https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEYD2A7AzgF3gEigLnkxgEsUBzAKFElgWXS1wKNMqA
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, it should due to
I just learned a lot from this PR!