astro-eslint-parser 0.4.5 → 0.5.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 +8 -10
- package/lib/context/parser-options.d.ts +2 -2
- package/lib/context/parser-options.js +8 -2
- package/lib/context/resolve-parser/espree.d.ts +2 -2
- package/lib/context/resolve-parser/index.d.ts +6 -4
- package/lib/context/resolve-parser/index.js +16 -12
- package/lib/context/resolve-parser/parser-object.d.ts +33 -0
- package/lib/context/resolve-parser/parser-object.js +27 -0
- package/lib/index.d.ts +1 -2
- package/lib/index.js +0 -6
- package/lib/parser/astro-parser/parse.js +3 -0
- package/lib/parser/script.js +4 -2
- package/lib/types.d.ts +0 -4
- package/package.json +14 -12
- package/lib/markdown/frontmatter.d.ts +0 -9
- package/lib/markdown/frontmatter.js +0 -53
- package/lib/markdown/index.d.ts +0 -19
- package/lib/markdown/index.js +0 -60
- package/lib/markdown/mdast-util-from-markdown-service.d.ts +0 -5
- package/lib/markdown/mdast-util-from-markdown-service.js +0 -12
- package/lib/markdown/mdast-util-from-markdown-worker.d.ts +0 -2
- package/lib/markdown/mdast-util-from-markdown-worker.js +0 -8
- package/lib/markdown/process-markdown.d.ts +0 -6
- package/lib/markdown/process-markdown.js +0 -82
- package/lib/markdown/yaml.d.ts +0 -9
- package/lib/markdown/yaml.js +0 -86
package/README.md
CHANGED
|
@@ -129,18 +129,16 @@ module.exports = {
|
|
|
129
129
|
}
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
-
|
|
132
|
+
When using JavaScript configuration (`.eslintrc.js`), you can also give the parser object directly.
|
|
133
133
|
|
|
134
|
-
|
|
134
|
+
```js
|
|
135
|
+
const tsParser = require("@typescript-eslint/parser")
|
|
135
136
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
"syntax": "auto",
|
|
142
|
-
}
|
|
143
|
-
}
|
|
137
|
+
module.exports = {
|
|
138
|
+
parser: "astro-eslint-parser",
|
|
139
|
+
parserOptions: {
|
|
140
|
+
parser: tsParser,
|
|
141
|
+
},
|
|
144
142
|
}
|
|
145
143
|
```
|
|
146
144
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ParserObject } from "./resolve-parser/parser-object";
|
|
2
2
|
export declare class ParserOptionsContext {
|
|
3
3
|
readonly parserOptions: any;
|
|
4
4
|
private readonly state;
|
|
5
5
|
constructor(options: any);
|
|
6
|
-
getParser():
|
|
6
|
+
getParser(): ParserObject;
|
|
7
7
|
isTypeScript(): boolean;
|
|
8
8
|
}
|
|
@@ -7,6 +7,7 @@ exports.ParserOptionsContext = void 0;
|
|
|
7
7
|
const path_1 = __importDefault(require("path"));
|
|
8
8
|
const fs_1 = __importDefault(require("fs"));
|
|
9
9
|
const resolve_parser_1 = require("./resolve-parser");
|
|
10
|
+
const parser_object_1 = require("./resolve-parser/parser-object");
|
|
10
11
|
class ParserOptionsContext {
|
|
11
12
|
constructor(options) {
|
|
12
13
|
this.state = {};
|
|
@@ -40,10 +41,15 @@ class ParserOptionsContext {
|
|
|
40
41
|
if (this.state.isTypeScript != null) {
|
|
41
42
|
return this.state.isTypeScript;
|
|
42
43
|
}
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
44
|
+
const parserValue = (0, resolve_parser_1.getParserForLang)({}, this.parserOptions?.parser);
|
|
45
|
+
if ((0, parser_object_1.maybeTSESLintParserObject)(parserValue) ||
|
|
46
|
+
parserValue === "@typescript-eslint/parser") {
|
|
45
47
|
return (this.state.isTypeScript = true);
|
|
46
48
|
}
|
|
49
|
+
if (typeof parserValue !== "string") {
|
|
50
|
+
return (this.state.isTypeScript = false);
|
|
51
|
+
}
|
|
52
|
+
const parserName = parserValue;
|
|
47
53
|
if (parserName.includes("@typescript-eslint/parser")) {
|
|
48
54
|
let targetPath = parserName;
|
|
49
55
|
while (targetPath) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BasicParserObject } from "./parser-object";
|
|
2
2
|
/**
|
|
3
3
|
* Load `espree` from the loaded ESLint.
|
|
4
4
|
* If the loaded ESLint was not found, just returns `require("espree")`.
|
|
5
5
|
*/
|
|
6
|
-
export declare function getEspree():
|
|
6
|
+
export declare function getEspree(): BasicParserObject;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import type { ParserObject } from "./parser-object";
|
|
2
|
+
declare type UserOptionParser = string | ParserObject | Record<string, string | ParserObject | undefined> | undefined;
|
|
3
|
+
/** Get parser for script lang */
|
|
4
|
+
export declare function getParserForLang(attrs: Record<string, string | undefined>, parser: UserOptionParser): string | ParserObject;
|
|
4
5
|
/** Get parser */
|
|
5
|
-
export declare function getParser(attrs: Record<string, string | undefined>, parser: any):
|
|
6
|
+
export declare function getParser(attrs: Record<string, string | undefined>, parser: any): ParserObject;
|
|
7
|
+
export {};
|
|
@@ -1,29 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getParser = exports.
|
|
3
|
+
exports.getParser = exports.getParserForLang = void 0;
|
|
4
4
|
const espree_1 = require("./espree");
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const parser_object_1 = require("./parser-object");
|
|
6
|
+
/** Get parser for script lang */
|
|
7
|
+
function getParserForLang(attrs, parser) {
|
|
7
8
|
if (parser) {
|
|
8
|
-
if (typeof parser === "string"
|
|
9
|
+
if (typeof parser === "string" || (0, parser_object_1.isParserObject)(parser)) {
|
|
9
10
|
return parser;
|
|
10
11
|
}
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
if (typeof
|
|
14
|
-
return
|
|
12
|
+
if (typeof parser === "object") {
|
|
13
|
+
const value = parser[attrs.lang || "js"];
|
|
14
|
+
if (typeof value === "string" || (0, parser_object_1.isParserObject)(value)) {
|
|
15
|
+
return value;
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
return "espree";
|
|
19
20
|
}
|
|
20
|
-
exports.
|
|
21
|
+
exports.getParserForLang = getParserForLang;
|
|
21
22
|
/** Get parser */
|
|
22
23
|
function getParser(attrs, parser) {
|
|
23
|
-
const
|
|
24
|
-
if (
|
|
24
|
+
const parserValue = getParserForLang(attrs, parser);
|
|
25
|
+
if ((0, parser_object_1.isParserObject)(parserValue)) {
|
|
26
|
+
return parserValue;
|
|
27
|
+
}
|
|
28
|
+
if (parserValue !== "espree") {
|
|
25
29
|
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
|
|
26
|
-
return require(
|
|
30
|
+
return require(parserValue);
|
|
27
31
|
}
|
|
28
32
|
return (0, espree_1.getEspree)();
|
|
29
33
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { TSESTree } from "@typescript-eslint/types";
|
|
2
|
+
import type { ESLintExtendedProgram } from "../../types";
|
|
3
|
+
import type * as tsESLintParser from "@typescript-eslint/parser";
|
|
4
|
+
declare type TSESLintParser = typeof tsESLintParser;
|
|
5
|
+
/**
|
|
6
|
+
* The type of basic ESLint custom parser.
|
|
7
|
+
* e.g. espree
|
|
8
|
+
*/
|
|
9
|
+
export declare type BasicParserObject = {
|
|
10
|
+
parse(code: string, options: any): TSESTree.Program;
|
|
11
|
+
parseForESLint: undefined;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The type of ESLint custom parser enhanced for ESLint.
|
|
15
|
+
* e.g. @babel/eslint-parser, @typescript-eslint/parser
|
|
16
|
+
*/
|
|
17
|
+
export declare type EnhancedParserObject = {
|
|
18
|
+
parseForESLint(code: string, options: any): ESLintExtendedProgram;
|
|
19
|
+
parse: undefined;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The type of ESLint (custom) parsers.
|
|
23
|
+
*/
|
|
24
|
+
export declare type ParserObject = EnhancedParserObject | BasicParserObject;
|
|
25
|
+
/** Checks whether given object is ParserObject */
|
|
26
|
+
export declare function isParserObject(value: unknown): value is ParserObject;
|
|
27
|
+
/** Checks whether given object is EnhancedParserObject */
|
|
28
|
+
export declare function isEnhancedParserObject(value: unknown): value is EnhancedParserObject;
|
|
29
|
+
/** Checks whether given object is BasicParserObject */
|
|
30
|
+
export declare function isBasicParserObject(value: unknown): value is BasicParserObject;
|
|
31
|
+
/** Checks whether given object is "@typescript-eslint/parser" */
|
|
32
|
+
export declare function maybeTSESLintParserObject(value: unknown): value is TSESLintParser;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.maybeTSESLintParserObject = exports.isBasicParserObject = exports.isEnhancedParserObject = exports.isParserObject = void 0;
|
|
4
|
+
/** Checks whether given object is ParserObject */
|
|
5
|
+
function isParserObject(value) {
|
|
6
|
+
return isEnhancedParserObject(value) || isBasicParserObject(value);
|
|
7
|
+
}
|
|
8
|
+
exports.isParserObject = isParserObject;
|
|
9
|
+
/** Checks whether given object is EnhancedParserObject */
|
|
10
|
+
function isEnhancedParserObject(value) {
|
|
11
|
+
return Boolean(value && typeof value.parseForESLint === "function");
|
|
12
|
+
}
|
|
13
|
+
exports.isEnhancedParserObject = isEnhancedParserObject;
|
|
14
|
+
/** Checks whether given object is BasicParserObject */
|
|
15
|
+
function isBasicParserObject(value) {
|
|
16
|
+
return Boolean(value && typeof value.parse === "function");
|
|
17
|
+
}
|
|
18
|
+
exports.isBasicParserObject = isBasicParserObject;
|
|
19
|
+
/** Checks whether given object is "@typescript-eslint/parser" */
|
|
20
|
+
function maybeTSESLintParserObject(value) {
|
|
21
|
+
return (isEnhancedParserObject(value) &&
|
|
22
|
+
isBasicParserObject(value) &&
|
|
23
|
+
typeof value.createProgram === "function" &&
|
|
24
|
+
typeof value.clearCaches === "function" &&
|
|
25
|
+
typeof value.version === "string");
|
|
26
|
+
}
|
|
27
|
+
exports.maybeTSESLintParserObject = maybeTSESLintParserObject;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { parseForESLint as parseAstro } from "./parser";
|
|
2
|
-
import { parseForESLint as parseMarkdown } from "./markdown";
|
|
3
2
|
import { parseTemplate, ParseTemplateResult } from "./astro-tools";
|
|
4
3
|
import * as AST from "./ast";
|
|
5
4
|
import { traverseNodes } from "./traverse";
|
|
@@ -8,6 +7,6 @@ export { AST, ParseError };
|
|
|
8
7
|
/**
|
|
9
8
|
* Parse source code
|
|
10
9
|
*/
|
|
11
|
-
export declare function parseForESLint(code: string, options?: any): ReturnType<typeof parseAstro
|
|
10
|
+
export declare function parseForESLint(code: string, options?: any): ReturnType<typeof parseAstro>;
|
|
12
11
|
export declare const VisitorKeys: import("eslint").SourceCode.VisitorKeys;
|
|
13
12
|
export { traverseNodes, parseTemplate, ParseTemplateResult };
|
package/lib/index.js
CHANGED
|
@@ -25,7 +25,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.parseTemplate = exports.traverseNodes = exports.VisitorKeys = exports.parseForESLint = exports.ParseError = exports.AST = void 0;
|
|
27
27
|
const parser_1 = require("./parser");
|
|
28
|
-
const markdown_1 = require("./markdown");
|
|
29
28
|
const astro_tools_1 = require("./astro-tools");
|
|
30
29
|
Object.defineProperty(exports, "parseTemplate", { enumerable: true, get: function () { return astro_tools_1.parseTemplate; } });
|
|
31
30
|
const AST = __importStar(require("./ast"));
|
|
@@ -39,11 +38,6 @@ Object.defineProperty(exports, "ParseError", { enumerable: true, get: function (
|
|
|
39
38
|
* Parse source code
|
|
40
39
|
*/
|
|
41
40
|
function parseForESLint(code, options) {
|
|
42
|
-
const syntax = options?.astroFeatures?.syntax ?? "auto";
|
|
43
|
-
if (syntax === "markdown" ||
|
|
44
|
-
(syntax === "auto" && options?.filePath?.endsWith(".md"))) {
|
|
45
|
-
return (0, markdown_1.parseForESLint)(code, options);
|
|
46
|
-
}
|
|
47
41
|
return (0, parser_1.parseForESLint)(code, options);
|
|
48
42
|
}
|
|
49
43
|
exports.parseForESLint = parseForESLint;
|
|
@@ -107,6 +107,9 @@ function fixLocations(node, ctx) {
|
|
|
107
107
|
(node, [parent]) => {
|
|
108
108
|
if (node.type === "frontmatter") {
|
|
109
109
|
start = node.position.start.offset = tokenIndex(ctx, "---", start);
|
|
110
|
+
if (!node.position.end) {
|
|
111
|
+
node.position.end = {};
|
|
112
|
+
}
|
|
110
113
|
start = node.position.end.offset =
|
|
111
114
|
tokenIndex(ctx, "---", start + 3 + node.value.length) + 3;
|
|
112
115
|
}
|
package/lib/parser/script.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.parseScript = void 0;
|
|
4
4
|
const debug_1 = require("../debug");
|
|
5
5
|
const ts_patch_1 = require("./ts-patch");
|
|
6
|
+
const parser_object_1 = require("../context/resolve-parser/parser-object");
|
|
6
7
|
/**
|
|
7
8
|
* Parse for script
|
|
8
9
|
*/
|
|
@@ -22,8 +23,9 @@ function parseScript(code, _ctx, parserOptions) {
|
|
|
22
23
|
scriptParserOptions.project) {
|
|
23
24
|
patchResult = (0, ts_patch_1.tsPatch)(scriptParserOptions);
|
|
24
25
|
}
|
|
25
|
-
const result =
|
|
26
|
-
parser.
|
|
26
|
+
const result = (0, parser_object_1.isEnhancedParserObject)(parser)
|
|
27
|
+
? parser.parseForESLint(code, scriptParserOptions)
|
|
28
|
+
: parser.parse(code, scriptParserOptions);
|
|
27
29
|
if ("ast" in result && result.ast != null) {
|
|
28
30
|
return result;
|
|
29
31
|
}
|
package/lib/types.d.ts
CHANGED
|
@@ -15,7 +15,3 @@ export interface ESLintExtendedProgram {
|
|
|
15
15
|
* The interface of a result of ESLint custom parser.
|
|
16
16
|
*/
|
|
17
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/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-eslint-parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Astro component parser for ESLint",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib"
|
|
8
8
|
],
|
|
9
9
|
"engines": {
|
|
10
|
-
"node": "^14.
|
|
10
|
+
"node": "^14.18.0 || >=16.0.0"
|
|
11
11
|
},
|
|
12
12
|
"scripts": {
|
|
13
13
|
"prebuild": "npm run -s clean",
|
|
@@ -15,14 +15,16 @@
|
|
|
15
15
|
"clean": "rimraf .nyc_output lib coverage",
|
|
16
16
|
"lint": "eslint . --ext .js,.ts,.json,.astro,.svelte",
|
|
17
17
|
"eslint-fix": "npm run lint -- --fix",
|
|
18
|
-
"test": "mocha --
|
|
18
|
+
"test": "npm run mocha -- \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
|
|
19
19
|
"cover": "nyc --reporter=lcov npm run test",
|
|
20
|
-
"debug": "mocha --
|
|
20
|
+
"debug": "npm run mocha -- \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
|
|
21
21
|
"preversion": "npm run lint && npm test",
|
|
22
|
-
"update-fixtures": "
|
|
23
|
-
"debug-parser": "ts
|
|
22
|
+
"update-fixtures": "env-cmd -e debug npm run ts -- ./tools/update-fixtures.ts",
|
|
23
|
+
"debug-parser": "npm run ts -- ./tools/parser-test.ts",
|
|
24
24
|
"eslint-playground": "eslint tests/fixtures --ext .astro --config .eslintrc-for-playground.js --format codeframe",
|
|
25
|
-
"benchmark": "ts
|
|
25
|
+
"benchmark": "npm run ts -- benchmark/index.ts",
|
|
26
|
+
"ts": "env-cmd -e basic node -r esbuild-register",
|
|
27
|
+
"mocha": "npm run ts -- ./node_modules/mocha/bin/mocha.js"
|
|
26
28
|
},
|
|
27
29
|
"repository": {
|
|
28
30
|
"type": "git",
|
|
@@ -47,9 +49,7 @@
|
|
|
47
49
|
"debug": "^4.3.4",
|
|
48
50
|
"eslint-visitor-keys": "^3.0.0",
|
|
49
51
|
"espree": "^9.0.0",
|
|
50
|
-
"
|
|
51
|
-
"synckit": "^0.7.1",
|
|
52
|
-
"yaml": "^2.1.1"
|
|
52
|
+
"synckit": "^0.8.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@ota-meshi/eslint-plugin": "^0.11.0",
|
|
@@ -68,6 +68,9 @@
|
|
|
68
68
|
"benchmark": "^2.1.4",
|
|
69
69
|
"chai": "^4.3.4",
|
|
70
70
|
"code-red": "^0.2.3",
|
|
71
|
+
"env-cmd": "^10.1.0",
|
|
72
|
+
"esbuild": "^0.15.0",
|
|
73
|
+
"esbuild-register": "^3.3.3",
|
|
71
74
|
"eslint": "^8.15.0",
|
|
72
75
|
"eslint-config-prettier": "^8.3.0",
|
|
73
76
|
"eslint-formatter-codeframe": "^7.32.1",
|
|
@@ -89,12 +92,11 @@
|
|
|
89
92
|
"mocha-chai-jest-snapshot": "^1.1.3",
|
|
90
93
|
"nyc": "^15.1.0",
|
|
91
94
|
"prettier": "^2.0.5",
|
|
92
|
-
"prettier-plugin-astro": "^0.
|
|
95
|
+
"prettier-plugin-astro": "^0.4.0",
|
|
93
96
|
"prettier-plugin-svelte": "^2.7.0",
|
|
94
97
|
"semver": "^7.3.5",
|
|
95
98
|
"string-replace-loader": "^3.0.3",
|
|
96
99
|
"svelte": "^3.48.0",
|
|
97
|
-
"ts-node": "^10.4.0",
|
|
98
100
|
"typescript": "~4.7.0"
|
|
99
101
|
}
|
|
100
102
|
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseFrontmatter = void 0;
|
|
4
|
-
/** Parse frontmatter */
|
|
5
|
-
function parseFrontmatter(code) {
|
|
6
|
-
if (!code.startsWith("---") || code[3] === "-") {
|
|
7
|
-
return {
|
|
8
|
-
frontmatter: null,
|
|
9
|
-
content: {
|
|
10
|
-
range: [0, code.length],
|
|
11
|
-
value: code,
|
|
12
|
-
},
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
let closeIndex = code.indexOf("\n---", 3);
|
|
16
|
-
let contentOpenIndex = closeIndex + 4;
|
|
17
|
-
if (closeIndex === -1) {
|
|
18
|
-
closeIndex = code.length;
|
|
19
|
-
contentOpenIndex = code.length;
|
|
20
|
-
}
|
|
21
|
-
let openIndex = 3;
|
|
22
|
-
if (code[openIndex] === "\r") {
|
|
23
|
-
openIndex++;
|
|
24
|
-
}
|
|
25
|
-
if (code[openIndex] === "\n") {
|
|
26
|
-
openIndex++;
|
|
27
|
-
}
|
|
28
|
-
if (openIndex < closeIndex && code[closeIndex] === "\n") {
|
|
29
|
-
closeIndex--;
|
|
30
|
-
}
|
|
31
|
-
if (openIndex < closeIndex && code[closeIndex] === "\r") {
|
|
32
|
-
closeIndex++;
|
|
33
|
-
}
|
|
34
|
-
if (code[contentOpenIndex] === "\r") {
|
|
35
|
-
contentOpenIndex++;
|
|
36
|
-
}
|
|
37
|
-
if (code[contentOpenIndex] === "\n") {
|
|
38
|
-
contentOpenIndex++;
|
|
39
|
-
}
|
|
40
|
-
const frontmatter = code.slice(openIndex, closeIndex + 1);
|
|
41
|
-
const content = code.slice(contentOpenIndex);
|
|
42
|
-
return {
|
|
43
|
-
frontmatter: {
|
|
44
|
-
range: [openIndex, closeIndex + 1],
|
|
45
|
-
value: frontmatter,
|
|
46
|
-
},
|
|
47
|
-
content: {
|
|
48
|
-
range: [contentOpenIndex, code.length],
|
|
49
|
-
value: content,
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
exports.parseFrontmatter = parseFrontmatter;
|
package/lib/markdown/index.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { ScopeManager } from "eslint-scope";
|
|
2
|
-
import type { AstroProgram } from "../ast";
|
|
3
|
-
import type { ParseResult } from "@astrojs/compiler/node";
|
|
4
|
-
/**
|
|
5
|
-
* Parse source code
|
|
6
|
-
*/
|
|
7
|
-
export declare function parseForESLint(code: string, options?: any): {
|
|
8
|
-
ast: AstroProgram;
|
|
9
|
-
services: Record<string, any> & {
|
|
10
|
-
isAstro: true;
|
|
11
|
-
getAstroAst: () => ParseResult;
|
|
12
|
-
isAstroMarkdown: true;
|
|
13
|
-
getAstroMarkdownFrontmatter: () => any;
|
|
14
|
-
};
|
|
15
|
-
visitorKeys: {
|
|
16
|
-
[type: string]: string[];
|
|
17
|
-
};
|
|
18
|
-
scopeManager: ScopeManager;
|
|
19
|
-
};
|
package/lib/markdown/index.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseForESLint = void 0;
|
|
4
|
-
const context_1 = require("../context");
|
|
5
|
-
const mdast_util_from_markdown_service_1 = require("./mdast-util-from-markdown-service");
|
|
6
|
-
const frontmatter_1 = require("./frontmatter");
|
|
7
|
-
const process_markdown_1 = require("./process-markdown");
|
|
8
|
-
const parser_1 = require("../parser");
|
|
9
|
-
const sort_1 = require("../parser/sort");
|
|
10
|
-
const script_1 = require("../context/script");
|
|
11
|
-
const yaml_1 = require("./yaml");
|
|
12
|
-
/**
|
|
13
|
-
* Parse source code
|
|
14
|
-
*/
|
|
15
|
-
function parseForESLint(code, options) {
|
|
16
|
-
const { frontmatter, content } = (0, frontmatter_1.parseFrontmatter)(code);
|
|
17
|
-
const ctx = new context_1.Context(code);
|
|
18
|
-
const root = (0, mdast_util_from_markdown_service_1.parseMarkdown)(content.value);
|
|
19
|
-
const scriptContext = new script_1.ScriptContext(ctx);
|
|
20
|
-
let yamlResult;
|
|
21
|
-
if (frontmatter) {
|
|
22
|
-
scriptContext.appendOriginal(frontmatter.range[0]);
|
|
23
|
-
yamlResult = (0, yaml_1.parseYaml)(frontmatter);
|
|
24
|
-
if (yamlResult) {
|
|
25
|
-
scriptContext.skipOriginalOffset(yamlResult.setupValueRange[0] - frontmatter.range[0]);
|
|
26
|
-
scriptContext.appendOriginal(yamlResult.setupValueRange[1]);
|
|
27
|
-
scriptContext.skipOriginalOffset(frontmatter.range[1] - yamlResult.setupValueRange[1]);
|
|
28
|
-
scriptContext.addToken("YAMLToken", [
|
|
29
|
-
frontmatter.range[0],
|
|
30
|
-
yamlResult.before,
|
|
31
|
-
]);
|
|
32
|
-
scriptContext.addToken("YAMLToken", [
|
|
33
|
-
yamlResult.after,
|
|
34
|
-
frontmatter.range[1],
|
|
35
|
-
]);
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
scriptContext.skipOriginalOffset(frontmatter.range[1] - frontmatter.range[0]);
|
|
39
|
-
scriptContext.addToken("YAMLToken", [
|
|
40
|
-
frontmatter.range[0],
|
|
41
|
-
frontmatter.range[1],
|
|
42
|
-
]);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
(0, process_markdown_1.processMarkdown)(scriptContext, root, content.range[0]);
|
|
46
|
-
scriptContext.appendOriginal(code.length);
|
|
47
|
-
const resultAstro = (0, parser_1.parseForESLint)(scriptContext.script, options);
|
|
48
|
-
scriptContext.restore(resultAstro);
|
|
49
|
-
(0, sort_1.sort)(resultAstro.ast.comments);
|
|
50
|
-
(0, sort_1.sort)(resultAstro.ast.tokens);
|
|
51
|
-
(0, parser_1.extractTokens)(resultAstro, ctx);
|
|
52
|
-
resultAstro.services = Object.assign(resultAstro.services || {}, {
|
|
53
|
-
isAstroMarkdown: true,
|
|
54
|
-
getAstroMarkdownFrontmatter() {
|
|
55
|
-
return yamlResult?.getYamlValue();
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
return resultAstro;
|
|
59
|
-
}
|
|
60
|
-
exports.parseForESLint = parseForESLint;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseMarkdown = void 0;
|
|
4
|
-
const synckit_1 = require("synckit");
|
|
5
|
-
const parseSync = (0, synckit_1.createSyncFn)(require.resolve("./mdast-util-from-markdown-worker"));
|
|
6
|
-
/**
|
|
7
|
-
* Parse code by `mdast-util-from-markdown`
|
|
8
|
-
*/
|
|
9
|
-
function parseMarkdown(code) {
|
|
10
|
-
return parseSync(code);
|
|
11
|
-
}
|
|
12
|
-
exports.parseMarkdown = parseMarkdown;
|
|
@@ -1,8 +0,0 @@
|
|
|
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 { fromMarkdown } = await dynamicImport("mdast-util-from-markdown");
|
|
7
|
-
return fromMarkdown(source);
|
|
8
|
-
});
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.processMarkdown = void 0;
|
|
4
|
-
const types_1 = require("@typescript-eslint/types");
|
|
5
|
-
/**
|
|
6
|
-
* Process the markdown ast to generate a ScriptContext.
|
|
7
|
-
*/
|
|
8
|
-
function processMarkdown(script, root, offset) {
|
|
9
|
-
walk(root, (node) => {
|
|
10
|
-
const start = node.position.start.offset + offset;
|
|
11
|
-
const end = node.position.end.offset + offset;
|
|
12
|
-
script.appendOriginal(start);
|
|
13
|
-
if (node.type === "code" || node.type === "inlineCode") {
|
|
14
|
-
script.appendScript(`<></>`);
|
|
15
|
-
script.skipOriginalOffset(end - start);
|
|
16
|
-
script.addRestoreNodeProcess((scriptNode, context) => {
|
|
17
|
-
if (scriptNode.range[0] === start &&
|
|
18
|
-
scriptNode.type === types_1.AST_NODE_TYPES.JSXFragment) {
|
|
19
|
-
delete scriptNode.openingFragment;
|
|
20
|
-
delete scriptNode.closingFragment;
|
|
21
|
-
delete scriptNode.expression;
|
|
22
|
-
delete scriptNode.children;
|
|
23
|
-
const text = script.originalCode.slice(start, end);
|
|
24
|
-
const mdNode = scriptNode;
|
|
25
|
-
mdNode.type = "AstroRawText";
|
|
26
|
-
mdNode.value = text;
|
|
27
|
-
mdNode.raw = text;
|
|
28
|
-
let parent = context.getParent(scriptNode);
|
|
29
|
-
while (parent) {
|
|
30
|
-
let update = false;
|
|
31
|
-
if (parent.range[0] > scriptNode.range[0]) {
|
|
32
|
-
parent.range[0] = scriptNode.range[0];
|
|
33
|
-
parent.loc.start = {
|
|
34
|
-
line: scriptNode.loc.start.line,
|
|
35
|
-
column: scriptNode.loc.start.column,
|
|
36
|
-
};
|
|
37
|
-
update = true;
|
|
38
|
-
}
|
|
39
|
-
if (parent.range[1] < scriptNode.range[1]) {
|
|
40
|
-
parent.range[1] = scriptNode.range[1];
|
|
41
|
-
parent.loc.end = {
|
|
42
|
-
line: scriptNode.loc.end.line,
|
|
43
|
-
column: scriptNode.loc.end.column,
|
|
44
|
-
};
|
|
45
|
-
update = true;
|
|
46
|
-
}
|
|
47
|
-
if (!update) {
|
|
48
|
-
break;
|
|
49
|
-
}
|
|
50
|
-
parent = context.getParent(parent);
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
return false;
|
|
55
|
-
});
|
|
56
|
-
script.addToken(types_1.AST_TOKEN_TYPES.JSXText, [start, end]);
|
|
57
|
-
}
|
|
58
|
-
}, (node) => {
|
|
59
|
-
// const start = node.position!.start.offset! + offset
|
|
60
|
-
const end = node.position.end.offset + offset;
|
|
61
|
-
script.appendOriginal(end);
|
|
62
|
-
});
|
|
63
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
64
|
-
if (scriptNode.type === "JSXText") {
|
|
65
|
-
const rawNode = scriptNode;
|
|
66
|
-
rawNode.type = "AstroRawText";
|
|
67
|
-
}
|
|
68
|
-
return false;
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
exports.processMarkdown = processMarkdown;
|
|
72
|
-
/** walk nodes */
|
|
73
|
-
function walk(parent, enter, leave, parents = []) {
|
|
74
|
-
const currParents = [parent, ...parents];
|
|
75
|
-
if ("children" in parent) {
|
|
76
|
-
for (const node of parent.children) {
|
|
77
|
-
enter(node, currParents);
|
|
78
|
-
walk(node, enter, leave, currParents);
|
|
79
|
-
leave(node, currParents);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
package/lib/markdown/yaml.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { MarkdownContent } from "./frontmatter";
|
|
2
|
-
export declare type FrontmatterYAMLResult = {
|
|
3
|
-
before: number;
|
|
4
|
-
setupValueRange: [number, number];
|
|
5
|
-
after: number;
|
|
6
|
-
getYamlValue: () => any;
|
|
7
|
-
};
|
|
8
|
-
/** Parse for yaml */
|
|
9
|
-
export declare function parseYaml(frontmatter: MarkdownContent): FrontmatterYAMLResult | null;
|
package/lib/markdown/yaml.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseYaml = void 0;
|
|
4
|
-
const yaml_1 = require("yaml");
|
|
5
|
-
/** Parse for yaml */
|
|
6
|
-
function parseYaml(frontmatter) {
|
|
7
|
-
const doc = (0, yaml_1.parseDocument)(frontmatter.value, { keepSourceTokens: true });
|
|
8
|
-
const range = getSetupRange(doc);
|
|
9
|
-
if (!range) {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
let before = range[0];
|
|
13
|
-
let after = range[1];
|
|
14
|
-
while (isSpace(frontmatter.value[before - 1])) {
|
|
15
|
-
before--;
|
|
16
|
-
}
|
|
17
|
-
while (isSpace(frontmatter.value[after])) {
|
|
18
|
-
after++;
|
|
19
|
-
}
|
|
20
|
-
return {
|
|
21
|
-
before: before + frontmatter.range[0],
|
|
22
|
-
setupValueRange: [
|
|
23
|
-
range[0] + frontmatter.range[0],
|
|
24
|
-
range[1] + frontmatter.range[0],
|
|
25
|
-
],
|
|
26
|
-
after: after + frontmatter.range[0],
|
|
27
|
-
getYamlValue: () => doc.toJS(),
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
exports.parseYaml = parseYaml;
|
|
31
|
-
/** Checks whether the given char is spaces, or not */
|
|
32
|
-
function isSpace(c) {
|
|
33
|
-
return c && !c.trim();
|
|
34
|
-
}
|
|
35
|
-
/** Get setup range */
|
|
36
|
-
function getSetupRange(ast) {
|
|
37
|
-
if (!ast.contents) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
if (!(0, yaml_1.isMap)(ast.contents)) {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
for (const item of ast.contents.items) {
|
|
44
|
-
if (!(0, yaml_1.isScalar)(item.key) || !(0, yaml_1.isScalar)(item.value)) {
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
if (item.key.value !== "setup") {
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
if (item.value.type === "PLAIN") {
|
|
51
|
-
return [item.value.range[0], item.value.range[1]];
|
|
52
|
-
}
|
|
53
|
-
if (item.value.type === "QUOTE_DOUBLE" ||
|
|
54
|
-
item.value.type === "QUOTE_SINGLE") {
|
|
55
|
-
return [item.value.range[0] + 1, item.value.range[1] - 1];
|
|
56
|
-
}
|
|
57
|
-
if (item.value.type === "BLOCK_FOLDED" ||
|
|
58
|
-
item.value.type === "BLOCK_LITERAL") {
|
|
59
|
-
const cst = item.value.srcToken;
|
|
60
|
-
let blockStart = cst.offset;
|
|
61
|
-
for (const token of cst.props) {
|
|
62
|
-
if (isCommentOrSpace(token) ||
|
|
63
|
-
token.type === "block-scalar-header") {
|
|
64
|
-
blockStart = token.offset + token.source.length;
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
/* istanbul ignore next */
|
|
68
|
-
throw new Error(`Unknown token:${token.type}`);
|
|
69
|
-
}
|
|
70
|
-
return [blockStart, item.value.range[1]];
|
|
71
|
-
}
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Checks whether the given cst is comments, spaces, or not
|
|
78
|
-
*/
|
|
79
|
-
function isCommentOrSpace(node) {
|
|
80
|
-
if (node.type === "space" ||
|
|
81
|
-
node.type === "newline" ||
|
|
82
|
-
node.type === "comment") {
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
return false;
|
|
86
|
-
}
|