astro-eslint-parser 0.0.11 → 0.0.14
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/lib/ast.d.ts +7 -1
- package/lib/astro/index.d.ts +29 -3
- package/lib/astro/index.js +169 -14
- package/lib/context/index.d.ts +3 -1
- package/lib/context/index.js +6 -0
- package/lib/context/script.d.ts +1 -1
- package/lib/context/script.js +7 -5
- package/lib/errors.d.ts +1 -0
- package/lib/errors.js +1 -0
- package/lib/parser/astro-parser/parse.js +38 -7
- package/lib/parser/process-template.js +119 -46
- package/lib/visitor-keys.js +1 -0
- package/package.json +2 -1
package/lib/ast.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSESTree } from "@typescript-eslint/types";
|
|
2
|
-
export declare type AstroNode = AstroProgram | AstroRootFragment | AstroHTMLComment | AstroDoctype | AstroShorthandAttribute | AstroTemplateLiteralAttribute | AstroRawText;
|
|
2
|
+
export declare type AstroNode = AstroProgram | AstroRootFragment | AstroHTMLComment | AstroDoctype | AstroShorthandAttribute | AstroTemplateLiteralAttribute | AstroRawText | AstroFragment;
|
|
3
3
|
/** Node of Astro program root */
|
|
4
4
|
export interface AstroProgram extends Omit<TSESTree.Program, "type" | "body"> {
|
|
5
5
|
type: "Program";
|
|
@@ -45,3 +45,9 @@ export interface AstroRawText extends Omit<TSESTree.JSXText, "type" | "parent">
|
|
|
45
45
|
type: "AstroRawText";
|
|
46
46
|
parent: AstroRootFragment | TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
47
47
|
}
|
|
48
|
+
/** Node of Astro fragment expression */
|
|
49
|
+
export interface AstroFragment extends Omit<TSESTree.BaseNode, "type" | "parent"> {
|
|
50
|
+
type: "AstroFragment";
|
|
51
|
+
children: TSESTree.JSXFragment["children"];
|
|
52
|
+
parent: TSESTree.JSXFragment["parent"];
|
|
53
|
+
}
|
package/lib/astro/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AttributeNode, CommentNode, Node, ParentNode, TagLikeNode } from "@astrojs/compiler/types";
|
|
1
|
+
import type { AttributeNode, CommentNode, ExpressionNode, Node, ParentNode, TagLikeNode } from "@astrojs/compiler/types";
|
|
2
2
|
import type { Context } from "../context";
|
|
3
3
|
/**
|
|
4
4
|
* Checks if the given node is TagLikeNode
|
|
@@ -9,13 +9,21 @@ export declare function isTag(node: Node): node is Node & TagLikeNode;
|
|
|
9
9
|
*/
|
|
10
10
|
export declare function isParent(node: Node): node is ParentNode;
|
|
11
11
|
/** walk element nodes */
|
|
12
|
-
export declare function walkElements(parent: ParentNode, code: string,
|
|
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
13
|
/** walk nodes */
|
|
14
|
-
export declare function walk(parent: ParentNode, code: string, enter: (n: Node | AttributeNode,
|
|
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
15
|
/**
|
|
16
16
|
* Get end offset of start tag
|
|
17
17
|
*/
|
|
18
18
|
export declare function getStartTagEndOffset(node: TagLikeNode, ctx: Context): number;
|
|
19
|
+
/**
|
|
20
|
+
* Get end offset of tag
|
|
21
|
+
*/
|
|
22
|
+
export declare function getTagEndOffset(node: TagLikeNode, parents: ParentNode[], ctx: Context): number;
|
|
23
|
+
/**
|
|
24
|
+
* Get end offset of Expression
|
|
25
|
+
*/
|
|
26
|
+
export declare function getExpressionEndOffset(node: ExpressionNode, parents: ParentNode[], ctx: Context): number;
|
|
19
27
|
/**
|
|
20
28
|
* Get end offset of attribute
|
|
21
29
|
*/
|
|
@@ -28,6 +36,24 @@ export declare function getAttributeValueStartOffset(node: AttributeNode, ctx: C
|
|
|
28
36
|
* Get end offset of comment
|
|
29
37
|
*/
|
|
30
38
|
export declare function getCommentEndOffset(node: CommentNode, ctx: Context): number;
|
|
39
|
+
/**
|
|
40
|
+
* Get content end offset
|
|
41
|
+
*/
|
|
42
|
+
export declare function getContentEndOffset(parent: ParentNode, parents: ParentNode[], ctx: Context): number;
|
|
43
|
+
/**
|
|
44
|
+
* If the given tag is a self-close tag, get the self-closing tag.
|
|
45
|
+
*/
|
|
46
|
+
export declare function getSelfClosingTag(node: TagLikeNode, parents: ParentNode[], ctx: Context): null | {
|
|
47
|
+
offset: number;
|
|
48
|
+
end: "/>" | ">";
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* If the given tag has a end tag, get the end tag.
|
|
52
|
+
*/
|
|
53
|
+
export declare function getEndTag(node: TagLikeNode, parents: ParentNode[], ctx: Context): null | {
|
|
54
|
+
offset: number;
|
|
55
|
+
tag: string;
|
|
56
|
+
};
|
|
31
57
|
/**
|
|
32
58
|
* Skip spaces
|
|
33
59
|
*/
|
package/lib/astro/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.skipSpaces = exports.getCommentEndOffset = exports.getAttributeValueStartOffset = exports.getAttributeEndOffset = exports.getStartTagEndOffset = exports.walk = exports.walkElements = exports.isParent = exports.isTag = void 0;
|
|
3
|
+
exports.skipSpaces = exports.getEndTag = exports.getSelfClosingTag = exports.getContentEndOffset = exports.getCommentEndOffset = exports.getAttributeValueStartOffset = exports.getAttributeEndOffset = exports.getExpressionEndOffset = exports.getTagEndOffset = exports.getStartTagEndOffset = exports.walk = exports.walkElements = exports.isParent = exports.isTag = void 0;
|
|
4
4
|
const errors_1 = require("../errors");
|
|
5
5
|
/**
|
|
6
6
|
* Checks if the given node is TagLikeNode
|
|
@@ -20,32 +20,30 @@ function isParent(node) {
|
|
|
20
20
|
}
|
|
21
21
|
exports.isParent = isParent;
|
|
22
22
|
/** walk element nodes */
|
|
23
|
-
function walkElements(parent, code,
|
|
23
|
+
function walkElements(parent, code, enter, leave, parents = []) {
|
|
24
24
|
const children = getSortedChildren(parent, code);
|
|
25
|
+
const currParents = [parent, ...parents];
|
|
25
26
|
for (const node of children) {
|
|
26
|
-
|
|
27
|
+
enter(node, currParents);
|
|
27
28
|
if (isParent(node)) {
|
|
28
|
-
walkElements(node, code,
|
|
29
|
+
walkElements(node, code, enter, leave, currParents);
|
|
29
30
|
}
|
|
31
|
+
leave(node, currParents);
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
exports.walkElements = walkElements;
|
|
33
35
|
/** walk nodes */
|
|
34
36
|
function walk(parent, code, enter, leave) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
enter(node, parent);
|
|
37
|
+
walkElements(parent, code, (node, parents) => {
|
|
38
|
+
enter(node, parents);
|
|
38
39
|
if (isTag(node)) {
|
|
40
|
+
const attrParents = [node, ...parents];
|
|
39
41
|
for (const attr of node.attributes) {
|
|
40
|
-
enter(attr,
|
|
41
|
-
leave
|
|
42
|
+
enter(attr, attrParents);
|
|
43
|
+
leave(attr, attrParents);
|
|
42
44
|
}
|
|
43
45
|
}
|
|
44
|
-
|
|
45
|
-
walk(node, code, enter, leave);
|
|
46
|
-
}
|
|
47
|
-
leave === null || leave === void 0 ? void 0 : leave(node, parent);
|
|
48
|
-
}
|
|
46
|
+
}, leave);
|
|
49
47
|
}
|
|
50
48
|
exports.walk = walk;
|
|
51
49
|
/**
|
|
@@ -65,6 +63,49 @@ function getStartTagEndOffset(node, ctx) {
|
|
|
65
63
|
return info.index + info.match.length;
|
|
66
64
|
}
|
|
67
65
|
exports.getStartTagEndOffset = getStartTagEndOffset;
|
|
66
|
+
/**
|
|
67
|
+
* Get end offset of tag
|
|
68
|
+
*/
|
|
69
|
+
function getTagEndOffset(node, parents, ctx) {
|
|
70
|
+
var _a;
|
|
71
|
+
if (((_a = node.position.end) === null || _a === void 0 ? void 0 : _a.offset) != null) {
|
|
72
|
+
return node.position.end.offset;
|
|
73
|
+
}
|
|
74
|
+
let beforeIndex;
|
|
75
|
+
if (node.children.length) {
|
|
76
|
+
const lastChild = node.children[node.children.length - 1];
|
|
77
|
+
beforeIndex = getEndOffset(lastChild, [node, ...parents], ctx);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
beforeIndex = getStartTagEndOffset(node, ctx);
|
|
81
|
+
}
|
|
82
|
+
beforeIndex = skipSpaces(ctx.code, beforeIndex);
|
|
83
|
+
if (ctx.code.startsWith(`</${node.name}`, beforeIndex)) {
|
|
84
|
+
beforeIndex = beforeIndex + 2 + node.name.length;
|
|
85
|
+
const info = getTokenInfo(ctx, [">"], beforeIndex);
|
|
86
|
+
return info.index + info.match.length;
|
|
87
|
+
}
|
|
88
|
+
return beforeIndex;
|
|
89
|
+
}
|
|
90
|
+
exports.getTagEndOffset = getTagEndOffset;
|
|
91
|
+
/**
|
|
92
|
+
* Get end offset of Expression
|
|
93
|
+
*/
|
|
94
|
+
function getExpressionEndOffset(node, parents, ctx) {
|
|
95
|
+
var _a;
|
|
96
|
+
if (((_a = node.position.end) === null || _a === void 0 ? void 0 : _a.offset) != null) {
|
|
97
|
+
return node.position.end.offset;
|
|
98
|
+
}
|
|
99
|
+
if (node.children.length) {
|
|
100
|
+
const lastChild = node.children[node.children.length - 1];
|
|
101
|
+
const beforeIndex = getEndOffset(lastChild, [node, ...parents], ctx);
|
|
102
|
+
const info = getTokenInfo(ctx, ["}"], beforeIndex);
|
|
103
|
+
return info.index + info.match.length;
|
|
104
|
+
}
|
|
105
|
+
const info = getTokenInfo(ctx, ["{", "}"], node.position.start.offset);
|
|
106
|
+
return info.index + info.match.length;
|
|
107
|
+
}
|
|
108
|
+
exports.getExpressionEndOffset = getExpressionEndOffset;
|
|
68
109
|
/**
|
|
69
110
|
* Get end offset of attribute
|
|
70
111
|
*/
|
|
@@ -122,6 +163,120 @@ function getCommentEndOffset(node, ctx) {
|
|
|
122
163
|
return info.index + info.match.length;
|
|
123
164
|
}
|
|
124
165
|
exports.getCommentEndOffset = getCommentEndOffset;
|
|
166
|
+
/**
|
|
167
|
+
* Get content end offset
|
|
168
|
+
*/
|
|
169
|
+
function getContentEndOffset(parent, parents, ctx) {
|
|
170
|
+
const code = ctx.code;
|
|
171
|
+
if (isTag(parent)) {
|
|
172
|
+
const end = getTagEndOffset(parent, parents, ctx);
|
|
173
|
+
if (code[end - 1] !== ">") {
|
|
174
|
+
return end;
|
|
175
|
+
}
|
|
176
|
+
const index = code.lastIndexOf("</", end);
|
|
177
|
+
if (index >= 0 && code.slice(index, end).trim() === parent.name) {
|
|
178
|
+
return index;
|
|
179
|
+
}
|
|
180
|
+
return end;
|
|
181
|
+
}
|
|
182
|
+
else if (parent.type === "expression") {
|
|
183
|
+
const end = getExpressionEndOffset(parent, parents, ctx);
|
|
184
|
+
return code.lastIndexOf("}", end);
|
|
185
|
+
}
|
|
186
|
+
else if (parent.type === "root") {
|
|
187
|
+
return code.length;
|
|
188
|
+
}
|
|
189
|
+
throw new Error(`unknown type: ${parent.type}`);
|
|
190
|
+
}
|
|
191
|
+
exports.getContentEndOffset = getContentEndOffset;
|
|
192
|
+
/**
|
|
193
|
+
* If the given tag is a self-close tag, get the self-closing tag.
|
|
194
|
+
*/
|
|
195
|
+
function getSelfClosingTag(node, parents, ctx) {
|
|
196
|
+
const children = node.children.filter((c) => c.type !== "text" || c.value.trim());
|
|
197
|
+
if (children.length > 0) {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
const parent = parents[0];
|
|
201
|
+
const code = ctx.code;
|
|
202
|
+
let nextElementIndex = code.length;
|
|
203
|
+
const childIndex = parent.children.indexOf(node);
|
|
204
|
+
if (childIndex === parent.children.length - 1) {
|
|
205
|
+
// last
|
|
206
|
+
nextElementIndex = getContentEndOffset(parent, parents.slice(1), ctx);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
const next = parent.children[childIndex + 1];
|
|
210
|
+
nextElementIndex = next.position.start.offset;
|
|
211
|
+
}
|
|
212
|
+
const endOffset = getStartTagEndOffset(node, ctx);
|
|
213
|
+
if (code.slice(endOffset, nextElementIndex).trim()) {
|
|
214
|
+
// has end tag
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
offset: endOffset,
|
|
219
|
+
end: code.slice(endOffset - 2, endOffset) === "/>" ? "/>" : ">",
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
exports.getSelfClosingTag = getSelfClosingTag;
|
|
223
|
+
/**
|
|
224
|
+
* If the given tag has a end tag, get the end tag.
|
|
225
|
+
*/
|
|
226
|
+
function getEndTag(node, parents, ctx) {
|
|
227
|
+
let beforeIndex;
|
|
228
|
+
if (node.children.length) {
|
|
229
|
+
const lastChild = node.children[node.children.length - 1];
|
|
230
|
+
beforeIndex = getEndOffset(lastChild, [node, ...parents], ctx);
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
beforeIndex = getStartTagEndOffset(node, ctx);
|
|
234
|
+
}
|
|
235
|
+
beforeIndex = skipSpaces(ctx.code, beforeIndex);
|
|
236
|
+
if (ctx.code.startsWith(`</${node.name}`, beforeIndex)) {
|
|
237
|
+
const offset = beforeIndex;
|
|
238
|
+
beforeIndex = beforeIndex + 2 + node.name.length;
|
|
239
|
+
const info = getTokenInfo(ctx, [">"], beforeIndex);
|
|
240
|
+
const end = info.index + info.match.length;
|
|
241
|
+
return {
|
|
242
|
+
offset,
|
|
243
|
+
tag: ctx.code.slice(offset, end),
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
exports.getEndTag = getEndTag;
|
|
249
|
+
/**
|
|
250
|
+
* Get end offset of tag
|
|
251
|
+
*/
|
|
252
|
+
function getEndOffset(node, parents, ctx) {
|
|
253
|
+
var _a;
|
|
254
|
+
if (((_a = node.position.end) === null || _a === void 0 ? void 0 : _a.offset) != null) {
|
|
255
|
+
return node.position.end.offset;
|
|
256
|
+
}
|
|
257
|
+
if (isTag(node))
|
|
258
|
+
return getTagEndOffset(node, parents, ctx);
|
|
259
|
+
if (node.type === "expression")
|
|
260
|
+
return getExpressionEndOffset(node, parents, ctx);
|
|
261
|
+
if (node.type === "comment")
|
|
262
|
+
return getCommentEndOffset(node, ctx);
|
|
263
|
+
if (node.type === "frontmatter") {
|
|
264
|
+
const start = node.position.start.offset;
|
|
265
|
+
return ctx.code.indexOf("---", start + 3) + 3;
|
|
266
|
+
}
|
|
267
|
+
if (node.type === "doctype") {
|
|
268
|
+
const start = node.position.start.offset;
|
|
269
|
+
return ctx.code.indexOf(">", start) + 1;
|
|
270
|
+
}
|
|
271
|
+
if (node.type === "text") {
|
|
272
|
+
const start = node.position.start.offset;
|
|
273
|
+
return start + node.value.length;
|
|
274
|
+
}
|
|
275
|
+
if (node.type === "root") {
|
|
276
|
+
return ctx.code.length;
|
|
277
|
+
}
|
|
278
|
+
throw new Error(`unknown type: ${node.type}`);
|
|
279
|
+
}
|
|
125
280
|
/**
|
|
126
281
|
* Get token info
|
|
127
282
|
*/
|
package/lib/context/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export declare class Context {
|
|
|
9
9
|
readonly parserOptions: any;
|
|
10
10
|
readonly locs: LinesAndColumns;
|
|
11
11
|
private readonly locsMap;
|
|
12
|
-
private state;
|
|
12
|
+
private readonly state;
|
|
13
13
|
constructor(code: string, parserOptions: any);
|
|
14
14
|
getLocFromIndex(index: number): {
|
|
15
15
|
line: number;
|
|
@@ -29,6 +29,8 @@ export declare class Context {
|
|
|
29
29
|
getText(range: TSESTree.Range): string;
|
|
30
30
|
isTypeScript(): boolean;
|
|
31
31
|
remapCR({ ast, visitorKeys }: ESLintExtendedProgram): void;
|
|
32
|
+
get originalAST(): any;
|
|
33
|
+
set originalAST(originalAST: any);
|
|
32
34
|
}
|
|
33
35
|
export declare class LinesAndColumns {
|
|
34
36
|
readonly code: string;
|
package/lib/context/index.js
CHANGED
|
@@ -129,6 +129,12 @@ class Context {
|
|
|
129
129
|
comment.range = remapRange(comment.range);
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
|
+
get originalAST() {
|
|
133
|
+
return this.state.originalAST;
|
|
134
|
+
}
|
|
135
|
+
set originalAST(originalAST) {
|
|
136
|
+
this.state.originalAST = originalAST;
|
|
137
|
+
}
|
|
132
138
|
}
|
|
133
139
|
exports.Context = Context;
|
|
134
140
|
class LinesAndColumns {
|
package/lib/context/script.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export declare class ScriptContext {
|
|
|
14
14
|
appendOriginal(index: number): void;
|
|
15
15
|
appendScript(fragment: string): void;
|
|
16
16
|
addToken(type: TSESTree.Token["type"], range: TSESTree.Range): void;
|
|
17
|
-
addRestoreNodeProcess(process: (node: TSESTree.Node, result: ESLintExtendedProgram) => boolean): void;
|
|
17
|
+
addRestoreNodeProcess(process: (node: TSESTree.Node, result: ESLintExtendedProgram, parent: TSESTree.Node) => boolean): void;
|
|
18
18
|
/**
|
|
19
19
|
* Restore AST nodes
|
|
20
20
|
*/
|
package/lib/context/script.js
CHANGED
|
@@ -52,12 +52,12 @@ class ScriptContext {
|
|
|
52
52
|
delete rootFragment.openingFragment;
|
|
53
53
|
rootFragment.type = "AstroRootFragment";
|
|
54
54
|
// remap locations
|
|
55
|
-
const traversed = new
|
|
55
|
+
const traversed = new Map();
|
|
56
56
|
(0, traverse_1.traverseNodes)(result.ast, {
|
|
57
57
|
visitorKeys: result.visitorKeys,
|
|
58
|
-
enterNode: (node) => {
|
|
58
|
+
enterNode: (node, p) => {
|
|
59
59
|
if (!traversed.has(node)) {
|
|
60
|
-
traversed.
|
|
60
|
+
traversed.set(node, p);
|
|
61
61
|
this.remapLocation(node);
|
|
62
62
|
}
|
|
63
63
|
},
|
|
@@ -78,8 +78,10 @@ class ScriptContext {
|
|
|
78
78
|
this.remapLocation(token);
|
|
79
79
|
}
|
|
80
80
|
let restoreNodeProcesses = this.restoreNodeProcesses;
|
|
81
|
-
for (const node of traversed) {
|
|
82
|
-
|
|
81
|
+
for (const [node, parent] of traversed) {
|
|
82
|
+
if (!parent)
|
|
83
|
+
continue;
|
|
84
|
+
restoreNodeProcesses = restoreNodeProcesses.filter((proc) => !proc(node, result, parent));
|
|
83
85
|
}
|
|
84
86
|
// Adjust program node location
|
|
85
87
|
const first = result.ast.body[0];
|
package/lib/errors.d.ts
CHANGED
package/lib/errors.js
CHANGED
|
@@ -31,7 +31,7 @@ const errors_1 = require("../../errors");
|
|
|
31
31
|
* Parse code by `@astrojs/compiler`
|
|
32
32
|
*/
|
|
33
33
|
function parse(code, ctx) {
|
|
34
|
-
const ast = parseByService(code).ast;
|
|
34
|
+
const ast = parseByService(code, ctx).ast;
|
|
35
35
|
const htmlElement = ast.children.find((n) => n.type === "element" && n.name === "html");
|
|
36
36
|
if (htmlElement) {
|
|
37
37
|
adjustHTML(ast, htmlElement, ctx);
|
|
@@ -43,13 +43,16 @@ exports.parse = parse;
|
|
|
43
43
|
/**
|
|
44
44
|
* Parse code by `@astrojs/compiler`
|
|
45
45
|
*/
|
|
46
|
-
function parseByService(code) {
|
|
46
|
+
function parseByService(code, ctx) {
|
|
47
47
|
const jsonAst = service.parse(code, { position: true }).ast;
|
|
48
|
+
ctx.originalAST = jsonAst;
|
|
48
49
|
try {
|
|
49
50
|
const ast = JSON.parse(jsonAst);
|
|
51
|
+
ctx.originalAST = ast;
|
|
50
52
|
return { ast };
|
|
51
53
|
}
|
|
52
54
|
catch (_a) {
|
|
55
|
+
// FIXME: Workaround for escape bugs
|
|
53
56
|
// Adjust because may get the wrong escape as JSON.
|
|
54
57
|
const ast = JSON.parse(jsonAst.replace(/\\./gu, (m) => {
|
|
55
58
|
try {
|
|
@@ -60,6 +63,7 @@ function parseByService(code) {
|
|
|
60
63
|
return `\\${m}`;
|
|
61
64
|
}
|
|
62
65
|
}));
|
|
66
|
+
ctx.originalAST = ast;
|
|
63
67
|
return { ast };
|
|
64
68
|
}
|
|
65
69
|
}
|
|
@@ -119,7 +123,7 @@ function fixLocations(node, ctx) {
|
|
|
119
123
|
let start = 0;
|
|
120
124
|
(0, astro_1.walk)(node, ctx.code,
|
|
121
125
|
// eslint-disable-next-line complexity -- X(
|
|
122
|
-
(node, parent) => {
|
|
126
|
+
(node, [parent]) => {
|
|
123
127
|
if (node.type === "frontmatter") {
|
|
124
128
|
start = node.position.start.offset = tokenIndex(ctx, "---", start);
|
|
125
129
|
start = node.position.end.offset =
|
|
@@ -155,12 +159,39 @@ function fixLocations(node, ctx) {
|
|
|
155
159
|
if (start < 0) {
|
|
156
160
|
start = ctx.code.length;
|
|
157
161
|
}
|
|
158
|
-
// Workaround for escape bugs
|
|
162
|
+
// FIXME: Workaround for escape bugs
|
|
159
163
|
node.value = ctx.code.slice(node.position.start.offset, start);
|
|
160
164
|
}
|
|
161
165
|
else {
|
|
162
|
-
|
|
163
|
-
|
|
166
|
+
const index = tokenIndexSafe(ctx.code, node.value, start);
|
|
167
|
+
if (index != null) {
|
|
168
|
+
start = node.position.start.offset = index;
|
|
169
|
+
start += node.value.length;
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
// FIXME: Workaround for escape bugs
|
|
173
|
+
node.position.start.offset = start;
|
|
174
|
+
const value = node.value.replace(/\s+/gu, "");
|
|
175
|
+
for (let charIndex = 0; charIndex < value.length; charIndex++) {
|
|
176
|
+
const char = value[charIndex];
|
|
177
|
+
const index = tokenIndexSafe(ctx.code, char, start);
|
|
178
|
+
if (index != null) {
|
|
179
|
+
start = index + 1;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
start = (0, astro_1.skipSpaces)(ctx.code, start);
|
|
183
|
+
if (ctx.code.startsWith("\\", start)) {
|
|
184
|
+
const codeChar = JSON.parse(`"\\${ctx.code[start + 1]}"`);
|
|
185
|
+
start += 2;
|
|
186
|
+
if (codeChar === char) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
start = tokenIndex(ctx, char, start) + 1;
|
|
191
|
+
}
|
|
192
|
+
start = (0, astro_1.skipSpaces)(ctx.code, start);
|
|
193
|
+
node.value = ctx.code.slice(node.position.start.offset, start);
|
|
194
|
+
}
|
|
164
195
|
}
|
|
165
196
|
}
|
|
166
197
|
else if (node.type === "expression") {
|
|
@@ -182,7 +213,7 @@ function fixLocations(node, ctx) {
|
|
|
182
213
|
else if (node.type === "root") {
|
|
183
214
|
// noop
|
|
184
215
|
}
|
|
185
|
-
}, (node, parent) => {
|
|
216
|
+
}, (node, [parent]) => {
|
|
186
217
|
if (node.type === "attribute") {
|
|
187
218
|
const attributes = parent.attributes;
|
|
188
219
|
if (attributes[attributes.length - 1] === node) {
|
|
@@ -17,8 +17,9 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
17
17
|
script.appendScript("<>");
|
|
18
18
|
fragmentOpened = true;
|
|
19
19
|
}
|
|
20
|
+
(0, astro_1.walkElements)(resultTemplate.ast, ctx.code,
|
|
20
21
|
// eslint-disable-next-line complexity -- X(
|
|
21
|
-
(
|
|
22
|
+
(node, parents) => {
|
|
22
23
|
if (node.type === "frontmatter") {
|
|
23
24
|
const start = node.position.start.offset;
|
|
24
25
|
script.appendOriginal(start);
|
|
@@ -32,7 +33,8 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
32
33
|
for (let index = 0; index < result.ast.body.length; index++) {
|
|
33
34
|
const st = result.ast.body[index];
|
|
34
35
|
if (st.type === types_1.AST_NODE_TYPES.EmptyStatement) {
|
|
35
|
-
if (st.range[0] === end - 3 &&
|
|
36
|
+
if (st.range[0] === end - 3 &&
|
|
37
|
+
st.range[1] === end) {
|
|
36
38
|
result.ast.body.splice(index, 1);
|
|
37
39
|
break;
|
|
38
40
|
}
|
|
@@ -47,8 +49,43 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
47
49
|
script.addToken(types_1.AST_TOKEN_TYPES.Punctuator, [end - 3, end]);
|
|
48
50
|
}
|
|
49
51
|
else if ((0, astro_1.isTag)(node)) {
|
|
52
|
+
// Process for multiple tag
|
|
53
|
+
const parent = parents[0];
|
|
54
|
+
if (parent.type === "expression") {
|
|
55
|
+
const index = parent.children.indexOf(node);
|
|
56
|
+
const before = parent.children[index - 1];
|
|
57
|
+
if (!before || !(0, astro_1.isTag)(before)) {
|
|
58
|
+
const after = parent.children[index + 1];
|
|
59
|
+
if (after &&
|
|
60
|
+
((0, astro_1.isTag)(after) || after.type === "comment")) {
|
|
61
|
+
const start = node.position.start.offset;
|
|
62
|
+
script.appendOriginal(start);
|
|
63
|
+
script.appendScript("<>");
|
|
64
|
+
script.addRestoreNodeProcess((scriptNode) => {
|
|
65
|
+
if (scriptNode.range[0] === start &&
|
|
66
|
+
scriptNode.type ===
|
|
67
|
+
types_1.AST_NODE_TYPES.JSXFragment) {
|
|
68
|
+
delete scriptNode.openingFragment;
|
|
69
|
+
delete scriptNode.closingFragment;
|
|
70
|
+
const fragmentNode = scriptNode;
|
|
71
|
+
fragmentNode.type = "AstroFragment";
|
|
72
|
+
const last = fragmentNode.children[fragmentNode.children.length - 1];
|
|
73
|
+
if (fragmentNode.range[1] < last.range[1]) {
|
|
74
|
+
fragmentNode.range[1] = last.range[1];
|
|
75
|
+
fragmentNode.loc.end =
|
|
76
|
+
ctx.getLocFromIndex(fragmentNode.range[1]);
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Process for attributes
|
|
50
86
|
for (const attr of node.attributes) {
|
|
51
|
-
if ((node.type === "component" ||
|
|
87
|
+
if ((node.type === "component" ||
|
|
88
|
+
node.type === "fragment") &&
|
|
52
89
|
(attr.kind === "quoted" ||
|
|
53
90
|
attr.kind === "empty" ||
|
|
54
91
|
attr.kind === "expression" ||
|
|
@@ -76,7 +113,8 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
76
113
|
types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
77
114
|
scriptNode.range[0] === start) {
|
|
78
115
|
const baseNameNode = scriptNode.name;
|
|
79
|
-
const nsn = Object.assign(Object.assign({}, baseNameNode), { type: types_1.AST_NODE_TYPES.JSXNamespacedName, namespace: Object.assign({ type: types_1.AST_NODE_TYPES.JSXIdentifier, name: attr.name.slice(0, colonIndex) }, ctx.getLocations(baseNameNode.range[0], baseNameNode.range[0] +
|
|
116
|
+
const nsn = Object.assign(Object.assign({}, baseNameNode), { type: types_1.AST_NODE_TYPES.JSXNamespacedName, namespace: Object.assign({ type: types_1.AST_NODE_TYPES.JSXIdentifier, name: attr.name.slice(0, colonIndex) }, ctx.getLocations(baseNameNode.range[0], baseNameNode.range[0] +
|
|
117
|
+
colonIndex)), name: Object.assign({ type: types_1.AST_NODE_TYPES.JSXIdentifier, name: attr.name.slice(colonIndex + 1) }, ctx.getLocations(baseNameNode.range[0] +
|
|
80
118
|
colonIndex +
|
|
81
119
|
1, baseNameNode.range[1])) });
|
|
82
120
|
scriptNode.name = nsn;
|
|
@@ -87,7 +125,8 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
87
125
|
const token = tokens[index];
|
|
88
126
|
if (token.range[0] ===
|
|
89
127
|
baseNameNode.range[0] &&
|
|
90
|
-
token.range[1] ===
|
|
128
|
+
token.range[1] ===
|
|
129
|
+
baseNameNode.range[1]) {
|
|
91
130
|
tokens.splice(index, 1);
|
|
92
131
|
break;
|
|
93
132
|
}
|
|
@@ -106,7 +145,8 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
106
145
|
: attr.name;
|
|
107
146
|
script.appendScript(`${jsxName}=`);
|
|
108
147
|
script.addRestoreNodeProcess((scriptNode) => {
|
|
109
|
-
if (scriptNode.type ===
|
|
148
|
+
if (scriptNode.type ===
|
|
149
|
+
types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
110
150
|
scriptNode.range[0] === start) {
|
|
111
151
|
const attrNode = scriptNode;
|
|
112
152
|
attrNode.type = "AstroShorthandAttribute";
|
|
@@ -130,7 +170,8 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
130
170
|
script.appendOriginal(end);
|
|
131
171
|
script.appendScript("}");
|
|
132
172
|
script.addRestoreNodeProcess((scriptNode) => {
|
|
133
|
-
if (scriptNode.type ===
|
|
173
|
+
if (scriptNode.type ===
|
|
174
|
+
types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
134
175
|
scriptNode.range[0] === attrStart) {
|
|
135
176
|
const attrNode = scriptNode;
|
|
136
177
|
attrNode.type = "AstroTemplateLiteralAttribute";
|
|
@@ -140,11 +181,13 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
140
181
|
});
|
|
141
182
|
}
|
|
142
183
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
184
|
+
// Process for start tag close
|
|
185
|
+
const closing = (0, astro_1.getSelfClosingTag)(node, parents, ctx);
|
|
186
|
+
if (closing && closing.end === ">") {
|
|
187
|
+
script.appendOriginal(closing.offset - 1);
|
|
146
188
|
script.appendScript("/");
|
|
147
189
|
}
|
|
190
|
+
// Process for raw text
|
|
148
191
|
if (node.name === "script" || node.name === "style") {
|
|
149
192
|
const text = node.children[0];
|
|
150
193
|
if (text && text.type === "text") {
|
|
@@ -176,15 +219,17 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
176
219
|
script.appendOriginal(start);
|
|
177
220
|
let targetType;
|
|
178
221
|
if (fragmentOpened) {
|
|
179
|
-
script.
|
|
222
|
+
script.appendOriginal(start + 1);
|
|
223
|
+
script.appendScript(`></`);
|
|
224
|
+
script.skipOriginalOffset(length - 2);
|
|
180
225
|
targetType = types_1.AST_NODE_TYPES.JSXFragment;
|
|
181
226
|
}
|
|
182
227
|
else {
|
|
183
228
|
script.appendScript(`0;`);
|
|
184
229
|
targetType = types_1.AST_NODE_TYPES.ExpressionStatement;
|
|
230
|
+
script.skipOriginalOffset(length);
|
|
185
231
|
}
|
|
186
|
-
script.
|
|
187
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
232
|
+
script.addRestoreNodeProcess((scriptNode, result) => {
|
|
188
233
|
if (scriptNode.range[0] === start &&
|
|
189
234
|
scriptNode.type === targetType) {
|
|
190
235
|
delete scriptNode.children;
|
|
@@ -194,6 +239,27 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
194
239
|
const commentNode = scriptNode;
|
|
195
240
|
commentNode.type = "AstroHTMLComment";
|
|
196
241
|
commentNode.value = node.value;
|
|
242
|
+
if (fragmentOpened) {
|
|
243
|
+
const removeTokenSet = new Set([
|
|
244
|
+
(token) => token.value === "<" &&
|
|
245
|
+
token.range[0] === scriptNode.range[0],
|
|
246
|
+
(token) => token.value === ">" &&
|
|
247
|
+
token.range[1] === scriptNode.range[1],
|
|
248
|
+
]);
|
|
249
|
+
const tokens = result.ast.tokens || [];
|
|
250
|
+
for (let index = tokens.length - 1; index >= 0; index--) {
|
|
251
|
+
const token = tokens[index];
|
|
252
|
+
for (const rt of removeTokenSet) {
|
|
253
|
+
if (rt(token)) {
|
|
254
|
+
tokens.splice(index, 1);
|
|
255
|
+
removeTokenSet.delete(rt);
|
|
256
|
+
if (!removeTokenSet.size) {
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
197
263
|
return true;
|
|
198
264
|
}
|
|
199
265
|
return false;
|
|
@@ -232,6 +298,46 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
232
298
|
});
|
|
233
299
|
script.addToken("HTMLDocType", [start, end]);
|
|
234
300
|
}
|
|
301
|
+
}, (node, parents) => {
|
|
302
|
+
if ((0, astro_1.isTag)(node)) {
|
|
303
|
+
const closing = (0, astro_1.getSelfClosingTag)(node, parents, ctx);
|
|
304
|
+
if (!closing) {
|
|
305
|
+
const end = (0, astro_1.getEndTag)(node, parents, ctx);
|
|
306
|
+
if (!end) {
|
|
307
|
+
const offset = (0, astro_1.getContentEndOffset)(node, parents, ctx);
|
|
308
|
+
script.appendOriginal(offset);
|
|
309
|
+
script.appendScript(`</${node.name}>`);
|
|
310
|
+
script.addRestoreNodeProcess((scriptNode, _result, parent) => {
|
|
311
|
+
if (scriptNode.range[0] === offset &&
|
|
312
|
+
scriptNode.type ===
|
|
313
|
+
types_1.AST_NODE_TYPES.JSXClosingElement &&
|
|
314
|
+
parent.type === types_1.AST_NODE_TYPES.JSXElement) {
|
|
315
|
+
parent.closingElement = null;
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
return false;
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
// Process for multiple tag
|
|
324
|
+
const parent = parents[0];
|
|
325
|
+
if (((0, astro_1.isTag)(node) || node.type === "comment") &&
|
|
326
|
+
parent.type === "expression") {
|
|
327
|
+
const index = parent.children.indexOf(node);
|
|
328
|
+
const after = parent.children[index + 1];
|
|
329
|
+
if (!after || (!(0, astro_1.isTag)(after) && after.type !== "comment")) {
|
|
330
|
+
const before = parent.children[index - 1];
|
|
331
|
+
if (before &&
|
|
332
|
+
((0, astro_1.isTag)(before) || before.type === "comment")) {
|
|
333
|
+
const end = (0, astro_1.isTag)(node)
|
|
334
|
+
? (0, astro_1.getTagEndOffset)(node, parents, ctx)
|
|
335
|
+
: (0, astro_1.getCommentEndOffset)(node, ctx);
|
|
336
|
+
script.appendOriginal(end);
|
|
337
|
+
script.appendScript("</>");
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
235
341
|
});
|
|
236
342
|
script.appendOriginal(ctx.code.length);
|
|
237
343
|
script.appendScript("</>");
|
|
@@ -249,36 +355,3 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
249
355
|
}
|
|
250
356
|
}
|
|
251
357
|
exports.processTemplate = processTemplate;
|
|
252
|
-
/**
|
|
253
|
-
* If the given tag is a void tag, get the self-closing tag.
|
|
254
|
-
*/
|
|
255
|
-
function getVoidSelfClosingTag(node, parent, ctx) {
|
|
256
|
-
var _a;
|
|
257
|
-
const children = node.children.filter((c) => c.type !== "text" || c.value.trim());
|
|
258
|
-
if (children.length > 0) {
|
|
259
|
-
return false;
|
|
260
|
-
}
|
|
261
|
-
const code = ctx.code;
|
|
262
|
-
let nextElementIndex = code.length;
|
|
263
|
-
const childIndex = parent.children.indexOf(node);
|
|
264
|
-
if (childIndex === parent.children.length - 1) {
|
|
265
|
-
// last
|
|
266
|
-
if ((_a = parent.position) === null || _a === void 0 ? void 0 : _a.end) {
|
|
267
|
-
nextElementIndex = parent.position.end.offset;
|
|
268
|
-
nextElementIndex = code.lastIndexOf("</", nextElementIndex);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
else {
|
|
272
|
-
const next = parent.children[childIndex + 1];
|
|
273
|
-
nextElementIndex = next.position.start.offset;
|
|
274
|
-
}
|
|
275
|
-
const endOffset = (0, astro_1.getStartTagEndOffset)(node, ctx);
|
|
276
|
-
if (code.slice(endOffset, nextElementIndex).trim()) {
|
|
277
|
-
// has end tag
|
|
278
|
-
return null;
|
|
279
|
-
}
|
|
280
|
-
return {
|
|
281
|
-
offset: endOffset,
|
|
282
|
-
end: code.slice(endOffset - 2, endOffset) === "/>" ? "/>" : ">",
|
|
283
|
-
};
|
|
284
|
-
}
|
package/lib/visitor-keys.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-eslint-parser",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"description": "Astro parser for ESLint",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
|
|
21
21
|
"preversion": "npm run lint && npm test",
|
|
22
22
|
"update-fixtures": "ts-node --transpile-only ./tools/update-fixtures.ts",
|
|
23
|
+
"debug-parser": "ts-node --transpile-only ./tools/parser-test.ts",
|
|
23
24
|
"eslint-playground": "eslint tests/fixtures --ext .astro --config .eslintrc-for-playground.js --format codeframe",
|
|
24
25
|
"benchmark": "ts-node --transpile-only benchmark/index.ts"
|
|
25
26
|
},
|