sveast 0.7.0 → 0.9.0

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.
Files changed (49) hide show
  1. package/README.md +197 -174
  2. package/core.d.ts +1 -1
  3. package/core.js +1 -1
  4. package/entities.js +1 -1
  5. package/index.d.ts +195 -2
  6. package/index.js +1 -1
  7. package/is-valid-type.js +1 -1
  8. package/lexer.d.ts +88 -0
  9. package/lexer.js +2 -2
  10. package/module.d.ts +1243 -0
  11. package/module.js +1 -0
  12. package/package.json +7 -2
  13. package/parse-imports-exports.js +1 -1
  14. package/parse-module.js +1 -1
  15. package/parse-sections.js +1 -0
  16. package/parse.js +1 -1
  17. package/runes.js +1 -0
  18. package/shared-1qapk0wt.js +2 -0
  19. package/shared-25z9p1fv.js +10 -0
  20. package/shared-3yc3kmnb.js +2 -0
  21. package/shared-40v3xx3k.js +2 -0
  22. package/shared-4gksznp7.js +3 -0
  23. package/shared-96tm9k91.js +2 -0
  24. package/shared-bxg3hhpk.js +5 -0
  25. package/shared-g9knrb0x.js +2 -0
  26. package/shared-k5pfyxpa.js +2 -0
  27. package/shared-np5jnnrw.js +2 -0
  28. package/shared-pnv8wmzz.js +3 -0
  29. package/shared-r8yddmex.js +2 -0
  30. package/shared-rf0vnv09.js +2 -0
  31. package/shared-rfa6cx21.js +3 -0
  32. package/shared-wentwa9g.js +2 -0
  33. package/shared-yqw5faw6.js +2 -0
  34. package/shared-zd01zsyh.js +10 -0
  35. package/typescript.js +1 -1
  36. package/walk.d.ts +80 -2
  37. package/walk.js +1 -1
  38. package/shared-1xjsv3h6.js +0 -2
  39. package/shared-as9can03.js +0 -10
  40. package/shared-b37zemjd.js +0 -2
  41. package/shared-drsxcq8p.js +0 -2
  42. package/shared-g16dt48m.js +0 -2
  43. package/shared-gyydaea2.js +0 -3
  44. package/shared-mwp38arc.js +0 -2
  45. package/shared-pmkdz7sc.js +0 -3
  46. package/shared-pt7hcxxy.js +0 -2
  47. package/shared-pyjcz2f8.js +0 -12
  48. package/shared-qnfwr59k.js +0 -2
  49. package/shared-yr6e6h72.js +0 -2
package/index.d.ts CHANGED
@@ -95,6 +95,94 @@ export type LexedStatement = LexedImport | LexedExport;
95
95
  */
96
96
  export declare function lexImportsExports(source: string): LexedStatement[];
97
97
 
98
+ /** An attribute of a top-level `<script>`, `<style>` or `<svelte:options>`. */
99
+ export interface LexedAttribute {
100
+ name: string;
101
+ /**
102
+ * The value as written, without its quotes: character references
103
+ * aren't decoded, and an expression keeps its braces, as in `"{true}"`
104
+ * for `runes={true}`. `true` for an attribute without a value.
105
+ */
106
+ value: string | true;
107
+ /** The offset of the name. */
108
+ start: number;
109
+ /** The offset after the value, or after the name without one. */
110
+ end: number;
111
+ }
112
+
113
+ /** The text between a top-level element's tags. */
114
+ export interface LexedContent {
115
+ start: number;
116
+ end: number;
117
+ }
118
+
119
+ /** A top-level `<script>`. */
120
+ export interface LexedScript {
121
+ /** The offset of `<script`. */
122
+ start: number;
123
+ /** The offset after `</script>`. */
124
+ end: number;
125
+ /** `"module"` for `<script module>` or `<script context="module">`. */
126
+ context: "default" | "module";
127
+ attributes: LexedAttribute[];
128
+ content: LexedContent;
129
+ }
130
+
131
+ /** The top-level `<style>`. */
132
+ export interface LexedStyle {
133
+ /** The offset of `<style`. */
134
+ start: number;
135
+ /** The offset after `</style>`. */
136
+ end: number;
137
+ attributes: LexedAttribute[];
138
+ content: LexedContent;
139
+ }
140
+
141
+ /** `<svelte:options>`. */
142
+ export interface LexedOptions {
143
+ /** The offset of `<svelte:options`. */
144
+ start: number;
145
+ /** The offset after `/>` or `</svelte:options>`. */
146
+ end: number;
147
+ attributes: LexedAttribute[];
148
+ }
149
+
150
+ /** A component's top-level sections, as {@link lexComponent} finds them. */
151
+ export interface LexedComponent {
152
+ /**
153
+ * Whether `parse` reads the component as TypeScript, which needs the
154
+ * TypeScript plugin: svelte decides it from the first `<script>` with a
155
+ * `lang`, so this is `true` when that one has `lang="ts"`.
156
+ */
157
+ typescript: boolean;
158
+ /** The `<script>` without `module`, as `parse`'s `instance`. */
159
+ instance: LexedScript | null;
160
+ /** The `<script module>`, as `parse`'s `module`. */
161
+ module: LexedScript | null;
162
+ /** The `<style>`, as `parse`'s `css`. */
163
+ css: LexedStyle | null;
164
+ /** `<svelte:options>`, as `parse`'s `options`. */
165
+ options: LexedOptions | null;
166
+ }
167
+
168
+ /**
169
+ * A component's top-level `<script>`, `<script module>`, `<style>` and
170
+ * `<svelte:options>`, with their attributes and the offsets of their tags
171
+ * and content, read without a parser: e.g. to find a component's
172
+ * language or imports, or to rewrite its scripts, without parsing the
173
+ * markup. The offsets are `parse`'s, so they're into the source without
174
+ * a leading byte order mark.
175
+ *
176
+ * The markup is skipped by tracking only tags, attribute values, blocks
177
+ * and the brackets, strings, comments and templates of each `{…}`
178
+ * expression, so a `<script>` inside an element, a block, a string or a
179
+ * comment isn't one. It stops after the last `<script`, `<style` or
180
+ * `<svelte:options`. Where `parse` accepts the component, the sections
181
+ * are the same as its `instance`, `module`, `css` and `options`; it
182
+ * doesn't check syntax and never throws.
183
+ */
184
+ export declare function lexComponent(input: string): LexedComponent;
185
+
98
186
  /**
99
187
  * Options for {@link parse}. The defaults give svelte's own output, minus
100
188
  * `loc` and `name_loc`.
@@ -1856,6 +1944,56 @@ interface Location {
1856
1944
  character: number;
1857
1945
  }
1858
1946
 
1947
+ /**
1948
+ * A function from an offset in `text` to its line, from 1, and column,
1949
+ * from 0, in UTF-16 code units: what `loc: true` gives the node that
1950
+ * starts there, without parsing with `loc`. Lines end at `\n`, as in
1951
+ * svelte's markup `loc` and in `ParseError`; acorn's `loc` in
1952
+ * scripts and modules also ends them at a `\r` not followed by `\n`, at
1953
+ * U+2028 and at U+2029. `parse` drops a leading byte order mark, so pass
1954
+ * the text without it. Lines are found on the first call; lookups in
1955
+ * source order are fastest.
1956
+ *
1957
+ * ```ts
1958
+ * import { createLocator, parse, walk } from "sveast";
1959
+ *
1960
+ * const locate = createLocator(source);
1961
+ * walk(parse(source), {
1962
+ * enter(node) {
1963
+ * if (node.type === "Component") console.log(node.name, locate(node.start).line);
1964
+ * },
1965
+ * });
1966
+ * ```
1967
+ */
1968
+ export declare function createLocator(text: string): (offset: number) => Position;
1969
+
1970
+ /**
1971
+ * Parses a component's top-level `<script>`s, `<style>` and
1972
+ * `<svelte:options>` as {@link parse} does, and skips the markup between
1973
+ * them as `lexComponent` skips it: `fragment` has no nodes, `comments`
1974
+ * only the scripts' comments, and errors in the markup aren't reported.
1975
+ * The rest is `parse`'s, offsets included. For a pass that only reads the
1976
+ * scripts, such as a component's props or its module script's exports.
1977
+ *
1978
+ * Throws a {@link ParseError} on a syntax error in a section.
1979
+ */
1980
+ export declare function parseSections(source: string, options?: ParseOptions): AST.Root;
1981
+
1982
+ /**
1983
+ * Whether svelte compiles the component in runes mode, as
1984
+ * `compile(source).metadata.runes` says: `<svelte:options runes>` if it
1985
+ * has one, otherwise whether it uses a rune, such as `$state` or
1986
+ * `$props`, or `await` outside a function in its instance script or
1987
+ * markup. A `$state` that subscribes to a store named `state` isn't a
1988
+ * rune, as in svelte.
1989
+ *
1990
+ * It reads `<svelte:options>` without parsing, and a component that has
1991
+ * no rune's name and no `await` isn't parsed either. Otherwise it parses
1992
+ * the scripts, and the markup only if one of them is in it, and throws a
1993
+ * `ParseError` on a syntax error there.
1994
+ */
1995
+ export declare function isRunesMode(source: string): boolean;
1996
+
1859
1997
  /**
1860
1998
  * The fields of each node type that hold child nodes, in source order.
1861
1999
  * Typed against the AST, so a node type without an entry, or a key that
@@ -2062,6 +2200,58 @@ export declare const visitorKeys: {
2062
2200
  readonly TSInstantiationExpression: readonly ["expression", "typeArguments"];
2063
2201
  };
2064
2202
 
2203
+ /** A table of the fields of each node type that {@link walk} visits, such as {@link visitorKeys} or {@link markupVisitorKeys}. */
2204
+ export type VisitorKeys = Readonly<Partial<Record<string, readonly string[]>>>;
2205
+
2206
+ /**
2207
+ * The fields of the markup's nodes that hold other markup nodes, for a
2208
+ * {@link walk} that never enters the scripts, the styles or the
2209
+ * expressions: `Root` holds only its `fragment`, an `ExpressionTag` or a
2210
+ * directive has no children, and a block holds only its fragments.
2211
+ */
2212
+ export declare const markupVisitorKeys: {
2213
+ readonly Root: readonly ["fragment"];
2214
+ readonly Fragment: readonly ["nodes"];
2215
+ readonly Text: readonly [];
2216
+ readonly Comment: readonly [];
2217
+ readonly ExpressionTag: readonly [];
2218
+ readonly HtmlTag: readonly [];
2219
+ readonly ConstTag: readonly [];
2220
+ readonly DeclarationTag: readonly [];
2221
+ readonly DebugTag: readonly [];
2222
+ readonly RenderTag: readonly [];
2223
+ readonly AttachTag: readonly [];
2224
+ readonly Attribute: readonly ["value"];
2225
+ readonly SpreadAttribute: readonly [];
2226
+ readonly AnimateDirective: readonly [];
2227
+ readonly BindDirective: readonly [];
2228
+ readonly ClassDirective: readonly [];
2229
+ readonly LetDirective: readonly [];
2230
+ readonly OnDirective: readonly [];
2231
+ readonly StyleDirective: readonly ["value"];
2232
+ readonly TransitionDirective: readonly [];
2233
+ readonly UseDirective: readonly [];
2234
+ readonly Component: readonly ["attributes", "fragment"];
2235
+ readonly TitleElement: readonly ["attributes", "fragment"];
2236
+ readonly SlotElement: readonly ["attributes", "fragment"];
2237
+ readonly RegularElement: readonly ["attributes", "fragment"];
2238
+ readonly SvelteBody: readonly ["attributes", "fragment"];
2239
+ readonly SvelteBoundary: readonly ["attributes", "fragment"];
2240
+ readonly SvelteComponent: readonly ["attributes", "fragment"];
2241
+ readonly SvelteDocument: readonly ["attributes", "fragment"];
2242
+ readonly SvelteElement: readonly ["attributes", "fragment"];
2243
+ readonly SvelteFragment: readonly ["attributes", "fragment"];
2244
+ readonly SvelteHead: readonly ["attributes", "fragment"];
2245
+ readonly SvelteOptions: readonly ["attributes", "fragment"];
2246
+ readonly SvelteSelf: readonly ["attributes", "fragment"];
2247
+ readonly SvelteWindow: readonly ["attributes", "fragment"];
2248
+ readonly IfBlock: readonly ["consequent", "alternate"];
2249
+ readonly EachBlock: readonly ["body", "fallback"];
2250
+ readonly AwaitBlock: readonly ["pending", "then", "catch"];
2251
+ readonly KeyBlock: readonly ["fragment"];
2252
+ readonly SnippetBlock: readonly ["body"];
2253
+ };
2254
+
2065
2255
  /**
2066
2256
  * Return it from `enter` or `leave` to end the walk: no further `enter` or
2067
2257
  * `leave` calls, including `leave` on the node's ancestors.
@@ -2094,9 +2284,12 @@ export interface Visitor {
2094
2284
  * Visits `node` and its descendants depth-first, in source order, calling
2095
2285
  * `visitor.enter` before a node's children and `visitor.leave` after them.
2096
2286
  * `parent[key]` is the node, or `parent[key][index]` when the field is an
2097
- * array; all three are `null` for `node` itself.
2287
+ * array; all three are `null` for `node` itself. `keys` says which fields
2288
+ * of each node type to visit, {@link visitorKeys} by default; with
2289
+ * {@link markupVisitorKeys}, only the markup is visited. A node whose
2290
+ * type it has no entry for throws.
2098
2291
  */
2099
- export declare function walk(node: AST.SvelteNode, visitor: Visitor): void;
2292
+ export declare function walk(node: AST.SvelteNode, visitor: Visitor, keys?: VisitorKeys): void;
2100
2293
 
2101
2294
  /**
2102
2295
  * The `Identifier` nodes a pattern binds, in source order: `a`, `c`, `d`,
package/index.js CHANGED
@@ -1 +1 @@
1
- import{isValidType as t}from"./is-valid-type.js";import{lexImportsExports as p}from"./lexer.js";import{parse as x}from"./parse.js";import{parseImportsExports as m}from"./parse-imports-exports.js";import{ParseError as a,parseModule as y}from"./parse-module.js";import{extractIdentifiers as l,isReference as n,SKIP as I,STOP as S,visitorKeys as c,walk as E}from"./walk.js";export{a as ParseError,I as SKIP,S as STOP,l as extractIdentifiers,n as isReference,t as isValidType,p as lexImportsExports,x as parse,m as parseImportsExports,y as parseModule,c as visitorKeys,E as walk};
1
+ import{isValidType as t}from"./is-valid-type.js";import{lexComponent as p,lexImportsExports as s}from"./lexer.js";import{parse as i}from"./parse.js";import{parseImportsExports as f}from"./parse-imports-exports.js";import{ParseError as d,parseModule as n}from"./parse-module.js";import{parseSections as L}from"./parse-sections.js";import{isRunesMode as l}from"./runes.js";import{createLocator as I,extractIdentifiers as u,isReference as E,markupVisitorKeys as P,SKIP as O,STOP as K,visitorKeys as V,walk as k}from"./walk.js";export{d as ParseError,O as SKIP,K as STOP,I as createLocator,u as extractIdentifiers,E as isReference,l as isRunesMode,t as isValidType,p as lexComponent,s as lexImportsExports,P as markupVisitorKeys,i as parse,f as parseImportsExports,n as parseModule,L as parseSections,V as visitorKeys,k as walk};
package/is-valid-type.js CHANGED
@@ -1 +1 @@
1
- import{t}from"./shared-as9can03.js";import{T}from"./shared-yr6e6h72.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(e){return e>=97&&e<=122||e>=65&&e<=90||e===95||e===36}function o(e){return e>=48&&e<=57}function h(e){return u(e)||o(e)}class l{text;pos=0;constructor(e){this.text=e}readAll(){if(!this.type())return!1;return this.peek(),this.pos===this.text.length}peek(){let e=this.text,s=this.pos,r=e.charCodeAt(s);while(r===32||r===9)r=e.charCodeAt(++s);return this.pos=s,r}eat(e){if(this.peek()!==e.charCodeAt(0))return!1;return this.pos++,!0}word(){if(!u(this.peek()))return;let e=this.pos,s=e+1;while(h(this.text.charCodeAt(s)))s++;return this.pos=s,this.text.slice(e,s)}name(){let e=this.word();return e!==void 0&&!a.has(e)}string(){let e=this.peek();if(e!==34&&e!==39)return!1;let s=this.text;for(let r=this.pos+1;r<s.length;r++){let i=s.charCodeAt(r);if(i===e)return this.pos=r+1,!0;if(i===92||i===10||i===13||i===8232||i===8233)return!1}return!1}number(){let e=this.text,s=this.pos;if(e.charCodeAt(s)===45)s++;let r=s;while(o(e.charCodeAt(s)))s++;if(s===r||s-r>1&&e.charCodeAt(r)===48)return!1;if(e.charCodeAt(s)===46){let n=++s;while(o(e.charCodeAt(s)))s++;if(s===n)return!1}let i=e.charCodeAt(s);if(h(i)||i===46)return!1;return this.pos=s,!0}type(){if(this.peek()===40){let e=this.pos;if(this.functionType())return!0;this.pos=e}do do if(!this.operatorType())return!1;while(this.eat("&"));while(this.eat("|"));return!0}operatorType(){let e=this.pos;if(this.word()==="keyof")return this.operatorType();if(this.pos=e,!this.primaryType())return!1;while(this.eat("[")){if(this.eat("]"))continue;if(!this.type()||!this.eat("]"))return!1}return!0}primaryType(){let e=this.peek();if(e===40)return this.pos++,this.type()&&this.eat(")");if(e===123)return this.objectType();if(e===91)return this.tupleType();if(e===34||e===39)return this.string();if(e===45||o(e))return this.number();let s=this.word();if(s===void 0)return!1;if(s==="typeof")return this.typeQuery();if(s==="import")return this.importType(!0);if(c.has(s)){let r=this.peek();return r!==46&&r!==60}if(a.has(s))return!1;while(this.eat("."))if(this.word()===void 0)return!1;return!this.eat("<")||this.typeArguments()}typeArguments(){do if(!this.type())return!1;while(this.eat(","));return this.eat(">")}typeQuery(){let e=this.pos;if(this.word()==="import")return this.importType(!1);this.pos=e;do if(!this.name())return!1;while(this.eat("."));return!0}importType(e){if(!this.eat("(")||!this.string()||!this.eat(")"))return!1;while(this.eat("."))if(!this.name())return!1;return!e||!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 e=this.peek();if(this.eat("[")){let s=this.peek();if(s===34||s===39){if(!this.string()||!this.eat("]"))return!1}else return this.name()&&this.eat(":")&&this.type()&&this.eat("]")&&this.eat(":")&&this.type()}else if(e===34||e===39){if(!this.string())return!1}else{let s=this.word();if(s===void 0||d.has(s))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 e=new Set;for(;;){this.peek();let s=this.text.startsWith("...",this.pos);if(s)this.pos+=3;let r=this.word();if(r===void 0||a.has(r)||e.has(r))return!1;if(e.add(r),!s)this.eat("?");if(!this.eat(":")||!this.type())return!1;if(s||!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(e){return new l(e).readAll()}var y=t.extend(T);function S(e,s={}){return f(e)||m(e,s.inline??!1)}function m(e,s){let r=!1,i=new y(s?{ecmaVersion:16,sourceType:"module",onComment:(n,b,A,p)=>{if(!n&&p===e.length)r=!0}}:{ecmaVersion:16,sourceType:"module"},e);try{return i.parseWholeType(),!r}catch(n){if(n instanceof SyntaxError)return!1;throw n}}export{S as isValidType,m as parsesAsType};
1
+ import{i}from"./shared-zd01zsyh.js";import{q}from"./shared-np5jnnrw.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(){if(!u(this.peek()))return;let t=this.pos,e=t+1;while(h(this.text.charCodeAt(e)))e++;return this.pos=e,this.text.slice(t,e)}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 r=e.charCodeAt(s);if(r===t)return this.pos=s+1,!0;if(r===92||r===10||r===13||r===8232||r===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 r=t.charCodeAt(e);if(h(r)||r===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(){let t=this.pos;if(this.word()==="keyof")return this.operatorType();if(this.pos=t,!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.word()===void 0)return!1;return!this.eat("<")||this.typeArguments()}typeArguments(){do if(!this.type())return!1;while(this.eat(","));return this.eat(">")}typeQuery(){let t=this.pos;if(this.word()==="import")return this.importType(!1);this.pos=t;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=i.extend(q);function C(t,e={}){return f(t)||m(t,e.inline??!1)}function m(t,e){let s=!1,r=new y(e?{ecmaVersion:16,sourceType:"module",onComment:(n,b,T,p)=>{if(!n&&p===t.length)s=!0}}:{ecmaVersion:16,sourceType:"module"},t);try{return r.parseWholeType(),!s}catch(n){if(n instanceof SyntaxError)return!1;throw n}}export{C as isValidType,m as parsesAsType};
package/lexer.d.ts CHANGED
@@ -77,3 +77,91 @@ export type LexedStatement = LexedImport | LexedExport;
77
77
  * TypeScript's `type` forms are always read.
78
78
  */
79
79
  export declare function lexImportsExports(source: string): LexedStatement[];
80
+
81
+ /** An attribute of a top-level `<script>`, `<style>` or `<svelte:options>`. */
82
+ export interface LexedAttribute {
83
+ name: string;
84
+ /**
85
+ * The value as written, without its quotes: character references
86
+ * aren't decoded, and an expression keeps its braces, as in `"{true}"`
87
+ * for `runes={true}`. `true` for an attribute without a value.
88
+ */
89
+ value: string | true;
90
+ /** The offset of the name. */
91
+ start: number;
92
+ /** The offset after the value, or after the name without one. */
93
+ end: number;
94
+ }
95
+
96
+ /** The text between a top-level element's tags. */
97
+ export interface LexedContent {
98
+ start: number;
99
+ end: number;
100
+ }
101
+
102
+ /** A top-level `<script>`. */
103
+ export interface LexedScript {
104
+ /** The offset of `<script`. */
105
+ start: number;
106
+ /** The offset after `</script>`. */
107
+ end: number;
108
+ /** `"module"` for `<script module>` or `<script context="module">`. */
109
+ context: "default" | "module";
110
+ attributes: LexedAttribute[];
111
+ content: LexedContent;
112
+ }
113
+
114
+ /** The top-level `<style>`. */
115
+ export interface LexedStyle {
116
+ /** The offset of `<style`. */
117
+ start: number;
118
+ /** The offset after `</style>`. */
119
+ end: number;
120
+ attributes: LexedAttribute[];
121
+ content: LexedContent;
122
+ }
123
+
124
+ /** `<svelte:options>`. */
125
+ export interface LexedOptions {
126
+ /** The offset of `<svelte:options`. */
127
+ start: number;
128
+ /** The offset after `/>` or `</svelte:options>`. */
129
+ end: number;
130
+ attributes: LexedAttribute[];
131
+ }
132
+
133
+ /** A component's top-level sections, as {@link lexComponent} finds them. */
134
+ export interface LexedComponent {
135
+ /**
136
+ * Whether `parse` reads the component as TypeScript, which needs the
137
+ * TypeScript plugin: svelte decides it from the first `<script>` with a
138
+ * `lang`, so this is `true` when that one has `lang="ts"`.
139
+ */
140
+ typescript: boolean;
141
+ /** The `<script>` without `module`, as `parse`'s `instance`. */
142
+ instance: LexedScript | null;
143
+ /** The `<script module>`, as `parse`'s `module`. */
144
+ module: LexedScript | null;
145
+ /** The `<style>`, as `parse`'s `css`. */
146
+ css: LexedStyle | null;
147
+ /** `<svelte:options>`, as `parse`'s `options`. */
148
+ options: LexedOptions | null;
149
+ }
150
+
151
+ /**
152
+ * A component's top-level `<script>`, `<script module>`, `<style>` and
153
+ * `<svelte:options>`, with their attributes and the offsets of their tags
154
+ * and content, read without a parser: e.g. to find a component's
155
+ * language or imports, or to rewrite its scripts, without parsing the
156
+ * markup. The offsets are `parse`'s, so they're into the source without
157
+ * a leading byte order mark.
158
+ *
159
+ * The markup is skipped by tracking only tags, attribute values, blocks
160
+ * and the brackets, strings, comments and templates of each `{…}`
161
+ * expression, so a `<script>` inside an element, a block, a string or a
162
+ * comment isn't one. It stops after the last `<script`, `<style` or
163
+ * `<svelte:options`. Where `parse` accepts the component, the sections
164
+ * are the same as its `instance`, `module`, `css` and `options`; it
165
+ * doesn't check syntax and never throws.
166
+ */
167
+ export declare function lexComponent(input: string): LexedComponent;
package/lexer.js CHANGED
@@ -1,2 +1,2 @@
1
- import{r,k,h,p,y}from"./shared-pmkdz7sc.js";function g(e){let t=new m(e),i=[];return k(e,!1,(n,s)=>{let a=t.statement(s);return i.push(a),a.end}),i}class m{source;pos=0;end=0;escapeEnd=0;constructor(e){this.source=e}statement(e){this.pos=this.end=e+6;let t=this.source.charCodeAt(e)===105?{kind:"import",start:e,end:0,source:null,typeOnly:!1,specifiers:[]}:{kind:"export",start:e,end:0,source:null,typeOnly:!1,specifiers:[]};if(!(t.kind==="import"?this.import(t):this.export(t)))t.source=null,t.specifiers=[];return t.end=this.end,t}import(e){let t=this.string();if(t!==null)return e.source=t,this.finish();if(this.isWord("type")){let a=this.peek(4);if(a===123||a===42||c(a)&&!this.isWordAfter(4,"from"))e.typeOnly=!0,this.advance(4)}let{specifiers:i,typeOnly:n}=e,s=this.at();if(s!==123&&s!==42){let a=this.pos,o=this.name();if(o===null)return!1;if(this.at()===61)return this.importEquals();if(i.push({kind:"default",imported:"default",local:o,typeOnly:n,start:a,end:this.end}),!this.eat(44))return this.from(e);s=this.at()}if(s===42){let a=this.pos;if(this.advance(1),!this.eatWord("as"))return!1;let o=this.name();if(o===null)return!1;return i.push({kind:"namespace",imported:"*",local:o,typeOnly:n,start:a,end:this.end}),this.from(e)}if(!this.eat(123))return!1;while(!this.eat(125)){let a=this.skip(),o=this.typeModifier()||n,u=this.name(),d=u??this.string()?.value;if(d===void 0)return!1;let l=u;if(this.eatWord("as"))l=this.name();if(l===null)return!1;if(i.push({kind:"named",imported:d,local:l,typeOnly:o,start:a,end:this.end}),!this.eat(44)&&this.at()!==125)return!1}return this.from(e)}export(e){if(this.eatWord("import")){if(this.isWord("type")&&c(this.peek(4))&&!this.isWordAfter(4,"from"))e.typeOnly=!0,this.advance(4);return this.name()!==null&&this.at()===61&&this.importEquals()}if(this.isWord("type")){let n=this.peek(4);if(n===123||n===42)e.typeOnly=!0,this.advance(4)}let{specifiers:t,typeOnly:i}=e;if(this.at()===42){let n=this.pos;this.advance(1);let s=null;if(this.eatWord("as")){if(s=this.moduleExportName(),s===null)return!1}return t.push({kind:s===null?"all":"namespace",local:"*",exported:s,typeOnly:i,start:n,end:this.end}),this.from(e)}if(!this.eat(123))return!1;while(!this.eat(125)){let n=this.skip(),s=this.typeModifier()||i,a=this.moduleExportName();if(a===null)return!1;let o=a;if(this.eatWord("as"))o=this.moduleExportName();if(o===null)return!1;if(t.push({kind:"named",local:a,exported:o,typeOnly:s,start:n,end:this.end}),!this.eat(44)&&this.at()!==125)return!1}return this.from(e)}typeModifier(){if(!this.isWord("type"))return!1;let e=this.peek(4);if(e===34||e===39||c(e)&&!this.isWordAfter(4,"as"))return this.advance(4),!0;return!1}importEquals(){if(this.advance(1),this.isWord("require")&&this.peek(7)===40){if(this.advance(7),this.eat(40),this.string()===null||!this.eat(41))return!1}else do if(this.name()===null)return!1;while(this.eat(46));return this.eat(59),!0}from(e){if(!this.eatWord("from"))return!1;return e.source=this.string(),e.source!==null&&this.finish()}finish(){let e=this.isWord("with")?4:0;if(this.isWord("assert")&&!x(this.source,this.end,this.pos))e=6;if(e!==0&&this.peek(e)===123){this.advance(e),this.eat(123);while(!this.eat(125))if(this.string()===null&&this.name()===null&&!this.eat(58)&&!this.eat(44))return!1}return this.eat(59),!0}skip(){return this.pos=p(this.source,this.pos),this.pos}at(){return this.source.charCodeAt(this.skip())}advance(e){this.pos+=e,this.end=this.pos}peek(e){return this.source.charCodeAt(p(this.source,this.pos+e))}isWordAfter(e,t){return y(this.source,p(this.source,this.pos+e),t)}eat(e){if(this.at()!==e)return!1;return this.advance(1),!0}isWord(e){return y(this.source,this.skip(),e)}eatWord(e){if(!this.isWord(e))return!1;return this.advance(e.length),!0}moduleExportName(){return this.name()??this.string()?.value??null}name(){let e=this.source,t=this.skip();if(!r(e.charCodeAt(t))||h(e.charCodeAt(t)))return null;let i="",n=t,s=t;while(s<e.length){let a=e.charCodeAt(s);if(a===92){if(e.charCodeAt(s+1)!==117)return null;let o=this.unicodeEscape(s+2);if(o===null)return null;i+=e.slice(n,s)+o,s=n=this.escapeEnd}else if(r(a))s++;else break}return this.advance(s-t),i+e.slice(n,s)}string(){let e=this.source,t=this.skip(),i=e.charCodeAt(t);if(i!==34&&i!==39)return null;let n="",s=t+1;for(let a=s;a<e.length;){let o=e.charCodeAt(a);if(o===i)return this.advance(a+1-t),{value:n+e.slice(s,a),start:t,end:a+1};if(o===10||o===13)return null;if(o===92){let u=this.escape(a+1);if(u===null)return null;n+=e.slice(s,a)+u,a=s=this.escapeEnd}else a++}return null}escape(e){let t=this.source.charCodeAt(e);switch(this.escapeEnd=e+1,t){case 110:return`
2
- `;case 114:return"\r";case 116:return"\t";case 98:return"\b";case 118:return"\v";case 102:return"\f";case 48:return h(this.source.charCodeAt(e+1))?null:"\x00";case 120:return this.hexEscape(e+1,2);case 117:return this.unicodeEscape(e+1);case 13:if(this.source.charCodeAt(e+1)===10)this.escapeEnd++;return"";case 10:case 8232:case 8233:return"";default:return h(t)||e>=this.source.length?null:String.fromCharCode(t)}}unicodeEscape(e){if(this.source.charCodeAt(e)!==123)return this.hexEscape(e,4);let t=0,i=e+1;for(;this.source.charCodeAt(i)!==125;i++){let n=f(this.source.charCodeAt(i));if(n===-1)return null;if(t=t*16+n,t>1114111)return null}if(i===e+1)return null;return this.escapeEnd=i+1,String.fromCodePoint(t)}hexEscape(e,t){let i=0;for(let n=e;n<e+t;n++){let s=f(this.source.charCodeAt(n));if(s===-1)return null;i=i*16+s}return this.escapeEnd=e+t,String.fromCharCode(i)}}function c(e){return e!==92&&r(e)&&!h(e)}function f(e){if(h(e))return e-48;let t=e|32;return t>=97&&t<=102?t-87:-1}function x(e,t,i){for(let n=t;n<i;n++){let s=e.charCodeAt(n);if(s===10||s===13||s===8232||s===8233)return!0}return!1}export{g as lexImportsExports};
1
+ import{r,W,u,m,_}from"./shared-rfa6cx21.js";import"./shared-40v3xx3k.js";import{H}from"./shared-pnv8wmzz.js";function g(e){let t=new p(e),n=[];return W(e,!1,(i,s)=>{let o=t.statement(s);return n.push(o),o.end}),n}class p{source;pos=0;end=0;escapeEnd=0;constructor(e){this.source=e}statement(e){this.pos=this.end=e+6;let t=this.source.charCodeAt(e)===105?{kind:"import",start:e,end:0,source:null,typeOnly:!1,specifiers:[]}:{kind:"export",start:e,end:0,source:null,typeOnly:!1,specifiers:[]};if(!(t.kind==="import"?this.import(t):this.export(t)))t.source=null,t.specifiers=[];return t.end=this.end,t}import(e){let t=this.string();if(t!==null)return e.source=t,this.finish();if(this.isWord("type")){let o=this.peek(4);if(o===123||o===42||c(o)&&!this.isWordAfter(4,"from"))e.typeOnly=!0,this.advance(4)}let{specifiers:n,typeOnly:i}=e,s=this.at();if(s!==123&&s!==42){let o=this.pos,a=this.name();if(a===null)return!1;if(this.at()===61)return this.importEquals();if(n.push({kind:"default",imported:"default",local:a,typeOnly:i,start:o,end:this.end}),!this.eat(44))return this.from(e);s=this.at()}if(s===42){let o=this.pos;if(this.advance(1),!this.eatWord("as"))return!1;let a=this.name();if(a===null)return!1;return n.push({kind:"namespace",imported:"*",local:a,typeOnly:i,start:o,end:this.end}),this.from(e)}if(!this.eat(123))return!1;while(!this.eat(125)){let o=this.skip(),a=this.typeModifier()||i,l=this.name(),d=l??this.string()?.value;if(d===void 0)return!1;let h=l;if(this.eatWord("as"))h=this.name();if(h===null)return!1;if(n.push({kind:"named",imported:d,local:h,typeOnly:a,start:o,end:this.end}),!this.eat(44)&&this.at()!==125)return!1}return this.from(e)}export(e){if(this.eatWord("import")){if(this.isWord("type")&&c(this.peek(4))&&!this.isWordAfter(4,"from"))e.typeOnly=!0,this.advance(4);return this.name()!==null&&this.at()===61&&this.importEquals()}if(this.isWord("type")){let i=this.peek(4);if(i===123||i===42)e.typeOnly=!0,this.advance(4)}let{specifiers:t,typeOnly:n}=e;if(this.at()===42){let i=this.pos;this.advance(1);let s=null;if(this.eatWord("as")){if(s=this.moduleExportName(),s===null)return!1}return t.push({kind:s===null?"all":"namespace",local:"*",exported:s,typeOnly:n,start:i,end:this.end}),this.from(e)}if(!this.eat(123))return!1;while(!this.eat(125)){let i=this.skip(),s=this.typeModifier()||n,o=this.moduleExportName();if(o===null)return!1;let a=o;if(this.eatWord("as"))a=this.moduleExportName();if(a===null)return!1;if(t.push({kind:"named",local:o,exported:a,typeOnly:s,start:i,end:this.end}),!this.eat(44)&&this.at()!==125)return!1}return this.from(e)}typeModifier(){if(!this.isWord("type"))return!1;let e=this.peek(4);if(e===34||e===39||c(e)&&!this.isWordAfter(4,"as"))return this.advance(4),!0;return!1}importEquals(){if(this.advance(1),this.isWord("require")&&this.peek(7)===40){if(this.advance(7),this.eat(40),this.string()===null||!this.eat(41))return!1}else do if(this.name()===null)return!1;while(this.eat(46));return this.eat(59),!0}from(e){if(!this.eatWord("from"))return!1;return e.source=this.string(),e.source!==null&&this.finish()}finish(){let e=this.isWord("with")?4:0;if(this.isWord("assert")&&!x(this.source,this.end,this.pos))e=6;if(e!==0&&this.peek(e)===123){this.advance(e),this.eat(123);while(!this.eat(125))if(this.string()===null&&this.name()===null&&!this.eat(58)&&!this.eat(44))return!1}return this.eat(59),!0}skip(){return this.pos=m(this.source,this.pos),this.pos}at(){return this.source.charCodeAt(this.skip())}advance(e){this.pos+=e,this.end=this.pos}peek(e){return this.source.charCodeAt(m(this.source,this.pos+e))}isWordAfter(e,t){return _(this.source,m(this.source,this.pos+e),t)}eat(e){if(this.at()!==e)return!1;return this.advance(1),!0}isWord(e){return _(this.source,this.skip(),e)}eatWord(e){if(!this.isWord(e))return!1;return this.advance(e.length),!0}moduleExportName(){return this.name()??this.string()?.value??null}name(){let e=this.source,t=this.skip();if(!r(e.charCodeAt(t))||u(e.charCodeAt(t)))return null;let n="",i=t,s=t;while(s<e.length){let o=e.charCodeAt(s);if(o===92){if(e.charCodeAt(s+1)!==117)return null;let a=this.unicodeEscape(s+2);if(a===null)return null;n+=e.slice(i,s)+a,s=i=this.escapeEnd}else if(r(o))s++;else break}return this.advance(s-t),n+e.slice(i,s)}string(){let e=this.source,t=this.skip(),n=e.charCodeAt(t);if(n!==34&&n!==39)return null;let i="",s=t+1;for(let o=s;o<e.length;){let a=e.charCodeAt(o);if(a===n)return this.advance(o+1-t),{value:i+e.slice(s,o),start:t,end:o+1};if(a===10||a===13)return null;if(a===92){let l=this.escape(o+1);if(l===null)return null;i+=e.slice(s,o)+l,o=s=this.escapeEnd}else o++}return null}escape(e){let t=this.source.charCodeAt(e);switch(this.escapeEnd=e+1,t){case 110:return`
2
+ `;case 114:return"\r";case 116:return"\t";case 98:return"\b";case 118:return"\v";case 102:return"\f";case 48:return u(this.source.charCodeAt(e+1))?null:"\x00";case 120:return this.hexEscape(e+1,2);case 117:return this.unicodeEscape(e+1);case 13:if(this.source.charCodeAt(e+1)===10)this.escapeEnd++;return"";case 10:case 8232:case 8233:return"";default:return u(t)||e>=this.source.length?null:String.fromCharCode(t)}}unicodeEscape(e){if(this.source.charCodeAt(e)!==123)return this.hexEscape(e,4);let t=0,n=e+1;for(;this.source.charCodeAt(n)!==125;n++){let i=f(this.source.charCodeAt(n));if(i===-1)return null;if(t=t*16+i,t>1114111)return null}if(n===e+1)return null;return this.escapeEnd=n+1,String.fromCodePoint(t)}hexEscape(e,t){let n=0;for(let i=e;i<e+t;i++){let s=f(this.source.charCodeAt(i));if(s===-1)return null;n=n*16+s}return this.escapeEnd=e+t,String.fromCharCode(n)}}function c(e){return e!==92&&r(e)&&!u(e)}function f(e){if(u(e))return e-48;let t=e|32;return t>=97&&t<=102?t-87:-1}function x(e,t,n){for(let i=t;i<n;i++){let s=e.charCodeAt(i);if(s===10||s===13||s===8232||s===8233)return!0}return!1}export{H as lexComponent,g as lexImportsExports};