astro-eslint-parser 0.0.2 → 0.0.5

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/README.md CHANGED
@@ -11,9 +11,14 @@ You can check it on [Online DEMO](https://ota-meshi.github.io/astro-eslint-parse
11
11
  [![NPM downloads](https://img.shields.io/npm/dy/astro-eslint-parser.svg)](http://www.npmtrends.com/astro-eslint-parser)
12
12
  [![NPM downloads](https://img.shields.io/npm/dt/astro-eslint-parser.svg)](http://www.npmtrends.com/astro-eslint-parser)
13
13
  [![Build Status](https://github.com/ota-meshi/astro-eslint-parser/workflows/CI/badge.svg?branch=main)](https://github.com/ota-meshi/astro-eslint-parser/actions?query=workflow%3ACI)
14
+ [![Coverage Status](https://coveralls.io/repos/github/ota-meshi/astro-eslint-parser/badge.svg?branch=main)](https://coveralls.io/github/ota-meshi/astro-eslint-parser?branch=main)
14
15
 
15
16
  This parser is in the ***experimental stages*** of development.
16
17
 
18
+ ⚠ Currently this parser relies heavily on the internal API of [@astrojs/compiler]. It may stop working in a future update of [@astrojs/compiler]. ⚠
19
+
20
+ [@astrojs/compiler]: https://github.com/withastro/compiler
21
+
17
22
  <!--
18
23
  ### ESLint Plugins Using astro-eslint-parser
19
24
 
@@ -74,7 +79,7 @@ For example:
74
79
 
75
80
  ### parserOptions.parser
76
81
 
77
- You can use `parserOptions.parser` property to specify a custom parser to parse `<script>` tags.
82
+ You can use `parserOptions.parser` property to specify a custom parser to parse scripts.
78
83
  Other properties than parser would be given to the specified parser.
79
84
  For example:
80
85
 
@@ -87,7 +92,7 @@ For example:
87
92
  }
88
93
  ```
89
94
 
90
- For example, if you are using the `"@typescript-eslint/parser"`, and if you want to use TypeScript in `<script>` of `.astro`, you need to add more `parserOptions` configuration.
95
+ For example, if you are using the `"@typescript-eslint/parser"`, and if you want to use TypeScript in `.astro`, you need to add more `parserOptions` configuration.
91
96
 
92
97
  ```js
93
98
  module.exports = {
@@ -96,13 +101,13 @@ module.exports = {
96
101
  parserOptions: {
97
102
  // ...
98
103
  project: "path/to/your/tsconfig.json",
99
- extraFileExtensions: [".astro"], // This is a required setting in `@typescript-eslint/parser` v4.24.0.
104
+ extraFileExtensions: [".astro"], // This is a required setting in `@typescript-eslint/parser` v5.
100
105
  },
101
106
  overrides: [
102
107
  {
103
108
  files: ["*.astro"],
104
109
  parser: "astro-eslint-parser",
105
- // Parse the `<script>` in `.astro` as TypeScript by adding the following configuration.
110
+ // Parse the script in `.astro` as TypeScript by adding the following configuration.
106
111
  parserOptions: {
107
112
  parser: "@typescript-eslint/parser",
108
113
  },
@@ -133,9 +138,20 @@ Example **.vscode/settings.json**:
133
138
  }
134
139
  ```
135
140
 
141
+ ## Compatibility With Existing ESLint Rules
142
+
143
+ Most of the rules in the ESLint core work for the script part, but some rules are incompatible.
144
+ This parser will generate a JSX compatible AST for most of the HTML part of the Astro component. Therefore, some rules of [eslint-plugin-react] may work.
145
+ For example, the [react/jsx-no-target-blank] rule works fine.
146
+
147
+ [semi]: https://eslint.org/docs/rules/semi
148
+ [eslint-plugin-react]: https://github.com/jsx-eslint/eslint-plugin-react/
149
+ [react/jsx-no-target-blank]: https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-target-blank.md
150
+
136
151
  ## Usage for Custom Rules / Plugins
137
152
 
138
153
  - TBA
154
+ - You can check the AST in the [Online DEMO](https://ota-meshi.github.io/astro-eslint-parser/). However, AST is subject to major changes in the future.
139
155
 
140
156
  <!-- - [AST.md](./docs/AST.md) is AST specification. You can check it on the [Online DEMO](https://ota-meshi.github.io/astro-eslint-parser/). -->
141
157
  <!-- - I have already [implemented some rules] in the [`@ota-meshi/eslint-plugin-astro`]. The source code for these rules will be helpful to you. -->
package/lib/ast.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { TSESTree } from "@typescript-eslint/types";
2
- export declare type AstroNode = AstroProgram | AstroRootFragment | AstroHTMLComment | AstroDoctype | AstroShorthandAttribute | AstroTemplateLiteralAttribute;
2
+ export declare type AstroNode = AstroProgram | AstroRootFragment | AstroHTMLComment | AstroDoctype | AstroShorthandAttribute | AstroTemplateLiteralAttribute | AstroRawText;
3
3
  /** Node of Astro program root */
4
4
  export interface AstroProgram extends Omit<TSESTree.Program, "type" | "body"> {
5
5
  type: "Program";
@@ -40,3 +40,8 @@ export interface AstroTemplateLiteralAttribute extends Omit<TSESTree.JSXAttribut
40
40
  };
41
41
  parent: TSESTree.JSXElement | TSESTree.JSXFragment;
42
42
  }
43
+ /** Node of Astro raw text */
44
+ export interface AstroRawText extends Omit<TSESTree.JSXText, "type" | "parent"> {
45
+ type: "AstroRawText";
46
+ parent: AstroRootFragment | TSESTree.JSXElement | TSESTree.JSXFragment;
47
+ }
@@ -8,9 +8,9 @@ export declare function isTag(node: Node): node is Node & TagLikeNode;
8
8
  */
9
9
  export declare function isParent(node: Node): node is ParentNode;
10
10
  /** walk element nodes */
11
- export declare function walkElements(parent: ParentNode, cb: (n: Node, parent: ParentNode) => void): void;
11
+ export declare function walkElements(parent: ParentNode, code: string, cb: (n: Node, parent: ParentNode) => void): void;
12
12
  /** walk nodes */
13
- export declare function walk(parent: ParentNode, enter: (n: Node | AttributeNode, parent: ParentNode) => void, leave?: (n: Node | AttributeNode, parent: ParentNode) => void): void;
13
+ export declare function walk(parent: ParentNode, code: string, enter: (n: Node | AttributeNode, parent: ParentNode) => void, leave?: (n: Node | AttributeNode, parent: ParentNode) => void): void;
14
14
  /**
15
15
  * Get end offset of start tag
16
16
  */
@@ -19,27 +19,19 @@ function isParent(node) {
19
19
  }
20
20
  exports.isParent = isParent;
21
21
  /** walk element nodes */
22
- function walkElements(parent, cb) {
23
- let children = parent.children;
24
- if (parent.type === "root" && children.every((n) => n.position)) {
25
- // The order of comments and frontmatter may be changed.
26
- children = [...children].sort((a, b) => a.position.start.offset - b.position.start.offset);
27
- }
22
+ function walkElements(parent, code, cb) {
23
+ const children = getSortedChildren(parent, code);
28
24
  for (const node of children) {
29
25
  cb(node, parent);
30
26
  if (isParent(node)) {
31
- walkElements(node, cb);
27
+ walkElements(node, code, cb);
32
28
  }
33
29
  }
34
30
  }
35
31
  exports.walkElements = walkElements;
36
32
  /** walk nodes */
37
- function walk(parent, enter, leave) {
38
- let children = parent.children;
39
- if (parent.type === "root" && children.every((n) => n.position)) {
40
- // The order of comments and frontmatter may be changed.
41
- children = [...children].sort((a, b) => a.position.start.offset - b.position.start.offset);
42
- }
33
+ function walk(parent, code, enter, leave) {
34
+ const children = getSortedChildren(parent, code);
43
35
  for (const node of children) {
44
36
  enter(node, parent);
45
37
  if (isTag(node)) {
@@ -49,7 +41,7 @@ function walk(parent, enter, leave) {
49
41
  }
50
42
  }
51
43
  if (isParent(node)) {
52
- walk(node, enter, leave);
44
+ walk(node, code, enter, leave);
53
45
  }
54
46
  leave === null || leave === void 0 ? void 0 : leave(node, parent);
55
47
  }
@@ -186,3 +178,41 @@ function skipSpaces(string, position) {
186
178
  return position;
187
179
  }
188
180
  exports.skipSpaces = skipSpaces;
181
+ /**
182
+ * Get children
183
+ */
184
+ function getSortedChildren(parent, code) {
185
+ var _a;
186
+ if (parent.type === "root" && ((_a = parent.children[0]) === null || _a === void 0 ? void 0 : _a.type) === "frontmatter") {
187
+ // The order of comments and frontmatter may be changed.
188
+ const children = [...parent.children];
189
+ if (children.every((n) => n.position)) {
190
+ return children.sort((a, b) => a.position.start.offset - b.position.start.offset);
191
+ }
192
+ let start = skipSpaces(code, 0);
193
+ if (code.startsWith("<!", start)) {
194
+ const frontmatter = children.shift();
195
+ const before = [];
196
+ let first;
197
+ while ((first = children.shift())) {
198
+ start = skipSpaces(code, start);
199
+ if (first.type === "comment" &&
200
+ code.startsWith("<!--", start)) {
201
+ start = code.indexOf("-->", start + 4) + 3;
202
+ before.push(first);
203
+ }
204
+ else if (first.type === "doctype" &&
205
+ code.startsWith("<!", start)) {
206
+ start = code.indexOf(">", start + 2) + 1;
207
+ before.push(first);
208
+ }
209
+ else {
210
+ children.unshift(first);
211
+ break;
212
+ }
213
+ }
214
+ return [...before, frontmatter, ...children];
215
+ }
216
+ }
217
+ return parent.children;
218
+ }
@@ -46,6 +46,11 @@ class ScriptContext {
46
46
  if (last.expression.type !== "JSXFragment") {
47
47
  throw new errors_1.ParseError("Unknown state error: Expected JSXFragment", last.expression.range[0], this.ctx);
48
48
  }
49
+ // Process for Astro
50
+ const rootFragment = (result.ast.body[result.ast.body.length - 1] = last.expression);
51
+ delete rootFragment.closingFragment;
52
+ delete rootFragment.openingFragment;
53
+ rootFragment.type = "AstroRootFragment";
49
54
  // remap locations
50
55
  const traversed = new Set();
51
56
  (0, traverse_1.traverseNodes)(result.ast, {
@@ -72,11 +77,6 @@ class ScriptContext {
72
77
  for (const token of result.ast.comments || []) {
73
78
  this.remapLocation(token);
74
79
  }
75
- // Process for Astro
76
- delete last.expression.closingFragment;
77
- delete last.expression.openingFragment;
78
- last.expression.type =
79
- "AstroRootFragment";
80
80
  let restoreNodeProcesses = this.restoreNodeProcesses;
81
81
  for (const node of traversed) {
82
82
  restoreNodeProcesses = restoreNodeProcesses.filter((proc) => !proc(node, result));
@@ -41,7 +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, (node) => {
44
+ (0, astro_1.walk)(node, code, (node) => {
45
45
  if (node.type === "frontmatter") {
46
46
  start = node.position.start.offset = tokenIndex(code, "---", start);
47
47
  start = node.position.end.offset =
@@ -81,6 +81,9 @@ function fixLocations(node, code) {
81
81
  if (!node.position) {
82
82
  node.position = { start: {}, end: {} };
83
83
  }
84
+ if (!node.position.end) {
85
+ node.position.end = {};
86
+ }
84
87
  start = node.position.start.offset = tokenIndex(code, "<!", start);
85
88
  start += 2;
86
89
  start = node.position.end.offset =
@@ -16,7 +16,7 @@ function processTemplate(ctx, resultTemplate) {
16
16
  fragmentOpened = true;
17
17
  }
18
18
  // eslint-disable-next-line complexity -- X(
19
- (0, astro_1.walkElements)(resultTemplate.ast, (node, parent) => {
19
+ (0, astro_1.walkElements)(resultTemplate.ast, ctx.code, (node, parent) => {
20
20
  if (node.type === "frontmatter") {
21
21
  const start = node.position.start.offset;
22
22
  script.appendOriginal(start);
@@ -147,8 +147,10 @@ function processTemplate(ctx, resultTemplate) {
147
147
  script.addRestoreNodeProcess((scriptNode) => {
148
148
  if (scriptNode.type === types_1.AST_NODE_TYPES.JSXElement &&
149
149
  scriptNode.range[0] === styleNodeStart) {
150
- const textNode = Object.assign({ type: types_1.AST_NODE_TYPES.JSXText, value: text.value, raw: text.value, parent: scriptNode }, ctx.getLocations(start, start + text.value.length));
151
- scriptNode.children = [textNode];
150
+ const textNode = Object.assign({ type: "AstroRawText", value: text.value, raw: text.value, parent: scriptNode }, ctx.getLocations(start, start + text.value.length));
151
+ scriptNode.children = [
152
+ textNode,
153
+ ];
152
154
  return true;
153
155
  }
154
156
  return false;
@@ -9,5 +9,6 @@ const astroKeys = {
9
9
  AstroDoctype: [],
10
10
  AstroShorthandAttribute: ["name", "value"],
11
11
  AstroTemplateLiteralAttribute: ["name", "value"],
12
+ AstroRawText: [],
12
13
  };
13
14
  exports.KEYS = (0, eslint_visitor_keys_1.unionWith)(astroKeys);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-eslint-parser",
3
- "version": "0.0.2",
3
+ "version": "0.0.5",
4
4
  "description": "Astro parser for ESLint",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -62,7 +62,7 @@
62
62
  "benchmark": "^2.1.4",
63
63
  "chai": "^4.3.4",
64
64
  "code-red": "^0.2.3",
65
- "eslint": "^8.2.0",
65
+ "eslint": "^8.14.0",
66
66
  "eslint-config-prettier": "^8.3.0",
67
67
  "eslint-formatter-codeframe": "^7.32.1",
68
68
  "eslint-plugin-eslint-comments": "^3.2.0",
@@ -71,12 +71,13 @@
71
71
  "eslint-plugin-node": "^11.1.0",
72
72
  "eslint-plugin-node-dependencies": "^0.8.0",
73
73
  "eslint-plugin-prettier": "^4.0.0",
74
+ "eslint-plugin-react": "^7.29.4",
74
75
  "eslint-plugin-regexp": "^1.5.0",
75
76
  "eslint-plugin-vue": "^8.0.3",
76
77
  "estree-walker": "^3.0.0",
77
78
  "locate-character": "^2.0.5",
78
79
  "magic-string": "^0.26.0",
79
- "mocha": "^9.1.3",
80
+ "mocha": "^10.0.0",
80
81
  "mocha-chai-jest-snapshot": "^1.1.3",
81
82
  "nyc": "^15.1.0",
82
83
  "prettier": "^2.0.5",