astro-eslint-parser 0.0.6 → 0.0.7
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,21 +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
|
-
const closeTagStart = tokenIndexSafe(code, `</${node.name}`, start);
|
|
111
|
+
const closeTagStart = tokenIndexSafe(ctx.code, `</${node.name}`, start);
|
|
111
112
|
if (closeTagStart != null) {
|
|
112
113
|
start = closeTagStart + 2 + node.name.length;
|
|
113
|
-
start = tokenIndex(
|
|
114
|
+
start = tokenIndex(ctx, ">", start) + 1;
|
|
114
115
|
}
|
|
115
116
|
}
|
|
116
117
|
else {
|
|
@@ -124,37 +125,37 @@ function fixLocations(node, code) {
|
|
|
124
125
|
/**
|
|
125
126
|
* Fix locations
|
|
126
127
|
*/
|
|
127
|
-
function fixLocationForAttr(node,
|
|
128
|
+
function fixLocationForAttr(node, ctx, start) {
|
|
128
129
|
if (node.kind === "empty") {
|
|
129
|
-
node.position.start.offset = tokenIndex(
|
|
130
|
+
node.position.start.offset = tokenIndex(ctx, node.name, start);
|
|
130
131
|
}
|
|
131
132
|
else if (node.kind === "quoted") {
|
|
132
|
-
node.position.start.offset = tokenIndex(
|
|
133
|
+
node.position.start.offset = tokenIndex(ctx, node.name, start);
|
|
133
134
|
}
|
|
134
135
|
else if (node.kind === "expression") {
|
|
135
|
-
node.position.start.offset = tokenIndex(
|
|
136
|
+
node.position.start.offset = tokenIndex(ctx, node.name, start);
|
|
136
137
|
}
|
|
137
138
|
else if (node.kind === "shorthand") {
|
|
138
|
-
node.position.start.offset = tokenIndex(
|
|
139
|
+
node.position.start.offset = tokenIndex(ctx, "{", start);
|
|
139
140
|
}
|
|
140
141
|
else if (node.kind === "spread") {
|
|
141
|
-
node.position.start.offset = tokenIndex(
|
|
142
|
+
node.position.start.offset = tokenIndex(ctx, "{", start);
|
|
142
143
|
}
|
|
143
144
|
else if (node.kind === "template-literal") {
|
|
144
|
-
node.position.start.offset = tokenIndex(
|
|
145
|
+
node.position.start.offset = tokenIndex(ctx, node.name, start);
|
|
145
146
|
}
|
|
146
147
|
else {
|
|
147
|
-
throw new
|
|
148
|
+
throw new errors_1.ParseError(`Unknown attr kind: ${node.kind}`, node.position.start.offset, ctx);
|
|
148
149
|
}
|
|
149
150
|
}
|
|
150
151
|
/**
|
|
151
152
|
* Get token index
|
|
152
153
|
*/
|
|
153
|
-
function tokenIndex(
|
|
154
|
-
const index = tokenIndexSafe(
|
|
154
|
+
function tokenIndex(ctx, token, position) {
|
|
155
|
+
const index = tokenIndexSafe(ctx.code, token, position);
|
|
155
156
|
if (index == null) {
|
|
156
|
-
const start = token.trim() === token ? (0, astro_1.skipSpaces)(
|
|
157
|
-
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);
|
|
158
159
|
}
|
|
159
160
|
return index;
|
|
160
161
|
}
|
package/lib/parser/index.js
CHANGED
|
@@ -115,8 +115,8 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
115
115
|
}
|
|
116
116
|
else if (attr.kind === "template-literal") {
|
|
117
117
|
const attrStart = attr.position.start.offset;
|
|
118
|
-
const start = (0, astro_1.getAttributeValueStartOffset)(attr, ctx
|
|
119
|
-
const end = (0, astro_1.getAttributeEndOffset)(attr, ctx
|
|
118
|
+
const start = (0, astro_1.getAttributeValueStartOffset)(attr, ctx);
|
|
119
|
+
const end = (0, astro_1.getAttributeEndOffset)(attr, ctx);
|
|
120
120
|
script.appendOriginal(start);
|
|
121
121
|
script.appendScript("{");
|
|
122
122
|
script.appendOriginal(end);
|
|
@@ -253,7 +253,7 @@ function getVoidSelfClosingTag(node, parent, ctx) {
|
|
|
253
253
|
const next = parent.children[childIndex + 1];
|
|
254
254
|
nextElementIndex = next.position.start.offset;
|
|
255
255
|
}
|
|
256
|
-
const endOffset = (0, astro_1.getStartTagEndOffset)(node,
|
|
256
|
+
const endOffset = (0, astro_1.getStartTagEndOffset)(node, ctx);
|
|
257
257
|
if (code.slice(endOffset, nextElementIndex).trim()) {
|
|
258
258
|
// has end tag
|
|
259
259
|
return null;
|