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
package/lib/parser/script.js
CHANGED
|
@@ -4,26 +4,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.parseScript = void 0;
|
|
7
|
-
const resolve_parser_1 = require("./resolve-parser");
|
|
8
7
|
const fs_1 = __importDefault(require("fs"));
|
|
9
8
|
const debug_1 = require("../debug");
|
|
10
9
|
/**
|
|
11
10
|
* Parse for script
|
|
12
11
|
*/
|
|
13
|
-
function parseScript(code,
|
|
14
|
-
|
|
15
|
-
const parser = (0, resolve_parser_1.getParser)({}, ctx.parserOptions.parser);
|
|
12
|
+
function parseScript(code, _ctx, parserOptions) {
|
|
13
|
+
const parser = parserOptions.getParser();
|
|
16
14
|
let removeFile = null;
|
|
17
15
|
try {
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
const scriptParserOptions = { ...parserOptions.parserOptions };
|
|
17
|
+
if (parserOptions.isTypeScript() &&
|
|
18
|
+
scriptParserOptions.filePath &&
|
|
19
|
+
scriptParserOptions.project) {
|
|
20
|
+
scriptParserOptions.filePath += ".tsx";
|
|
21
|
+
if (!fs_1.default.existsSync(scriptParserOptions.filePath)) {
|
|
22
|
+
fs_1.default.writeFileSync(scriptParserOptions.filePath, "/* temp for astro-eslint-parser */");
|
|
23
|
+
removeFile = scriptParserOptions.filePath;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
-
const result =
|
|
26
|
+
const result = parser.parseForESLint?.(code, scriptParserOptions) ??
|
|
27
|
+
parser.parse?.(code, scriptParserOptions);
|
|
27
28
|
if ("ast" in result && result.ast != null) {
|
|
28
29
|
return result;
|
|
29
30
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ParseResult } from "@astrojs/compiler";
|
|
2
|
+
import { Context } from "../context";
|
|
3
|
+
export declare type TemplateResult = {
|
|
4
|
+
result: ParseResult;
|
|
5
|
+
context: Context;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Parse the astro component template.
|
|
9
|
+
*/
|
|
10
|
+
export declare function parseTemplate(code: string): TemplateResult;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseTemplate = void 0;
|
|
4
|
+
const astro_1 = require("../astro");
|
|
5
|
+
const context_1 = require("../context");
|
|
6
|
+
const errors_1 = require("../errors");
|
|
7
|
+
const parse_1 = require("./astro-parser/parse");
|
|
8
|
+
const lru_cache_1 = require("./lru-cache");
|
|
9
|
+
const lruCache = new lru_cache_1.LruCache(5);
|
|
10
|
+
/**
|
|
11
|
+
* Parse the astro component template.
|
|
12
|
+
*/
|
|
13
|
+
function parseTemplate(code) {
|
|
14
|
+
const cache = lruCache.get(code);
|
|
15
|
+
if (cache) {
|
|
16
|
+
return cache;
|
|
17
|
+
}
|
|
18
|
+
const ctx = new context_1.Context(code);
|
|
19
|
+
const normalized = ctx.locs.getNormalizedLineFeed();
|
|
20
|
+
const ctxForAstro = normalized.needRemap
|
|
21
|
+
? new context_1.Context(normalized.code)
|
|
22
|
+
: ctx;
|
|
23
|
+
try {
|
|
24
|
+
const result = (0, parse_1.parse)(normalized?.code ?? code, ctxForAstro);
|
|
25
|
+
if (normalized.needRemap) {
|
|
26
|
+
remap(result, normalized, code, ctxForAstro);
|
|
27
|
+
ctx.originalAST = ctxForAstro.originalAST;
|
|
28
|
+
}
|
|
29
|
+
const templateResult = {
|
|
30
|
+
result,
|
|
31
|
+
context: ctx,
|
|
32
|
+
};
|
|
33
|
+
lruCache.set(code, templateResult);
|
|
34
|
+
return templateResult;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
if (typeof e.pos === "number") {
|
|
38
|
+
const err = new errors_1.ParseError(e.message, normalized?.remapIndex(e.pos), ctx);
|
|
39
|
+
err.astroCompilerError = e;
|
|
40
|
+
throw err;
|
|
41
|
+
}
|
|
42
|
+
throw e;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.parseTemplate = parseTemplate;
|
|
46
|
+
/** Remap */
|
|
47
|
+
function remap(result, normalized, originalCode, ctxForAstro) {
|
|
48
|
+
const remapDataMap = new Map();
|
|
49
|
+
(0, astro_1.walk)(result.ast, normalized.code, (node) => {
|
|
50
|
+
const start = normalized.remapIndex(node.position.start.offset);
|
|
51
|
+
let end, value;
|
|
52
|
+
if (node.position.end) {
|
|
53
|
+
end = normalized.remapIndex(node.position.end.offset);
|
|
54
|
+
if (node.position.start.offset === start &&
|
|
55
|
+
node.position.end.offset === end) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (node.type === "text") {
|
|
60
|
+
value = originalCode.slice(start, normalized.remapIndex((0, astro_1.getEndOffset)(node, ctxForAstro)));
|
|
61
|
+
}
|
|
62
|
+
else if (node.type === "comment") {
|
|
63
|
+
value = originalCode.slice(start + 4, normalized.remapIndex((0, astro_1.getEndOffset)(node, ctxForAstro)) - 3);
|
|
64
|
+
}
|
|
65
|
+
else if (node.type === "attribute") {
|
|
66
|
+
if (node.kind !== "empty" &&
|
|
67
|
+
node.kind !== "shorthand" &&
|
|
68
|
+
node.kind !== "spread") {
|
|
69
|
+
let valueStart = normalized.remapIndex((0, astro_1.calcAttributeValueStartOffset)(node, ctxForAstro));
|
|
70
|
+
let valueEnd = normalized.remapIndex((0, astro_1.calcAttributeEndOffset)(node, ctxForAstro));
|
|
71
|
+
if (node.kind !== "quoted" ||
|
|
72
|
+
originalCode[valueStart] === '"' ||
|
|
73
|
+
originalCode[valueStart] === "'") {
|
|
74
|
+
valueStart++;
|
|
75
|
+
valueEnd--;
|
|
76
|
+
}
|
|
77
|
+
value = originalCode.slice(valueStart, valueEnd);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
remapDataMap.set(node, {
|
|
81
|
+
start,
|
|
82
|
+
end,
|
|
83
|
+
value,
|
|
84
|
+
});
|
|
85
|
+
}, (_node) => {
|
|
86
|
+
/* noop */
|
|
87
|
+
});
|
|
88
|
+
for (const [node, remapData] of remapDataMap) {
|
|
89
|
+
node.position.start.offset = remapData.start;
|
|
90
|
+
if (node.position.end) {
|
|
91
|
+
node.position.end.offset = remapData.end;
|
|
92
|
+
}
|
|
93
|
+
if (node.type === "text" ||
|
|
94
|
+
node.type === "comment" ||
|
|
95
|
+
(node.type === "attribute" &&
|
|
96
|
+
node.kind !== "empty" &&
|
|
97
|
+
node.kind !== "shorthand" &&
|
|
98
|
+
node.kind !== "spread")) {
|
|
99
|
+
node.value = remapData.value;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { TSESTree } from "@typescript-eslint/types";
|
|
2
|
+
import type { ScopeManager } from "eslint-scope";
|
|
3
|
+
/**
|
|
4
|
+
* The parsing result of ESLint custom parsers.
|
|
5
|
+
*/
|
|
6
|
+
export interface ESLintExtendedProgram {
|
|
7
|
+
ast: TSESTree.Program;
|
|
8
|
+
services?: Record<string, any>;
|
|
9
|
+
visitorKeys?: {
|
|
10
|
+
[type: string]: string[];
|
|
11
|
+
};
|
|
12
|
+
scopeManager?: ScopeManager;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The interface of a result of ESLint custom parser.
|
|
16
|
+
*/
|
|
17
|
+
export declare type ESLintCustomParserResult = ESLintExtendedProgram["ast"] | ESLintExtendedProgram;
|
|
18
|
+
export interface ESLintCustomParser {
|
|
19
|
+
parse(code: string, options: any): ESLintCustomParserResult;
|
|
20
|
+
parseForESLint?(code: string, options: any): ESLintCustomParserResult;
|
|
21
|
+
}
|
package/lib/types.js
ADDED
package/lib/visitor-keys.js
CHANGED
|
@@ -4,12 +4,11 @@ exports.KEYS = void 0;
|
|
|
4
4
|
const eslint_visitor_keys_1 = require("eslint-visitor-keys");
|
|
5
5
|
const astroKeys = {
|
|
6
6
|
Program: ["body"],
|
|
7
|
-
|
|
7
|
+
AstroFragment: ["children"],
|
|
8
8
|
AstroHTMLComment: [],
|
|
9
9
|
AstroDoctype: [],
|
|
10
10
|
AstroShorthandAttribute: ["name", "value"],
|
|
11
11
|
AstroTemplateLiteralAttribute: ["name", "value"],
|
|
12
12
|
AstroRawText: [],
|
|
13
|
-
AstroFragment: ["children"],
|
|
14
13
|
};
|
|
15
14
|
exports.KEYS = (0, eslint_visitor_keys_1.unionWith)(astroKeys);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-eslint-parser",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "Astro parser for ESLint",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Astro component parser for ESLint",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib"
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"prebuild": "npm run -s clean",
|
|
14
14
|
"build": "tsc --project ./tsconfig.build.json",
|
|
15
15
|
"clean": "rimraf .nyc_output lib coverage",
|
|
16
|
-
"lint": "eslint . --ext .js,.ts,.json",
|
|
16
|
+
"lint": "eslint . --ext .js,.ts,.json,.astro,.svelte",
|
|
17
17
|
"eslint-fix": "npm run lint -- --fix",
|
|
18
18
|
"test": "mocha --require ts-node/register \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
|
|
19
19
|
"cover": "nyc --reporter=lcov npm run test",
|
|
@@ -42,13 +42,16 @@
|
|
|
42
42
|
},
|
|
43
43
|
"homepage": "https://github.com/ota-meshi/astro-eslint-parser#readme",
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@astrojs/compiler": "^0.14.
|
|
45
|
+
"@astrojs/compiler": "^0.14.3",
|
|
46
|
+
"@typescript-eslint/types": "^5.25.0",
|
|
46
47
|
"debug": "^4.3.4",
|
|
47
48
|
"eslint-visitor-keys": "^3.0.0",
|
|
48
|
-
"espree": "^9.0.0"
|
|
49
|
+
"espree": "^9.0.0",
|
|
50
|
+
"synckit": "^0.7.1"
|
|
49
51
|
},
|
|
50
52
|
"devDependencies": {
|
|
51
53
|
"@ota-meshi/eslint-plugin": "^0.10.0",
|
|
54
|
+
"@ota-meshi/eslint-plugin-svelte": "^0.34.0",
|
|
52
55
|
"@types/benchmark": "^2.1.1",
|
|
53
56
|
"@types/chai": "^4.3.0",
|
|
54
57
|
"@types/debug": "^4.1.7",
|
|
@@ -64,18 +67,17 @@
|
|
|
64
67
|
"benchmark": "^2.1.4",
|
|
65
68
|
"chai": "^4.3.4",
|
|
66
69
|
"code-red": "^0.2.3",
|
|
67
|
-
"eslint": "^8.
|
|
70
|
+
"eslint": "^8.15.0",
|
|
68
71
|
"eslint-config-prettier": "^8.3.0",
|
|
69
72
|
"eslint-formatter-codeframe": "^7.32.1",
|
|
70
73
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
|
71
|
-
"eslint-plugin-json-schema-validator": "^
|
|
74
|
+
"eslint-plugin-json-schema-validator": "^3.0.0",
|
|
72
75
|
"eslint-plugin-jsonc": "^2.0.0",
|
|
73
76
|
"eslint-plugin-node": "^11.1.0",
|
|
74
77
|
"eslint-plugin-node-dependencies": "^0.8.0",
|
|
75
78
|
"eslint-plugin-prettier": "^4.0.0",
|
|
76
79
|
"eslint-plugin-react": "^7.29.4",
|
|
77
80
|
"eslint-plugin-regexp": "^1.5.0",
|
|
78
|
-
"eslint-plugin-vue": "^8.0.3",
|
|
79
81
|
"estree-walker": "^3.0.0",
|
|
80
82
|
"locate-character": "^2.0.5",
|
|
81
83
|
"magic-string": "^0.26.0",
|
|
@@ -83,11 +85,12 @@
|
|
|
83
85
|
"mocha-chai-jest-snapshot": "^1.1.3",
|
|
84
86
|
"nyc": "^15.1.0",
|
|
85
87
|
"prettier": "^2.0.5",
|
|
86
|
-
"prettier-plugin-astro": "^0.0
|
|
88
|
+
"prettier-plugin-astro": "^0.1.0-0",
|
|
89
|
+
"prettier-plugin-svelte": "^2.7.0",
|
|
87
90
|
"semver": "^7.3.5",
|
|
88
91
|
"string-replace-loader": "^3.0.3",
|
|
92
|
+
"svelte": "^3.48.0",
|
|
89
93
|
"ts-node": "^10.4.0",
|
|
90
|
-
"typescript": "~4.6.0"
|
|
91
|
-
"vue-eslint-parser": "^8.0.1"
|
|
94
|
+
"typescript": "~4.6.0"
|
|
92
95
|
}
|
|
93
96
|
}
|
package/lib/ast.d.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import type { TSESTree } from "@typescript-eslint/types";
|
|
2
|
-
export declare type AstroNode = AstroProgram | AstroRootFragment | AstroHTMLComment | AstroDoctype | AstroShorthandAttribute | AstroTemplateLiteralAttribute | AstroRawText | AstroFragment;
|
|
3
|
-
/** Node of Astro program root */
|
|
4
|
-
export interface AstroProgram extends Omit<TSESTree.Program, "type" | "body"> {
|
|
5
|
-
type: "Program";
|
|
6
|
-
body: (TSESTree.Program["body"][number] | AstroRootFragment | AstroHTMLComment)[];
|
|
7
|
-
sourceType: "script" | "module";
|
|
8
|
-
comments: TSESTree.Comment[];
|
|
9
|
-
tokens: TSESTree.Token[];
|
|
10
|
-
parent: undefined;
|
|
11
|
-
}
|
|
12
|
-
/** Node of Astro fragment root */
|
|
13
|
-
export interface AstroRootFragment extends Omit<TSESTree.BaseNode, "type" | "parent"> {
|
|
14
|
-
type: "AstroRootFragment";
|
|
15
|
-
children: TSESTree.JSXFragment["children"];
|
|
16
|
-
parent: AstroProgram;
|
|
17
|
-
}
|
|
18
|
-
/** Node of Astro html comment */
|
|
19
|
-
export interface AstroHTMLComment extends Omit<TSESTree.BaseNode, "type" | "parent"> {
|
|
20
|
-
type: "AstroHTMLComment";
|
|
21
|
-
value: string;
|
|
22
|
-
parent: AstroRootFragment | TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
23
|
-
}
|
|
24
|
-
/** Node of Astro doctype */
|
|
25
|
-
export interface AstroDoctype extends Omit<TSESTree.BaseNode, "type" | "parent"> {
|
|
26
|
-
type: "AstroDoctype";
|
|
27
|
-
parent: AstroRootFragment;
|
|
28
|
-
}
|
|
29
|
-
/** Node of Astro shorthand attribute */
|
|
30
|
-
export interface AstroShorthandAttribute extends Omit<TSESTree.JSXAttribute, "type" | "parent"> {
|
|
31
|
-
type: "AstroShorthandAttribute";
|
|
32
|
-
value: TSESTree.JSXExpressionContainer;
|
|
33
|
-
parent: TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
34
|
-
}
|
|
35
|
-
/** Node of Astro template-literal attribute */
|
|
36
|
-
export interface AstroTemplateLiteralAttribute extends Omit<TSESTree.JSXAttribute, "type" | "parent"> {
|
|
37
|
-
type: "AstroTemplateLiteralAttribute";
|
|
38
|
-
value: TSESTree.JSXExpressionContainer & {
|
|
39
|
-
expression: TSESTree.TemplateLiteral;
|
|
40
|
-
};
|
|
41
|
-
parent: TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
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
|
-
}
|
|
48
|
-
/** Node of Astro fragment expression */
|
|
49
|
-
export interface AstroFragment extends Omit<TSESTree.BaseNode, "type" | "parent"> {
|
|
50
|
-
type: "AstroFragment";
|
|
51
|
-
children: TSESTree.JSXFragment["children"];
|
|
52
|
-
parent: TSESTree.JSXFragment["parent"];
|
|
53
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export default class Go {
|
|
2
|
-
importObject: {
|
|
3
|
-
go: {
|
|
4
|
-
'runtime.wasmExit': (sp: any) => void;
|
|
5
|
-
'runtime.wasmWrite': (sp: any) => void;
|
|
6
|
-
'runtime.resetMemoryDataView': (sp: any) => void;
|
|
7
|
-
'runtime.nanotime1': (sp: any) => void;
|
|
8
|
-
'runtime.walltime': (sp: any) => void;
|
|
9
|
-
'runtime.scheduleTimeoutEvent': (sp: any) => void;
|
|
10
|
-
'runtime.clearTimeoutEvent': (sp: any) => void;
|
|
11
|
-
'runtime.getRandomData': (sp: any) => void;
|
|
12
|
-
'syscall/js.finalizeRef': (sp: any) => void;
|
|
13
|
-
'syscall/js.stringVal': (sp: any) => void;
|
|
14
|
-
'syscall/js.valueGet': (sp: any) => void;
|
|
15
|
-
'syscall/js.valueSet': (sp: any) => void;
|
|
16
|
-
'syscall/js.valueDelete': (sp: any) => void;
|
|
17
|
-
'syscall/js.valueIndex': (sp: any) => void;
|
|
18
|
-
'syscall/js.valueSetIndex': (sp: any) => void;
|
|
19
|
-
'syscall/js.valueCall': (sp: any) => void;
|
|
20
|
-
'syscall/js.valueInvoke': (sp: any) => void;
|
|
21
|
-
'syscall/js.valueNew': (sp: any) => void;
|
|
22
|
-
'syscall/js.valueLength': (sp: any) => void;
|
|
23
|
-
'syscall/js.valuePrepareString': (sp: any) => void;
|
|
24
|
-
'syscall/js.valueLoadString': (sp: any) => void;
|
|
25
|
-
'syscall/js.valueInstanceOf': (sp: any) => void;
|
|
26
|
-
'syscall/js.copyBytesToGo': (sp: any) => void;
|
|
27
|
-
'syscall/js.copyBytesToJS': (sp: any) => void;
|
|
28
|
-
debug: (value: any) => void;
|
|
29
|
-
};
|
|
30
|
-
};
|
|
31
|
-
constructor();
|
|
32
|
-
run(instance: any): Promise<void>;
|
|
33
|
-
private _resume;
|
|
34
|
-
private _makeFuncWrapper;
|
|
35
|
-
}
|