astro-eslint-parser 0.0.1 → 0.0.2

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.
@@ -41,9 +41,7 @@ exports.parse = parse;
41
41
  function fixLocations(node, code) {
42
42
  // FIXME: Adjust because the parser does not return the correct location.
43
43
  let start = 0;
44
- (0, astro_1.walk)(node,
45
- // eslint-disable-next-line complexity -- ignore
46
- (node) => {
44
+ (0, astro_1.walk)(node, (node) => {
47
45
  if (node.type === "frontmatter") {
48
46
  start = node.position.start.offset = tokenIndex(code, "---", start);
49
47
  start = node.position.end.offset =
@@ -58,11 +56,7 @@ function fixLocations(node, code) {
58
56
  }
59
57
  start = node.position.start.offset = tokenIndex(code, "<", start);
60
58
  start += 1;
61
- if (node.type === "element" ||
62
- node.type === "component" ||
63
- node.type === "custom-element") {
64
- start += node.name.length;
65
- }
59
+ start += node.name.length;
66
60
  if (!node.attributes.length) {
67
61
  start = (0, astro_1.getStartTagEndOffset)(node, code);
68
62
  }
@@ -106,20 +100,18 @@ function fixLocations(node, code) {
106
100
  if (node.type === "expression") {
107
101
  start = tokenIndex(code, "}", start) + 1;
108
102
  }
109
- else if (node.type === "fragment") {
110
- start = tokenIndex(code, "</>", start) + 3;
111
- }
112
- else if (node.type === "element" ||
103
+ else if (node.type === "fragment" ||
104
+ node.type === "element" ||
113
105
  node.type === "component" ||
114
106
  node.type === "custom-element") {
115
107
  if (!node.position.end) {
116
108
  return;
117
109
  }
118
- start =
119
- tokenIndex(code, `</${node.name}`, start) +
120
- 2 +
121
- node.name.length;
122
- start = tokenIndex(code, ">", start) + 1;
110
+ const closeTagStart = tokenIndexSafe(code, `</${node.name}`, start);
111
+ if (closeTagStart != null) {
112
+ start = closeTagStart + 2 + node.name.length;
113
+ start = tokenIndex(code, ">", start) + 1;
114
+ }
123
115
  }
124
116
  else {
125
117
  return;
@@ -159,9 +151,20 @@ function fixLocationForAttr(node, code, start) {
159
151
  * Get token index
160
152
  */
161
153
  function tokenIndex(string, token, position) {
154
+ const index = tokenIndexSafe(string, token, position);
155
+ if (index == null) {
156
+ const start = token.trim() === token ? (0, astro_1.skipSpaces)(string, position) : position;
157
+ throw new Error(`Unknown token at ${start}, expected: ${JSON.stringify(token)}, actual: ${JSON.stringify(string.slice(start, start + 10))}`);
158
+ }
159
+ return index;
160
+ }
161
+ /**
162
+ * Get token index
163
+ */
164
+ function tokenIndexSafe(string, token, position) {
162
165
  const index = token.trim() === token ? (0, astro_1.skipSpaces)(string, position) : position;
163
166
  if (string.startsWith(token, index)) {
164
167
  return index;
165
168
  }
166
- throw new Error(`Unknown token at ${index}, expected: ${JSON.stringify(token)}, actual: ${JSON.stringify(string.slice(index, index + 10))}`);
169
+ return null;
167
170
  }
@@ -15,6 +15,7 @@ function processTemplate(ctx, resultTemplate) {
15
15
  script.appendScript("<>");
16
16
  fragmentOpened = true;
17
17
  }
18
+ // eslint-disable-next-line complexity -- X(
18
19
  (0, astro_1.walkElements)(resultTemplate.ast, (node, parent) => {
19
20
  if (node.type === "frontmatter") {
20
21
  const start = node.position.start.offset;
@@ -45,6 +46,56 @@ function processTemplate(ctx, resultTemplate) {
45
46
  }
46
47
  else if ((0, astro_1.isTag)(node)) {
47
48
  for (const attr of node.attributes) {
49
+ if ((node.type === "component" || node.type === "fragment") &&
50
+ (attr.kind === "quoted" ||
51
+ attr.kind === "empty" ||
52
+ attr.kind === "expression" ||
53
+ attr.kind === "template-literal")) {
54
+ const colonIndex = attr.name.indexOf(":");
55
+ if (colonIndex >= 0) {
56
+ const start = attr.position.start.offset;
57
+ script.appendOriginal(start + colonIndex);
58
+ script.skipOriginalOffset(1);
59
+ script.appendScript(`_`);
60
+ script.addToken(types_1.AST_TOKEN_TYPES.JSXIdentifier, [
61
+ start,
62
+ start + colonIndex,
63
+ ]);
64
+ script.addToken(types_1.AST_TOKEN_TYPES.Punctuator, [
65
+ start + colonIndex,
66
+ start + colonIndex + 1,
67
+ ]);
68
+ script.addToken(types_1.AST_TOKEN_TYPES.JSXIdentifier, [
69
+ start + colonIndex + 1,
70
+ start + attr.name.length,
71
+ ]);
72
+ script.addRestoreNodeProcess((scriptNode, result) => {
73
+ if (scriptNode.type ===
74
+ types_1.AST_NODE_TYPES.JSXAttribute &&
75
+ scriptNode.range[0] === start) {
76
+ const baseNameNode = scriptNode.name;
77
+ 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] + colonIndex)), name: Object.assign({ type: types_1.AST_NODE_TYPES.JSXIdentifier, name: attr.name.slice(colonIndex + 1) }, ctx.getLocations(baseNameNode.range[0] +
78
+ colonIndex +
79
+ 1, baseNameNode.range[1])) });
80
+ scriptNode.name = nsn;
81
+ nsn.namespace.parent = nsn;
82
+ nsn.name.parent = nsn;
83
+ const tokens = result.ast.tokens || [];
84
+ for (let index = 0; index < tokens.length; index++) {
85
+ const token = tokens[index];
86
+ if (token.range[0] ===
87
+ baseNameNode.range[0] &&
88
+ token.range[1] === baseNameNode.range[1]) {
89
+ tokens.splice(index, 1);
90
+ break;
91
+ }
92
+ }
93
+ return true;
94
+ }
95
+ return false;
96
+ });
97
+ }
98
+ }
48
99
  if (attr.kind === "shorthand") {
49
100
  const start = attr.position.start.offset;
50
101
  script.appendOriginal(start);
@@ -181,10 +232,9 @@ exports.processTemplate = processTemplate;
181
232
  * If the given tag is a void tag, get the self-closing tag.
182
233
  */
183
234
  function getVoidSelfClosingTag(node, parent, ctx) {
184
- if (node.type === "fragment") {
185
- return false;
186
- }
187
- if (node.children.length > 0) {
235
+ var _a;
236
+ const children = node.children.filter((c) => c.type !== "text" || c.value.trim());
237
+ if (children.length > 0) {
188
238
  return false;
189
239
  }
190
240
  const code = ctx.code;
@@ -192,8 +242,10 @@ function getVoidSelfClosingTag(node, parent, ctx) {
192
242
  const childIndex = parent.children.indexOf(node);
193
243
  if (childIndex === parent.children.length - 1) {
194
244
  // last
195
- nextElementIndex = parent.position.end.offset;
196
- nextElementIndex = code.lastIndexOf("</", nextElementIndex);
245
+ if ((_a = parent.position) === null || _a === void 0 ? void 0 : _a.end) {
246
+ nextElementIndex = parent.position.end.offset;
247
+ nextElementIndex = code.lastIndexOf("</", nextElementIndex);
248
+ }
197
249
  }
198
250
  else {
199
251
  const next = parent.children[childIndex + 1];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-eslint-parser",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Astro parser for ESLint",
5
5
  "main": "lib/index.js",
6
6
  "files": [