astro-eslint-parser 0.0.16 → 0.1.0
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 +12 -7
- package/lib/ast/astro.d.ts +49 -0
- package/lib/{ast.js → ast/astro.js} +0 -0
- package/lib/ast/base.d.ts +6 -0
- package/lib/ast/base.js +2 -0
- package/lib/ast/index.d.ts +8 -0
- package/lib/ast/index.js +18 -0
- package/lib/ast/jsx.d.ts +91 -0
- package/lib/ast/jsx.js +2 -0
- package/lib/astro/index.js +2 -4
- package/lib/astro-tools/index.d.ts +21 -0
- package/lib/astro-tools/index.js +26 -0
- package/lib/context/index.d.ts +13 -8
- package/lib/context/index.js +51 -100
- package/lib/context/parser-options.d.ts +8 -0
- package/lib/context/parser-options.js +71 -0
- package/lib/{parser → context/resolve-parser}/espree.d.ts +1 -1
- package/lib/{parser → context/resolve-parser}/espree.js +1 -1
- package/lib/context/resolve-parser/index.d.ts +5 -0
- package/lib/{parser/resolve-parser.js → context/resolve-parser/index.js} +0 -0
- package/lib/context/script.d.ts +1 -1
- package/lib/context/script.js +16 -18
- package/lib/index.d.ts +2 -1
- package/lib/index.js +3 -1
- package/lib/parser/astro-parser/astrojs-compiler-service.d.ts +2 -4
- package/lib/parser/astro-parser/astrojs-compiler-service.js +3 -12
- package/lib/parser/astro-parser/astrojs-compiler-worker.d.ts +1 -0
- package/lib/parser/astro-parser/astrojs-compiler-worker.js +11 -0
- package/lib/parser/astro-parser/parse.js +11 -51
- package/lib/parser/index.d.ts +0 -17
- package/lib/parser/index.js +6 -31
- package/lib/parser/lru-cache.d.ts +7 -0
- package/lib/parser/lru-cache.js +32 -0
- package/lib/parser/process-template.js +87 -48
- package/lib/parser/script.d.ts +3 -2
- package/lib/parser/script.js +12 -11
- package/lib/parser/template.d.ts +10 -0
- package/lib/parser/template.js +102 -0
- package/lib/types.d.ts +21 -0
- package/lib/types.js +2 -0
- package/lib/visitor-keys.js +1 -2
- package/package.json +14 -11
- package/lib/ast.d.ts +0 -53
- package/lib/parser/astro-parser/wasm_exec.d.ts +0 -35
- package/lib/parser/astro-parser/wasm_exec.js +0 -470
- package/lib/parser/resolve-parser.d.ts +0 -16
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ESLintCustomParser } from "../../types";
|
|
2
|
+
/** Get parser name */
|
|
3
|
+
export declare function getParserName(attrs: Record<string, string | undefined>, parser: any): string;
|
|
4
|
+
/** Get parser */
|
|
5
|
+
export declare function getParser(attrs: Record<string, string | undefined>, parser: any): ESLintCustomParser;
|
|
File without changes
|
package/lib/context/script.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Context } from ".";
|
|
2
|
-
import type { ESLintExtendedProgram } from "../parser";
|
|
3
2
|
import type { TSESTree } from "@typescript-eslint/types";
|
|
3
|
+
import type { ESLintExtendedProgram } from "../types";
|
|
4
4
|
declare class RestoreNodeProcessContext {
|
|
5
5
|
readonly result: ESLintExtendedProgram;
|
|
6
6
|
readonly removeTokens: Set<(token: TSESTree.Token) => boolean>;
|
package/lib/context/script.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ScriptContext = void 0;
|
|
4
4
|
const traverse_1 = require("../traverse");
|
|
5
|
-
const errors_1 = require("../errors");
|
|
6
5
|
class RestoreNodeProcessContext {
|
|
7
6
|
constructor(result) {
|
|
8
7
|
this.removeTokens = new Set();
|
|
@@ -26,6 +25,9 @@ class ScriptContext {
|
|
|
26
25
|
this.consumedIndex += offset;
|
|
27
26
|
}
|
|
28
27
|
appendOriginal(index) {
|
|
28
|
+
if (this.consumedIndex >= index) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
29
31
|
this.offsets.push({
|
|
30
32
|
original: this.consumedIndex,
|
|
31
33
|
script: this.script.length,
|
|
@@ -47,20 +49,7 @@ class ScriptContext {
|
|
|
47
49
|
/**
|
|
48
50
|
* Restore AST nodes
|
|
49
51
|
*/
|
|
50
|
-
// eslint-disable-next-line complexity -- X(
|
|
51
52
|
restore(result) {
|
|
52
|
-
const last = result.ast.body[result.ast.body.length - 1];
|
|
53
|
-
if (last.type !== "ExpressionStatement") {
|
|
54
|
-
throw new errors_1.ParseError("Unknown state error: Expected ExpressionStatement", last.range[0], this.ctx);
|
|
55
|
-
}
|
|
56
|
-
if (last.expression.type !== "JSXFragment") {
|
|
57
|
-
throw new errors_1.ParseError("Unknown state error: Expected JSXFragment", last.expression.range[0], this.ctx);
|
|
58
|
-
}
|
|
59
|
-
// Process for Astro
|
|
60
|
-
const rootFragment = (result.ast.body[result.ast.body.length - 1] = last.expression);
|
|
61
|
-
delete rootFragment.closingFragment;
|
|
62
|
-
delete rootFragment.openingFragment;
|
|
63
|
-
rootFragment.type = "AstroRootFragment";
|
|
64
53
|
// remap locations
|
|
65
54
|
const traversed = new Map();
|
|
66
55
|
(0, traverse_1.traverseNodes)(result.ast, {
|
|
@@ -110,10 +99,16 @@ class ScriptContext {
|
|
|
110
99
|
}
|
|
111
100
|
}
|
|
112
101
|
// Adjust program node location
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
result.ast.
|
|
116
|
-
result.ast.
|
|
102
|
+
const firstOffset = Math.min(...[
|
|
103
|
+
result.ast.body[0],
|
|
104
|
+
result.ast.tokens?.[0],
|
|
105
|
+
result.ast.comments?.[0],
|
|
106
|
+
]
|
|
107
|
+
.filter(Boolean)
|
|
108
|
+
.map((t) => t.range[0]));
|
|
109
|
+
if (firstOffset < result.ast.range[0]) {
|
|
110
|
+
result.ast.range[0] = firstOffset;
|
|
111
|
+
result.ast.loc.start = this.ctx.getLocFromIndex(firstOffset);
|
|
117
112
|
}
|
|
118
113
|
}
|
|
119
114
|
remapLocation(node) {
|
|
@@ -142,6 +137,9 @@ class ScriptContext {
|
|
|
142
137
|
}
|
|
143
138
|
}
|
|
144
139
|
getRemapRange(start, end) {
|
|
140
|
+
if (!this.offsets.length) {
|
|
141
|
+
return [start, end];
|
|
142
|
+
}
|
|
145
143
|
let lastStart = this.offsets[0];
|
|
146
144
|
let lastEnd = this.offsets[0];
|
|
147
145
|
for (const offset of this.offsets) {
|
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { parseForESLint } from "./parser";
|
|
2
|
+
import { parseTemplate, ParseTemplateResult } from "./astro-tools";
|
|
2
3
|
import * as AST from "./ast";
|
|
3
4
|
import { traverseNodes } from "./traverse";
|
|
4
5
|
import { ParseError } from "./errors";
|
|
5
6
|
export { AST, ParseError };
|
|
6
7
|
export { parseForESLint };
|
|
7
8
|
export declare const VisitorKeys: import("eslint").SourceCode.VisitorKeys;
|
|
8
|
-
export { traverseNodes };
|
|
9
|
+
export { traverseNodes, parseTemplate, ParseTemplateResult };
|
package/lib/index.js
CHANGED
|
@@ -23,9 +23,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.traverseNodes = exports.VisitorKeys = exports.parseForESLint = exports.ParseError = exports.AST = void 0;
|
|
26
|
+
exports.parseTemplate = exports.traverseNodes = exports.VisitorKeys = exports.parseForESLint = exports.ParseError = exports.AST = void 0;
|
|
27
27
|
const parser_1 = require("./parser");
|
|
28
28
|
Object.defineProperty(exports, "parseForESLint", { enumerable: true, get: function () { return parser_1.parseForESLint; } });
|
|
29
|
+
const astro_tools_1 = require("./astro-tools");
|
|
30
|
+
Object.defineProperty(exports, "parseTemplate", { enumerable: true, get: function () { return astro_tools_1.parseTemplate; } });
|
|
29
31
|
const AST = __importStar(require("./ast"));
|
|
30
32
|
exports.AST = AST;
|
|
31
33
|
const traverse_1 = require("./traverse");
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import type { ParseOptions } from "@astrojs/compiler";
|
|
1
|
+
import type { ParseOptions, ParseResult } from "@astrojs/compiler";
|
|
2
2
|
/**
|
|
3
3
|
* Parse code by `@astrojs/compiler`
|
|
4
4
|
*/
|
|
5
|
-
export declare function parse(code: string, options: ParseOptions):
|
|
6
|
-
ast: string;
|
|
7
|
-
};
|
|
5
|
+
export declare function parse(code: string, options: ParseOptions): ParseResult;
|
|
@@ -1,21 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.parse = void 0;
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const go = new wasm_exec_1.default();
|
|
10
|
-
const wasmBuffer = fs_1.default.readFileSync(require.resolve("@astrojs/compiler/astro.wasm"));
|
|
11
|
-
const mod = new WebAssembly.Module(wasmBuffer);
|
|
12
|
-
const instance = new WebAssembly.Instance(mod, go.importObject);
|
|
13
|
-
void go.run(instance);
|
|
14
|
-
const service = globalThis["@astrojs/compiler"];
|
|
4
|
+
const synckit_1 = require("synckit");
|
|
5
|
+
const parseSync = (0, synckit_1.createSyncFn)(require.resolve("./astrojs-compiler-worker"));
|
|
15
6
|
/**
|
|
16
7
|
* Parse code by `@astrojs/compiler`
|
|
17
8
|
*/
|
|
18
9
|
function parse(code, options) {
|
|
19
|
-
return
|
|
10
|
+
return parseSync(code, options);
|
|
20
11
|
}
|
|
21
12
|
exports.parse = parse;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const synckit_1 = require("synckit");
|
|
4
|
+
const dynamicImport = new Function("m", "return import(m)");
|
|
5
|
+
(0, synckit_1.runAsWorker)(async (source) => {
|
|
6
|
+
const { parse } = await dynamicImport("@astrojs/compiler");
|
|
7
|
+
// console.time("parse")
|
|
8
|
+
const result = parse(source);
|
|
9
|
+
// console.timeEnd("parse")
|
|
10
|
+
return result;
|
|
11
|
+
});
|
|
@@ -31,7 +31,11 @@ const errors_1 = require("../../errors");
|
|
|
31
31
|
* Parse code by `@astrojs/compiler`
|
|
32
32
|
*/
|
|
33
33
|
function parse(code, ctx) {
|
|
34
|
-
const ast =
|
|
34
|
+
const ast = service.parse(code, { position: true }).ast;
|
|
35
|
+
if (!ast.children) {
|
|
36
|
+
// If the source code is empty, the children property may not be available.
|
|
37
|
+
ast.children = [];
|
|
38
|
+
}
|
|
35
39
|
const htmlElement = ast.children.find((n) => n.type === "element" && n.name === "html");
|
|
36
40
|
if (htmlElement) {
|
|
37
41
|
adjustHTML(ast, htmlElement, ctx);
|
|
@@ -40,45 +44,17 @@ function parse(code, ctx) {
|
|
|
40
44
|
return { ast };
|
|
41
45
|
}
|
|
42
46
|
exports.parse = parse;
|
|
43
|
-
/**
|
|
44
|
-
* Parse code by `@astrojs/compiler`
|
|
45
|
-
*/
|
|
46
|
-
function parseByService(code, ctx) {
|
|
47
|
-
const jsonAst = service.parse(code, { position: true }).ast;
|
|
48
|
-
ctx.originalAST = jsonAst;
|
|
49
|
-
try {
|
|
50
|
-
const ast = JSON.parse(jsonAst);
|
|
51
|
-
ctx.originalAST = ast;
|
|
52
|
-
return { ast };
|
|
53
|
-
}
|
|
54
|
-
catch (_a) {
|
|
55
|
-
// FIXME: Workaround for escape bugs
|
|
56
|
-
// Adjust because may get the wrong escape as JSON.
|
|
57
|
-
const ast = JSON.parse(jsonAst.replace(/\\./gu, (m) => {
|
|
58
|
-
try {
|
|
59
|
-
JSON.parse(`"${m}"`);
|
|
60
|
-
return m;
|
|
61
|
-
}
|
|
62
|
-
catch (_a) {
|
|
63
|
-
return `\\${m}`;
|
|
64
|
-
}
|
|
65
|
-
}));
|
|
66
|
-
ctx.originalAST = ast;
|
|
67
|
-
return { ast };
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
47
|
/**
|
|
71
48
|
* Adjust <html> element node
|
|
72
49
|
*/
|
|
73
50
|
function adjustHTML(ast, htmlElement, ctx) {
|
|
74
|
-
var _a;
|
|
75
51
|
const htmlEnd = ctx.code.indexOf("</html");
|
|
76
52
|
if (htmlEnd == null) {
|
|
77
53
|
return;
|
|
78
54
|
}
|
|
79
55
|
const children = [...htmlElement.children];
|
|
80
56
|
for (const child of children) {
|
|
81
|
-
const offset =
|
|
57
|
+
const offset = child.position?.start.offset;
|
|
82
58
|
if (offset != null) {
|
|
83
59
|
if (htmlEnd <= offset) {
|
|
84
60
|
htmlElement.children.splice(htmlElement.children.indexOf(child), 1);
|
|
@@ -94,14 +70,13 @@ function adjustHTML(ast, htmlElement, ctx) {
|
|
|
94
70
|
* Adjust <body> element node
|
|
95
71
|
*/
|
|
96
72
|
function adjustHTMLBody(ast, htmlElement, htmlEnd, bodyElement, ctx) {
|
|
97
|
-
var _a;
|
|
98
73
|
const bodyEnd = ctx.code.indexOf("</body");
|
|
99
74
|
if (bodyEnd == null) {
|
|
100
75
|
return;
|
|
101
76
|
}
|
|
102
77
|
const children = [...bodyElement.children];
|
|
103
78
|
for (const child of children) {
|
|
104
|
-
const offset =
|
|
79
|
+
const offset = child.position?.start.offset;
|
|
105
80
|
if (offset != null) {
|
|
106
81
|
if (bodyEnd <= offset) {
|
|
107
82
|
bodyElement.children.splice(bodyElement.children.indexOf(child), 1);
|
|
@@ -165,8 +140,6 @@ function fixLocations(node, ctx) {
|
|
|
165
140
|
if (start < 0) {
|
|
166
141
|
start = ctx.code.length;
|
|
167
142
|
}
|
|
168
|
-
// FIXME: Workaround for escape bugs
|
|
169
|
-
node.value = ctx.code.slice(node.position.start.offset, start);
|
|
170
143
|
}
|
|
171
144
|
else {
|
|
172
145
|
const index = tokenIndexSafe(ctx.code, node.value, start);
|
|
@@ -175,25 +148,12 @@ function fixLocations(node, ctx) {
|
|
|
175
148
|
start += node.value.length;
|
|
176
149
|
}
|
|
177
150
|
else {
|
|
178
|
-
// FIXME:
|
|
151
|
+
// FIXME: Some white space may be removed.
|
|
179
152
|
node.position.start.offset = start;
|
|
180
153
|
const value = node.value.replace(/\s+/gu, "");
|
|
181
|
-
for (
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
if (index != null) {
|
|
185
|
-
start = index + 1;
|
|
186
|
-
continue;
|
|
187
|
-
}
|
|
188
|
-
start = (0, astro_1.skipSpaces)(ctx.code, start);
|
|
189
|
-
if (ctx.code.startsWith("\\", start)) {
|
|
190
|
-
const codeChar = JSON.parse(`"\\${ctx.code[start + 1]}"`);
|
|
191
|
-
start += 2;
|
|
192
|
-
if (codeChar === char) {
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
start = tokenIndex(ctx, char, start) + 1;
|
|
154
|
+
for (const char of value) {
|
|
155
|
+
const index = tokenIndex(ctx, char, start);
|
|
156
|
+
start = index + 1;
|
|
197
157
|
}
|
|
198
158
|
start = (0, astro_1.skipSpaces)(ctx.code, start);
|
|
199
159
|
node.value = ctx.code.slice(node.position.start.offset, start);
|
package/lib/parser/index.d.ts
CHANGED
|
@@ -1,19 +1,6 @@
|
|
|
1
|
-
import { Context } from "../context";
|
|
2
1
|
import type { AstroProgram } from "../ast";
|
|
3
|
-
import type { TSESTree } from "@typescript-eslint/types";
|
|
4
2
|
import type { ScopeManager } from "eslint-scope";
|
|
5
3
|
import type { ParseResult } from "@astrojs/compiler";
|
|
6
|
-
/**
|
|
7
|
-
* The parsing result of ESLint custom parsers.
|
|
8
|
-
*/
|
|
9
|
-
export interface ESLintExtendedProgram {
|
|
10
|
-
ast: TSESTree.Program;
|
|
11
|
-
services?: Record<string, any>;
|
|
12
|
-
visitorKeys?: {
|
|
13
|
-
[type: string]: string[];
|
|
14
|
-
};
|
|
15
|
-
scopeManager?: ScopeManager;
|
|
16
|
-
}
|
|
17
4
|
/**
|
|
18
5
|
* Parse source code
|
|
19
6
|
*/
|
|
@@ -28,7 +15,3 @@ export declare function parseForESLint(code: string, options?: any): {
|
|
|
28
15
|
};
|
|
29
16
|
scopeManager: ScopeManager;
|
|
30
17
|
};
|
|
31
|
-
/**
|
|
32
|
-
* Parse for template
|
|
33
|
-
*/
|
|
34
|
-
export declare function parseTemplate(code: string, ctx: Context): ParseResult;
|
package/lib/parser/index.js
CHANGED
|
@@ -1,28 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.parseForESLint = void 0;
|
|
4
4
|
const visitor_keys_1 = require("../visitor-keys");
|
|
5
|
-
const context_1 = require("../context");
|
|
6
5
|
const types_1 = require("@typescript-eslint/types");
|
|
7
6
|
const script_1 = require("./script");
|
|
8
7
|
const sort_1 = require("./sort");
|
|
9
|
-
const errors_1 = require("../errors");
|
|
10
|
-
const parse_1 = require("./astro-parser/parse");
|
|
11
8
|
const process_template_1 = require("./process-template");
|
|
9
|
+
const template_1 = require("./template");
|
|
10
|
+
const parser_options_1 = require("../context/parser-options");
|
|
12
11
|
/**
|
|
13
12
|
* Parse source code
|
|
14
13
|
*/
|
|
15
14
|
function parseForESLint(code, options) {
|
|
16
|
-
const
|
|
17
|
-
parserOptions.ecmaFeatures = Object.assign(Object.assign({}, (parserOptions.ecmaFeatures || {})), { jsx: true });
|
|
18
|
-
parserOptions.sourceType = "module";
|
|
19
|
-
if (parserOptions.ecmaVersion <= 5 || parserOptions.ecmaVersion == null) {
|
|
20
|
-
parserOptions.ecmaVersion = 2015;
|
|
21
|
-
}
|
|
22
|
-
const ctx = new context_1.Context(code, parserOptions);
|
|
23
|
-
const resultTemplate = parseTemplate(ctx.code, ctx);
|
|
15
|
+
const { result: resultTemplate, context: ctx } = (0, template_1.parseTemplate)(code);
|
|
24
16
|
const scriptContext = (0, process_template_1.processTemplate)(ctx, resultTemplate);
|
|
25
|
-
const
|
|
17
|
+
const parserOptions = new parser_options_1.ParserOptionsContext(options);
|
|
18
|
+
const resultScript = (0, script_1.parseScript)(scriptContext.script, ctx, parserOptions);
|
|
26
19
|
scriptContext.restore(resultScript);
|
|
27
20
|
(0, sort_1.sort)(resultScript.ast.comments);
|
|
28
21
|
(0, sort_1.sort)(resultScript.ast.tokens);
|
|
@@ -34,7 +27,6 @@ function parseForESLint(code, options) {
|
|
|
34
27
|
},
|
|
35
28
|
});
|
|
36
29
|
resultScript.visitorKeys = Object.assign({}, visitor_keys_1.KEYS, resultScript.visitorKeys);
|
|
37
|
-
ctx.remapCR(resultScript);
|
|
38
30
|
return resultScript;
|
|
39
31
|
}
|
|
40
32
|
exports.parseForESLint = parseForESLint;
|
|
@@ -77,20 +69,3 @@ function extractTokens(ast, ctx) {
|
|
|
77
69
|
return /^[^\w$]$/iu.test(c);
|
|
78
70
|
}
|
|
79
71
|
}
|
|
80
|
-
/**
|
|
81
|
-
* Parse for template
|
|
82
|
-
*/
|
|
83
|
-
function parseTemplate(code, ctx) {
|
|
84
|
-
try {
|
|
85
|
-
return (0, parse_1.parse)(code, ctx);
|
|
86
|
-
}
|
|
87
|
-
catch (e) {
|
|
88
|
-
if (typeof e.pos === "number") {
|
|
89
|
-
const err = new errors_1.ParseError(e.message, e.pos, ctx);
|
|
90
|
-
err.astroCompilerError = e;
|
|
91
|
-
throw err;
|
|
92
|
-
}
|
|
93
|
-
throw e;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
exports.parseTemplate = parseTemplate;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LruCache = void 0;
|
|
4
|
+
class LruCache extends Map {
|
|
5
|
+
constructor(capacity) {
|
|
6
|
+
super();
|
|
7
|
+
this.capacity = capacity;
|
|
8
|
+
}
|
|
9
|
+
get(key) {
|
|
10
|
+
if (!this.has(key)) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const value = super.get(key);
|
|
14
|
+
this.set(key, value);
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
set(key, value) {
|
|
18
|
+
this.delete(key);
|
|
19
|
+
super.set(key, value);
|
|
20
|
+
if (this.size > this.capacity) {
|
|
21
|
+
this.deleteOldestEntry();
|
|
22
|
+
}
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
deleteOldestEntry() {
|
|
26
|
+
for (const entry of this) {
|
|
27
|
+
this.delete(entry[0]);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.LruCache = LruCache;
|
|
@@ -11,30 +11,47 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
11
11
|
let uniqueIdSeq = 0;
|
|
12
12
|
const usedUniqueIds = new Set();
|
|
13
13
|
const script = new script_1.ScriptContext(ctx);
|
|
14
|
-
const frontmatter = resultTemplate.ast.children.find((n) => n.type === "frontmatter");
|
|
15
14
|
let fragmentOpened = false;
|
|
16
|
-
|
|
15
|
+
/** Open astro root fragment */
|
|
16
|
+
function openRootFragment(startOffset) {
|
|
17
17
|
script.appendScript("<>");
|
|
18
18
|
fragmentOpened = true;
|
|
19
|
+
script.addRestoreNodeProcess((scriptNode, { result }) => {
|
|
20
|
+
if (scriptNode.type === types_1.AST_NODE_TYPES.ExpressionStatement &&
|
|
21
|
+
scriptNode.expression.type === types_1.AST_NODE_TYPES.JSXFragment &&
|
|
22
|
+
scriptNode.range[0] === startOffset &&
|
|
23
|
+
result.ast.body.includes(scriptNode)) {
|
|
24
|
+
const index = result.ast.body.indexOf(scriptNode);
|
|
25
|
+
const rootFragment = (result.ast.body[index] =
|
|
26
|
+
scriptNode.expression);
|
|
27
|
+
delete rootFragment.closingFragment;
|
|
28
|
+
delete rootFragment.openingFragment;
|
|
29
|
+
rootFragment.type = "AstroFragment";
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
});
|
|
19
34
|
}
|
|
20
35
|
(0, astro_1.walkElements)(resultTemplate.ast, ctx.code,
|
|
21
36
|
// eslint-disable-next-line complexity -- X(
|
|
22
37
|
(node, [parent]) => {
|
|
23
38
|
if (node.type === "frontmatter") {
|
|
24
39
|
const start = node.position.start.offset;
|
|
40
|
+
if (fragmentOpened) {
|
|
41
|
+
script.appendScript("</>;");
|
|
42
|
+
fragmentOpened = false;
|
|
43
|
+
}
|
|
25
44
|
script.appendOriginal(start);
|
|
26
45
|
script.skipOriginalOffset(3);
|
|
27
46
|
const end = (0, astro_1.getEndOffset)(node, ctx);
|
|
28
47
|
script.appendOriginal(end - 3);
|
|
29
|
-
script.appendScript("
|
|
30
|
-
fragmentOpened = true;
|
|
48
|
+
script.appendScript(";");
|
|
31
49
|
script.skipOriginalOffset(3);
|
|
32
50
|
script.addRestoreNodeProcess((_scriptNode, { result }) => {
|
|
33
51
|
for (let index = 0; index < result.ast.body.length; index++) {
|
|
34
52
|
const st = result.ast.body[index];
|
|
35
53
|
if (st.type === types_1.AST_NODE_TYPES.EmptyStatement) {
|
|
36
|
-
if (st.range[0] === end - 3 &&
|
|
37
|
-
st.range[1] === end) {
|
|
54
|
+
if (st.range[0] === end - 3 && st.range[1] <= end) {
|
|
38
55
|
result.ast.body.splice(index, 1);
|
|
39
56
|
break;
|
|
40
57
|
}
|
|
@@ -81,6 +98,11 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
81
98
|
}
|
|
82
99
|
}
|
|
83
100
|
}
|
|
101
|
+
const start = node.position.start.offset;
|
|
102
|
+
script.appendOriginal(start);
|
|
103
|
+
if (!fragmentOpened) {
|
|
104
|
+
openRootFragment(start);
|
|
105
|
+
}
|
|
84
106
|
// Process for attributes
|
|
85
107
|
for (const attr of node.attributes) {
|
|
86
108
|
if ((node.type === "component" ||
|
|
@@ -112,10 +134,23 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
112
134
|
types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
113
135
|
scriptNode.range[0] === start) {
|
|
114
136
|
const baseNameNode = scriptNode.name;
|
|
115
|
-
const nsn =
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
137
|
+
const nsn = {
|
|
138
|
+
...baseNameNode,
|
|
139
|
+
type: types_1.AST_NODE_TYPES.JSXNamespacedName,
|
|
140
|
+
namespace: {
|
|
141
|
+
type: types_1.AST_NODE_TYPES.JSXIdentifier,
|
|
142
|
+
name: attr.name.slice(0, colonIndex),
|
|
143
|
+
...ctx.getLocations(baseNameNode.range[0], baseNameNode.range[0] +
|
|
144
|
+
colonIndex),
|
|
145
|
+
},
|
|
146
|
+
name: {
|
|
147
|
+
type: types_1.AST_NODE_TYPES.JSXIdentifier,
|
|
148
|
+
name: attr.name.slice(colonIndex + 1),
|
|
149
|
+
...ctx.getLocations(baseNameNode.range[0] +
|
|
150
|
+
colonIndex +
|
|
151
|
+
1, baseNameNode.range[1]),
|
|
152
|
+
},
|
|
153
|
+
};
|
|
119
154
|
scriptNode.name = nsn;
|
|
120
155
|
nsn.namespace.parent = nsn;
|
|
121
156
|
nsn.name.parent = nsn;
|
|
@@ -190,7 +225,13 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
190
225
|
script.addRestoreNodeProcess((scriptNode) => {
|
|
191
226
|
if (scriptNode.type === types_1.AST_NODE_TYPES.JSXElement &&
|
|
192
227
|
scriptNode.range[0] === styleNodeStart) {
|
|
193
|
-
const textNode =
|
|
228
|
+
const textNode = {
|
|
229
|
+
type: "AstroRawText",
|
|
230
|
+
value: text.value,
|
|
231
|
+
raw: text.value,
|
|
232
|
+
parent: scriptNode,
|
|
233
|
+
...ctx.getLocations(start, start + text.value.length),
|
|
234
|
+
};
|
|
194
235
|
scriptNode.children = [
|
|
195
236
|
textNode,
|
|
196
237
|
];
|
|
@@ -210,21 +251,16 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
210
251
|
const end = (0, astro_1.getEndOffset)(node, ctx);
|
|
211
252
|
const length = end - start;
|
|
212
253
|
script.appendOriginal(start);
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
script.appendOriginal(start + 1);
|
|
216
|
-
script.appendScript(`></`);
|
|
217
|
-
script.skipOriginalOffset(length - 2);
|
|
218
|
-
targetType = types_1.AST_NODE_TYPES.JSXFragment;
|
|
219
|
-
}
|
|
220
|
-
else {
|
|
221
|
-
script.appendScript(`0;`);
|
|
222
|
-
targetType = types_1.AST_NODE_TYPES.ExpressionStatement;
|
|
223
|
-
script.skipOriginalOffset(length);
|
|
254
|
+
if (!fragmentOpened) {
|
|
255
|
+
openRootFragment(start);
|
|
224
256
|
}
|
|
257
|
+
script.appendOriginal(start + 1);
|
|
258
|
+
script.appendScript(`></`);
|
|
259
|
+
script.skipOriginalOffset(length - 2);
|
|
260
|
+
script.appendOriginal(end);
|
|
225
261
|
script.addRestoreNodeProcess((scriptNode, context) => {
|
|
226
262
|
if (scriptNode.range[0] === start &&
|
|
227
|
-
scriptNode.type ===
|
|
263
|
+
scriptNode.type === types_1.AST_NODE_TYPES.JSXFragment) {
|
|
228
264
|
delete scriptNode.children;
|
|
229
265
|
delete scriptNode.openingFragment;
|
|
230
266
|
delete scriptNode.closingFragment;
|
|
@@ -232,12 +268,10 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
232
268
|
const commentNode = scriptNode;
|
|
233
269
|
commentNode.type = "AstroHTMLComment";
|
|
234
270
|
commentNode.value = node.value;
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
token.range[1] === scriptNode.range[1]);
|
|
240
|
-
}
|
|
271
|
+
context.addRemoveToken((token) => token.value === "<" &&
|
|
272
|
+
token.range[0] === scriptNode.range[0]);
|
|
273
|
+
context.addRemoveToken((token) => token.value === ">" &&
|
|
274
|
+
token.range[1] === scriptNode.range[1]);
|
|
241
275
|
return true;
|
|
242
276
|
}
|
|
243
277
|
return false;
|
|
@@ -252,39 +286,39 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
252
286
|
const end = (0, astro_1.getEndOffset)(node, ctx);
|
|
253
287
|
const length = end - start;
|
|
254
288
|
script.appendOriginal(start);
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
script.appendOriginal(start + 1);
|
|
258
|
-
script.appendScript(`></`);
|
|
259
|
-
script.skipOriginalOffset(length - 2);
|
|
260
|
-
targetType = types_1.AST_NODE_TYPES.JSXFragment;
|
|
261
|
-
}
|
|
262
|
-
else {
|
|
263
|
-
script.appendScript(`0;`);
|
|
264
|
-
targetType = types_1.AST_NODE_TYPES.ExpressionStatement;
|
|
265
|
-
script.skipOriginalOffset(length);
|
|
289
|
+
if (!fragmentOpened) {
|
|
290
|
+
openRootFragment(start);
|
|
266
291
|
}
|
|
292
|
+
script.appendOriginal(start + 1);
|
|
293
|
+
script.appendScript(`></`);
|
|
294
|
+
script.skipOriginalOffset(length - 2);
|
|
295
|
+
script.appendOriginal(end);
|
|
267
296
|
script.addRestoreNodeProcess((scriptNode, context) => {
|
|
268
297
|
if (scriptNode.range[0] === start &&
|
|
269
|
-
scriptNode.type ===
|
|
298
|
+
scriptNode.type === types_1.AST_NODE_TYPES.JSXFragment) {
|
|
270
299
|
delete scriptNode.children;
|
|
271
300
|
delete scriptNode.openingFragment;
|
|
272
301
|
delete scriptNode.closingFragment;
|
|
273
302
|
delete scriptNode.expression;
|
|
274
303
|
const doctypeNode = scriptNode;
|
|
275
304
|
doctypeNode.type = "AstroDoctype";
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
token.range[1] === scriptNode.range[1]);
|
|
281
|
-
}
|
|
305
|
+
context.addRemoveToken((token) => token.value === "<" &&
|
|
306
|
+
token.range[0] === scriptNode.range[0]);
|
|
307
|
+
context.addRemoveToken((token) => token.value === ">" &&
|
|
308
|
+
token.range[1] === scriptNode.range[1]);
|
|
282
309
|
return true;
|
|
283
310
|
}
|
|
284
311
|
return false;
|
|
285
312
|
});
|
|
286
313
|
script.addToken("HTMLDocType", [start, end]);
|
|
287
314
|
}
|
|
315
|
+
else {
|
|
316
|
+
const start = node.position.start.offset;
|
|
317
|
+
script.appendOriginal(start);
|
|
318
|
+
if (!fragmentOpened) {
|
|
319
|
+
openRootFragment(start);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
288
322
|
}, (node, [parent]) => {
|
|
289
323
|
if ((0, astro_1.isTag)(node)) {
|
|
290
324
|
const closing = (0, astro_1.getSelfClosingTag)(node, ctx);
|
|
@@ -323,8 +357,13 @@ function processTemplate(ctx, resultTemplate) {
|
|
|
323
357
|
}
|
|
324
358
|
}
|
|
325
359
|
});
|
|
360
|
+
if (fragmentOpened) {
|
|
361
|
+
const last = resultTemplate.ast.children[resultTemplate.ast.children.length - 1];
|
|
362
|
+
const end = (0, astro_1.getEndOffset)(last, ctx);
|
|
363
|
+
script.appendOriginal(end);
|
|
364
|
+
script.appendScript("</>");
|
|
365
|
+
}
|
|
326
366
|
script.appendOriginal(ctx.code.length);
|
|
327
|
-
script.appendScript("</>");
|
|
328
367
|
return script;
|
|
329
368
|
/**
|
|
330
369
|
* Generate unique id
|
package/lib/parser/script.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { ESLintExtendedProgram } from ".";
|
|
2
1
|
import type { Context } from "../context";
|
|
2
|
+
import type { ParserOptionsContext } from "../context/parser-options";
|
|
3
|
+
import type { ESLintExtendedProgram } from "../types";
|
|
3
4
|
/**
|
|
4
5
|
* Parse for script
|
|
5
6
|
*/
|
|
6
|
-
export declare function parseScript(code: string,
|
|
7
|
+
export declare function parseScript(code: string, _ctx: Context, parserOptions: ParserOptionsContext): ESLintExtendedProgram;
|