astro-eslint-parser 0.0.14 → 0.0.15

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.
@@ -19,11 +19,11 @@ export declare function getStartTagEndOffset(node: TagLikeNode, ctx: Context): n
19
19
  /**
20
20
  * Get end offset of tag
21
21
  */
22
- export declare function getTagEndOffset(node: TagLikeNode, parents: ParentNode[], ctx: Context): number;
22
+ export declare function getTagEndOffset(node: TagLikeNode, ctx: Context): number;
23
23
  /**
24
24
  * Get end offset of Expression
25
25
  */
26
- export declare function getExpressionEndOffset(node: ExpressionNode, parents: ParentNode[], ctx: Context): number;
26
+ export declare function getExpressionEndOffset(node: ExpressionNode, ctx: Context): number;
27
27
  /**
28
28
  * Get end offset of attribute
29
29
  */
@@ -39,18 +39,18 @@ export declare function getCommentEndOffset(node: CommentNode, ctx: Context): nu
39
39
  /**
40
40
  * Get content end offset
41
41
  */
42
- export declare function getContentEndOffset(parent: ParentNode, parents: ParentNode[], ctx: Context): number;
42
+ export declare function getContentEndOffset(parent: ParentNode, ctx: Context): number;
43
43
  /**
44
44
  * If the given tag is a self-close tag, get the self-closing tag.
45
45
  */
46
- export declare function getSelfClosingTag(node: TagLikeNode, parents: ParentNode[], ctx: Context): null | {
46
+ export declare function getSelfClosingTag(node: TagLikeNode, parent: ParentNode, ctx: Context): null | {
47
47
  offset: number;
48
48
  end: "/>" | ">";
49
49
  };
50
50
  /**
51
51
  * If the given tag has a end tag, get the end tag.
52
52
  */
53
- export declare function getEndTag(node: TagLikeNode, parents: ParentNode[], ctx: Context): null | {
53
+ export declare function getEndTag(node: TagLikeNode, ctx: Context): null | {
54
54
  offset: number;
55
55
  tag: string;
56
56
  };
@@ -66,7 +66,7 @@ exports.getStartTagEndOffset = getStartTagEndOffset;
66
66
  /**
67
67
  * Get end offset of tag
68
68
  */
69
- function getTagEndOffset(node, parents, ctx) {
69
+ function getTagEndOffset(node, ctx) {
70
70
  var _a;
71
71
  if (((_a = node.position.end) === null || _a === void 0 ? void 0 : _a.offset) != null) {
72
72
  return node.position.end.offset;
@@ -74,7 +74,7 @@ function getTagEndOffset(node, parents, ctx) {
74
74
  let beforeIndex;
75
75
  if (node.children.length) {
76
76
  const lastChild = node.children[node.children.length - 1];
77
- beforeIndex = getEndOffset(lastChild, [node, ...parents], ctx);
77
+ beforeIndex = getEndOffset(lastChild, ctx);
78
78
  }
79
79
  else {
80
80
  beforeIndex = getStartTagEndOffset(node, ctx);
@@ -91,14 +91,14 @@ exports.getTagEndOffset = getTagEndOffset;
91
91
  /**
92
92
  * Get end offset of Expression
93
93
  */
94
- function getExpressionEndOffset(node, parents, ctx) {
94
+ function getExpressionEndOffset(node, ctx) {
95
95
  var _a;
96
96
  if (((_a = node.position.end) === null || _a === void 0 ? void 0 : _a.offset) != null) {
97
97
  return node.position.end.offset;
98
98
  }
99
99
  if (node.children.length) {
100
100
  const lastChild = node.children[node.children.length - 1];
101
- const beforeIndex = getEndOffset(lastChild, [node, ...parents], ctx);
101
+ const beforeIndex = getEndOffset(lastChild, ctx);
102
102
  const info = getTokenInfo(ctx, ["}"], beforeIndex);
103
103
  return info.index + info.match.length;
104
104
  }
@@ -166,21 +166,22 @@ exports.getCommentEndOffset = getCommentEndOffset;
166
166
  /**
167
167
  * Get content end offset
168
168
  */
169
- function getContentEndOffset(parent, parents, ctx) {
169
+ function getContentEndOffset(parent, ctx) {
170
170
  const code = ctx.code;
171
171
  if (isTag(parent)) {
172
- const end = getTagEndOffset(parent, parents, ctx);
172
+ const end = getTagEndOffset(parent, ctx);
173
173
  if (code[end - 1] !== ">") {
174
174
  return end;
175
175
  }
176
- const index = code.lastIndexOf("</", end);
177
- if (index >= 0 && code.slice(index, end).trim() === parent.name) {
176
+ const index = code.lastIndexOf("</", end - 1);
177
+ if (index >= 0 &&
178
+ code.slice(index + 2, end - 1).trim() === parent.name) {
178
179
  return index;
179
180
  }
180
181
  return end;
181
182
  }
182
183
  else if (parent.type === "expression") {
183
- const end = getExpressionEndOffset(parent, parents, ctx);
184
+ const end = getExpressionEndOffset(parent, ctx);
184
185
  return code.lastIndexOf("}", end);
185
186
  }
186
187
  else if (parent.type === "root") {
@@ -192,18 +193,16 @@ exports.getContentEndOffset = getContentEndOffset;
192
193
  /**
193
194
  * If the given tag is a self-close tag, get the self-closing tag.
194
195
  */
195
- function getSelfClosingTag(node, parents, ctx) {
196
- const children = node.children.filter((c) => c.type !== "text" || c.value.trim());
197
- if (children.length > 0) {
196
+ function getSelfClosingTag(node, parent, ctx) {
197
+ if (node.children.length > 0) {
198
198
  return null;
199
199
  }
200
- const parent = parents[0];
201
200
  const code = ctx.code;
202
201
  let nextElementIndex = code.length;
203
202
  const childIndex = parent.children.indexOf(node);
204
203
  if (childIndex === parent.children.length - 1) {
205
204
  // last
206
- nextElementIndex = getContentEndOffset(parent, parents.slice(1), ctx);
205
+ nextElementIndex = getContentEndOffset(parent, ctx);
207
206
  }
208
207
  else {
209
208
  const next = parent.children[childIndex + 1];
@@ -223,11 +222,11 @@ exports.getSelfClosingTag = getSelfClosingTag;
223
222
  /**
224
223
  * If the given tag has a end tag, get the end tag.
225
224
  */
226
- function getEndTag(node, parents, ctx) {
225
+ function getEndTag(node, ctx) {
227
226
  let beforeIndex;
228
227
  if (node.children.length) {
229
228
  const lastChild = node.children[node.children.length - 1];
230
- beforeIndex = getEndOffset(lastChild, [node, ...parents], ctx);
229
+ beforeIndex = getEndOffset(lastChild, ctx);
231
230
  }
232
231
  else {
233
232
  beforeIndex = getStartTagEndOffset(node, ctx);
@@ -249,15 +248,15 @@ exports.getEndTag = getEndTag;
249
248
  /**
250
249
  * Get end offset of tag
251
250
  */
252
- function getEndOffset(node, parents, ctx) {
251
+ function getEndOffset(node, ctx) {
253
252
  var _a;
254
253
  if (((_a = node.position.end) === null || _a === void 0 ? void 0 : _a.offset) != null) {
255
254
  return node.position.end.offset;
256
255
  }
257
256
  if (isTag(node))
258
- return getTagEndOffset(node, parents, ctx);
257
+ return getTagEndOffset(node, ctx);
259
258
  if (node.type === "expression")
260
- return getExpressionEndOffset(node, parents, ctx);
259
+ return getExpressionEndOffset(node, ctx);
261
260
  if (node.type === "comment")
262
261
  return getCommentEndOffset(node, ctx);
263
262
  if (node.type === "frontmatter") {
@@ -219,19 +219,20 @@ function fixLocations(node, ctx) {
219
219
  if (attributes[attributes.length - 1] === node) {
220
220
  start = (0, astro_1.getStartTagEndOffset)(parent, ctx);
221
221
  }
222
- return;
223
222
  }
224
- if (node.type === "expression") {
223
+ else if (node.type === "expression") {
225
224
  start = tokenIndex(ctx, "}", start) + 1;
226
225
  }
227
226
  else if (node.type === "fragment" ||
228
227
  node.type === "element" ||
229
228
  node.type === "component" ||
230
229
  node.type === "custom-element") {
231
- const closeTagStart = tokenIndexSafe(ctx.code, `</${node.name}`, start);
232
- if (closeTagStart != null) {
233
- start = closeTagStart + 2 + node.name.length;
234
- start = tokenIndex(ctx, ">", start) + 1;
230
+ if (!(0, astro_1.getSelfClosingTag)(node, parent, ctx)) {
231
+ const closeTagStart = tokenIndexSafe(ctx.code, `</${node.name}`, start);
232
+ if (closeTagStart != null) {
233
+ start = closeTagStart + 2 + node.name.length;
234
+ start = tokenIndex(ctx, ">", start) + 1;
235
+ }
235
236
  }
236
237
  }
237
238
  else {
@@ -19,7 +19,7 @@ function processTemplate(ctx, resultTemplate) {
19
19
  }
20
20
  (0, astro_1.walkElements)(resultTemplate.ast, ctx.code,
21
21
  // eslint-disable-next-line complexity -- X(
22
- (node, parents) => {
22
+ (node, [parent]) => {
23
23
  if (node.type === "frontmatter") {
24
24
  const start = node.position.start.offset;
25
25
  script.appendOriginal(start);
@@ -50,7 +50,6 @@ function processTemplate(ctx, resultTemplate) {
50
50
  }
51
51
  else if ((0, astro_1.isTag)(node)) {
52
52
  // Process for multiple tag
53
- const parent = parents[0];
54
53
  if (parent.type === "expression") {
55
54
  const index = parent.children.indexOf(node);
56
55
  const before = parent.children[index - 1];
@@ -182,7 +181,7 @@ function processTemplate(ctx, resultTemplate) {
182
181
  }
183
182
  }
184
183
  // Process for start tag close
185
- const closing = (0, astro_1.getSelfClosingTag)(node, parents, ctx);
184
+ const closing = (0, astro_1.getSelfClosingTag)(node, parent, ctx);
186
185
  if (closing && closing.end === ">") {
187
186
  script.appendOriginal(closing.offset - 1);
188
187
  script.appendScript("/");
@@ -298,13 +297,13 @@ function processTemplate(ctx, resultTemplate) {
298
297
  });
299
298
  script.addToken("HTMLDocType", [start, end]);
300
299
  }
301
- }, (node, parents) => {
300
+ }, (node, [parent]) => {
302
301
  if ((0, astro_1.isTag)(node)) {
303
- const closing = (0, astro_1.getSelfClosingTag)(node, parents, ctx);
302
+ const closing = (0, astro_1.getSelfClosingTag)(node, parent, ctx);
304
303
  if (!closing) {
305
- const end = (0, astro_1.getEndTag)(node, parents, ctx);
304
+ const end = (0, astro_1.getEndTag)(node, ctx);
306
305
  if (!end) {
307
- const offset = (0, astro_1.getContentEndOffset)(node, parents, ctx);
306
+ const offset = (0, astro_1.getContentEndOffset)(node, ctx);
308
307
  script.appendOriginal(offset);
309
308
  script.appendScript(`</${node.name}>`);
310
309
  script.addRestoreNodeProcess((scriptNode, _result, parent) => {
@@ -321,7 +320,6 @@ function processTemplate(ctx, resultTemplate) {
321
320
  }
322
321
  }
323
322
  // Process for multiple tag
324
- const parent = parents[0];
325
323
  if (((0, astro_1.isTag)(node) || node.type === "comment") &&
326
324
  parent.type === "expression") {
327
325
  const index = parent.children.indexOf(node);
@@ -331,7 +329,7 @@ function processTemplate(ctx, resultTemplate) {
331
329
  if (before &&
332
330
  ((0, astro_1.isTag)(before) || before.type === "comment")) {
333
331
  const end = (0, astro_1.isTag)(node)
334
- ? (0, astro_1.getTagEndOffset)(node, parents, ctx)
332
+ ? (0, astro_1.getTagEndOffset)(node, ctx)
335
333
  : (0, astro_1.getCommentEndOffset)(node, ctx);
336
334
  script.appendOriginal(end);
337
335
  script.appendScript("</>");
@@ -30,7 +30,9 @@ function parseScript(code, ctx) {
30
30
  return { ast: result };
31
31
  }
32
32
  catch (e) {
33
- (0, debug_1.debug)("[script] parsing error:", e.message, `@ ${JSON.stringify(code)}`);
33
+ (0, debug_1.debug)("[script] parsing error:", e.message, `@ ${JSON.stringify(code)}
34
+
35
+ ${code}`);
34
36
  throw e;
35
37
  }
36
38
  finally {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-eslint-parser",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "Astro parser for ESLint",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -19,7 +19,7 @@
19
19
  "cover": "nyc --reporter=lcov npm run test",
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
- "update-fixtures": "ts-node --transpile-only ./tools/update-fixtures.ts",
22
+ "update-fixtures": "DEBUG='astro-eslint-parser' ts-node --transpile-only ./tools/update-fixtures.ts",
23
23
  "debug-parser": "ts-node --transpile-only ./tools/parser-test.ts",
24
24
  "eslint-playground": "eslint tests/fixtures --ext .astro --config .eslintrc-for-playground.js --format codeframe",
25
25
  "benchmark": "ts-node --transpile-only benchmark/index.ts"