sveast 0.9.2 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -3
- package/core.js +1 -1
- package/entities.js +1 -1
- package/index.d.ts +437 -7
- package/index.js +1 -1
- package/is-valid-type.js +1 -1
- package/lexer.js +3 -3
- package/module.js +1 -1
- package/package.json +6 -1
- package/parse-imports-exports.js +1 -1
- package/parse-module.js +1 -1
- package/parse-sections.js +1 -1
- package/parse.js +1 -1
- package/runes.js +1 -1
- package/scope.d.ts +1792 -0
- package/scope.js +1 -0
- package/shared-2by923ar.js +3 -0
- package/shared-3371ehgf.js +2 -0
- package/shared-4wqe6a27.js +5 -0
- package/shared-4zk9ntab.js +2 -0
- package/shared-artd3d80.js +2 -0
- package/shared-c9gzmr05.js +2 -0
- package/shared-h6c4cer7.js +2 -0
- package/shared-hjcsmz9f.js +2 -0
- package/shared-jxkrk6hx.js +10 -0
- package/{shared-k5pfyxpa.js → shared-n0fpkmn8.js} +2 -2
- package/shared-n1wnwhk8.js +3 -0
- package/shared-nbaaegmc.js +2 -0
- package/shared-rh41v5ez.js +3 -0
- package/shared-shxvsq2a.js +2 -0
- package/shared-stdj6se1.js +2 -0
- package/{shared-3yc3kmnb.js → shared-v0nj05q2.js} +2 -2
- package/shared-vjpq32kx.js +2 -0
- package/shared-xyqvjc3m.js +10 -0
- package/shared-znzncsf2.js +2 -0
- package/typescript.d.ts +3 -2
- package/typescript.js +1 -1
- package/walk.d.ts +248 -7
- package/walk.js +1 -1
- package/shared-00hyas4z.js +0 -2
- package/shared-40v3xx3k.js +0 -2
- package/shared-4y1phpr0.js +0 -3
- package/shared-5kdpxrm3.js +0 -5
- package/shared-a454y0yc.js +0 -2
- package/shared-fcgh2ks8.js +0 -2
- package/shared-ff56bedk.js +0 -2
- package/shared-np5jnnrw.js +0 -2
- package/shared-r6mb2ytj.js +0 -2
- package/shared-t30czg6b.js +0 -2
- package/shared-tgpdmrqm.js +0 -3
- package/shared-wkbh341y.js +0 -2
- package/shared-x7fez4k2.js +0 -3
- package/shared-yxr92tk5.js +0 -10
- package/shared-zd01zsyh.js +0 -10
package/README.md
CHANGED
|
@@ -31,9 +31,11 @@ ast.fragment.nodes; // [Text, RegularElement]
|
|
|
31
31
|
| [`createParser`, `createModuleParser`](#smaller-bundles-sveastcore-and-sveastmodule) | The parsers without TypeScript, named HTML entities or the template parser, for a smaller bundle | `sveast/core`, `sveast/module` |
|
|
32
32
|
| [`isValidType`](#isvalidtypetext-options--boolean) | Whether a JSDoc type is safe to copy into a `.d.ts` | `sveast` |
|
|
33
33
|
| [`isRunesMode`](#isrunesmodesource--boolean) | Whether svelte compiles a component in runes mode, mostly without parsing it | `sveast` |
|
|
34
|
-
| [`walk`, `visitorKeys`, `markupVisitorKeys`](#walknode-visitor-keys--void) | Visit, skip, stop at or replace nodes, in the whole AST
|
|
34
|
+
| [`walk`, `visitorKeys`, `markupVisitorKeys`, `valueVisitorKeys`, `skipTypeWrappers`](#walknode-visitor-keys--void) | Visit, skip, stop at or replace nodes, in the whole AST, only the markup, or everything but TypeScript's types | `sveast`, `sveast/walk` |
|
|
35
|
+
| [`children`, `forEachChild`](#childrennode-keys--node-and-foreachchildnode-callback-keys--void) | A node's children, cached or without allocating | `sveast`, `sveast/walk` |
|
|
35
36
|
| [`isReference`](#isreferencenode-parent--boolean), [`extractIdentifiers`](#extractidentifierspattern--identifier) | Which identifiers are references, and which ones a pattern declares | `sveast`, `sveast/walk` |
|
|
36
|
-
| [`
|
|
37
|
+
| [`analyzeScopes`](#analyzescopesast--scopes) | Which declaration each name refers to, and every read and write of it | `sveast`, `sveast/scope` |
|
|
38
|
+
| [`createLocator`, `locate`](#createlocatorsource--offset--position) | Lines and columns for the nodes you report, without parsing with `loc` | `sveast`, `sveast/walk` |
|
|
37
39
|
|
|
38
40
|
## Migrating from svelte/compiler
|
|
39
41
|
|
|
@@ -307,7 +309,24 @@ walk(
|
|
|
307
309
|
);
|
|
308
310
|
```
|
|
309
311
|
|
|
310
|
-
`
|
|
312
|
+
`valueVisitorKeys` leaves out TypeScript's types: no type annotation, type parameter or type argument, no `implements`, and no type in `as` or `satisfies`. It reaches every value and binding, so an `Identifier` it reaches is never a type's name, and a tool that rewrites values can't fold `size` in `type S = typeof size` or `let a: typeof size`. It still enters a declaration that holds only types, such as `type`, `interface` or an index signature, but not its children, and of `declare function f(): void` it visits only the name. The TypeScript nodes whose children it visits hold values: `as`, `satisfies`, `<T>a`, `a!` and `a<T>` their expression, an enum its members, a namespace its body, `import a = b` and `export =` their value, and a parameter property its parameter. Walking with it takes about as long as walking everything.
|
|
313
|
+
|
|
314
|
+
`skipTypeWrappers(node)` gives the value inside those wrappers: `a` for `a as T`, `a satisfies T`, `<T>a`, `a!`, `a<T>` and any nesting of them, and `node` itself for anything else, for code that reads values, such as an evaluator deciding whether `size as Size` is a constant.
|
|
315
|
+
|
|
316
|
+
`sveast/walk` exports `walk`, `SKIP`, `STOP`, `visitorKeys`, `markupVisitorKeys`, `valueVisitorKeys`, `skipTypeWrappers`, `children`, `forEachChild`, `isReference`, `extractIdentifiers`, `createLocator`, `locate` and the types without loading a parser, for code that walks ASTs from elsewhere, such as a cache.
|
|
317
|
+
|
|
318
|
+
### `children(node, keys?) => Node[]` and `forEachChild(node, callback, keys?) => void`
|
|
319
|
+
|
|
320
|
+
The child nodes `walk` would visit next, in source order, for a traversal of your own, such as one that evaluates expressions and decides per node what to visit. `forEachChild(node, (child, key, index) => ...)` calls back with each child, the field that holds it, and its index when the field is an array, and allocates nothing. `children(node)` returns an array that it makes on the first call for each node and returns again after, so a tool that walks the same tree many times lists each node's children once. ASTs don't change after `parse`, so the cache stays right unless you change one, for instance by replacing a node with `walk`; don't change the array. Both take `keys` as `walk` does and throw on a node type it has no entry for.
|
|
321
|
+
|
|
322
|
+
```ts
|
|
323
|
+
import { forEachChild, valueVisitorKeys } from "sveast/walk";
|
|
324
|
+
|
|
325
|
+
function visit(node) {
|
|
326
|
+
if (node.type === "ConditionalExpression") return evaluateBranches(node);
|
|
327
|
+
forEachChild(node, visit, valueVisitorKeys);
|
|
328
|
+
}
|
|
329
|
+
```
|
|
311
330
|
|
|
312
331
|
### `isReference(node, parent) => boolean`
|
|
313
332
|
|
|
@@ -340,6 +359,52 @@ if (declaration.type === "VariableDeclaration") {
|
|
|
340
359
|
}
|
|
341
360
|
```
|
|
342
361
|
|
|
362
|
+
### `analyzeScopes(ast) => Scopes`
|
|
363
|
+
|
|
364
|
+
Finds the scopes of a component, its scripts and markup together, or of a module: which declaration each name refers to, and where each binding is read, written or subscribed to. It resolves names as svelte's analyzer does, which `tests/scope.test.ts` checks on the corpus: on 12,548 components from the corpus, svelte's tests and the Svelte projects listed under parity, they agree on all 208,797 names they both resolve, but for a class expression's name inside its body, a snippet's own name and a `var` declared twice, where sveast follows JavaScript.
|
|
365
|
+
|
|
366
|
+
```ts
|
|
367
|
+
import { parse } from "sveast";
|
|
368
|
+
import { analyzeScopes } from "sveast/scope";
|
|
369
|
+
|
|
370
|
+
const ast = parse(`<script>
|
|
371
|
+
let { size = "md" } = $props();
|
|
372
|
+
function label(size) { return size; }
|
|
373
|
+
</script>
|
|
374
|
+
<Tooltip let:direction={actual}>{actual} {size}</Tooltip>`);
|
|
375
|
+
const scopes = analyzeScopes(ast);
|
|
376
|
+
|
|
377
|
+
const size = scopes.instance.get("size");
|
|
378
|
+
size?.prop; // "size"
|
|
379
|
+
size?.rune; // "$props"
|
|
380
|
+
size?.initial; // the Literal "md"
|
|
381
|
+
size?.references.length; // 1: the parameter in `label` is another binding
|
|
382
|
+
size?.references.some((reference) => reference.writes || reference.mutates); // false
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
It returns:
|
|
386
|
+
|
|
387
|
+
- **`module`** and **`instance`**, the scopes of `<script module>` and `<script>` (one scope, for a module). A `Scope` has the `node` that makes it, its `parent`, its `declarations` by name, and `get(name)`, the binding a name refers to there, or `null` for a global.
|
|
388
|
+
- **`scopeOf(node)`**, the scope a node is in; `null` in a type, the `<style>` or another AST.
|
|
389
|
+
- **`bindingOf(identifier)`**, the binding an `Identifier` declares or refers to; `null` for a global, a property key or a name in a type.
|
|
390
|
+
- **`referenceOf(identifier)`**, the `Reference` an `Identifier` is, to tell a read from a write or a store subscription; `null` for one that declares a binding or isn't a reference.
|
|
391
|
+
- **`declaredIn(node)`**, the bindings of the scope a node makes: a function's parameters (its body's declarations are its body's), a block's or an each block's declarations, a snippet's parameters, or an element's `let:` names.
|
|
392
|
+
|
|
393
|
+
A `Binding` has its `name`, its `kind`, the `Identifier` that declares it as `node` (`null` for an each block's index and for `let:x` without a value), the `declaration` that holds it, its `scope` and its `references`. `initial` is what it starts as, as svelte's analyzer has it: an initializer, or for a prop destructured from `$props()`, its default without `$bindable`. `rune` is the rune its initializer calls, such as `"$state"`, `"$derived"` or `"$props"`, unless a binding or a store shadows it, and `prop` the name a parent passes a prop as: `"class"` for `klass` in `let { class: klass } = $props()` or `export { klass as class }`, `"size"` for `export let size`.
|
|
394
|
+
|
|
395
|
+
A `Reference` has its `Identifier` as `node`, its `parent`, its `scope` and its `binding`, and says whether it `reads` the value (all but a plain `=`'s target), `writes` it (`=`, `+=`, `++`, `bind:`, `for (a of b)`), or `mutates` it (`a` in `a.b = c` and `bind:value={a.b}`), and whether it's a `store` subscription. Names the markup holds as strings, such as `Foo` in `<Foo.Bar>`, `a` in `use:a`, `transition:a` and `animate:a`, and `color` in `style:color`, get a new `Identifier` with the name's `start` and `end`.
|
|
396
|
+
|
|
397
|
+
The markup's scopes are svelte's:
|
|
398
|
+
|
|
399
|
+
- `{#each items as item, i (key)}` declares `item` and `i` for the key and the body, but not for `{:else}`.
|
|
400
|
+
- A snippet's name is declared where the snippet is, and its parameters inside it. A top-level snippet is in the markup's scope, so a `<script>` that uses one refers to a global, as in svelte's analyzer.
|
|
401
|
+
- `{@const}` declares a name for its whole block or element, `{:then value}` and `{:catch error}` for their branch.
|
|
402
|
+
- `let:` declares a name for the element's attributes and children, or for a component's default slot; children with a `slot` attribute get their own scope. `let:a={b}` declares `b`, `let:a` declares `a`.
|
|
403
|
+
- `$count`, without a declaration of `$count`, is a subscription to `count`, unless it's a rune: `$state` refers to a store `state` only if `state` isn't itself made by a rune.
|
|
404
|
+
- `$: a = b` declares `a` if nothing else does, and writes it: the assignment is one of its references.
|
|
405
|
+
|
|
406
|
+
Names in TypeScript's types aren't references, and `type` and `interface` declare nothing; it skips them with `valueVisitorKeys`. Enums, enum members and namespaces are declared. It doesn't change the AST, and takes less than half as long as parsing it.
|
|
407
|
+
|
|
343
408
|
### `createLocator(source) => (offset) => Position`
|
|
344
409
|
|
|
345
410
|
A function from an offset to its `{ line, column }`, line from 1 and column from 0, as `loc: true` gives them: for a tool that parses without `loc`, which is faster, and needs lines only for what it reports. Lines are found on the first call, and each lookup starts its search at the line found last, so lookups in source order are fastest. Lines end at `\n`, as in the markup's `loc` and `ParseError`; acorn's `loc` in scripts and modules also ends them at a lone `\r`, U+2028 and U+2029. `parse` drops a leading byte order mark, so pass the source without one.
|
|
@@ -355,6 +420,14 @@ walk(parse(source), {
|
|
|
355
420
|
});
|
|
356
421
|
```
|
|
357
422
|
|
|
423
|
+
`locate(source, offset)` gives the same `{ line, column }` without keeping a locator: it keeps the lines of the last 64 sources it was called with, so a tool that reports nodes from many files, in any order, looks lines up once per file rather than scanning each from the start.
|
|
424
|
+
|
|
425
|
+
```ts
|
|
426
|
+
import { locate } from "sveast";
|
|
427
|
+
|
|
428
|
+
`${file}:${locate(source, node.start).line}`;
|
|
429
|
+
```
|
|
430
|
+
|
|
358
431
|
### Types
|
|
359
432
|
|
|
360
433
|
`AST` is svelte's `AST` namespace (`AST.Root`, `AST.RegularElement`, `AST.CSS.Rule`, ...), corrected to match what the parser returns: `name_loc` and a comment's `loc` are optional, `Root.instance`/`module` are absent rather than `null` when there's no such `<script>`, `Root.js` is declared, and every directive has `modifiers`. Each function's options and results are exported too, e.g. `ParseOptions` and `LexedStatement`.
|
package/core.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./shared-
|
|
1
|
+
import"./shared-stdj6se1.js";import"./shared-4wqe6a27.js";import"./shared-xyqvjc3m.js";import{e}from"./shared-jxkrk6hx.js";import"./shared-nbaaegmc.js";import{Ee}from"./shared-n0fpkmn8.js";import{U}from"./shared-rh41v5ez.js";import"./shared-2by923ar.js";import{T}from"./shared-artd3d80.js";import{k}from"./shared-hjcsmz9f.js";import{ee,kt}from"./shared-h6c4cer7.js";function u(o={}){let p=ee(o.typescript),s={typescript:p,entityNames:kt(o.entities)??Ee};return{parse:(t,r)=>U(t,r,s),parseModule:(t,r={})=>T(t,r,p),parseImportsExports:(t,r={})=>k(t,r,p)}}export{e as ParseError,u as createParser};
|
package/entities.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./shared-
|
|
1
|
+
import"./shared-n0fpkmn8.js";import{v}from"./shared-v0nj05q2.js";import{Tt}from"./shared-h6c4cer7.js";var p=Tt(v);export{p as entities};
|
package/index.d.ts
CHANGED
|
@@ -278,8 +278,7 @@ export interface ParseImportsExportsOptions {
|
|
|
278
278
|
|
|
279
279
|
/**
|
|
280
280
|
* Parses a Svelte component into svelte's modern AST, the same as
|
|
281
|
-
* `parse(source, { modern: true })` from svelte/compiler.
|
|
282
|
-
* `Parser` constructor (`phases/1-parse/index.js`).
|
|
281
|
+
* `parse(source, { modern: true })` from svelte/compiler.
|
|
283
282
|
*
|
|
284
283
|
* Throws a {@link ParseError} on a syntax error.
|
|
285
284
|
*/
|
|
@@ -2019,13 +2018,197 @@ export declare function parseSections(source: string, options?: ParseOptions): A
|
|
|
2019
2018
|
*/
|
|
2020
2019
|
export declare function isRunesMode(source: string): boolean;
|
|
2021
2020
|
|
|
2021
|
+
/** How a {@link Binding} is declared. */
|
|
2022
|
+
export type BindingKind = "import" | "var" | "let" | "const" | "using" | "function" | "class" | "enum" | "enum-member" | "namespace" | "param" | "catch" | "reactive" | "each" | "each-index" | "let-directive" | "snippet" | "snippet-param" | "const-tag" | "await-value" | "await-error";
|
|
2023
|
+
|
|
2024
|
+
/** A name declared in a {@link Scope}. */
|
|
2025
|
+
export interface Binding {
|
|
2026
|
+
readonly name: string;
|
|
2027
|
+
/**
|
|
2028
|
+
* What declares it: `"import"`, `"var"`, `"let"`, `"const"` (`{let}`
|
|
2029
|
+
* and `{const}` tags included), `"using"`, `"function"`, `"class"`,
|
|
2030
|
+
* `"enum"`, `"enum-member"`, `"namespace"`, `"param"`, `"catch"`,
|
|
2031
|
+
* `"reactive"` (`x` in `$: x = ...` without a declaration of `x`),
|
|
2032
|
+
* `"each"` and `"each-index"` (`item` and `i` in `{#each items as item,
|
|
2033
|
+
* i}`), `"let-directive"`, `"snippet"` (its name), `"snippet-param"`,
|
|
2034
|
+
* `"const-tag"` (`{@const}`), `"await-value"` and `"await-error"` (`{:then
|
|
2035
|
+
* value}` and `{:catch error}`).
|
|
2036
|
+
*/
|
|
2037
|
+
readonly kind: BindingKind;
|
|
2038
|
+
/**
|
|
2039
|
+
* The `Identifier` that declares it; `null` for an each block's index
|
|
2040
|
+
* and `let:x` without a value, whose names are strings.
|
|
2041
|
+
*/
|
|
2042
|
+
readonly node: Identifier | null;
|
|
2043
|
+
/**
|
|
2044
|
+
* The node that declares it: the import specifier, the
|
|
2045
|
+
* `VariableDeclarator`, the function or class, the function a
|
|
2046
|
+
* parameter belongs to, the `CatchClause`, the `LabeledStatement` of
|
|
2047
|
+
* `$:`, the `EachBlock`, `LetDirective`, `SnippetBlock` or `AwaitBlock`,
|
|
2048
|
+
* the enum, enum member or namespace, or `import a = b`.
|
|
2049
|
+
*/
|
|
2050
|
+
readonly declaration: AST.SvelteNode;
|
|
2051
|
+
/**
|
|
2052
|
+
* What it starts as, as svelte's analyzer has it: a variable's
|
|
2053
|
+
* initializer, the function or class, the `ImportDeclaration`, the
|
|
2054
|
+
* `SnippetBlock` for a snippet's name, the `EachBlock` for an index,
|
|
2055
|
+
* the right side of `$: x = ...`, an enum member's initializer. For a
|
|
2056
|
+
* prop destructured from `$props()`, its default, without `$bindable`:
|
|
2057
|
+
* `"md"` in `let { size = "md" } = $props()` or in `let { size =
|
|
2058
|
+
* $bindable("md") } = $props()`. `null` otherwise.
|
|
2059
|
+
*/
|
|
2060
|
+
readonly initial: AST.SvelteNode | null;
|
|
2061
|
+
readonly scope: Scope;
|
|
2062
|
+
/**
|
|
2063
|
+
* Every use of it, in source order: reads, writes, and `$name`
|
|
2064
|
+
* subscriptions if it's a store. Not the identifiers that declare it,
|
|
2065
|
+
* but for `x` in `$: x = ...`, which declares `x` and writes it each
|
|
2066
|
+
* time the statement runs.
|
|
2067
|
+
*/
|
|
2068
|
+
readonly references: readonly Reference[];
|
|
2069
|
+
/**
|
|
2070
|
+
* The rune its declarator's initializer calls, such as `"$state"`,
|
|
2071
|
+
* `"$state.raw"`, `"$derived"`, `"$derived.by"` or `"$props"`, unless
|
|
2072
|
+
* a binding or a store shadows it; `null` otherwise.
|
|
2073
|
+
*/
|
|
2074
|
+
readonly rune: string | null;
|
|
2075
|
+
/**
|
|
2076
|
+
* The name a parent passes it as, for a prop: `"class"` for `klass` in
|
|
2077
|
+
* `let { class: klass } = $props()` and in `export { klass as class }`,
|
|
2078
|
+
* `"size"` in `export let size`. `null` otherwise, including for `rest`
|
|
2079
|
+
* in `let { a, ...rest } = $props()`.
|
|
2080
|
+
*/
|
|
2081
|
+
readonly prop: string | null;
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
/** A use of a name. */
|
|
2085
|
+
export interface Reference {
|
|
2086
|
+
/**
|
|
2087
|
+
* The `Identifier`. For a name the AST holds only as a string, such as
|
|
2088
|
+
* `Foo` in `<Foo.Bar>`, `a` in `use:a`, `transition:a`, `in:a`,
|
|
2089
|
+
* `out:a` and `animate:a`, and `color` in `style:color`, a new
|
|
2090
|
+
* `Identifier` with the name's `start` and `end`.
|
|
2091
|
+
*/
|
|
2092
|
+
readonly node: Identifier;
|
|
2093
|
+
/** The node that holds it. */
|
|
2094
|
+
readonly parent: AST.SvelteNode;
|
|
2095
|
+
/** The scope it's in. */
|
|
2096
|
+
readonly scope: Scope;
|
|
2097
|
+
/** What it refers to; `null` for a global, such as `window` or a rune. */
|
|
2098
|
+
readonly binding: Binding | null;
|
|
2099
|
+
/**
|
|
2100
|
+
* Whether it reads the value: all but the target of a plain `=`, and
|
|
2101
|
+
* every store subscription.
|
|
2102
|
+
*/
|
|
2103
|
+
readonly reads: boolean;
|
|
2104
|
+
/**
|
|
2105
|
+
* Whether it assigns a new value: the target of `=`, `+=` and the like,
|
|
2106
|
+
* `++` and `--`, `bind:`, or `for (a of b)`.
|
|
2107
|
+
*/
|
|
2108
|
+
readonly writes: boolean;
|
|
2109
|
+
/**
|
|
2110
|
+
* Whether it changes a property of the value: `a` in `a.b = c`,
|
|
2111
|
+
* `a.b++` and `bind:value={a.b}`.
|
|
2112
|
+
*/
|
|
2113
|
+
readonly mutates: boolean;
|
|
2114
|
+
/**
|
|
2115
|
+
* Whether it's a store subscription: `$count` refers to `count`, and
|
|
2116
|
+
* `$count = 1` mutates it.
|
|
2117
|
+
*/
|
|
2118
|
+
readonly store: boolean;
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2022
2121
|
/**
|
|
2023
|
-
* The
|
|
2024
|
-
*
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2122
|
+
* The names a function, block, element or other node declares, and the
|
|
2123
|
+
* scopes around it.
|
|
2124
|
+
*/
|
|
2125
|
+
export interface Scope {
|
|
2126
|
+
/**
|
|
2127
|
+
* The node that makes it, or `null` for a component's `<script>` or
|
|
2128
|
+
* `<script module>` that isn't there.
|
|
2129
|
+
*/
|
|
2130
|
+
readonly node: AST.SvelteNode | null;
|
|
2131
|
+
readonly parent: Scope | null;
|
|
2132
|
+
/** Its own bindings, by name, in the order they're declared. */
|
|
2133
|
+
readonly declarations: ReadonlyMap<string, Binding>;
|
|
2134
|
+
/**
|
|
2135
|
+
* The binding `name` refers to here, declared in this scope or one
|
|
2136
|
+
* around it; `null` if none does.
|
|
2137
|
+
*/
|
|
2138
|
+
get(name: string): Binding | null;
|
|
2139
|
+
}
|
|
2140
|
+
|
|
2141
|
+
/** What {@link analyzeScopes} found. */
|
|
2142
|
+
export interface Scopes {
|
|
2143
|
+
/** The `<script module>`'s scope, or a module's. */
|
|
2144
|
+
readonly module: Scope;
|
|
2145
|
+
/** The `<script>`'s scope, inside `module`; a module's, for a module. */
|
|
2146
|
+
readonly instance: Scope;
|
|
2147
|
+
/**
|
|
2148
|
+
* The scope `node` is in: for a function, the scope around it, while
|
|
2149
|
+
* its parameters are in its own. `null` for a node in a type, in the
|
|
2150
|
+
* `<style>` or outside the AST.
|
|
2151
|
+
*/
|
|
2152
|
+
scopeOf(node: AST.SvelteNode): Scope | null;
|
|
2153
|
+
/**
|
|
2154
|
+
* The binding an `Identifier` declares or refers to; `null` for a
|
|
2155
|
+
* global, and for a name that isn't a reference, such as a property
|
|
2156
|
+
* key or a name in a type.
|
|
2157
|
+
*/
|
|
2158
|
+
bindingOf(node: Identifier): Binding | null;
|
|
2159
|
+
/**
|
|
2160
|
+
* The use an `Identifier` is, with whether it reads, writes or
|
|
2161
|
+
* mutates its binding or subscribes to it as a store; `null` for an
|
|
2162
|
+
* `Identifier` that declares a binding or isn't a reference. A global's
|
|
2163
|
+
* use has no binding.
|
|
2164
|
+
*/
|
|
2165
|
+
referenceOf(node: Identifier): Reference | null;
|
|
2166
|
+
/**
|
|
2167
|
+
* The bindings of the scope `node` makes, by name; empty if it makes
|
|
2168
|
+
* none. A function's are its parameters, and a function expression's
|
|
2169
|
+
* name; its body's declarations are its body's.
|
|
2170
|
+
*/
|
|
2171
|
+
declaredIn(node: AST.SvelteNode): ReadonlyMap<string, Binding>;
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
/**
|
|
2175
|
+
* Finds every scope, binding and reference in a component, its scripts
|
|
2176
|
+
* and its markup together, or in a module, as svelte's analyzer does:
|
|
2177
|
+
* which declaration each name refers to, and where each binding is read,
|
|
2178
|
+
* written or subscribed to.
|
|
2179
|
+
*
|
|
2180
|
+
* Besides JavaScript's scopes, it knows the markup's: `{#each items as
|
|
2181
|
+
* item, i (key)}` declares `item` and `i` for the key and the body, a
|
|
2182
|
+
* snippet its parameters, `{:then value}` and `{:catch error}` their
|
|
2183
|
+
* names, `{@const}` a name for its whole fragment, and `let:` a name for
|
|
2184
|
+
* the element, or for a component's default or named slot (`let:a={b}`
|
|
2185
|
+
* declares `b`). Store subscriptions such as `$count` are uses of
|
|
2186
|
+
* `count`, unless they're runes. `bind:` writes its target, and
|
|
2187
|
+
* `$: x = ...` declares `x` if nothing else does. A binding records the
|
|
2188
|
+
* rune that initializes it, such as `$state` or `$props`, and the name a
|
|
2189
|
+
* prop is passed as.
|
|
2190
|
+
*
|
|
2191
|
+
* Names in TypeScript types aren't references, and types aren't
|
|
2192
|
+
* declarations: `interface` and `type` declare nothing. A top-level
|
|
2193
|
+
* snippet is declared in the markup's scope, as in svelte's analyzer, so
|
|
2194
|
+
* a `<script>` that uses it refers to a global.
|
|
2195
|
+
*
|
|
2196
|
+
* ```ts
|
|
2197
|
+
* import { parse } from "sveast";
|
|
2198
|
+
* import { analyzeScopes } from "sveast/scope";
|
|
2199
|
+
*
|
|
2200
|
+
* const ast = parse(source);
|
|
2201
|
+
* const scopes = analyzeScopes(ast);
|
|
2202
|
+
* const size = scopes.instance.get("size");
|
|
2203
|
+
* size?.prop; // "size", for `let { size } = $props()`
|
|
2204
|
+
* size?.references.some((reference) => reference.writes || reference.mutates);
|
|
2205
|
+
* ```
|
|
2206
|
+
*
|
|
2207
|
+
* It reads the AST but doesn't change it.
|
|
2028
2208
|
*/
|
|
2209
|
+
export declare function analyzeScopes(ast: AST.Root | Program): Scopes;
|
|
2210
|
+
|
|
2211
|
+
/** The fields of each node type that hold child nodes, in source order. */
|
|
2029
2212
|
export declare const visitorKeys: {
|
|
2030
2213
|
readonly Root: readonly ["module", "instance", "fragment", "css"];
|
|
2031
2214
|
readonly Script: readonly ["attributes", "content"];
|
|
@@ -2145,6 +2328,7 @@ export declare const visitorKeys: {
|
|
|
2145
2328
|
readonly AwaitExpression: readonly ["argument"];
|
|
2146
2329
|
readonly ImportExpression: readonly ["source", "options", "arguments"];
|
|
2147
2330
|
readonly ChainExpression: readonly ["expression"];
|
|
2331
|
+
readonly ParenthesizedExpression: readonly ["expression"];
|
|
2148
2332
|
readonly MetaProperty: readonly ["meta", "property"];
|
|
2149
2333
|
readonly ObjectPattern: readonly ["decorators", "properties", "typeAnnotation"];
|
|
2150
2334
|
readonly ArrayPattern: readonly ["decorators", "elements", "typeAnnotation"];
|
|
@@ -2277,6 +2461,220 @@ export declare const markupVisitorKeys: {
|
|
|
2277
2461
|
readonly SnippetBlock: readonly ["body"];
|
|
2278
2462
|
};
|
|
2279
2463
|
|
|
2464
|
+
/**
|
|
2465
|
+
* {@link visitorKeys} without TypeScript's types: a {@link walk} with it
|
|
2466
|
+
* reaches every value and binding but no type annotation, type
|
|
2467
|
+
* parameter, type argument, `implements` clause or type in `as` and
|
|
2468
|
+
* `satisfies`, so an `Identifier` it reaches in a TypeScript node is
|
|
2469
|
+
* never a type's name. It still enters a declaration that holds only
|
|
2470
|
+
* types, such as `type T = ...`, `interface I {}` or an index signature,
|
|
2471
|
+
* but not its children, and of `declare function f(): void` it visits
|
|
2472
|
+
* only the name. The
|
|
2473
|
+
* TypeScript nodes it enters children of hold values: `as`,
|
|
2474
|
+
* `satisfies`, `<T>a`, `a!` and `a<T>` hold their expression, an enum
|
|
2475
|
+
* its members, a namespace its body, `import a = b` and `export =` their
|
|
2476
|
+
* value, and a parameter property its parameter.
|
|
2477
|
+
*/
|
|
2478
|
+
export declare const valueVisitorKeys: {
|
|
2479
|
+
readonly FunctionDeclaration: readonly ["id", "params", "body"];
|
|
2480
|
+
readonly FunctionExpression: readonly ["id", "params", "body"];
|
|
2481
|
+
readonly ArrowFunctionExpression: readonly ["params", "body"];
|
|
2482
|
+
readonly ClassDeclaration: readonly ["decorators", "id", "superClass", "body"];
|
|
2483
|
+
readonly ClassExpression: readonly ["decorators", "id", "superClass", "body"];
|
|
2484
|
+
readonly MethodDefinition: readonly ["decorators", "key", "value"];
|
|
2485
|
+
readonly PropertyDefinition: readonly ["decorators", "key", "value"];
|
|
2486
|
+
readonly Identifier: readonly ["decorators"];
|
|
2487
|
+
readonly CallExpression: readonly ["callee", "arguments"];
|
|
2488
|
+
readonly NewExpression: readonly ["callee", "arguments"];
|
|
2489
|
+
readonly TaggedTemplateExpression: readonly ["tag", "quasi"];
|
|
2490
|
+
readonly RestElement: readonly ["decorators", "argument"];
|
|
2491
|
+
readonly ObjectPattern: readonly ["decorators", "properties"];
|
|
2492
|
+
readonly ArrayPattern: readonly ["decorators", "elements"];
|
|
2493
|
+
readonly TSTypeAnnotation: readonly [];
|
|
2494
|
+
readonly TSTypeParameterDeclaration: readonly [];
|
|
2495
|
+
readonly TSTypeParameterInstantiation: readonly [];
|
|
2496
|
+
readonly TSTypeParameter: readonly [];
|
|
2497
|
+
readonly TSArrayType: readonly [];
|
|
2498
|
+
readonly TSTupleType: readonly [];
|
|
2499
|
+
readonly TSNamedTupleMember: readonly [];
|
|
2500
|
+
readonly TSOptionalType: readonly [];
|
|
2501
|
+
readonly TSRestType: readonly [];
|
|
2502
|
+
readonly TSUnionType: readonly [];
|
|
2503
|
+
readonly TSIntersectionType: readonly [];
|
|
2504
|
+
readonly TSConditionalType: readonly [];
|
|
2505
|
+
readonly TSInferType: readonly [];
|
|
2506
|
+
readonly TSParenthesizedType: readonly [];
|
|
2507
|
+
readonly TSTypeOperator: readonly [];
|
|
2508
|
+
readonly TSIndexedAccessType: readonly [];
|
|
2509
|
+
readonly TSMappedType: readonly [];
|
|
2510
|
+
readonly TSLiteralType: readonly [];
|
|
2511
|
+
readonly TSTypeReference: readonly [];
|
|
2512
|
+
readonly TSTypeQuery: readonly [];
|
|
2513
|
+
readonly TSImportType: readonly [];
|
|
2514
|
+
readonly TSTypeLiteral: readonly [];
|
|
2515
|
+
readonly TSPropertySignature: readonly [];
|
|
2516
|
+
readonly TSMethodSignature: readonly [];
|
|
2517
|
+
readonly TSCallSignatureDeclaration: readonly [];
|
|
2518
|
+
readonly TSConstructSignatureDeclaration: readonly [];
|
|
2519
|
+
readonly TSFunctionType: readonly [];
|
|
2520
|
+
readonly TSConstructorType: readonly [];
|
|
2521
|
+
readonly TSIndexSignature: readonly [];
|
|
2522
|
+
readonly TSTypePredicate: readonly [];
|
|
2523
|
+
readonly TSInterfaceDeclaration: readonly [];
|
|
2524
|
+
readonly TSInterfaceBody: readonly [];
|
|
2525
|
+
readonly TSExpressionWithTypeArguments: readonly [];
|
|
2526
|
+
readonly TSTypeAliasDeclaration: readonly [];
|
|
2527
|
+
readonly TSNamespaceExportDeclaration: readonly [];
|
|
2528
|
+
readonly TSDeclareFunction: readonly ["id"];
|
|
2529
|
+
readonly TSDeclareMethod: readonly [];
|
|
2530
|
+
readonly TSAsExpression: readonly ["expression"];
|
|
2531
|
+
readonly TSSatisfiesExpression: readonly ["expression"];
|
|
2532
|
+
readonly TSTypeAssertion: readonly ["expression"];
|
|
2533
|
+
readonly TSInstantiationExpression: readonly ["expression"];
|
|
2534
|
+
readonly Root: readonly ["module", "instance", "fragment", "css"];
|
|
2535
|
+
readonly Script: readonly ["attributes", "content"];
|
|
2536
|
+
readonly Fragment: readonly ["nodes"];
|
|
2537
|
+
readonly Text: readonly [];
|
|
2538
|
+
readonly Comment: readonly [];
|
|
2539
|
+
readonly ExpressionTag: readonly ["expression"];
|
|
2540
|
+
readonly HtmlTag: readonly ["expression"];
|
|
2541
|
+
readonly ConstTag: readonly ["declaration"];
|
|
2542
|
+
readonly DeclarationTag: readonly ["declaration"];
|
|
2543
|
+
readonly DebugTag: readonly ["identifiers"];
|
|
2544
|
+
readonly RenderTag: readonly ["expression"];
|
|
2545
|
+
readonly AttachTag: readonly ["expression"];
|
|
2546
|
+
readonly Attribute: readonly ["value"];
|
|
2547
|
+
readonly SpreadAttribute: readonly ["expression"];
|
|
2548
|
+
readonly AnimateDirective: readonly ["expression"];
|
|
2549
|
+
readonly BindDirective: readonly ["expression"];
|
|
2550
|
+
readonly ClassDirective: readonly ["expression"];
|
|
2551
|
+
readonly LetDirective: readonly ["expression"];
|
|
2552
|
+
readonly OnDirective: readonly ["expression"];
|
|
2553
|
+
readonly StyleDirective: readonly ["value"];
|
|
2554
|
+
readonly TransitionDirective: readonly ["expression"];
|
|
2555
|
+
readonly UseDirective: readonly ["expression"];
|
|
2556
|
+
readonly Component: readonly ["attributes", "fragment"];
|
|
2557
|
+
readonly TitleElement: readonly ["attributes", "fragment"];
|
|
2558
|
+
readonly SlotElement: readonly ["attributes", "fragment"];
|
|
2559
|
+
readonly RegularElement: readonly ["attributes", "fragment"];
|
|
2560
|
+
readonly SvelteBody: readonly ["attributes", "fragment"];
|
|
2561
|
+
readonly SvelteBoundary: readonly ["attributes", "fragment"];
|
|
2562
|
+
readonly SvelteComponent: readonly ["expression", "attributes", "fragment"];
|
|
2563
|
+
readonly SvelteDocument: readonly ["attributes", "fragment"];
|
|
2564
|
+
readonly SvelteElement: readonly ["tag", "attributes", "fragment"];
|
|
2565
|
+
readonly SvelteFragment: readonly ["attributes", "fragment"];
|
|
2566
|
+
readonly SvelteHead: readonly ["attributes", "fragment"];
|
|
2567
|
+
readonly SvelteOptions: readonly ["attributes", "fragment"];
|
|
2568
|
+
readonly SvelteSelf: readonly ["attributes", "fragment"];
|
|
2569
|
+
readonly SvelteWindow: readonly ["attributes", "fragment"];
|
|
2570
|
+
readonly IfBlock: readonly ["test", "consequent", "alternate"];
|
|
2571
|
+
readonly EachBlock: readonly ["expression", "context", "key", "body", "fallback"];
|
|
2572
|
+
readonly AwaitBlock: readonly ["expression", "pending", "value", "then", "error", "catch"];
|
|
2573
|
+
readonly KeyBlock: readonly ["expression", "fragment"];
|
|
2574
|
+
readonly SnippetBlock: readonly ["expression", "parameters", "body"];
|
|
2575
|
+
readonly StyleSheet: readonly ["attributes", "children"];
|
|
2576
|
+
readonly Rule: readonly ["prelude", "block"];
|
|
2577
|
+
readonly Atrule: readonly ["block"];
|
|
2578
|
+
readonly SelectorList: readonly ["children"];
|
|
2579
|
+
readonly ComplexSelector: readonly ["children"];
|
|
2580
|
+
readonly RelativeSelector: readonly ["combinator", "selectors"];
|
|
2581
|
+
readonly Combinator: readonly [];
|
|
2582
|
+
readonly TypeSelector: readonly [];
|
|
2583
|
+
readonly IdSelector: readonly [];
|
|
2584
|
+
readonly ClassSelector: readonly [];
|
|
2585
|
+
readonly AttributeSelector: readonly [];
|
|
2586
|
+
readonly PseudoElementSelector: readonly ["args"];
|
|
2587
|
+
readonly PseudoClassSelector: readonly ["args"];
|
|
2588
|
+
readonly Percentage: readonly [];
|
|
2589
|
+
readonly Nth: readonly [];
|
|
2590
|
+
readonly NestingSelector: readonly [];
|
|
2591
|
+
readonly Block: readonly ["children"];
|
|
2592
|
+
readonly Declaration: readonly [];
|
|
2593
|
+
readonly Program: readonly ["body"];
|
|
2594
|
+
readonly ExpressionStatement: readonly ["expression"];
|
|
2595
|
+
readonly BlockStatement: readonly ["body"];
|
|
2596
|
+
readonly StaticBlock: readonly ["body"];
|
|
2597
|
+
readonly EmptyStatement: readonly [];
|
|
2598
|
+
readonly DebuggerStatement: readonly [];
|
|
2599
|
+
readonly WithStatement: readonly ["object", "body"];
|
|
2600
|
+
readonly ReturnStatement: readonly ["argument"];
|
|
2601
|
+
readonly LabeledStatement: readonly ["label", "body"];
|
|
2602
|
+
readonly BreakStatement: readonly ["label"];
|
|
2603
|
+
readonly ContinueStatement: readonly ["label"];
|
|
2604
|
+
readonly IfStatement: readonly ["test", "consequent", "alternate"];
|
|
2605
|
+
readonly SwitchStatement: readonly ["discriminant", "cases"];
|
|
2606
|
+
readonly SwitchCase: readonly ["test", "consequent"];
|
|
2607
|
+
readonly ThrowStatement: readonly ["argument"];
|
|
2608
|
+
readonly TryStatement: readonly ["block", "handler", "finalizer"];
|
|
2609
|
+
readonly CatchClause: readonly ["param", "body"];
|
|
2610
|
+
readonly WhileStatement: readonly ["test", "body"];
|
|
2611
|
+
readonly DoWhileStatement: readonly ["body", "test"];
|
|
2612
|
+
readonly ForStatement: readonly ["init", "test", "update", "body"];
|
|
2613
|
+
readonly ForInStatement: readonly ["left", "right", "body"];
|
|
2614
|
+
readonly ForOfStatement: readonly ["left", "right", "body"];
|
|
2615
|
+
readonly VariableDeclaration: readonly ["declarations"];
|
|
2616
|
+
readonly VariableDeclarator: readonly ["id", "init"];
|
|
2617
|
+
readonly ClassBody: readonly ["body"];
|
|
2618
|
+
readonly Property: readonly ["decorators", "key", "value"];
|
|
2619
|
+
readonly ThisExpression: readonly [];
|
|
2620
|
+
readonly Super: readonly [];
|
|
2621
|
+
readonly Literal: readonly [];
|
|
2622
|
+
readonly PrivateIdentifier: readonly [];
|
|
2623
|
+
readonly ArrayExpression: readonly ["elements"];
|
|
2624
|
+
readonly ObjectExpression: readonly ["properties"];
|
|
2625
|
+
readonly UnaryExpression: readonly ["argument"];
|
|
2626
|
+
readonly UpdateExpression: readonly ["argument"];
|
|
2627
|
+
readonly BinaryExpression: readonly ["left", "right"];
|
|
2628
|
+
readonly LogicalExpression: readonly ["left", "right"];
|
|
2629
|
+
readonly AssignmentExpression: readonly ["left", "right"];
|
|
2630
|
+
readonly MemberExpression: readonly ["object", "property"];
|
|
2631
|
+
readonly ConditionalExpression: readonly ["test", "consequent", "alternate"];
|
|
2632
|
+
readonly SequenceExpression: readonly ["expressions"];
|
|
2633
|
+
readonly TemplateLiteral: readonly ["quasis", "expressions"];
|
|
2634
|
+
readonly TemplateElement: readonly [];
|
|
2635
|
+
readonly SpreadElement: readonly ["argument"];
|
|
2636
|
+
readonly YieldExpression: readonly ["argument"];
|
|
2637
|
+
readonly AwaitExpression: readonly ["argument"];
|
|
2638
|
+
readonly ImportExpression: readonly ["source", "options", "arguments"];
|
|
2639
|
+
readonly ChainExpression: readonly ["expression"];
|
|
2640
|
+
readonly ParenthesizedExpression: readonly ["expression"];
|
|
2641
|
+
readonly MetaProperty: readonly ["meta", "property"];
|
|
2642
|
+
readonly AssignmentPattern: readonly ["decorators", "left", "right"];
|
|
2643
|
+
readonly ImportDeclaration: readonly ["specifiers", "source", "attributes"];
|
|
2644
|
+
readonly ImportSpecifier: readonly ["imported", "local"];
|
|
2645
|
+
readonly ImportDefaultSpecifier: readonly ["local"];
|
|
2646
|
+
readonly ImportNamespaceSpecifier: readonly ["local"];
|
|
2647
|
+
readonly ImportAttribute: readonly ["key", "value"];
|
|
2648
|
+
readonly ExportNamedDeclaration: readonly ["declaration", "specifiers", "source", "attributes"];
|
|
2649
|
+
readonly ExportSpecifier: readonly ["local", "exported"];
|
|
2650
|
+
readonly ExportDefaultDeclaration: readonly ["declaration"];
|
|
2651
|
+
readonly ExportAllDeclaration: readonly ["exported", "source", "attributes"];
|
|
2652
|
+
readonly Decorator: readonly ["expression"];
|
|
2653
|
+
readonly TSAnyKeyword: readonly [];
|
|
2654
|
+
readonly TSBigIntKeyword: readonly [];
|
|
2655
|
+
readonly TSBooleanKeyword: readonly [];
|
|
2656
|
+
readonly TSNeverKeyword: readonly [];
|
|
2657
|
+
readonly TSNullKeyword: readonly [];
|
|
2658
|
+
readonly TSNumberKeyword: readonly [];
|
|
2659
|
+
readonly TSObjectKeyword: readonly [];
|
|
2660
|
+
readonly TSStringKeyword: readonly [];
|
|
2661
|
+
readonly TSSymbolKeyword: readonly [];
|
|
2662
|
+
readonly TSUndefinedKeyword: readonly [];
|
|
2663
|
+
readonly TSUnknownKeyword: readonly [];
|
|
2664
|
+
readonly TSVoidKeyword: readonly [];
|
|
2665
|
+
readonly TSThisType: readonly [];
|
|
2666
|
+
readonly TSQualifiedName: readonly ["left", "right"];
|
|
2667
|
+
readonly TSEnumDeclaration: readonly ["id", "members"];
|
|
2668
|
+
readonly TSEnumMember: readonly ["id", "initializer"];
|
|
2669
|
+
readonly TSModuleDeclaration: readonly ["id", "body"];
|
|
2670
|
+
readonly TSModuleBlock: readonly ["body"];
|
|
2671
|
+
readonly TSImportEqualsDeclaration: readonly ["id", "moduleReference"];
|
|
2672
|
+
readonly TSExternalModuleReference: readonly ["expression"];
|
|
2673
|
+
readonly TSExportAssignment: readonly ["expression"];
|
|
2674
|
+
readonly TSParameterProperty: readonly ["parameter"];
|
|
2675
|
+
readonly TSNonNullExpression: readonly ["expression"];
|
|
2676
|
+
};
|
|
2677
|
+
|
|
2280
2678
|
/**
|
|
2281
2679
|
* Return it from `enter` or `leave` to end the walk: no further `enter` or
|
|
2282
2680
|
* `leave` calls, including `leave` on the node's ancestors.
|
|
@@ -2316,6 +2714,22 @@ export interface Visitor {
|
|
|
2316
2714
|
*/
|
|
2317
2715
|
export declare function walk(node: AST.SvelteNode, visitor: Visitor, keys?: VisitorKeys): void;
|
|
2318
2716
|
|
|
2717
|
+
/**
|
|
2718
|
+
* Calls `callback` with each of `node`'s child nodes in source order, as
|
|
2719
|
+
* {@link walk} would visit them, with the field that holds the child and
|
|
2720
|
+
* its index when the field is an array. Allocates nothing. `keys` is as
|
|
2721
|
+
* in {@link walk}.
|
|
2722
|
+
*/
|
|
2723
|
+
export declare function forEachChild(node: AST.SvelteNode, callback: (child: AST.SvelteNode, key: string, index: number | null) => void, keys?: VisitorKeys): void;
|
|
2724
|
+
|
|
2725
|
+
/**
|
|
2726
|
+
* `node`'s child nodes in source order, as {@link walk} would visit them.
|
|
2727
|
+
* The array is made on the first call for each node and `keys`, and the
|
|
2728
|
+
* same one is returned after, so don't change it; a node that
|
|
2729
|
+
* {@link walk} replaces later isn't in it. `keys` is as in {@link walk}.
|
|
2730
|
+
*/
|
|
2731
|
+
export declare function children(node: AST.SvelteNode, keys?: VisitorKeys): readonly AST.SvelteNode[];
|
|
2732
|
+
|
|
2319
2733
|
/**
|
|
2320
2734
|
* The `Identifier` nodes a pattern binds, in source order: `a`, `c`, `d`,
|
|
2321
2735
|
* `e` and `f` in `{ a, b: [c, ...d], e = 1, ...f }`. Not the property
|
|
@@ -2341,3 +2755,19 @@ export declare function extractIdentifiers(pattern: Pattern | TSParameterPropert
|
|
|
2341
2755
|
* property, an enum's name, or the name or target of `import a = b`.
|
|
2342
2756
|
*/
|
|
2343
2757
|
export declare function isReference(node: AST.SvelteNode, parent: AST.SvelteNode | null): boolean;
|
|
2758
|
+
|
|
2759
|
+
/**
|
|
2760
|
+
* The `{ line, column }` of `offset` in `text`, line from 1 and column
|
|
2761
|
+
* from 0, as {@link createLocator}'s function gives it, without keeping
|
|
2762
|
+
* a locator: the lines of the last 64 texts are kept, so calls for
|
|
2763
|
+
* several files in any order cost a lookup each after the first.
|
|
2764
|
+
*
|
|
2765
|
+
* ```ts
|
|
2766
|
+
* import { locate } from "sveast";
|
|
2767
|
+
*
|
|
2768
|
+
* `${file}:${locate(source, node.start).line}`;
|
|
2769
|
+
* ```
|
|
2770
|
+
*/
|
|
2771
|
+
export declare function locate(text: string, offset: number): Position;
|
|
2772
|
+
|
|
2773
|
+
export declare function skipTypeWrappers(node: AST.SvelteNode): AST.SvelteNode;
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{isValidType as
|
|
1
|
+
import{isValidType as o}from"./is-valid-type.js";import{lexComponent as p,lexImportsExports as s,lexStrings as i}from"./lexer.js";import{parse as n}from"./parse.js";import{parseImportsExports as a}from"./parse-imports-exports.js";import{ParseError as f,parseModule as y}from"./parse-module.js";import{parseSections as l}from"./parse-sections.js";import{isRunesMode as L}from"./runes.js";import{analyzeScopes as E}from"./scope.js";import{children as K,createLocator as P,extractIdentifiers as O,forEachChild as V,isReference as g,locate as k,markupVisitorKeys as C,SKIP as T,STOP as h,skipTypeWrappers as v,valueVisitorKeys as M,visitorKeys as R,walk as w}from"./walk.js";export{f as ParseError,T as SKIP,h as STOP,E as analyzeScopes,K as children,P as createLocator,O as extractIdentifiers,V as forEachChild,g as isReference,L as isRunesMode,o as isValidType,p as lexComponent,s as lexImportsExports,i as lexStrings,k as locate,C as markupVisitorKeys,n as parse,a as parseImportsExports,y as parseModule,l as parseSections,v as skipTypeWrappers,M as valueVisitorKeys,R as visitorKeys,w as walk};
|
package/is-valid-type.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{r}from"./shared-xyqvjc3m.js";import{z}from"./shared-c9gzmr05.js";var a=new Set("abstract accessor arguments as asserts async await break case catch class const continue debugger declare default delete do else enum eval export extends false finally for function get global if implements import in infer instanceof interface intrinsic is keyof let module namespace new null of out override package private protected public readonly return satisfies set static super switch this throw true try type typeof unique var void while with yield".split(" ")),c=new Set("any bigint boolean false never null number object string symbol true undefined unknown void".split(" ")),d=new Set("get set new readonly static public private protected".split(" "));function u(t){return t>=97&&t<=122||t>=65&&t<=90||t===95||t===36}function o(t){return t>=48&&t<=57}function h(t){return u(t)||o(t)}class l{text;pos=0;constructor(t){this.text=t}readAll(){if(!this.type())return!1;return this.peek(),this.pos===this.text.length}peek(){let t=this.text,e=this.pos,s=t.charCodeAt(e);while(s===32||s===9)s=t.charCodeAt(++e);return this.pos=e,s}eat(t){if(this.peek()!==t.charCodeAt(0))return!1;return this.pos++,!0}word(){let t=this.pos;return this.skipWord()?this.text.slice(t,this.pos):void 0}skipWord(){if(!u(this.peek()))return!1;let t=this.pos+1;while(h(this.text.charCodeAt(t)))t++;return this.pos=t,!0}keyword(t){this.peek();let e=this.pos+t.length;if(!this.text.startsWith(t,this.pos)||h(this.text.charCodeAt(e)))return!1;return this.pos=e,!0}name(){let t=this.word();return t!==void 0&&!a.has(t)}string(){let t=this.peek();if(t!==34&&t!==39)return!1;let e=this.text;for(let s=this.pos+1;s<e.length;s++){let i=e.charCodeAt(s);if(i===t)return this.pos=s+1,!0;if(i===92||i===10||i===13||i===8232||i===8233)return!1}return!1}number(){let t=this.text,e=this.pos;if(t.charCodeAt(e)===45)e++;let s=e;while(o(t.charCodeAt(e)))e++;if(e===s||e-s>1&&t.charCodeAt(s)===48)return!1;if(t.charCodeAt(e)===46){let n=++e;while(o(t.charCodeAt(e)))e++;if(e===n)return!1}let i=t.charCodeAt(e);if(h(i)||i===46)return!1;return this.pos=e,!0}type(){if(this.peek()===40){let t=this.pos;if(this.functionType())return!0;this.pos=t}do do if(!this.operatorType())return!1;while(this.eat("&"));while(this.eat("|"));return!0}operatorType(){if(this.keyword("keyof"))return this.operatorType();if(!this.primaryType())return!1;while(this.eat("[")){if(this.eat("]"))continue;if(!this.type()||!this.eat("]"))return!1}return!0}primaryType(){let t=this.peek();if(t===40)return this.pos++,this.type()&&this.eat(")");if(t===123)return this.objectType();if(t===91)return this.tupleType();if(t===34||t===39)return this.string();if(t===45||o(t))return this.number();let e=this.word();if(e===void 0)return!1;if(e==="typeof")return this.typeQuery();if(e==="import")return this.importType(!0);if(c.has(e)){let s=this.peek();return s!==46&&s!==60}if(a.has(e))return!1;while(this.eat("."))if(!this.skipWord())return!1;return!this.eat("<")||this.typeArguments()}typeArguments(){do if(!this.type())return!1;while(this.eat(","));return this.eat(">")}typeQuery(){if(this.keyword("import"))return this.importType(!1);do if(!this.name())return!1;while(this.eat("."));return!0}importType(t){if(!this.eat("(")||!this.string()||!this.eat(")"))return!1;while(this.eat("."))if(!this.name())return!1;return!t||!this.eat("<")||this.typeArguments()}objectType(){this.pos++;while(!this.eat("}")){if(!this.typeMember())return!1;if(!this.eat(";")&&!this.eat(",")&&this.peek()!==125)return!1}return!0}typeMember(){let t=this.peek();if(this.eat("[")){let e=this.peek();if(e===34||e===39){if(!this.string()||!this.eat("]"))return!1}else return this.name()&&this.eat(":")&&this.type()&&this.eat("]")&&this.eat(":")&&this.type()}else if(t===34||t===39){if(!this.string())return!1}else{let e=this.word();if(e===void 0||d.has(e))return!1}return this.eat("?"),this.eat(":")&&this.type()}tupleType(){if(this.pos++,this.eat("]"))return!0;do if(!this.type())return!1;while(this.eat(","));return this.eat("]")}functionType(){if(this.pos++,!this.eat(")")){let t=new Set;for(;;){this.peek();let e=this.text.startsWith("...",this.pos);if(e)this.pos+=3;let s=this.word();if(s===void 0||a.has(s)||t.has(s))return!1;if(t.add(s),!e)this.eat("?");if(!this.eat(":")||!this.type())return!1;if(e||!this.eat(","))break}if(!this.eat(")"))return!1}if(this.peek(),!this.text.startsWith("=>",this.pos))return!1;return this.pos+=2,this.type()}}function f(t){return new l(t).readAll()}var y=r.extend(z);function k(t,e={}){return f(t)||m(t,e.inline??!1)}function m(t,e){let s=!1,i=new y(e?{ecmaVersion:16,sourceType:"module",onComment:(n,b,A,p)=>{if(!n&&p===t.length)s=!0}}:{ecmaVersion:16,sourceType:"module"},t);try{return i.parseWholeType(),!s}catch(n){if(n instanceof SyntaxError)return!1;throw n}}export{k as isValidType,m as parsesAsType};
|