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.
@@ -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, code: string): number;
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, code: string): number;
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, code: string): number;
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, code: string): number;
30
+ export declare function getCommentEndOffset(node: CommentNode, ctx: Context): number;
30
31
  /**
31
32
  * Skip spaces
32
33
  */
@@ -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, code) {
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, code);
58
+ beforeCloseIndex = getAttributeEndOffset(lastAttr, ctx);
58
59
  }
59
60
  else {
60
- const info = getTokenInfo(code, [`<${node.name}`], node.position.start.offset);
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(code, [[">", "/>"]], beforeCloseIndex);
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, code) {
71
+ function getAttributeEndOffset(node, ctx) {
71
72
  let info;
72
73
  if (node.kind === "empty") {
73
- info = getTokenInfo(code, [node.name], node.position.start.offset);
74
+ info = getTokenInfo(ctx, [node.name], node.position.start.offset);
74
75
  }
75
76
  else if (node.kind === "quoted") {
76
- info = getTokenInfo(code, [[`"${node.value}"`, `'${node.value}'`, node.value]], getAttributeValueStartOffset(node, code));
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(code, ["{", node.value, "}"], getAttributeValueStartOffset(node, code));
80
+ info = getTokenInfo(ctx, ["{", node.value, "}"], getAttributeValueStartOffset(node, ctx));
80
81
  }
81
82
  else if (node.kind === "shorthand") {
82
- info = getTokenInfo(code, ["{", node.name, "}"], node.position.start.offset);
83
+ info = getTokenInfo(ctx, ["{", node.name, "}"], node.position.start.offset);
83
84
  }
84
85
  else if (node.kind === "spread") {
85
- info = getTokenInfo(code, ["{", "...", node.name, "}"], node.position.start.offset);
86
+ info = getTokenInfo(ctx, ["{", "...", node.name, "}"], node.position.start.offset);
86
87
  }
87
88
  else if (node.kind === "template-literal") {
88
- info = getTokenInfo(code, [`\`${node.value}\``], getAttributeValueStartOffset(node, code));
89
+ info = getTokenInfo(ctx, [`\`${node.value}\``], getAttributeValueStartOffset(node, ctx));
89
90
  }
90
91
  else {
91
- throw new Error(`Unknown attr kind: ${node.kind}`);
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, code) {
100
+ function getAttributeValueStartOffset(node, ctx) {
100
101
  let info;
101
102
  if (node.kind === "quoted") {
102
- info = getTokenInfo(code, [node.name, "=", [`"`, `'`, node.value]], node.position.start.offset);
103
+ info = getTokenInfo(ctx, [node.name, "=", [`"`, `'`, node.value]], node.position.start.offset);
103
104
  }
104
105
  else if (node.kind === "expression") {
105
- info = getTokenInfo(code, [node.name, "=", "{"], node.position.start.offset);
106
+ info = getTokenInfo(ctx, [node.name, "=", "{"], node.position.start.offset);
106
107
  }
107
108
  else if (node.kind === "template-literal") {
108
- info = getTokenInfo(code, [node.name, "=", "`"], node.position.start.offset);
109
+ info = getTokenInfo(ctx, [node.name, "=", "`"], node.position.start.offset);
109
110
  }
110
111
  else {
111
- throw new Error(`Unknown attr kind: ${node.kind}`);
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, code) {
120
- const info = getTokenInfo(code, ["<!--", node.value, "-->"], node.position.start.offset);
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(string, tokens, position) {
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 Error(`Unknown token at ${index}, expected: ${JSON.stringify(t)}, actual: ${JSON.stringify(string.slice(index, index + 10))}`);
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(string, position) : position;
147
- if (string.startsWith(search, index)) {
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, code);
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, code) {
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(code, "---", start);
47
+ start = node.position.start.offset = tokenIndex(ctx, "---", start);
47
48
  start = node.position.end.offset =
48
- tokenIndex(code, "---", start + 3 + node.value.length) + 3;
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(code, "<", start);
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, code);
62
+ start = (0, astro_1.getStartTagEndOffset)(node, ctx);
62
63
  }
63
64
  }
64
65
  else if (node.type === "attribute") {
65
- fixLocationForAttr(node, code, start);
66
- start = (0, astro_1.getAttributeEndOffset)(node, code);
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(code, "<!--", start);
70
- start = (0, astro_1.getCommentEndOffset)(node, code);
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(code, node.value, start);
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(code, "{", start);
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(code, "<!", start);
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, code);
100
+ start = (0, astro_1.getStartTagEndOffset)(parent, ctx);
100
101
  }
101
102
  return;
102
103
  }
103
104
  if (node.type === "expression") {
104
- start = tokenIndex(code, "}", start) + 1;
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(code, ">", start) + 1;
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, code, start) {
128
+ function fixLocationForAttr(node, ctx, start) {
128
129
  if (node.kind === "empty") {
129
- node.position.start.offset = tokenIndex(code, node.name, start);
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(code, node.name, start);
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(code, node.name, start);
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(code, "{", start);
139
+ node.position.start.offset = tokenIndex(ctx, "{", start);
139
140
  }
140
141
  else if (node.kind === "spread") {
141
- node.position.start.offset = tokenIndex(code, "{", start);
142
+ node.position.start.offset = tokenIndex(ctx, "{", start);
142
143
  }
143
144
  else if (node.kind === "template-literal") {
144
- node.position.start.offset = tokenIndex(code, node.name, start);
145
+ node.position.start.offset = tokenIndex(ctx, node.name, start);
145
146
  }
146
147
  else {
147
- throw new Error(`Unknown attr kind: ${node.kind}`);
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(string, token, position) {
154
- const index = tokenIndexSafe(string, token, position);
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)(string, position) : position;
157
- throw new Error(`Unknown token at ${start}, expected: ${JSON.stringify(token)}, actual: ${JSON.stringify(string.slice(start, start + 10))}`);
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
  }
@@ -82,7 +82,7 @@ function extractTokens(ast, ctx) {
82
82
  */
83
83
  function parseTemplate(code, ctx) {
84
84
  try {
85
- return (0, parse_1.parse)(code);
85
+ return (0, parse_1.parse)(code, ctx);
86
86
  }
87
87
  catch (e) {
88
88
  if (typeof e.pos === "number") {
@@ -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.code);
119
- const end = (0, astro_1.getAttributeEndOffset)(attr, ctx.code);
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, code);
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-eslint-parser",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Astro parser for ESLint",
5
5
  "main": "lib/index.js",
6
6
  "files": [