astro-eslint-parser 0.5.1 → 0.6.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 (58) hide show
  1. package/lib/index.d.ts +345 -9
  2. package/lib/index.js +1745 -42
  3. package/lib/index.mjs +1719 -0
  4. package/package.json +9 -7
  5. package/lib/ast/astro.d.ts +0 -49
  6. package/lib/ast/astro.js +0 -2
  7. package/lib/ast/base.d.ts +0 -6
  8. package/lib/ast/base.js +0 -2
  9. package/lib/ast/index.d.ts +0 -8
  10. package/lib/ast/index.js +0 -18
  11. package/lib/ast/jsx.d.ts +0 -91
  12. package/lib/ast/jsx.js +0 -2
  13. package/lib/astro/index.d.ts +0 -56
  14. package/lib/astro/index.js +0 -357
  15. package/lib/astro-tools/index.d.ts +0 -21
  16. package/lib/astro-tools/index.js +0 -26
  17. package/lib/context/index.d.ts +0 -55
  18. package/lib/context/index.js +0 -158
  19. package/lib/context/parser-options.d.ts +0 -8
  20. package/lib/context/parser-options.js +0 -77
  21. package/lib/context/resolve-parser/espree.d.ts +0 -6
  22. package/lib/context/resolve-parser/espree.js +0 -41
  23. package/lib/context/resolve-parser/index.d.ts +0 -7
  24. package/lib/context/resolve-parser/index.js +0 -34
  25. package/lib/context/resolve-parser/parser-object.d.ts +0 -33
  26. package/lib/context/resolve-parser/parser-object.js +0 -27
  27. package/lib/context/script.d.ts +0 -34
  28. package/lib/context/script.js +0 -178
  29. package/lib/debug.d.ts +0 -2
  30. package/lib/debug.js +0 -8
  31. package/lib/errors.d.ts +0 -14
  32. package/lib/errors.js +0 -20
  33. package/lib/parser/astro-parser/astrojs-compiler-service.d.ts +0 -5
  34. package/lib/parser/astro-parser/astrojs-compiler-service.js +0 -12
  35. package/lib/parser/astro-parser/astrojs-compiler-worker.d.ts +0 -1
  36. package/lib/parser/astro-parser/astrojs-compiler-worker.js +0 -11
  37. package/lib/parser/astro-parser/parse.d.ts +0 -6
  38. package/lib/parser/astro-parser/parse.js +0 -270
  39. package/lib/parser/index.d.ts +0 -21
  40. package/lib/parser/index.js +0 -72
  41. package/lib/parser/lru-cache.d.ts +0 -7
  42. package/lib/parser/lru-cache.js +0 -32
  43. package/lib/parser/process-template.d.ts +0 -7
  44. package/lib/parser/process-template.js +0 -390
  45. package/lib/parser/script.d.ts +0 -7
  46. package/lib/parser/script.js +0 -44
  47. package/lib/parser/sort.d.ts +0 -6
  48. package/lib/parser/sort.js +0 -15
  49. package/lib/parser/template.d.ts +0 -10
  50. package/lib/parser/template.js +0 -102
  51. package/lib/parser/ts-patch.d.ts +0 -8
  52. package/lib/parser/ts-patch.js +0 -65
  53. package/lib/traverse.d.ts +0 -27
  54. package/lib/traverse.js +0 -93
  55. package/lib/types.d.ts +0 -17
  56. package/lib/types.js +0 -2
  57. package/lib/visitor-keys.d.ts +0 -2
  58. package/lib/visitor-keys.js +0 -14
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "astro-eslint-parser",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "Astro component parser for ESLint",
5
5
  "main": "lib/index.js",
6
+ "module": "lib/index.mjs",
6
7
  "files": [
7
8
  "lib"
8
9
  ],
@@ -11,7 +12,7 @@
11
12
  },
12
13
  "scripts": {
13
14
  "prebuild": "npm run -s clean",
14
- "build": "tsc --project ./tsconfig.build.json",
15
+ "build": "tsup",
15
16
  "clean": "rimraf .nyc_output lib coverage",
16
17
  "lint": "eslint . --ext .js,.ts,.json,.astro,.svelte",
17
18
  "eslint-fix": "npm run lint -- --fix",
@@ -46,13 +47,13 @@
46
47
  "dependencies": {
47
48
  "@astrojs/compiler": "0.18.0 - 0.23.0 || ^0.23.0",
48
49
  "@typescript-eslint/types": "^5.25.0",
50
+ "astrojs-compiler-sync": "^0.2.0",
49
51
  "debug": "^4.3.4",
50
52
  "eslint-visitor-keys": "^3.0.0",
51
- "espree": "^9.0.0",
52
- "synckit": "^0.8.0"
53
+ "espree": "^9.0.0"
53
54
  },
54
55
  "devDependencies": {
55
- "@ota-meshi/eslint-plugin": "^0.11.0",
56
+ "@ota-meshi/eslint-plugin": "^0.12.0",
56
57
  "@types/benchmark": "^2.1.1",
57
58
  "@types/chai": "^4.3.0",
58
59
  "@types/debug": "^4.1.7",
@@ -84,7 +85,7 @@
84
85
  "eslint-plugin-prettier": "^4.0.0",
85
86
  "eslint-plugin-react": "^7.29.4",
86
87
  "eslint-plugin-regexp": "^1.5.0",
87
- "eslint-plugin-simple-import-sort": "^7.0.0",
88
+ "eslint-plugin-simple-import-sort": "^8.0.0",
88
89
  "eslint-plugin-svelte": "^2.0.0",
89
90
  "estree-walker": "^3.0.0",
90
91
  "locate-character": "^2.0.5",
@@ -98,6 +99,7 @@
98
99
  "semver": "^7.3.5",
99
100
  "string-replace-loader": "^3.0.3",
100
101
  "svelte": "^3.48.0",
101
- "typescript": "~4.7.0"
102
+ "tsup": "^6.2.3",
103
+ "typescript": "~4.8.0"
102
104
  }
103
105
  }
@@ -1,49 +0,0 @@
1
- import type { JSXAttribute, JSXElement, JSXExpression, JSXExpressionContainer, JSXFragment, JSXText } from "./jsx";
2
- import type { TSESTree as ES } from "@typescript-eslint/types";
3
- import type { BaseNode } from "./base";
4
- export declare type AstroNode = AstroProgram | AstroFragment | AstroHTMLComment | AstroDoctype | AstroShorthandAttribute | AstroTemplateLiteralAttribute | AstroRawText;
5
- export declare type AstroChild = JSXElement | JSXFragment | JSXExpression | JSXText | AstroHTMLComment | AstroRawText;
6
- export declare type AstroParentNode = JSXElement | JSXFragment | AstroFragment;
7
- /** Node of Astro program root */
8
- export interface AstroProgram extends Omit<ES.Program, "type" | "body"> {
9
- type: "Program";
10
- body: (ES.Program["body"][number] | AstroFragment)[];
11
- sourceType: "script" | "module";
12
- comments: ES.Comment[];
13
- tokens: ES.Token[];
14
- parent?: undefined;
15
- }
16
- /** Node of Astro fragment */
17
- export interface AstroFragment extends BaseNode {
18
- type: "AstroFragment";
19
- children: AstroChild[];
20
- parent?: AstroParentNode;
21
- }
22
- /** Node of Astro html comment */
23
- export interface AstroHTMLComment extends BaseNode {
24
- type: "AstroHTMLComment";
25
- value: string;
26
- parent?: AstroParentNode;
27
- }
28
- /** Node of Astro doctype */
29
- export interface AstroDoctype extends BaseNode {
30
- type: "AstroDoctype";
31
- parent?: AstroFragment;
32
- }
33
- /** Node of Astro shorthand attribute */
34
- export interface AstroShorthandAttribute extends Omit<JSXAttribute, "type"> {
35
- type: "AstroShorthandAttribute";
36
- value: JSXExpressionContainer;
37
- }
38
- /** Node of Astro template-literal attribute */
39
- export interface AstroTemplateLiteralAttribute extends Omit<JSXAttribute, "type"> {
40
- type: "AstroTemplateLiteralAttribute";
41
- value: JSXExpressionContainer & {
42
- expression: ES.TemplateLiteral;
43
- };
44
- }
45
- /** Node of Astro raw text */
46
- export interface AstroRawText extends Omit<JSXText, "type"> {
47
- type: "AstroRawText";
48
- parent?: JSXElement;
49
- }
package/lib/ast/astro.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/lib/ast/base.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import type { TSESTree } from "@typescript-eslint/types";
2
- export interface BaseNode {
3
- loc: TSESTree.SourceLocation;
4
- range: TSESTree.Range;
5
- type: string;
6
- }
package/lib/ast/base.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,8 +0,0 @@
1
- export * from "./astro";
2
- export * from "./jsx";
3
- import type { TSESTree } from "@typescript-eslint/types";
4
- export declare type Comment = TSESTree.Comment;
5
- export declare type Token = TSESTree.Token;
6
- export declare type SourceLocation = TSESTree.SourceLocation;
7
- export declare type Range = TSESTree.Range;
8
- export declare type Position = TSESTree.Position;
package/lib/ast/index.js DELETED
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./astro"), exports);
18
- __exportStar(require("./jsx"), exports);
package/lib/ast/jsx.d.ts DELETED
@@ -1,91 +0,0 @@
1
- import type { TSESTree as ES } from "@typescript-eslint/types";
2
- import type { AstroFragment, AstroHTMLComment, AstroRawText, AstroShorthandAttribute, AstroTemplateLiteralAttribute } from "./astro";
3
- import type { BaseNode } from "./base";
4
- export declare type JSXNode = JSXAttribute | JSXClosingElement | JSXClosingFragment | JSXElement | JSXEmptyExpression | JSXExpressionContainer | JSXFragment | JSXIdentifier | JSXMemberExpression | JSXNamespacedName | JSXOpeningElement | JSXOpeningFragment | JSXSpreadAttribute | JSXSpreadChild | JSXText;
5
- export declare type JSXChild = JSXElement | JSXFragment | JSXExpression | JSXText | AstroHTMLComment | AstroRawText;
6
- export declare type JSXParentNode = JSXElement | JSXFragment | AstroFragment;
7
- export interface JSXElement extends BaseNode {
8
- type: "JSXElement";
9
- openingElement: JSXOpeningElement;
10
- closingElement: JSXClosingElement | null;
11
- children: JSXChild[];
12
- parent?: JSXParentNode;
13
- }
14
- export interface JSXFragment extends BaseNode {
15
- type: "JSXFragment";
16
- openingFragment: JSXOpeningFragment;
17
- closingFragment: JSXClosingFragment;
18
- children: JSXChild[];
19
- parent?: JSXParentNode;
20
- }
21
- export interface JSXOpeningElement extends BaseNode {
22
- type: "JSXOpeningElement";
23
- typeParameters?: ES.TSTypeParameterInstantiation;
24
- selfClosing: boolean;
25
- name: JSXTagNameExpression;
26
- attributes: (JSXAttribute | JSXSpreadAttribute | AstroShorthandAttribute | AstroTemplateLiteralAttribute)[];
27
- parent?: JSXElement;
28
- }
29
- export interface JSXClosingElement extends BaseNode {
30
- type: "JSXClosingElement";
31
- name: JSXTagNameExpression;
32
- parent?: JSXElement;
33
- }
34
- export interface JSXClosingFragment extends BaseNode {
35
- type: "JSXClosingFragment";
36
- parent?: JSXFragment;
37
- }
38
- export interface JSXOpeningFragment extends BaseNode {
39
- type: "JSXOpeningFragment";
40
- parent?: JSXFragment;
41
- }
42
- export interface JSXAttribute extends BaseNode {
43
- type: "JSXAttribute";
44
- name: JSXIdentifier | JSXNamespacedName;
45
- value: JSXExpression | ES.Literal | null;
46
- parent?: JSXOpeningElement;
47
- }
48
- export interface JSXSpreadAttribute extends BaseNode {
49
- type: "JSXSpreadAttribute";
50
- argument: ES.Expression;
51
- parent?: JSXOpeningElement;
52
- }
53
- export declare type JSXTagNameExpression = JSXIdentifier | JSXMemberExpression | JSXNamespacedName;
54
- export interface JSXIdentifier extends BaseNode {
55
- type: "JSXIdentifier";
56
- name: string;
57
- parent?: JSXAttribute | AstroShorthandAttribute | AstroTemplateLiteralAttribute | JSXMemberExpression | JSXNamespacedName | JSXOpeningElement | JSXClosingElement;
58
- }
59
- export interface JSXMemberExpression extends BaseNode {
60
- type: "JSXMemberExpression";
61
- object: JSXTagNameExpression;
62
- property: JSXIdentifier;
63
- parent?: JSXMemberExpression | JSXOpeningElement | JSXClosingElement;
64
- }
65
- export interface JSXNamespacedName extends BaseNode {
66
- type: "JSXNamespacedName";
67
- namespace: JSXIdentifier;
68
- name: JSXIdentifier;
69
- parent?: JSXAttribute | AstroShorthandAttribute | AstroTemplateLiteralAttribute | JSXMemberExpression | JSXOpeningElement | JSXClosingElement;
70
- }
71
- export declare type JSXExpression = JSXExpressionContainer | JSXSpreadChild;
72
- export interface JSXExpressionContainer extends BaseNode {
73
- type: "JSXExpressionContainer";
74
- expression: ES.Expression | JSXEmptyExpression;
75
- parent?: JSXAttribute | AstroShorthandAttribute | AstroTemplateLiteralAttribute | JSXParentNode;
76
- }
77
- export interface JSXSpreadChild extends BaseNode {
78
- type: "JSXSpreadChild";
79
- expression: ES.Expression;
80
- parent?: JSXAttribute | JSXParentNode;
81
- }
82
- export interface JSXEmptyExpression extends BaseNode {
83
- type: "JSXEmptyExpression";
84
- parent?: JSXExpressionContainer;
85
- }
86
- export interface JSXText extends BaseNode {
87
- type: "JSXText";
88
- value: string;
89
- raw: string;
90
- parent?: JSXParentNode;
91
- }
package/lib/ast/jsx.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,56 +0,0 @@
1
- import type { AttributeNode, CommentNode, Node, ParentNode, TagLikeNode } from "@astrojs/compiler/types";
2
- import type { Context } from "../context";
3
- /**
4
- * Checks if the given node is TagLikeNode
5
- */
6
- export declare function isTag(node: Node): node is Node & TagLikeNode;
7
- /**
8
- * Checks if the given node is ParentNode
9
- */
10
- export declare function isParent(node: Node): node is ParentNode;
11
- /** walk element nodes */
12
- export declare function walkElements(parent: ParentNode, code: string, enter: (n: Node, parents: ParentNode[]) => void, leave: (n: Node, parents: ParentNode[]) => void, parents?: ParentNode[]): void;
13
- /** walk nodes */
14
- export declare function walk(parent: ParentNode, code: string, enter: (n: Node | AttributeNode, parents: ParentNode[]) => void, leave: (n: Node | AttributeNode, parents: ParentNode[]) => void): void;
15
- /**
16
- * Get end offset of start tag
17
- */
18
- export declare function calcStartTagEndOffset(node: TagLikeNode, ctx: Context): number;
19
- /**
20
- * Get end offset of attribute
21
- */
22
- export declare function calcAttributeEndOffset(node: AttributeNode, ctx: Context): number;
23
- /**
24
- * Get start offset of attribute value
25
- */
26
- export declare function calcAttributeValueStartOffset(node: AttributeNode, ctx: Context): number;
27
- /**
28
- * Get end offset of tag
29
- */
30
- export declare function getEndOffset(node: Node, ctx: Context): number;
31
- /**
32
- * Get content end offset
33
- */
34
- export declare function calcContentEndOffset(parent: ParentNode, ctx: Context): number;
35
- /**
36
- * If the given tag is a self-close tag, get the self-closing tag.
37
- */
38
- export declare function getSelfClosingTag(node: TagLikeNode, ctx: Context): null | {
39
- offset: number;
40
- end: "/>" | ">";
41
- };
42
- /**
43
- * If the given tag has a end tag, get the end tag.
44
- */
45
- export declare function getEndTag(node: TagLikeNode, ctx: Context): null | {
46
- offset: number;
47
- tag: string;
48
- };
49
- /**
50
- * Get end offset of comment
51
- */
52
- export declare function calcCommentEndOffset(node: CommentNode, ctx: Context): number;
53
- /**
54
- * Skip spaces
55
- */
56
- export declare function skipSpaces(string: string, position: number): number;
@@ -1,357 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.skipSpaces = exports.calcCommentEndOffset = exports.getEndTag = exports.getSelfClosingTag = exports.calcContentEndOffset = exports.getEndOffset = exports.calcAttributeValueStartOffset = exports.calcAttributeEndOffset = exports.calcStartTagEndOffset = exports.walk = exports.walkElements = exports.isParent = exports.isTag = void 0;
4
- const errors_1 = require("../errors");
5
- /**
6
- * Checks if the given node is TagLikeNode
7
- */
8
- function isTag(node) {
9
- return (node.type === "element" ||
10
- node.type === "custom-element" ||
11
- node.type === "component" ||
12
- node.type === "fragment");
13
- }
14
- exports.isTag = isTag;
15
- /**
16
- * Checks if the given node is ParentNode
17
- */
18
- function isParent(node) {
19
- return Array.isArray(node.children);
20
- }
21
- exports.isParent = isParent;
22
- /** walk element nodes */
23
- function walkElements(parent, code, enter, leave, parents = []) {
24
- const children = getSortedChildren(parent, code);
25
- const currParents = [parent, ...parents];
26
- for (const node of children) {
27
- enter(node, currParents);
28
- if (isParent(node)) {
29
- walkElements(node, code, enter, leave, currParents);
30
- }
31
- leave(node, currParents);
32
- }
33
- }
34
- exports.walkElements = walkElements;
35
- /** walk nodes */
36
- function walk(parent, code, enter, leave) {
37
- walkElements(parent, code, (node, parents) => {
38
- enter(node, parents);
39
- if (isTag(node)) {
40
- const attrParents = [node, ...parents];
41
- for (const attr of node.attributes) {
42
- enter(attr, attrParents);
43
- leave(attr, attrParents);
44
- }
45
- }
46
- }, leave);
47
- }
48
- exports.walk = walk;
49
- /**
50
- * Get end offset of start tag
51
- */
52
- function calcStartTagEndOffset(node, ctx) {
53
- const lastAttr = node.attributes[node.attributes.length - 1];
54
- let beforeCloseIndex;
55
- if (lastAttr) {
56
- beforeCloseIndex = calcAttributeEndOffset(lastAttr, ctx);
57
- }
58
- else {
59
- const info = getTokenInfo(ctx, [`<${node.name}`], node.position.start.offset);
60
- beforeCloseIndex = info.index + info.match.length;
61
- }
62
- const info = getTokenInfo(ctx, [[">", "/>"]], beforeCloseIndex);
63
- return info.index + info.match.length;
64
- }
65
- exports.calcStartTagEndOffset = calcStartTagEndOffset;
66
- /**
67
- * Get end offset of attribute
68
- */
69
- function calcAttributeEndOffset(node, ctx) {
70
- let info;
71
- if (node.kind === "empty") {
72
- info = getTokenInfo(ctx, [node.name], node.position.start.offset);
73
- }
74
- else if (node.kind === "quoted") {
75
- info = getTokenInfo(ctx, [[`"${node.value}"`, `'${node.value}'`, node.value]], calcAttributeValueStartOffset(node, ctx));
76
- }
77
- else if (node.kind === "expression") {
78
- info = getTokenInfo(ctx, ["{", node.value, "}"], calcAttributeValueStartOffset(node, ctx));
79
- }
80
- else if (node.kind === "shorthand") {
81
- info = getTokenInfo(ctx, ["{", node.name, "}"], node.position.start.offset);
82
- }
83
- else if (node.kind === "spread") {
84
- info = getTokenInfo(ctx, ["{", "...", node.name, "}"], node.position.start.offset);
85
- }
86
- else if (node.kind === "template-literal") {
87
- info = getTokenInfo(ctx, [`\`${node.value}\``], calcAttributeValueStartOffset(node, ctx));
88
- }
89
- else {
90
- throw new errors_1.ParseError(`Unknown attr kind: ${node.kind}`, node.position.start.offset, ctx);
91
- }
92
- return info.index + info.match.length;
93
- }
94
- exports.calcAttributeEndOffset = calcAttributeEndOffset;
95
- /**
96
- * Get start offset of attribute value
97
- */
98
- function calcAttributeValueStartOffset(node, ctx) {
99
- let info;
100
- if (node.kind === "quoted") {
101
- info = getTokenInfo(ctx, [node.name, "=", [`"`, `'`, node.value]], node.position.start.offset);
102
- }
103
- else if (node.kind === "expression") {
104
- info = getTokenInfo(ctx, [node.name, "=", "{"], node.position.start.offset);
105
- }
106
- else if (node.kind === "template-literal") {
107
- info = getTokenInfo(ctx, [node.name, "=", "`"], node.position.start.offset);
108
- }
109
- else {
110
- throw new errors_1.ParseError(`Unknown attr kind: ${node.kind}`, node.position.start.offset, ctx);
111
- }
112
- return info.index;
113
- }
114
- exports.calcAttributeValueStartOffset = calcAttributeValueStartOffset;
115
- /**
116
- * Get end offset of tag
117
- */
118
- function getEndOffset(node, ctx) {
119
- if (node.position.end?.offset != null) {
120
- return node.position.end.offset;
121
- }
122
- if (isTag(node))
123
- return calcTagEndOffset(node, ctx);
124
- if (node.type === "expression")
125
- return calcExpressionEndOffset(node, ctx);
126
- if (node.type === "comment")
127
- return calcCommentEndOffset(node, ctx);
128
- if (node.type === "frontmatter") {
129
- const start = node.position.start.offset;
130
- return ctx.code.indexOf("---", start + 3) + 3;
131
- }
132
- if (node.type === "doctype") {
133
- const start = node.position.start.offset;
134
- return ctx.code.indexOf(">", start) + 1;
135
- }
136
- if (node.type === "text") {
137
- const start = node.position.start.offset;
138
- return start + node.value.length;
139
- }
140
- if (node.type === "root") {
141
- return ctx.code.length;
142
- }
143
- throw new Error(`unknown type: ${node.type}`);
144
- }
145
- exports.getEndOffset = getEndOffset;
146
- /**
147
- * Get content end offset
148
- */
149
- function calcContentEndOffset(parent, ctx) {
150
- const code = ctx.code;
151
- if (isTag(parent)) {
152
- const end = getEndOffset(parent, ctx);
153
- if (code[end - 1] !== ">") {
154
- return end;
155
- }
156
- const index = code.lastIndexOf("</", end - 1);
157
- if (index >= 0 &&
158
- code.slice(index + 2, end - 1).trim() === parent.name) {
159
- return index;
160
- }
161
- return end;
162
- }
163
- else if (parent.type === "expression") {
164
- const end = getEndOffset(parent, ctx);
165
- return code.lastIndexOf("}", end);
166
- }
167
- else if (parent.type === "root") {
168
- return code.length;
169
- }
170
- throw new Error(`unknown type: ${parent.type}`);
171
- }
172
- exports.calcContentEndOffset = calcContentEndOffset;
173
- /**
174
- * If the given tag is a self-close tag, get the self-closing tag.
175
- */
176
- function getSelfClosingTag(node, ctx) {
177
- if (node.children.length > 0) {
178
- return null;
179
- }
180
- const code = ctx.code;
181
- const startTagEndOffset = calcStartTagEndOffset(node, ctx);
182
- if (code.startsWith("/>", startTagEndOffset - 2)) {
183
- return {
184
- offset: startTagEndOffset,
185
- end: "/>",
186
- };
187
- }
188
- if (code.startsWith(`</${node.name}`, startTagEndOffset)) {
189
- return null;
190
- }
191
- return {
192
- offset: startTagEndOffset,
193
- end: ">",
194
- };
195
- }
196
- exports.getSelfClosingTag = getSelfClosingTag;
197
- /**
198
- * If the given tag has a end tag, get the end tag.
199
- */
200
- function getEndTag(node, ctx) {
201
- let beforeIndex;
202
- if (node.children.length) {
203
- const lastChild = node.children[node.children.length - 1];
204
- beforeIndex = getEndOffset(lastChild, ctx);
205
- }
206
- else {
207
- beforeIndex = calcStartTagEndOffset(node, ctx);
208
- }
209
- beforeIndex = skipSpaces(ctx.code, beforeIndex);
210
- if (ctx.code.startsWith(`</${node.name}`, beforeIndex)) {
211
- const offset = beforeIndex;
212
- beforeIndex = beforeIndex + 2 + node.name.length;
213
- const info = getTokenInfo(ctx, [">"], beforeIndex);
214
- const end = info.index + info.match.length;
215
- return {
216
- offset,
217
- tag: ctx.code.slice(offset, end),
218
- };
219
- }
220
- return null;
221
- }
222
- exports.getEndTag = getEndTag;
223
- /**
224
- * Get end offset of comment
225
- */
226
- function calcCommentEndOffset(node, ctx) {
227
- const info = getTokenInfo(ctx, ["<!--", node.value, "-->"], node.position.start.offset);
228
- return info.index + info.match.length;
229
- }
230
- exports.calcCommentEndOffset = calcCommentEndOffset;
231
- /**
232
- * Get end offset of tag
233
- */
234
- function calcTagEndOffset(node, ctx) {
235
- let beforeIndex;
236
- if (node.children.length) {
237
- const lastChild = node.children[node.children.length - 1];
238
- beforeIndex = getEndOffset(lastChild, ctx);
239
- }
240
- else {
241
- beforeIndex = calcStartTagEndOffset(node, ctx);
242
- }
243
- beforeIndex = skipSpaces(ctx.code, beforeIndex);
244
- if (ctx.code.startsWith(`</${node.name}`, beforeIndex)) {
245
- beforeIndex = beforeIndex + 2 + node.name.length;
246
- const info = getTokenInfo(ctx, [">"], beforeIndex);
247
- return info.index + info.match.length;
248
- }
249
- return beforeIndex;
250
- }
251
- /**
252
- * Get end offset of Expression
253
- */
254
- function calcExpressionEndOffset(node, ctx) {
255
- if (node.children.length) {
256
- const lastChild = node.children[node.children.length - 1];
257
- const beforeIndex = getEndOffset(lastChild, ctx);
258
- const info = getTokenInfo(ctx, ["}"], beforeIndex);
259
- return info.index + info.match.length;
260
- }
261
- const info = getTokenInfo(ctx, ["{", "}"], node.position.start.offset);
262
- return info.index + info.match.length;
263
- }
264
- /**
265
- * Get token info
266
- */
267
- function getTokenInfo(ctx, tokens, position) {
268
- let lastMatch;
269
- for (const t of tokens) {
270
- const index = lastMatch
271
- ? lastMatch.index + lastMatch.match.length
272
- : position;
273
- const m = typeof t === "string"
274
- ? matchOfStr(t, index)
275
- : matchOfForMulti(t, index);
276
- if (m == null) {
277
- throw new errors_1.ParseError(`Unknown token at ${index}, expected: ${JSON.stringify(t)}, actual: ${JSON.stringify(ctx.code.slice(index, index + 10))}`, index, ctx);
278
- }
279
- lastMatch = m;
280
- }
281
- return lastMatch;
282
- /**
283
- * For string
284
- */
285
- function matchOfStr(search, position) {
286
- const index = search.trim() === search ? skipSpaces(ctx.code, position) : position;
287
- if (ctx.code.startsWith(search, index)) {
288
- return {
289
- match: search,
290
- index,
291
- };
292
- }
293
- return null;
294
- }
295
- /**
296
- * For multi
297
- */
298
- function matchOfForMulti(search, position) {
299
- for (const s of search) {
300
- const m = matchOfStr(s, position);
301
- if (m) {
302
- return m;
303
- }
304
- }
305
- return null;
306
- }
307
- }
308
- /**
309
- * Skip spaces
310
- */
311
- function skipSpaces(string, position) {
312
- const re = /\s*/g;
313
- re.lastIndex = position;
314
- const match = re.exec(string);
315
- if (match) {
316
- return match.index + match[0].length;
317
- }
318
- return position;
319
- }
320
- exports.skipSpaces = skipSpaces;
321
- /**
322
- * Get children
323
- */
324
- function getSortedChildren(parent, code) {
325
- if (parent.type === "root" && parent.children[0]?.type === "frontmatter") {
326
- // The order of comments and frontmatter may be changed.
327
- const children = [...parent.children];
328
- if (children.every((n) => n.position)) {
329
- return children.sort((a, b) => a.position.start.offset - b.position.start.offset);
330
- }
331
- let start = skipSpaces(code, 0);
332
- if (code.startsWith("<!", start)) {
333
- const frontmatter = children.shift();
334
- const before = [];
335
- let first;
336
- while ((first = children.shift())) {
337
- start = skipSpaces(code, start);
338
- if (first.type === "comment" &&
339
- code.startsWith("<!--", start)) {
340
- start = code.indexOf("-->", start + 4) + 3;
341
- before.push(first);
342
- }
343
- else if (first.type === "doctype" &&
344
- code.startsWith("<!", start)) {
345
- start = code.indexOf(">", start + 2) + 1;
346
- before.push(first);
347
- }
348
- else {
349
- children.unshift(first);
350
- break;
351
- }
352
- }
353
- return [...before, frontmatter, ...children];
354
- }
355
- }
356
- return parent.children;
357
- }
@@ -1,21 +0,0 @@
1
- import type { ParseResult } from "@astrojs/compiler";
2
- import type { AttributeNode, Node, ParentNode } from "@astrojs/compiler/types";
3
- export interface ParseTemplateResult {
4
- result: ParseResult;
5
- getEndOffset: (node: Node) => number;
6
- calcAttributeValueStartOffset: (node: AttributeNode) => number;
7
- calcAttributeEndOffset: (node: AttributeNode) => number;
8
- walk: (parent: ParentNode, enter: (n: Node | AttributeNode, parents: ParentNode[]) => void, leave?: (n: Node | AttributeNode, parents: ParentNode[]) => void) => void;
9
- getLocFromIndex: (index: number) => {
10
- line: number;
11
- column: number;
12
- };
13
- getIndexFromLoc: (loc: {
14
- line: number;
15
- column: number;
16
- }) => number;
17
- }
18
- /**
19
- * Parse the astro component template.
20
- */
21
- export declare function parseTemplate(code: string): ParseTemplateResult;