svelte-ast 0.0.0 → 0.12.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.
- package/LICENSE +47 -1
- package/README.md +560 -2
- package/a11y.d.ts +1636 -0
- package/a11y.js +36 -0
- package/core.d.ts +1779 -0
- package/core.js +1 -0
- package/entities.d.ts +10 -0
- package/entities.js +1 -0
- package/index.d.ts +2844 -0
- package/index.js +1 -0
- package/is-valid-type.js +1 -0
- package/lexer.d.ts +224 -0
- package/lexer.js +3 -0
- package/module.d.ts +1243 -0
- package/module.js +1 -0
- package/package.json +72 -3
- package/parse-imports-exports.js +1 -0
- package/parse-module.js +1 -0
- package/parse-sections.js +1 -0
- package/parse.js +1 -0
- package/runes.js +1 -0
- package/scope.d.ts +1831 -0
- package/scope.js +1 -0
- package/shared-2yyqqbp5.js +2 -0
- package/shared-3654hw2t.js +2 -0
- package/shared-45k6qp45.js +2 -0
- package/shared-5tpb3h6x.js +2 -0
- package/shared-8598bnrd.js +2 -0
- package/shared-8gdz1w3m.js +2 -0
- package/shared-97v1a9gx.js +2 -0
- package/shared-a23q4q40.js +3 -0
- package/shared-bzqbnvee.js +2 -0
- package/shared-csw62q5w.js +2 -0
- package/shared-dgdrqgdn.js +2 -0
- package/shared-drb44hb9.js +2 -0
- package/shared-e4gbs4rb.js +3 -0
- package/shared-hdyvph21.js +2 -0
- package/shared-j53a13yb.js +3 -0
- package/shared-jre53ftf.js +2 -0
- package/shared-m0dcdh24.js +5 -0
- package/shared-m3dr31c3.js +2 -0
- package/shared-np2e2ssk.js +2 -0
- package/shared-s46wfvhw.js +10 -0
- package/shared-tqkk8zh6.js +2 -0
- package/shared-w9n1rzqm.js +10 -0
- package/typescript.d.ts +10 -0
- package/typescript.js +1 -0
- package/walk.d.ts +2210 -0
- package/walk.js +1 -0
- package/warnings.d.ts +1680 -0
- package/warnings.js +6 -0
- package/index.ts +0 -1
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isValidType}from"./is-valid-type.js";import{lexComponent,lexDynamicImports,lexImportsExports,lexStrings}from"./lexer.js";import{parse}from"./parse.js";import{parseImportsExports}from"./parse-imports-exports.js";import{ParseError,parseModule}from"./parse-module.js";import{parseSections}from"./parse-sections.js";import{isRunesMode}from"./runes.js";import{analyzeScopes}from"./scope.js";import{children,createLocator,extractIdentifiers,forEachChild,isReference,locate,markupVisitorKeys,SKIP,STOP,skipTypeWrappers,valueVisitorKeys,visitorKeys,walk}from"./walk.js";export{ParseError,SKIP,STOP,analyzeScopes,children,createLocator,extractIdentifiers,forEachChild,isReference,isRunesMode,isValidType,lexComponent,lexDynamicImports,lexImportsExports,lexStrings,locate,markupVisitorKeys,parse,parseImportsExports,parseModule,parseSections,skipTypeWrappers,valueVisitorKeys,visitorKeys,walk};
|
package/is-valid-type.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Parser}from"./shared-s46wfvhw.js";import{tsPlugin}from"./shared-csw62q5w.js";var RESERVED_WORDS=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(" ")),STANDALONE_WORDS=new Set("any bigint boolean false never null number object string symbol true undefined unknown void".split(" ")),MODIFIER_KEYS=new Set("get set new readonly static public private protected".split(" "));function isWordStart(code){return code>=97&&code<=122||code>=65&&code<=90||code===95||code===36}function isDigit(code){return code>=48&&code<=57}function isWordPart(code){return isWordStart(code)||isDigit(code)}class CommonTypeReader{text;pos=0;constructor(text){this.text=text}readAll(){if(!this.type())return!1;return this.peek(),this.pos===this.text.length}peek(){let text=this.text,pos=this.pos,code=text.charCodeAt(pos);while(code===32||code===9)code=text.charCodeAt(++pos);return this.pos=pos,code}eat(char){if(this.peek()!==char.charCodeAt(0))return!1;return this.pos++,!0}word(){let start=this.pos;return this.skipWord()?this.text.slice(start,this.pos):void 0}skipWord(){if(!isWordStart(this.peek()))return!1;let end=this.pos+1;while(isWordPart(this.text.charCodeAt(end)))end++;return this.pos=end,!0}keyword(word){this.peek();let end=this.pos+word.length;if(!this.text.startsWith(word,this.pos)||isWordPart(this.text.charCodeAt(end)))return!1;return this.pos=end,!0}name(){let word=this.word();return word!==void 0&&!RESERVED_WORDS.has(word)}string(){let quote=this.peek();if(quote!==34&"e!==39)return!1;let text=this.text;for(let i=this.pos+1;i<text.length;i++){let code=text.charCodeAt(i);if(code===quote)return this.pos=i+1,!0;if(code===92||code===10||code===13||code===8232||code===8233)return!1}return!1}number(){let text=this.text,i=this.pos;if(text.charCodeAt(i)===45)i++;let digits=i;while(isDigit(text.charCodeAt(i)))i++;if(i===digits||i-digits>1&&text.charCodeAt(digits)===48)return!1;if(text.charCodeAt(i)===46){let fraction=++i;while(isDigit(text.charCodeAt(i)))i++;if(i===fraction)return!1}let next=text.charCodeAt(i);if(isWordPart(next)||next===46)return!1;return this.pos=i,!0}type(){if(this.peek()===40){let start=this.pos;if(this.functionType())return!0;this.pos=start}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 code=this.peek();if(code===40)return this.pos++,this.type()&&this.eat(")");if(code===123)return this.objectType();if(code===91)return this.tupleType();if(code===34||code===39)return this.string();if(code===45||isDigit(code))return this.number();let word=this.word();if(word===void 0)return!1;if(word==="typeof")return this.typeQuery();if(word==="import")return this.importType(!0);if(STANDALONE_WORDS.has(word)){let next=this.peek();return next!==46&&next!==60}if(RESERVED_WORDS.has(word))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(allowTypeArguments){if(!this.eat("(")||!this.string()||!this.eat(")"))return!1;while(this.eat("."))if(!this.name())return!1;return!allowTypeArguments||!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 code=this.peek();if(this.eat("[")){let next=this.peek();if(next===34||next===39){if(!this.string()||!this.eat("]"))return!1}else return this.name()&&this.eat(":")&&this.type()&&this.eat("]")&&this.eat(":")&&this.type()}else if(code===34||code===39){if(!this.string())return!1}else{let key=this.word();if(key===void 0||MODIFIER_KEYS.has(key))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 names=new Set;for(;;){this.peek();let rest=this.text.startsWith("...",this.pos);if(rest)this.pos+=3;let name=this.word();if(name===void 0||RESERVED_WORDS.has(name)||names.has(name))return!1;if(names.add(name),!rest)this.eat("?");if(!this.eat(":")||!this.type())return!1;if(rest||!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 isCommonType(text){return new CommonTypeReader(text).readAll()}var TypeParser=Parser.extend(tsPlugin);function isValidType(text,options={}){return isCommonType(text)||parsesAsType(text,options.inline??!1)}function parsesAsType(text,inline){let lineCommentAtEnd=!1,parser=new TypeParser(inline?{ecmaVersion:16,sourceType:"module",onComment:(block,_text,_start,end)=>{if(!block&&end===text.length)lineCommentAtEnd=!0}}:{ecmaVersion:16,sourceType:"module"},text);try{return parser.parseWholeType(),!lineCommentAtEnd}catch(error){if(error instanceof SyntaxError)return!1;throw error}}export{isValidType,parsesAsType};
|
package/lexer.d.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/** A module specifier: the string after `from`, or after `import` in `import "a"`. */
|
|
2
|
+
export interface LexedSource {
|
|
3
|
+
/** The string's value, with escapes decoded. */
|
|
4
|
+
value: string;
|
|
5
|
+
/** The offset of the opening quote. */
|
|
6
|
+
start: number;
|
|
7
|
+
/** The offset after the closing quote. */
|
|
8
|
+
end: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** A name an `import` binds. */
|
|
12
|
+
export interface LexedImportSpecifier {
|
|
13
|
+
/** `"default"` for `a` in `import a from`, `"namespace"` for `* as a`, `"named"` for a name in braces. */
|
|
14
|
+
kind: "default" | "namespace" | "named";
|
|
15
|
+
/** The name in the imported module: `"default"`, `"*"` for a namespace, or the name before `as`, with a string's value for `"a-b" as c`. */
|
|
16
|
+
imported: string;
|
|
17
|
+
/** The name it's bound to in this module. */
|
|
18
|
+
local: string;
|
|
19
|
+
/** Whether it's only a type: in `import type`, or with `type` before it. */
|
|
20
|
+
typeOnly: boolean;
|
|
21
|
+
/** The offset of the name, of `*`, or of `type` before the name. */
|
|
22
|
+
start: number;
|
|
23
|
+
/** The offset after the local name. */
|
|
24
|
+
end: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A name an `export … from` re-exports, or, with `exportLists`, one an `export { … }` without `from` exports. */
|
|
28
|
+
export interface LexedExportSpecifier {
|
|
29
|
+
/** `"all"` for `export *`, `"namespace"` for `export * as a`, `"named"` for a name in braces, `"local"` for a name in an `export { … }` without `from`. */
|
|
30
|
+
kind: "all" | "namespace" | "named" | "local";
|
|
31
|
+
/** The name in the other module: `"*"` for `all` and `namespace`, or the name before `as`, with a string's value for `"a-b" as c`; for `local`, the binding in this module. */
|
|
32
|
+
local: string;
|
|
33
|
+
/** The name it's exported as, or `null` for `export *`. */
|
|
34
|
+
exported: string | null;
|
|
35
|
+
/** Whether it's only a type: in `export type`, or with `type` before it. */
|
|
36
|
+
typeOnly: boolean;
|
|
37
|
+
/** The offset of the name, of `*`, or of `type` before the name. */
|
|
38
|
+
start: number;
|
|
39
|
+
/** The offset after the exported name, or after `*` for `export *`. */
|
|
40
|
+
end: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface LexedStatementBase {
|
|
44
|
+
/** The offset of `import` or `export`. */
|
|
45
|
+
start: number;
|
|
46
|
+
/** The offset after the statement's `;`, or its last token without one. */
|
|
47
|
+
end: number;
|
|
48
|
+
/** The module it imports from, or `null` for an `export { … }` without `from`, or a statement the lexer doesn't recognize. */
|
|
49
|
+
source: LexedSource | null;
|
|
50
|
+
/** Whether it's `import type` or `export type`. */
|
|
51
|
+
typeOnly: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** An `import` statement. */
|
|
55
|
+
export interface LexedImport extends LexedStatementBase {
|
|
56
|
+
kind: "import";
|
|
57
|
+
specifiers: LexedImportSpecifier[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** An `export … from` statement, or, with `exportLists`, an `export { … }` without `from`. */
|
|
61
|
+
export interface LexedExport extends LexedStatementBase {
|
|
62
|
+
kind: "export";
|
|
63
|
+
specifiers: LexedExportSpecifier[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type LexedStatement = LexedImport | LexedExport;
|
|
67
|
+
|
|
68
|
+
/** Options for {@link lexImportsExports}. */
|
|
69
|
+
export interface LexImportsExportsOptions {
|
|
70
|
+
/** Also return the module's `export { a, b as c }` statements without `from`, whose specifiers have kind `"local"`. Default `false`. */
|
|
71
|
+
exportLists?: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** An `import(…)`, a `require(…)` or Vite's `import.meta.glob(…)`. */
|
|
75
|
+
export interface LexedDynamicImport {
|
|
76
|
+
/** `"import"` for `import(…)`, `"require"` for `require(…)`, `"glob"` for `import.meta.glob(…)`. */
|
|
77
|
+
kind: "import" | "require" | "glob";
|
|
78
|
+
/** The first argument, if it's a string or a template without `${…}` followed by `)` or `,`; `null` for a computed one. A template's `start` and `end` include its backticks. */
|
|
79
|
+
source: LexedSource | null;
|
|
80
|
+
/** The offset of `import` or `require`. */
|
|
81
|
+
start: number;
|
|
82
|
+
/** The offset after the closing `)`, or the end of the module without one. */
|
|
83
|
+
end: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A string literal, or the text of a template literal between two of its delimiters. */
|
|
87
|
+
export interface LexedString {
|
|
88
|
+
/** `"string"` for a string literal, where `parseModule` has a `Literal`; `"template"` for a template's text, where it has a `TemplateElement`. */
|
|
89
|
+
kind: "string" | "template";
|
|
90
|
+
/** The value with escapes decoded, as a `Literal`'s `value` or a `TemplateElement`'s `value.cooked`: a template's line breaks read as `\n`. `null` for an escape that acorn rejects in a module, or that leaves a tagged template's `cooked` `null`. */
|
|
91
|
+
value: string | null;
|
|
92
|
+
/** The offset of the opening quote, or of a template's text, after its backtick or `}`. */
|
|
93
|
+
start: number;
|
|
94
|
+
/** The offset after the closing quote, or of the backtick or `${` after a template's text. */
|
|
95
|
+
end: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A module's top-level `import` statements and `export … from`
|
|
100
|
+
* re-exports, read without a parser: each statement's offsets, its
|
|
101
|
+
* source and the names it imports or re-exports. It finds statements as
|
|
102
|
+
* {@link parseImportsExports} does with `localExports: false`, and on
|
|
103
|
+
* every statement that accepts, gives the same offsets, sources and names,
|
|
104
|
+
* but it doesn't check syntax and never throws. A statement it can't read,
|
|
105
|
+
* including TypeScript's `import a = require("a")`, is returned with
|
|
106
|
+
* `source: null` and no specifiers, ending where it stopped reading.
|
|
107
|
+
* TypeScript's `type` forms are always read.
|
|
108
|
+
*/
|
|
109
|
+
export declare function lexImportsExports(source: string, options?: LexImportsExportsOptions): LexedStatement[];
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A module's `import(…)` and `require(…)` calls and Vite's
|
|
113
|
+
* `import.meta.glob(…)`, at any depth, in source order, read without a
|
|
114
|
+
* parser: each call's offsets and its first argument, if it's a string or
|
|
115
|
+
* a template without `${…}`. Comments, strings, templates and regular
|
|
116
|
+
* expressions are skipped, as {@link lexStrings} skips them, and it never
|
|
117
|
+
* throws. Where {@link parseModule} accepts the module, they are its
|
|
118
|
+
* `ImportExpression`s, its calls of an identifier `require` and of
|
|
119
|
+
* `import.meta.glob`, and, in TypeScript, its `import("a")` types and
|
|
120
|
+
* `import a = require("a")`. A method named `import` or `require` isn't a
|
|
121
|
+
* call.
|
|
122
|
+
*/
|
|
123
|
+
export declare function lexDynamicImports(source: string): LexedDynamicImport[];
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A module's string literals and the text of its template literals, in
|
|
127
|
+
* source order, read without a parser: the offsets and value of each
|
|
128
|
+
* `Literal` with a string value and each `TemplateElement` that
|
|
129
|
+
* {@link parseModule} returns, for a tool that only needs a script's
|
|
130
|
+
* strings, such as one that looks for class names. Comments and regular
|
|
131
|
+
* expressions are skipped, as `parseModule` skips them. It tells a regular
|
|
132
|
+
* expression from a division by the token before it, as acorn's tokenizer
|
|
133
|
+
* does, and doesn't check syntax, so it never throws. A template's raw
|
|
134
|
+
* text is `source.slice(start, end)`, with `\r\n` and `\r` as `\n`.
|
|
135
|
+
*/
|
|
136
|
+
export declare function lexStrings(source: string): LexedString[];
|
|
137
|
+
|
|
138
|
+
/** An attribute of a top-level `<script>`, `<style>` or `<svelte:options>`. */
|
|
139
|
+
export interface LexedAttribute {
|
|
140
|
+
name: string;
|
|
141
|
+
/**
|
|
142
|
+
* The value as written, without its quotes: character references
|
|
143
|
+
* aren't decoded, and an expression keeps its braces, as in `"{true}"`
|
|
144
|
+
* for `runes={true}`. `true` for an attribute without a value.
|
|
145
|
+
*/
|
|
146
|
+
value: string | true;
|
|
147
|
+
/** The offset of the name. */
|
|
148
|
+
start: number;
|
|
149
|
+
/** The offset after the value, or after the name without one. */
|
|
150
|
+
end: number;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The text between a top-level element's tags. */
|
|
154
|
+
export interface LexedContent {
|
|
155
|
+
start: number;
|
|
156
|
+
end: number;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** A top-level `<script>`. */
|
|
160
|
+
export interface LexedScript {
|
|
161
|
+
/** The offset of `<script`. */
|
|
162
|
+
start: number;
|
|
163
|
+
/** The offset after `</script>`. */
|
|
164
|
+
end: number;
|
|
165
|
+
/** `"module"` for `<script module>` or `<script context="module">`. */
|
|
166
|
+
context: "default" | "module";
|
|
167
|
+
attributes: LexedAttribute[];
|
|
168
|
+
content: LexedContent;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The top-level `<style>`. */
|
|
172
|
+
export interface LexedStyle {
|
|
173
|
+
/** The offset of `<style`. */
|
|
174
|
+
start: number;
|
|
175
|
+
/** The offset after `</style>`. */
|
|
176
|
+
end: number;
|
|
177
|
+
attributes: LexedAttribute[];
|
|
178
|
+
content: LexedContent;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** `<svelte:options>`. */
|
|
182
|
+
export interface LexedOptions {
|
|
183
|
+
/** The offset of `<svelte:options`. */
|
|
184
|
+
start: number;
|
|
185
|
+
/** The offset after `/>` or `</svelte:options>`. */
|
|
186
|
+
end: number;
|
|
187
|
+
attributes: LexedAttribute[];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** A component's top-level sections, as {@link lexComponent} finds them. */
|
|
191
|
+
export interface LexedComponent {
|
|
192
|
+
/**
|
|
193
|
+
* Whether `parse` reads the component as TypeScript, which needs the
|
|
194
|
+
* TypeScript plugin: svelte decides it from the first `<script>` with a
|
|
195
|
+
* `lang`, so this is `true` when that one has `lang="ts"`.
|
|
196
|
+
*/
|
|
197
|
+
typescript: boolean;
|
|
198
|
+
/** The `<script>` without `module`, as `parse`'s `instance`. */
|
|
199
|
+
instance: LexedScript | null;
|
|
200
|
+
/** The `<script module>`, as `parse`'s `module`. */
|
|
201
|
+
module: LexedScript | null;
|
|
202
|
+
/** The `<style>`, as `parse`'s `css`. */
|
|
203
|
+
css: LexedStyle | null;
|
|
204
|
+
/** `<svelte:options>`, as `parse`'s `options`. */
|
|
205
|
+
options: LexedOptions | null;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* A component's top-level `<script>`, `<script module>`, `<style>` and
|
|
210
|
+
* `<svelte:options>`, with their attributes and the offsets of their tags
|
|
211
|
+
* and content, read without a parser: e.g. to find a component's
|
|
212
|
+
* language or imports, or to rewrite its scripts, without parsing the
|
|
213
|
+
* markup. The offsets are `parse`'s, so they're into the source without
|
|
214
|
+
* a leading byte order mark.
|
|
215
|
+
*
|
|
216
|
+
* The markup is skipped by tracking only tags, attribute values, blocks
|
|
217
|
+
* and the brackets, strings, comments and templates of each `{…}`
|
|
218
|
+
* expression, so a `<script>` inside an element, a block, a string or a
|
|
219
|
+
* comment isn't one. It stops after the last `<script`, `<style` or
|
|
220
|
+
* `<svelte:options`. Where `parse` accepts the component, the sections
|
|
221
|
+
* are the same as its `instance`, `module`, `css` and `options`; it
|
|
222
|
+
* doesn't check syntax and never throws.
|
|
223
|
+
*/
|
|
224
|
+
export declare function lexComponent(input: string): LexedComponent;
|
package/lexer.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import{isDigit}from"./shared-5tpb3h6x.js";import{isWordCode,scan,scanLiterals,scanCalls,closingBracket,lineEnd,skipTrivia,isWordAt}from"./shared-e4gbs4rb.js";import"./shared-dgdrqgdn.js";import{lexComponent2}from"./shared-a23q4q40.js";function lexImportsExports(source,options={}){let lexer=new Lexer(source);lexer.exportLists=options.exportLists??!1;let statements=[];return scan(source,!1,(_start,keyword)=>{let statement=lexer.statement(keyword);return statements.push(statement),statement.end},lexer.exportLists),statements}function lexDynamicImports(source){let lexer=new Lexer(source),imports=[];return scanCalls(source,(start,member)=>{let call=lexer.dynamicImport(start,member);if(call!==null)imports.push(call)}),imports}function lexStrings(source){let lexer=new Lexer(source),strings=[];return scanLiterals(source,(start,end,template)=>{let quote=template?0:1;strings.push({kind:template?"template":"string",value:lexer.cooked(start+quote,end-quote,template),start,end})}),strings}class Lexer{source;pos=0;end=0;escapeEnd=0;exportLists=!1;constructor(source){this.source=source}statement(keyword){this.pos=this.end=keyword+6;let statement={kind:this.source.charCodeAt(keyword)===105?"import":"export",start:keyword,end:0,source:null,typeOnly:!1,specifiers:[]};if(!(statement.kind==="import"?this.import(statement):this.export(statement)))statement.source=null,statement.specifiers=[];return statement.end=this.end,statement}import(statement){let module=this.string();if(module!==null)return statement.source=module,this.finish();if(this.isWord("type")){let next=this.peekAfter(4);if(next===123||next===42||isIdentifierStart(next)&&!this.isWordAfter(4,"from"))statement.typeOnly=!0,this.advance(4)}let{specifiers,typeOnly}=statement,next=this.at();if(next!==123&&next!==42){let start=this.pos,local=this.name();if(local===null)return!1;if(this.at()===61)return this.importEquals();if(specifiers.push({kind:"default",imported:"default",local,typeOnly,start,end:this.end}),!this.eat(44))return this.from(statement);next=this.at()}if(next===42){let start=this.pos;if(this.advance(1),!this.eatWord("as"))return!1;let local=this.name();if(local===null)return!1;return specifiers.push({kind:"namespace",imported:"*",local,typeOnly,start,end:this.end}),this.from(statement)}if(!this.eat(123))return!1;while(!this.eat(125)){let start=this.skip(),named=this.typeModifier()||typeOnly,name=this.name(),imported=name??this.string()?.value;if(imported===void 0)return!1;let local=name;if(this.eatWord("as"))local=this.name();if(local===null)return!1;if(specifiers.push({kind:"named",imported,local,typeOnly:named,start,end:this.end}),!this.eat(44)&&this.at()!==125)return!1}return this.from(statement)}export(statement){if(this.eatWord("import")){if(this.isWord("type")&&isIdentifierStart(this.peekAfter(4))&&!this.isWordAfter(4,"from"))statement.typeOnly=!0,this.advance(4);return this.name()!==null&&this.at()===61&&this.importEquals()}if(this.isWord("type")){let next=this.peekAfter(4);if(next===123||next===42)statement.typeOnly=!0,this.advance(4)}let{specifiers,typeOnly}=statement;if(this.at()===42){let start=this.pos;this.advance(1);let exported=null;if(this.eatWord("as")){if(exported=this.moduleExportName(),exported===null)return!1}return specifiers.push({kind:exported===null?"all":"namespace",local:"*",exported,typeOnly,start,end:this.end}),this.from(statement)}if(!this.eat(123))return!1;while(!this.eat(125)){let start=this.skip(),named=this.typeModifier()||typeOnly,local=this.moduleExportName();if(local===null)return!1;let exported=local;if(this.eatWord("as"))exported=this.moduleExportName();if(exported===null)return!1;if(specifiers.push({kind:"named",local,exported,typeOnly:named,start,end:this.end}),!this.eat(44)&&this.at()!==125)return!1}if(this.exportLists&&!this.isWord("from")){for(let specifier of specifiers)specifier.kind="local";return this.eat(59),!0}return this.from(statement)}dynamicImport(start,member){let isImport=this.source.charCodeAt(start)===105;this.pos=this.end=start+(isImport?6:7);let kind=isImport?"import":"require";if(isImport&&this.eat(46)){if(!this.eatWord("meta")||!this.eat(46)||!this.eatWord("glob")||!this.typeArguments())return null;kind="glob"}if(!this.eat(40))return null;let argument=this.skip(),source=this.string()??this.template(),close=this.at()===41?this.pos:-1;if(close===-1){if(this.at()!==44)source=null;close=closingBracket(this.source,argument)}if(member&&kind!=="glob"){let after=this.source.charCodeAt(skipTrivia(this.source,close+1));if(after===123||after===58)return null}return{kind,source,start,end:Math.min(close+1,this.source.length)}}typeArguments(){if(this.at()!==60)return!0;let source=this.source,depth=0;for(let i=this.pos;i<source.length;i=skipTrivia(source,i+1)){let code=source.charCodeAt(i);if(code===34||code===39||code===96){let end=source.indexOf(source[i],i+1);if(end===-1)return!1;i=end}else if(code===60)depth++;else if(code===62&&source.charCodeAt(i-1)!==61){if(--depth===0)return this.advance(i+1-this.pos),!0}}return!1}template(){let source=this.source,start=this.skip();if(source.charCodeAt(start)!==96)return null;for(let i=start+1;i<source.length;i++){let code=source.charCodeAt(i);if(code===96){let value=this.cooked(start+1,i,!0);if(value===null)return null;return this.advance(i+1-start),{value,start,end:i+1}}if(code===92)i++;else if(code===36&&source.charCodeAt(i+1)===123)return null}return null}typeModifier(){if(!this.isWord("type"))return!1;let next=this.peekAfter(4);if(next===34||next===39||isIdentifierStart(next)&&!this.isWordAfter(4,"as"))return this.advance(4),!0;return!1}importEquals(){if(this.advance(1),this.isWord("require")&&this.peekAfter(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(statement){if(!this.eatWord("from"))return!1;return statement.source=this.string(),statement.source!==null&&this.finish()}finish(){let keyword=this.isWord("with")?4:0;if(this.isWord("assert")&&lineEnd(this.source,this.end)>=this.pos)keyword=6;if(keyword!==0&&this.peekAfter(keyword)===123){this.advance(keyword),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=skipTrivia(this.source,this.pos),this.pos}at(){return this.source.charCodeAt(this.skip())}advance(length){this.pos+=length,this.end=this.pos}peekAfter(length){return this.source.charCodeAt(skipTrivia(this.source,this.pos+length))}isWordAfter(length,word){return isWordAt(this.source,skipTrivia(this.source,this.pos+length),word)}eat(code){if(this.at()!==code)return!1;return this.advance(1),!0}isWord(word){return isWordAt(this.source,this.skip(),word)}eatWord(word){if(!this.isWord(word))return!1;return this.advance(word.length),!0}moduleExportName(){return this.name()??this.string()?.value??null}name(){let source=this.source,start=this.skip();if(!isWordCode(source.charCodeAt(start))||isDigit(source.charCodeAt(start)))return null;let value="",chunk=start,i=start;while(i<source.length){let code=source.charCodeAt(i);if(code===92){if(source.charCodeAt(i+1)!==117)return null;let escaped=this.unicodeEscape(i+2);if(escaped===null)return null;value+=source.slice(chunk,i)+escaped,i=chunk=this.escapeEnd}else if(isWordCode(code))i++;else break}return this.advance(i-start),value+source.slice(chunk,i)}string(){let source=this.source,start=this.skip(),quote=source.charCodeAt(start);if(quote!==34&"e!==39)return null;let value="",chunk=start+1;for(let i=chunk;i<source.length;){let code=source.charCodeAt(i);if(code===quote)return this.advance(i+1-start),{value:value+source.slice(chunk,i),start,end:i+1};if(code===10||code===13)return null;if(code===92){let escaped=this.escape(i+1);if(escaped===null)return null;value+=source.slice(chunk,i)+escaped,i=chunk=this.escapeEnd}else i++}return null}cooked(from,to,template){let source=this.source,value="",chunk=from;for(let i=from;i<to;){let code=source.charCodeAt(i);if(code===92){let escaped=this.escape(i+1);if(escaped===null)return null;value+=source.slice(chunk,i)+escaped,i=chunk=this.escapeEnd}else if(code===13&&template)value+=`${source.slice(chunk,i)}
|
|
2
|
+
`,i+=source.charCodeAt(i+1)===10?2:1,chunk=i;else i++}return value+source.slice(chunk,to)}escape(pos){let code=this.source.charCodeAt(pos);switch(this.escapeEnd=pos+1,code){case 110:return`
|
|
3
|
+
`;case 114:return"\r";case 116:return"\t";case 98:return"\b";case 118:return"\v";case 102:return"\f";case 48:return isDigit(this.source.charCodeAt(pos+1))?null:"\x00";case 120:return this.hexEscape(pos+1,2);case 117:return this.unicodeEscape(pos+1);case 13:if(this.source.charCodeAt(pos+1)===10)this.escapeEnd++;return"";case 10:case 8232:case 8233:return"";default:return isDigit(code)||pos>=this.source.length?null:String.fromCharCode(code)}}unicodeEscape(pos){if(this.source.charCodeAt(pos)!==123)return this.hexEscape(pos,4);let value=0,i=pos+1;for(;this.source.charCodeAt(i)!==125;i++){let digit=hexValue(this.source.charCodeAt(i));if(digit===-1)return null;if(value=value*16+digit,value>1114111)return null}if(i===pos+1)return null;return this.escapeEnd=i+1,String.fromCodePoint(value)}hexEscape(pos,length){let value=0;for(let i=pos;i<pos+length;i++){let digit=hexValue(this.source.charCodeAt(i));if(digit===-1)return null;value=value*16+digit}return this.escapeEnd=pos+length,String.fromCharCode(value)}}function isIdentifierStart(code){return code!==92&&isWordCode(code)&&!isDigit(code)}function hexValue(code){if(isDigit(code))return code-48;let lower=code|32;return lower>=97&&lower<=102?lower-87:-1}export{lexComponent2 as lexComponent,lexDynamicImports,lexImportsExports,lexStrings};
|