astro-eslint-parser 0.0.5 → 0.0.8
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/astro/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AttributeNode, CommentNode, Node, ParentNode, TagLikeNode } from "@astrojs/compiler/types";
|
|
2
|
+
import type { Context } from "../context";
|
|
2
3
|
/**
|
|
3
4
|
* Checks if the given node is TagLikeNode
|
|
4
5
|
*/
|
|
@@ -14,19 +15,19 @@ export declare function walk(parent: ParentNode, code: string, enter: (n: Node |
|
|
|
14
15
|
/**
|
|
15
16
|
* Get end offset of start tag
|
|
16
17
|
*/
|
|
17
|
-
export declare function getStartTagEndOffset(node: TagLikeNode,
|
|
18
|
+
export declare function getStartTagEndOffset(node: TagLikeNode, ctx: Context): number;
|
|
18
19
|
/**
|
|
19
20
|
* Get end offset of attribute
|
|
20
21
|
*/
|
|
21
|
-
export declare function getAttributeEndOffset(node: AttributeNode,
|
|
22
|
+
export declare function getAttributeEndOffset(node: AttributeNode, ctx: Context): number;
|
|
22
23
|
/**
|
|
23
24
|
* Get start offset of attribute value
|
|
24
25
|
*/
|
|
25
|
-
export declare function getAttributeValueStartOffset(node: AttributeNode,
|
|
26
|
+
export declare function getAttributeValueStartOffset(node: AttributeNode, ctx: Context): number;
|
|
26
27
|
/**
|
|
27
28
|
* Get end offset of comment
|
|
28
29
|
*/
|
|
29
|
-
export declare function getCommentEndOffset(node: CommentNode,
|
|
30
|
+
export declare function getCommentEndOffset(node: CommentNode, ctx: Context): number;
|
|
30
31
|
/**
|
|
31
32
|
* Skip spaces
|
|
32
33
|
*/
|
package/lib/astro/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.skipSpaces = exports.getCommentEndOffset = exports.getAttributeValueStartOffset = exports.getAttributeEndOffset = exports.getStartTagEndOffset = exports.walk = exports.walkElements = exports.isParent = exports.isTag = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
4
5
|
/**
|
|
5
6
|
* Checks if the given node is TagLikeNode
|
|
6
7
|
*/
|
|
@@ -50,45 +51,45 @@ exports.walk = walk;
|
|
|
50
51
|
/**
|
|
51
52
|
* Get end offset of start tag
|
|
52
53
|
*/
|
|
53
|
-
function getStartTagEndOffset(node,
|
|
54
|
+
function getStartTagEndOffset(node, ctx) {
|
|
54
55
|
const lastAttr = node.attributes[node.attributes.length - 1];
|
|
55
56
|
let beforeCloseIndex;
|
|
56
57
|
if (lastAttr) {
|
|
57
|
-
beforeCloseIndex = getAttributeEndOffset(lastAttr,
|
|
58
|
+
beforeCloseIndex = getAttributeEndOffset(lastAttr, ctx);
|
|
58
59
|
}
|
|
59
60
|
else {
|
|
60
|
-
const info = getTokenInfo(
|
|
61
|
+
const info = getTokenInfo(ctx, [`<${node.name}`], node.position.start.offset);
|
|
61
62
|
beforeCloseIndex = info.index + info.match.length;
|
|
62
63
|
}
|
|
63
|
-
const info = getTokenInfo(
|
|
64
|
+
const info = getTokenInfo(ctx, [[">", "/>"]], beforeCloseIndex);
|
|
64
65
|
return info.index + info.match.length;
|
|
65
66
|
}
|
|
66
67
|
exports.getStartTagEndOffset = getStartTagEndOffset;
|
|
67
68
|
/**
|
|
68
69
|
* Get end offset of attribute
|
|
69
70
|
*/
|
|
70
|
-
function getAttributeEndOffset(node,
|
|
71
|
+
function getAttributeEndOffset(node, ctx) {
|
|
71
72
|
let info;
|
|
72
73
|
if (node.kind === "empty") {
|
|
73
|
-
info = getTokenInfo(
|
|
74
|
+
info = getTokenInfo(ctx, [node.name], node.position.start.offset);
|
|
74
75
|
}
|
|
75
76
|
else if (node.kind === "quoted") {
|
|
76
|
-
info = getTokenInfo(
|
|
77
|
+
info = getTokenInfo(ctx, [[`"${node.value}"`, `'${node.value}'`, node.value]], getAttributeValueStartOffset(node, ctx));
|
|
77
78
|
}
|
|
78
79
|
else if (node.kind === "expression") {
|
|
79
|
-
info = getTokenInfo(
|
|
80
|
+
info = getTokenInfo(ctx, ["{", node.value, "}"], getAttributeValueStartOffset(node, ctx));
|
|
80
81
|
}
|
|
81
82
|
else if (node.kind === "shorthand") {
|
|
82
|
-
info = getTokenInfo(
|
|
83
|
+
info = getTokenInfo(ctx, ["{", node.name, "}"], node.position.start.offset);
|
|
83
84
|
}
|
|
84
85
|
else if (node.kind === "spread") {
|
|
85
|
-
info = getTokenInfo(
|
|
86
|
+
info = getTokenInfo(ctx, ["{", "...", node.name, "}"], node.position.start.offset);
|
|
86
87
|
}
|
|
87
88
|
else if (node.kind === "template-literal") {
|
|
88
|
-
info = getTokenInfo(
|
|
89
|
+
info = getTokenInfo(ctx, [`\`${node.value}\``], getAttributeValueStartOffset(node, ctx));
|
|
89
90
|
}
|
|
90
91
|
else {
|
|
91
|
-
throw new
|
|
92
|
+
throw new errors_1.ParseError(`Unknown attr kind: ${node.kind}`, node.position.start.offset, ctx);
|
|
92
93
|
}
|
|
93
94
|
return info.index + info.match.length;
|
|
94
95
|
}
|
|
@@ -96,19 +97,19 @@ exports.getAttributeEndOffset = getAttributeEndOffset;
|
|
|
96
97
|
/**
|
|
97
98
|
* Get start offset of attribute value
|
|
98
99
|
*/
|
|
99
|
-
function getAttributeValueStartOffset(node,
|
|
100
|
+
function getAttributeValueStartOffset(node, ctx) {
|
|
100
101
|
let info;
|
|
101
102
|
if (node.kind === "quoted") {
|
|
102
|
-
info = getTokenInfo(
|
|
103
|
+
info = getTokenInfo(ctx, [node.name, "=", [`"`, `'`, node.value]], node.position.start.offset);
|
|
103
104
|
}
|
|
104
105
|
else if (node.kind === "expression") {
|
|
105
|
-
info = getTokenInfo(
|
|
106
|
+
info = getTokenInfo(ctx, [node.name, "=", "{"], node.position.start.offset);
|
|
106
107
|
}
|
|
107
108
|
else if (node.kind === "template-literal") {
|
|
108
|
-
info = getTokenInfo(
|
|
109
|
+
info = getTokenInfo(ctx, [node.name, "=", "`"], node.position.start.offset);
|
|
109
110
|
}
|
|
110
111
|
else {
|
|
111
|
-
throw new
|
|
112
|
+
throw new errors_1.ParseError(`Unknown attr kind: ${node.kind}`, node.position.start.offset, ctx);
|
|
112
113
|
}
|
|
113
114
|
return info.index;
|
|
114
115
|
}
|
|
@@ -116,15 +117,15 @@ exports.getAttributeValueStartOffset = getAttributeValueStartOffset;
|
|
|
116
117
|
/**
|
|
117
118
|
* Get end offset of comment
|
|
118
119
|
*/
|
|
119
|
-
function getCommentEndOffset(node,
|
|
120
|
-
const info = getTokenInfo(
|
|
120
|
+
function getCommentEndOffset(node, ctx) {
|
|
121
|
+
const info = getTokenInfo(ctx, ["<!--", node.value, "-->"], node.position.start.offset);
|
|
121
122
|
return info.index + info.match.length;
|
|
122
123
|
}
|
|
123
124
|
exports.getCommentEndOffset = getCommentEndOffset;
|
|
124
125
|
/**
|
|
125
126
|
* Get token info
|
|
126
127
|
*/
|
|
127
|
-
function getTokenInfo(
|
|
128
|
+
function getTokenInfo(ctx, tokens, position) {
|
|
128
129
|
let lastMatch;
|
|
129
130
|
for (const t of tokens) {
|
|
130
131
|
const index = lastMatch
|
|
@@ -134,7 +135,7 @@ function getTokenInfo(string, tokens, position) {
|
|
|
134
135
|
? matchOfStr(t, index)
|
|
135
136
|
: matchOfForMulti(t, index);
|
|
136
137
|
if (m == null) {
|
|
137
|
-
throw new
|
|
138
|
+
throw new errors_1.ParseError(`Unknown token at ${index}, expected: ${JSON.stringify(t)}, actual: ${JSON.stringify(ctx.code.slice(index, index + 10))}`, index, ctx);
|
|
138
139
|
}
|
|
139
140
|
lastMatch = m;
|
|
140
141
|
}
|
|
@@ -143,8 +144,8 @@ function getTokenInfo(string, tokens, position) {
|
|
|
143
144
|
* For string
|
|
144
145
|
*/
|
|
145
146
|
function matchOfStr(search, position) {
|
|
146
|
-
const index = search.trim() === search ? skipSpaces(
|
|
147
|
-
if (
|
|
147
|
+
const index = search.trim() === search ? skipSpaces(ctx.code, position) : position;
|
|
148
|
+
if (ctx.code.startsWith(search, index)) {
|
|
148
149
|
return {
|
|
149
150
|
match: search,
|
|
150
151
|
index,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ParseResult } from "@astrojs/compiler";
|
|
2
|
+
import type { Context } from "../../context";
|
|
2
3
|
/**
|
|
3
4
|
* Parse code by `@astrojs/compiler`
|
|
4
5
|
*/
|
|
5
|
-
export declare function parse(code: string): ParseResult;
|
|
6
|
+
export declare function parse(code: string, ctx: Context): ParseResult;
|
|
@@ -26,26 +26,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.parse = void 0;
|
|
27
27
|
const service = __importStar(require("./astrojs-compiler-service"));
|
|
28
28
|
const astro_1 = require("../../astro");
|
|
29
|
+
const errors_1 = require("../../errors");
|
|
29
30
|
/**
|
|
30
31
|
* Parse code by `@astrojs/compiler`
|
|
31
32
|
*/
|
|
32
|
-
function parse(code) {
|
|
33
|
+
function parse(code, ctx) {
|
|
33
34
|
const ast = service.parse(code, { position: true }).ast;
|
|
34
|
-
fixLocations(ast,
|
|
35
|
+
fixLocations(ast, ctx);
|
|
35
36
|
return { ast };
|
|
36
37
|
}
|
|
37
38
|
exports.parse = parse;
|
|
38
39
|
/**
|
|
39
40
|
* Fix locations
|
|
40
41
|
*/
|
|
41
|
-
function fixLocations(node,
|
|
42
|
+
function fixLocations(node, ctx) {
|
|
42
43
|
// FIXME: Adjust because the parser does not return the correct location.
|
|
43
44
|
let start = 0;
|
|
44
|
-
(0, astro_1.walk)(node, code, (node) => {
|
|
45
|
+
(0, astro_1.walk)(node, ctx.code, (node) => {
|
|
45
46
|
if (node.type === "frontmatter") {
|
|
46
|
-
start = node.position.start.offset = tokenIndex(
|
|
47
|
+
start = node.position.start.offset = tokenIndex(ctx, "---", start);
|
|
47
48
|
start = node.position.end.offset =
|
|
48
|
-
tokenIndex(
|
|
49
|
+
tokenIndex(ctx, "---", start + 3 + node.value.length) + 3;
|
|
49
50
|
}
|
|
50
51
|
else if (node.type === "fragment" ||
|
|
51
52
|
node.type === "element" ||
|
|
@@ -54,27 +55,27 @@ function fixLocations(node, code) {
|
|
|
54
55
|
if (!node.position) {
|
|
55
56
|
node.position = { start: {}, end: {} };
|
|
56
57
|
}
|
|
57
|
-
start = node.position.start.offset = tokenIndex(
|
|
58
|
+
start = node.position.start.offset = tokenIndex(ctx, "<", start);
|
|
58
59
|
start += 1;
|
|
59
60
|
start += node.name.length;
|
|
60
61
|
if (!node.attributes.length) {
|
|
61
|
-
start = (0, astro_1.getStartTagEndOffset)(node,
|
|
62
|
+
start = (0, astro_1.getStartTagEndOffset)(node, ctx);
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
else if (node.type === "attribute") {
|
|
65
|
-
fixLocationForAttr(node,
|
|
66
|
-
start = (0, astro_1.getAttributeEndOffset)(node,
|
|
66
|
+
fixLocationForAttr(node, ctx, start);
|
|
67
|
+
start = (0, astro_1.getAttributeEndOffset)(node, ctx);
|
|
67
68
|
}
|
|
68
69
|
else if (node.type === "comment") {
|
|
69
|
-
node.position.start.offset = tokenIndex(
|
|
70
|
-
start = (0, astro_1.getCommentEndOffset)(node,
|
|
70
|
+
node.position.start.offset = tokenIndex(ctx, "<!--", start);
|
|
71
|
+
start = (0, astro_1.getCommentEndOffset)(node, ctx);
|
|
71
72
|
}
|
|
72
73
|
else if (node.type === "text") {
|
|
73
|
-
start = node.position.start.offset = tokenIndex(
|
|
74
|
+
start = node.position.start.offset = tokenIndex(ctx, node.value, start);
|
|
74
75
|
start += node.value.length;
|
|
75
76
|
}
|
|
76
77
|
else if (node.type === "expression") {
|
|
77
|
-
start = node.position.start.offset = tokenIndex(
|
|
78
|
+
start = node.position.start.offset = tokenIndex(ctx, "{", start);
|
|
78
79
|
start += 1;
|
|
79
80
|
}
|
|
80
81
|
else if (node.type === "doctype") {
|
|
@@ -84,10 +85,10 @@ function fixLocations(node, code) {
|
|
|
84
85
|
if (!node.position.end) {
|
|
85
86
|
node.position.end = {};
|
|
86
87
|
}
|
|
87
|
-
start = node.position.start.offset = tokenIndex(
|
|
88
|
+
start = node.position.start.offset = tokenIndex(ctx, "<!", start);
|
|
88
89
|
start += 2;
|
|
89
90
|
start = node.position.end.offset =
|
|
90
|
-
code.indexOf(">", start) + 1;
|
|
91
|
+
ctx.code.indexOf(">", start) + 1;
|
|
91
92
|
}
|
|
92
93
|
else if (node.type === "root") {
|
|
93
94
|
// noop
|
|
@@ -96,24 +97,21 @@ function fixLocations(node, code) {
|
|
|
96
97
|
if (node.type === "attribute") {
|
|
97
98
|
const attributes = parent.attributes;
|
|
98
99
|
if (attributes[attributes.length - 1] === node) {
|
|
99
|
-
start = (0, astro_1.getStartTagEndOffset)(parent,
|
|
100
|
+
start = (0, astro_1.getStartTagEndOffset)(parent, ctx);
|
|
100
101
|
}
|
|
101
102
|
return;
|
|
102
103
|
}
|
|
103
104
|
if (node.type === "expression") {
|
|
104
|
-
start = tokenIndex(
|
|
105
|
+
start = tokenIndex(ctx, "}", start) + 1;
|
|
105
106
|
}
|
|
106
107
|
else if (node.type === "fragment" ||
|
|
107
108
|
node.type === "element" ||
|
|
108
109
|
node.type === "component" ||
|
|
109
110
|
node.type === "custom-element") {
|
|
110
|
-
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
const closeTagStart = tokenIndexSafe(code, `</${node.name}`, start);
|
|
111
|
+
const closeTagStart = tokenIndexSafe(ctx.code, `</${node.name}`, start);
|
|
114
112
|
if (closeTagStart != null) {
|
|
115
113
|
start = closeTagStart + 2 + node.name.length;
|
|
116
|
-
start = tokenIndex(
|
|
114
|
+
start = tokenIndex(ctx, ">", start) + 1;
|
|
117
115
|
}
|
|
118
116
|
}
|
|
119
117
|
else {
|
|
@@ -127,37 +125,37 @@ function fixLocations(node, code) {
|
|
|
127
125
|
/**
|
|
128
126
|
* Fix locations
|
|
129
127
|
*/
|
|
130
|
-
function fixLocationForAttr(node,
|
|
128
|
+
function fixLocationForAttr(node, ctx, start) {
|
|
131
129
|
if (node.kind === "empty") {
|
|
132
|
-
node.position.start.offset = tokenIndex(
|
|
130
|
+
node.position.start.offset = tokenIndex(ctx, node.name, start);
|
|
133
131
|
}
|
|
134
132
|
else if (node.kind === "quoted") {
|
|
135
|
-
node.position.start.offset = tokenIndex(
|
|
133
|
+
node.position.start.offset = tokenIndex(ctx, node.name, start);
|
|
136
134
|
}
|
|
137
135
|
else if (node.kind === "expression") {
|
|
138
|
-
node.position.start.offset = tokenIndex(
|
|
136
|
+
node.position.start.offset = tokenIndex(ctx, node.name, start);
|
|
139
137
|
}
|
|
140
138
|
else if (node.kind === "shorthand") {
|
|
141
|
-
node.position.start.offset = tokenIndex(
|
|
139
|
+
node.position.start.offset = tokenIndex(ctx, "{", start);
|
|
142
140
|
}
|
|
143
141
|
else if (node.kind === "spread") {
|
|
144
|
-
node.position.start.offset = tokenIndex(
|
|
142
|
+
node.position.start.offset = tokenIndex(ctx, "{", start);
|
|
145
143
|
}
|
|
146
144
|
else if (node.kind === "template-literal") {
|
|
147
|
-
node.position.start.offset = tokenIndex(
|
|
145
|
+
node.position.start.offset = tokenIndex(ctx, node.name, start);
|
|
148
146
|
}
|
|
149
147
|
else {
|
|
150
|
-
throw new
|
|
148
|
+
throw new errors_1.ParseError(`Unknown attr kind: ${node.kind}`, node.position.start.offset, ctx);
|
|
151
149
|
}
|
|
152
150
|
}
|
|
153
151
|
/**
|
|
154
152
|
* Get token index
|
|
155
153
|
*/
|
|
156
|
-
function tokenIndex(
|
|
157
|
-
const index = tokenIndexSafe(
|
|
154
|
+
function tokenIndex(ctx, token, position) {
|
|
155
|
+
const index = tokenIndexSafe(ctx.code, token, position);
|
|
158
156
|
if (index == null) {
|
|
159
|
-
const start = token.trim() === token ? (0, astro_1.skipSpaces)(
|
|
160
|
-
throw new
|
|
157
|
+
const start = token.trim() === token ? (0, astro_1.skipSpaces)(ctx.code, position) : position;
|
|
158
|
+
throw new errors_1.ParseError(`Unknown token at ${start}, expected: ${JSON.stringify(token)}, actual: ${JSON.stringify(ctx.code.slice(start, start + 10))}`, start, ctx);
|
|
161
159
|
}
|
|
162
160
|
return index;
|
|
163
161
|
}
|
package/lib/parser/index.js
CHANGED
|
@@ -8,6 +8,8 @@ const script_1 = require("../context/script");
|
|
|
8
8
|
* Process the template to generate a ScriptContext.
|
|
9
9
|
*/
|
|
10
10
|
function processTemplate(ctx, resultTemplate) {
|
|
11
|
+
let uniqueIdSeq = 0;
|
|
12
|
+
const usedUniqueIds = new Set();
|
|
11
13
|
const script = new script_1.ScriptContext(ctx);
|
|
12
14
|
const frontmatter = resultTemplate.ast.children.find((n) => n.type === "frontmatter");
|
|
13
15
|
let fragmentOpened = false;
|
|
@@ -99,13 +101,19 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
99
101
|
if (attr.kind === "shorthand") {
|
|
100
102
|
const start = attr.position.start.offset;
|
|
101
103
|
script.appendOriginal(start);
|
|
102
|
-
|
|
104
|
+
const jsxName = /[\s"'[\]{}]/u.test(attr.name)
|
|
105
|
+
? generateUniqueId(attr.name)
|
|
106
|
+
: attr.name;
|
|
107
|
+
script.appendScript(`${jsxName}=`);
|
|
103
108
|
script.addRestoreNodeProcess((scriptNode) => {
|
|
104
109
|
if (scriptNode.type === types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
105
110
|
scriptNode.range[0] === start) {
|
|
106
111
|
const attrNode = scriptNode;
|
|
107
112
|
attrNode.type = "AstroShorthandAttribute";
|
|
108
113
|
const locs = ctx.getLocations(...attrNode.value.expression.range);
|
|
114
|
+
if (jsxName !== attr.name) {
|
|
115
|
+
attrNode.name.name = attr.name;
|
|
116
|
+
}
|
|
109
117
|
attrNode.name.range = locs.range;
|
|
110
118
|
attrNode.name.loc = locs.loc;
|
|
111
119
|
return true;
|
|
@@ -115,8 +123,8 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
115
123
|
}
|
|
116
124
|
else if (attr.kind === "template-literal") {
|
|
117
125
|
const attrStart = attr.position.start.offset;
|
|
118
|
-
const start = (0, astro_1.getAttributeValueStartOffset)(attr, ctx
|
|
119
|
-
const end = (0, astro_1.getAttributeEndOffset)(attr, ctx
|
|
126
|
+
const start = (0, astro_1.getAttributeValueStartOffset)(attr, ctx);
|
|
127
|
+
const end = (0, astro_1.getAttributeEndOffset)(attr, ctx);
|
|
120
128
|
script.appendOriginal(start);
|
|
121
129
|
script.appendScript("{");
|
|
122
130
|
script.appendOriginal(end);
|
|
@@ -228,6 +236,17 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
228
236
|
script.appendOriginal(ctx.code.length);
|
|
229
237
|
script.appendScript("</>");
|
|
230
238
|
return script;
|
|
239
|
+
/**
|
|
240
|
+
* Generate unique id
|
|
241
|
+
*/
|
|
242
|
+
function generateUniqueId(base) {
|
|
243
|
+
let candidate = `$_${base.replace(/\W/g, "_")}${uniqueIdSeq++}`;
|
|
244
|
+
while (usedUniqueIds.has(candidate) || ctx.code.includes(candidate)) {
|
|
245
|
+
candidate = `$_${base.replace(/\W/g, "_")}${uniqueIdSeq++}`;
|
|
246
|
+
}
|
|
247
|
+
usedUniqueIds.add(candidate);
|
|
248
|
+
return candidate;
|
|
249
|
+
}
|
|
231
250
|
}
|
|
232
251
|
exports.processTemplate = processTemplate;
|
|
233
252
|
/**
|
|
@@ -253,7 +272,7 @@ function getVoidSelfClosingTag(node, parent, ctx) {
|
|
|
253
272
|
const next = parent.children[childIndex + 1];
|
|
254
273
|
nextElementIndex = next.position.start.offset;
|
|
255
274
|
}
|
|
256
|
-
const endOffset = (0, astro_1.getStartTagEndOffset)(node,
|
|
275
|
+
const endOffset = (0, astro_1.getStartTagEndOffset)(node, ctx);
|
|
257
276
|
if (code.slice(endOffset, nextElementIndex).trim()) {
|
|
258
277
|
// has end tag
|
|
259
278
|
return null;
|