astro-eslint-parser 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/context/script.d.ts +0 -5
- package/lib/context/script.js +1 -207
- package/lib/parser/index.js +2 -2
- package/lib/parser/process-template.d.ts +7 -0
- package/lib/parser/process-template.js +211 -0
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -135,7 +135,7 @@ Example **.vscode/settings.json**:
|
|
|
135
135
|
|
|
136
136
|
## Usage for Custom Rules / Plugins
|
|
137
137
|
|
|
138
|
-
-
|
|
138
|
+
- TBA
|
|
139
139
|
|
|
140
140
|
<!-- - [AST.md](./docs/AST.md) is AST specification. You can check it on the [Online DEMO](https://ota-meshi.github.io/astro-eslint-parser/). -->
|
|
141
141
|
<!-- - I have already [implemented some rules] in the [`@ota-meshi/eslint-plugin-astro`]. The source code for these rules will be helpful to you. -->
|
package/lib/context/script.d.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
import type { ParseResult } from "@astrojs/compiler";
|
|
2
1
|
import type { Context } from ".";
|
|
3
2
|
import type { ESLintExtendedProgram } from "../parser";
|
|
4
3
|
import type { TSESTree } from "@typescript-eslint/types";
|
|
5
|
-
/**
|
|
6
|
-
* Process the template to generate a ScriptContext.
|
|
7
|
-
*/
|
|
8
|
-
export declare function processTemplate(ctx: Context, resultTemplate: ParseResult): ScriptContext;
|
|
9
4
|
export declare class ScriptContext {
|
|
10
5
|
private readonly ctx;
|
|
11
6
|
script: string;
|
package/lib/context/script.js
CHANGED
|
@@ -1,214 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ScriptContext =
|
|
3
|
+
exports.ScriptContext = void 0;
|
|
4
4
|
const traverse_1 = require("../traverse");
|
|
5
|
-
const types_1 = require("@typescript-eslint/types");
|
|
6
5
|
const errors_1 = require("../errors");
|
|
7
|
-
const astro_1 = require("../astro");
|
|
8
|
-
/**
|
|
9
|
-
* Process the template to generate a ScriptContext.
|
|
10
|
-
*/
|
|
11
|
-
function processTemplate(ctx, resultTemplate) {
|
|
12
|
-
const script = new ScriptContext(ctx);
|
|
13
|
-
const frontmatter = resultTemplate.ast.children.find((n) => n.type === "frontmatter");
|
|
14
|
-
let fragmentOpened = false;
|
|
15
|
-
if (!frontmatter) {
|
|
16
|
-
script.appendScript("<>");
|
|
17
|
-
fragmentOpened = true;
|
|
18
|
-
}
|
|
19
|
-
(0, astro_1.walkElements)(resultTemplate.ast, (node, parent) => {
|
|
20
|
-
if (node.type === "frontmatter") {
|
|
21
|
-
const start = node.position.start.offset;
|
|
22
|
-
script.appendOriginal(start);
|
|
23
|
-
script.skipOriginalOffset(3);
|
|
24
|
-
const end = node.position.end.offset;
|
|
25
|
-
script.appendOriginal(end - 3);
|
|
26
|
-
script.appendScript(";<>");
|
|
27
|
-
fragmentOpened = true;
|
|
28
|
-
script.skipOriginalOffset(3);
|
|
29
|
-
script.addRestoreNodeProcess((_scriptNode, result) => {
|
|
30
|
-
for (let index = 0; index < result.ast.body.length; index++) {
|
|
31
|
-
const st = result.ast.body[index];
|
|
32
|
-
if (st.type === types_1.AST_NODE_TYPES.EmptyStatement) {
|
|
33
|
-
if (st.range[0] === end - 3 && st.range[1] === end) {
|
|
34
|
-
result.ast.body.splice(index, 1);
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return true;
|
|
40
|
-
});
|
|
41
|
-
script.addToken(types_1.AST_TOKEN_TYPES.Punctuator, [
|
|
42
|
-
node.position.start.offset,
|
|
43
|
-
node.position.start.offset + 3,
|
|
44
|
-
]);
|
|
45
|
-
script.addToken(types_1.AST_TOKEN_TYPES.Punctuator, [end - 3, end]);
|
|
46
|
-
}
|
|
47
|
-
else if ((0, astro_1.isTag)(node)) {
|
|
48
|
-
for (const attr of node.attributes) {
|
|
49
|
-
if (attr.kind === "shorthand") {
|
|
50
|
-
const start = attr.position.start.offset;
|
|
51
|
-
script.appendOriginal(start);
|
|
52
|
-
script.appendScript(`${attr.name}=`);
|
|
53
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
54
|
-
if (scriptNode.type === types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
55
|
-
scriptNode.range[0] === start) {
|
|
56
|
-
const attrNode = scriptNode;
|
|
57
|
-
attrNode.type = "AstroShorthandAttribute";
|
|
58
|
-
const locs = ctx.getLocations(...attrNode.value.expression.range);
|
|
59
|
-
attrNode.name.range = locs.range;
|
|
60
|
-
attrNode.name.loc = locs.loc;
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
return false;
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
else if (attr.kind === "template-literal") {
|
|
67
|
-
const start = (0, astro_1.getAttributeValueStartOffset)(attr, ctx.code);
|
|
68
|
-
const end = (0, astro_1.getAttributeEndOffset)(attr, ctx.code);
|
|
69
|
-
script.appendOriginal(start);
|
|
70
|
-
script.appendScript("{");
|
|
71
|
-
script.appendOriginal(end);
|
|
72
|
-
script.appendScript("}");
|
|
73
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
74
|
-
if (scriptNode.type === types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
75
|
-
scriptNode.range[0] === start) {
|
|
76
|
-
const attrNode = scriptNode;
|
|
77
|
-
attrNode.type = "AstroTemplateLiteralAttribute";
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
return false;
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
const end = getVoidSelfClosingTag(node, parent, ctx);
|
|
85
|
-
if (end && end.end === ">") {
|
|
86
|
-
script.appendOriginal(end.offset - 1);
|
|
87
|
-
script.appendScript("/");
|
|
88
|
-
}
|
|
89
|
-
if (node.name === "script" || node.name === "style") {
|
|
90
|
-
const text = node.children[0];
|
|
91
|
-
if (text && text.type === "text") {
|
|
92
|
-
const styleNodeStart = node.position.start.offset;
|
|
93
|
-
const start = text.position.start.offset;
|
|
94
|
-
script.appendOriginal(start);
|
|
95
|
-
script.skipOriginalOffset(text.value.length);
|
|
96
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
97
|
-
if (scriptNode.type === types_1.AST_NODE_TYPES.JSXElement &&
|
|
98
|
-
scriptNode.range[0] === styleNodeStart) {
|
|
99
|
-
const textNode = Object.assign({ type: types_1.AST_NODE_TYPES.JSXText, value: text.value, raw: text.value, parent: scriptNode }, ctx.getLocations(start, start + text.value.length));
|
|
100
|
-
scriptNode.children = [textNode];
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
return false;
|
|
104
|
-
});
|
|
105
|
-
script.addToken(types_1.AST_TOKEN_TYPES.JSXText, [
|
|
106
|
-
start,
|
|
107
|
-
start + text.value.length,
|
|
108
|
-
]);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
else if (node.type === "comment") {
|
|
113
|
-
const start = node.position.start.offset;
|
|
114
|
-
const length = 4 + node.value.length + 3;
|
|
115
|
-
script.appendOriginal(start);
|
|
116
|
-
let targetType;
|
|
117
|
-
if (fragmentOpened) {
|
|
118
|
-
script.appendScript(`<></>`);
|
|
119
|
-
targetType = types_1.AST_NODE_TYPES.JSXFragment;
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
script.appendScript(`0;`);
|
|
123
|
-
targetType = types_1.AST_NODE_TYPES.ExpressionStatement;
|
|
124
|
-
}
|
|
125
|
-
script.skipOriginalOffset(length);
|
|
126
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
127
|
-
if (scriptNode.range[0] === start &&
|
|
128
|
-
scriptNode.type === targetType) {
|
|
129
|
-
delete scriptNode.children;
|
|
130
|
-
delete scriptNode.openingFragment;
|
|
131
|
-
delete scriptNode.closingFragment;
|
|
132
|
-
delete scriptNode.expression;
|
|
133
|
-
const commentNode = scriptNode;
|
|
134
|
-
commentNode.type = "AstroHTMLComment";
|
|
135
|
-
commentNode.value = node.value;
|
|
136
|
-
return true;
|
|
137
|
-
}
|
|
138
|
-
return false;
|
|
139
|
-
});
|
|
140
|
-
script.addToken("HTMLComment", [
|
|
141
|
-
start,
|
|
142
|
-
start + length,
|
|
143
|
-
]);
|
|
144
|
-
}
|
|
145
|
-
else if (node.type === "doctype") {
|
|
146
|
-
const start = node.position.start.offset;
|
|
147
|
-
const end = node.position.end.offset;
|
|
148
|
-
script.appendOriginal(start);
|
|
149
|
-
let targetType;
|
|
150
|
-
if (fragmentOpened) {
|
|
151
|
-
script.appendScript(`<></>`);
|
|
152
|
-
targetType = types_1.AST_NODE_TYPES.JSXFragment;
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
script.appendScript(`0;`);
|
|
156
|
-
targetType = types_1.AST_NODE_TYPES.ExpressionStatement;
|
|
157
|
-
}
|
|
158
|
-
script.skipOriginalOffset(end - start);
|
|
159
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
160
|
-
if (scriptNode.range[0] === start &&
|
|
161
|
-
scriptNode.type === targetType) {
|
|
162
|
-
delete scriptNode.children;
|
|
163
|
-
delete scriptNode.openingFragment;
|
|
164
|
-
delete scriptNode.closingFragment;
|
|
165
|
-
delete scriptNode.expression;
|
|
166
|
-
const doctypeNode = scriptNode;
|
|
167
|
-
doctypeNode.type = "AstroDoctype";
|
|
168
|
-
return true;
|
|
169
|
-
}
|
|
170
|
-
return false;
|
|
171
|
-
});
|
|
172
|
-
script.addToken("HTMLDocType", [start, end]);
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
script.appendOriginal(ctx.code.length);
|
|
176
|
-
script.appendScript("</>");
|
|
177
|
-
return script;
|
|
178
|
-
}
|
|
179
|
-
exports.processTemplate = processTemplate;
|
|
180
|
-
/**
|
|
181
|
-
* If the given tag is a void tag, get the self-closing tag.
|
|
182
|
-
*/
|
|
183
|
-
function getVoidSelfClosingTag(node, parent, ctx) {
|
|
184
|
-
if (node.type === "fragment") {
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
if (node.children.length > 0) {
|
|
188
|
-
return false;
|
|
189
|
-
}
|
|
190
|
-
const code = ctx.code;
|
|
191
|
-
let nextElementIndex = code.length;
|
|
192
|
-
const childIndex = parent.children.indexOf(node);
|
|
193
|
-
if (childIndex === parent.children.length - 1) {
|
|
194
|
-
// last
|
|
195
|
-
nextElementIndex = parent.position.end.offset;
|
|
196
|
-
nextElementIndex = code.lastIndexOf("</", nextElementIndex);
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
199
|
-
const next = parent.children[childIndex + 1];
|
|
200
|
-
nextElementIndex = next.position.start.offset;
|
|
201
|
-
}
|
|
202
|
-
const endOffset = (0, astro_1.getStartTagEndOffset)(node, code);
|
|
203
|
-
if (code.slice(endOffset, nextElementIndex).trim()) {
|
|
204
|
-
// has end tag
|
|
205
|
-
return null;
|
|
206
|
-
}
|
|
207
|
-
return {
|
|
208
|
-
offset: endOffset,
|
|
209
|
-
end: code.slice(endOffset - 2, endOffset) === "/>" ? "/>" : ">",
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
6
|
class ScriptContext {
|
|
213
7
|
constructor(ctx) {
|
|
214
8
|
this.script = "";
|
package/lib/parser/index.js
CHANGED
|
@@ -8,7 +8,7 @@ const script_1 = require("./script");
|
|
|
8
8
|
const sort_1 = require("./sort");
|
|
9
9
|
const errors_1 = require("../errors");
|
|
10
10
|
const parse_1 = require("./astro-parser/parse");
|
|
11
|
-
const
|
|
11
|
+
const process_template_1 = require("./process-template");
|
|
12
12
|
/**
|
|
13
13
|
* Parse source code
|
|
14
14
|
*/
|
|
@@ -21,7 +21,7 @@ function parseForESLint(code, options) {
|
|
|
21
21
|
}
|
|
22
22
|
const ctx = new context_1.Context(code, parserOptions);
|
|
23
23
|
const resultTemplate = parseTemplate(ctx.code, ctx);
|
|
24
|
-
const scriptContext = (0,
|
|
24
|
+
const scriptContext = (0, process_template_1.processTemplate)(ctx, resultTemplate);
|
|
25
25
|
const resultScript = (0, script_1.parseScript)(scriptContext.script, ctx);
|
|
26
26
|
scriptContext.restore(resultScript);
|
|
27
27
|
(0, sort_1.sort)(resultScript.ast.comments);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ParseResult } from "@astrojs/compiler";
|
|
2
|
+
import type { Context } from "../context";
|
|
3
|
+
import { ScriptContext } from "../context/script";
|
|
4
|
+
/**
|
|
5
|
+
* Process the template to generate a ScriptContext.
|
|
6
|
+
*/
|
|
7
|
+
export declare function processTemplate(ctx: Context, resultTemplate: ParseResult): ScriptContext;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processTemplate = void 0;
|
|
4
|
+
const types_1 = require("@typescript-eslint/types");
|
|
5
|
+
const astro_1 = require("../astro");
|
|
6
|
+
const script_1 = require("../context/script");
|
|
7
|
+
/**
|
|
8
|
+
* Process the template to generate a ScriptContext.
|
|
9
|
+
*/
|
|
10
|
+
function processTemplate(ctx, resultTemplate) {
|
|
11
|
+
const script = new script_1.ScriptContext(ctx);
|
|
12
|
+
const frontmatter = resultTemplate.ast.children.find((n) => n.type === "frontmatter");
|
|
13
|
+
let fragmentOpened = false;
|
|
14
|
+
if (!frontmatter) {
|
|
15
|
+
script.appendScript("<>");
|
|
16
|
+
fragmentOpened = true;
|
|
17
|
+
}
|
|
18
|
+
(0, astro_1.walkElements)(resultTemplate.ast, (node, parent) => {
|
|
19
|
+
if (node.type === "frontmatter") {
|
|
20
|
+
const start = node.position.start.offset;
|
|
21
|
+
script.appendOriginal(start);
|
|
22
|
+
script.skipOriginalOffset(3);
|
|
23
|
+
const end = node.position.end.offset;
|
|
24
|
+
script.appendOriginal(end - 3);
|
|
25
|
+
script.appendScript(";<>");
|
|
26
|
+
fragmentOpened = true;
|
|
27
|
+
script.skipOriginalOffset(3);
|
|
28
|
+
script.addRestoreNodeProcess((_scriptNode, result) => {
|
|
29
|
+
for (let index = 0; index < result.ast.body.length; index++) {
|
|
30
|
+
const st = result.ast.body[index];
|
|
31
|
+
if (st.type === types_1.AST_NODE_TYPES.EmptyStatement) {
|
|
32
|
+
if (st.range[0] === end - 3 && st.range[1] === end) {
|
|
33
|
+
result.ast.body.splice(index, 1);
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
});
|
|
40
|
+
script.addToken(types_1.AST_TOKEN_TYPES.Punctuator, [
|
|
41
|
+
node.position.start.offset,
|
|
42
|
+
node.position.start.offset + 3,
|
|
43
|
+
]);
|
|
44
|
+
script.addToken(types_1.AST_TOKEN_TYPES.Punctuator, [end - 3, end]);
|
|
45
|
+
}
|
|
46
|
+
else if ((0, astro_1.isTag)(node)) {
|
|
47
|
+
for (const attr of node.attributes) {
|
|
48
|
+
if (attr.kind === "shorthand") {
|
|
49
|
+
const start = attr.position.start.offset;
|
|
50
|
+
script.appendOriginal(start);
|
|
51
|
+
script.appendScript(`${attr.name}=`);
|
|
52
|
+
script.addRestoreNodeProcess((scriptNode) => {
|
|
53
|
+
if (scriptNode.type === types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
54
|
+
scriptNode.range[0] === start) {
|
|
55
|
+
const attrNode = scriptNode;
|
|
56
|
+
attrNode.type = "AstroShorthandAttribute";
|
|
57
|
+
const locs = ctx.getLocations(...attrNode.value.expression.range);
|
|
58
|
+
attrNode.name.range = locs.range;
|
|
59
|
+
attrNode.name.loc = locs.loc;
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else if (attr.kind === "template-literal") {
|
|
66
|
+
const attrStart = attr.position.start.offset;
|
|
67
|
+
const start = (0, astro_1.getAttributeValueStartOffset)(attr, ctx.code);
|
|
68
|
+
const end = (0, astro_1.getAttributeEndOffset)(attr, ctx.code);
|
|
69
|
+
script.appendOriginal(start);
|
|
70
|
+
script.appendScript("{");
|
|
71
|
+
script.appendOriginal(end);
|
|
72
|
+
script.appendScript("}");
|
|
73
|
+
script.addRestoreNodeProcess((scriptNode) => {
|
|
74
|
+
if (scriptNode.type === types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
75
|
+
scriptNode.range[0] === attrStart) {
|
|
76
|
+
const attrNode = scriptNode;
|
|
77
|
+
attrNode.type = "AstroTemplateLiteralAttribute";
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const end = getVoidSelfClosingTag(node, parent, ctx);
|
|
85
|
+
if (end && end.end === ">") {
|
|
86
|
+
script.appendOriginal(end.offset - 1);
|
|
87
|
+
script.appendScript("/");
|
|
88
|
+
}
|
|
89
|
+
if (node.name === "script" || node.name === "style") {
|
|
90
|
+
const text = node.children[0];
|
|
91
|
+
if (text && text.type === "text") {
|
|
92
|
+
const styleNodeStart = node.position.start.offset;
|
|
93
|
+
const start = text.position.start.offset;
|
|
94
|
+
script.appendOriginal(start);
|
|
95
|
+
script.skipOriginalOffset(text.value.length);
|
|
96
|
+
script.addRestoreNodeProcess((scriptNode) => {
|
|
97
|
+
if (scriptNode.type === types_1.AST_NODE_TYPES.JSXElement &&
|
|
98
|
+
scriptNode.range[0] === styleNodeStart) {
|
|
99
|
+
const textNode = Object.assign({ type: types_1.AST_NODE_TYPES.JSXText, value: text.value, raw: text.value, parent: scriptNode }, ctx.getLocations(start, start + text.value.length));
|
|
100
|
+
scriptNode.children = [textNode];
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
104
|
+
});
|
|
105
|
+
script.addToken(types_1.AST_TOKEN_TYPES.JSXText, [
|
|
106
|
+
start,
|
|
107
|
+
start + text.value.length,
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else if (node.type === "comment") {
|
|
113
|
+
const start = node.position.start.offset;
|
|
114
|
+
const length = 4 + node.value.length + 3;
|
|
115
|
+
script.appendOriginal(start);
|
|
116
|
+
let targetType;
|
|
117
|
+
if (fragmentOpened) {
|
|
118
|
+
script.appendScript(`<></>`);
|
|
119
|
+
targetType = types_1.AST_NODE_TYPES.JSXFragment;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
script.appendScript(`0;`);
|
|
123
|
+
targetType = types_1.AST_NODE_TYPES.ExpressionStatement;
|
|
124
|
+
}
|
|
125
|
+
script.skipOriginalOffset(length);
|
|
126
|
+
script.addRestoreNodeProcess((scriptNode) => {
|
|
127
|
+
if (scriptNode.range[0] === start &&
|
|
128
|
+
scriptNode.type === targetType) {
|
|
129
|
+
delete scriptNode.children;
|
|
130
|
+
delete scriptNode.openingFragment;
|
|
131
|
+
delete scriptNode.closingFragment;
|
|
132
|
+
delete scriptNode.expression;
|
|
133
|
+
const commentNode = scriptNode;
|
|
134
|
+
commentNode.type = "AstroHTMLComment";
|
|
135
|
+
commentNode.value = node.value;
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
});
|
|
140
|
+
script.addToken("HTMLComment", [
|
|
141
|
+
start,
|
|
142
|
+
start + length,
|
|
143
|
+
]);
|
|
144
|
+
}
|
|
145
|
+
else if (node.type === "doctype") {
|
|
146
|
+
const start = node.position.start.offset;
|
|
147
|
+
const end = node.position.end.offset;
|
|
148
|
+
script.appendOriginal(start);
|
|
149
|
+
let targetType;
|
|
150
|
+
if (fragmentOpened) {
|
|
151
|
+
script.appendScript(`<></>`);
|
|
152
|
+
targetType = types_1.AST_NODE_TYPES.JSXFragment;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
script.appendScript(`0;`);
|
|
156
|
+
targetType = types_1.AST_NODE_TYPES.ExpressionStatement;
|
|
157
|
+
}
|
|
158
|
+
script.skipOriginalOffset(end - start);
|
|
159
|
+
script.addRestoreNodeProcess((scriptNode) => {
|
|
160
|
+
if (scriptNode.range[0] === start &&
|
|
161
|
+
scriptNode.type === targetType) {
|
|
162
|
+
delete scriptNode.children;
|
|
163
|
+
delete scriptNode.openingFragment;
|
|
164
|
+
delete scriptNode.closingFragment;
|
|
165
|
+
delete scriptNode.expression;
|
|
166
|
+
const doctypeNode = scriptNode;
|
|
167
|
+
doctypeNode.type = "AstroDoctype";
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
return false;
|
|
171
|
+
});
|
|
172
|
+
script.addToken("HTMLDocType", [start, end]);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
script.appendOriginal(ctx.code.length);
|
|
176
|
+
script.appendScript("</>");
|
|
177
|
+
return script;
|
|
178
|
+
}
|
|
179
|
+
exports.processTemplate = processTemplate;
|
|
180
|
+
/**
|
|
181
|
+
* If the given tag is a void tag, get the self-closing tag.
|
|
182
|
+
*/
|
|
183
|
+
function getVoidSelfClosingTag(node, parent, ctx) {
|
|
184
|
+
if (node.type === "fragment") {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
if (node.children.length > 0) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
const code = ctx.code;
|
|
191
|
+
let nextElementIndex = code.length;
|
|
192
|
+
const childIndex = parent.children.indexOf(node);
|
|
193
|
+
if (childIndex === parent.children.length - 1) {
|
|
194
|
+
// last
|
|
195
|
+
nextElementIndex = parent.position.end.offset;
|
|
196
|
+
nextElementIndex = code.lastIndexOf("</", nextElementIndex);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
const next = parent.children[childIndex + 1];
|
|
200
|
+
nextElementIndex = next.position.start.offset;
|
|
201
|
+
}
|
|
202
|
+
const endOffset = (0, astro_1.getStartTagEndOffset)(node, code);
|
|
203
|
+
if (code.slice(endOffset, nextElementIndex).trim()) {
|
|
204
|
+
// has end tag
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
offset: endOffset,
|
|
209
|
+
end: code.slice(endOffset - 2, endOffset) === "/>" ? "/>" : ">",
|
|
210
|
+
};
|
|
211
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-eslint-parser",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1",
|
|
4
4
|
"description": "Astro parser for ESLint",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib"
|
|
8
8
|
],
|
|
9
9
|
"engines": {
|
|
10
|
-
"node": "^
|
|
10
|
+
"node": "^14.17.0 || >=16.0.0"
|
|
11
11
|
},
|
|
12
12
|
"scripts": {
|
|
13
13
|
"prebuild": "npm run -s clean",
|
|
@@ -43,7 +43,6 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@astrojs/compiler": "^0.14.2",
|
|
45
45
|
"debug": "^4.3.4",
|
|
46
|
-
"eslint-scope": "^7.0.0",
|
|
47
46
|
"eslint-visitor-keys": "^3.0.0",
|
|
48
47
|
"espree": "^9.0.0"
|
|
49
48
|
},
|