@cloudalgo/eslint-parser-apex 0.1.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/LICENSE +28 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +114 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, CloudAlgo
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export interface ApexNode {
|
|
2
|
+
type: string;
|
|
3
|
+
/** Children — the only property ESLint traverses via visitorKeys. */
|
|
4
|
+
body: ApexNode[];
|
|
5
|
+
loc: {
|
|
6
|
+
start: Pos;
|
|
7
|
+
end: Pos;
|
|
8
|
+
};
|
|
9
|
+
range: [number, number];
|
|
10
|
+
/** Original ANTLR context, unwrapped in the plugin adapter so rules get it. */
|
|
11
|
+
_antlr: any;
|
|
12
|
+
}
|
|
13
|
+
interface Pos {
|
|
14
|
+
line: number;
|
|
15
|
+
column: number;
|
|
16
|
+
}
|
|
17
|
+
export declare const visitorKeys: Record<string, string[]>;
|
|
18
|
+
export interface ParseResult {
|
|
19
|
+
ast: any;
|
|
20
|
+
visitorKeys: Record<string, string[]>;
|
|
21
|
+
/** Syntax errors collected during parse. */
|
|
22
|
+
errors: Array<{
|
|
23
|
+
line: number;
|
|
24
|
+
column: number;
|
|
25
|
+
message: string;
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* ESLint custom parser entrypoint.
|
|
30
|
+
* ESLint calls `parseForESLint(text, options)` first; falls back to `parse`.
|
|
31
|
+
*/
|
|
32
|
+
export declare function parse(text: string, _options?: unknown): any;
|
|
33
|
+
declare const defaultExport: {
|
|
34
|
+
parse: typeof parse;
|
|
35
|
+
parseForESLint: typeof parseForESLint;
|
|
36
|
+
meta: {
|
|
37
|
+
name: string;
|
|
38
|
+
version: string;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
export default defaultExport;
|
|
42
|
+
export declare function parseForESLint(text: string, _options?: unknown): ParseResult;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { ApexParserFactory } from "@apexdevtools/apex-parser";
|
|
2
|
+
// ESLint needs to know which property holds children for each node type.
|
|
3
|
+
// We use a single "body" property for all types, so every type maps to ['body'].
|
|
4
|
+
export const visitorKeys = new Proxy({}, { get: (_t, _k) => ["body"] });
|
|
5
|
+
// ─── ANTLR → ESTree conversion ────────────────────────────────────────────────
|
|
6
|
+
function toPos(token, isEnd = false) {
|
|
7
|
+
if (!token)
|
|
8
|
+
return { line: 1, column: 0 };
|
|
9
|
+
return {
|
|
10
|
+
line: token.line ?? 1,
|
|
11
|
+
column: isEnd
|
|
12
|
+
? (token.column ?? 0) + (token.text?.length ?? 1)
|
|
13
|
+
: (token.column ?? 0),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function convertNode(antlr) {
|
|
17
|
+
const start = antlr.start;
|
|
18
|
+
const stop = antlr.stop;
|
|
19
|
+
const node = {
|
|
20
|
+
type: antlr.constructor?.name ?? "Unknown",
|
|
21
|
+
body: [],
|
|
22
|
+
loc: {
|
|
23
|
+
start: toPos(start, false),
|
|
24
|
+
end: toPos(stop, true),
|
|
25
|
+
},
|
|
26
|
+
range: [start?.startIndex ?? 0, (stop?.stopIndex ?? 0) + 1],
|
|
27
|
+
_antlr: antlr,
|
|
28
|
+
};
|
|
29
|
+
const count = antlr.getChildCount?.() ?? 0;
|
|
30
|
+
for (let i = 0; i < count; i++) {
|
|
31
|
+
const child = antlr.getChild(i);
|
|
32
|
+
// Only recurse into context nodes; skip terminal tokens
|
|
33
|
+
if (child?.constructor?.name?.endsWith("Context")) {
|
|
34
|
+
node.body.push(convertNode(child));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return node;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Extract ANTLR tokens from the token stream into ESLint's token format.
|
|
41
|
+
* ESLint's SourceCode constructor validates that `ast.tokens` exists and is
|
|
42
|
+
* an array; the content is used for eslint-disable comments and token-level
|
|
43
|
+
* rules. We include all non-hidden-channel tokens.
|
|
44
|
+
*/
|
|
45
|
+
function extractTokens(parser, vocabulary) {
|
|
46
|
+
const stream = parser.inputStream ?? parser.tokenStream ?? parser._input;
|
|
47
|
+
if (!stream)
|
|
48
|
+
return [];
|
|
49
|
+
// Fill the stream so all tokens are available
|
|
50
|
+
stream.fill?.();
|
|
51
|
+
const raw = stream.tokens ?? [];
|
|
52
|
+
return raw
|
|
53
|
+
.filter((t) => t.channel === 0 && t.type !== -1 /* EOF */)
|
|
54
|
+
.map((t) => {
|
|
55
|
+
const typeName = vocabulary?.getSymbolicName?.(t.type) ??
|
|
56
|
+
vocabulary?.getLiteralName?.(t.type) ??
|
|
57
|
+
"Token";
|
|
58
|
+
const text = t.text ?? "";
|
|
59
|
+
const startCol = t.column ?? 0;
|
|
60
|
+
const endCol = startCol + text.length;
|
|
61
|
+
return {
|
|
62
|
+
type: typeName,
|
|
63
|
+
value: text,
|
|
64
|
+
range: [t.startIndex ?? 0, (t.stopIndex ?? 0) + 1],
|
|
65
|
+
loc: {
|
|
66
|
+
start: { line: t.line ?? 1, column: startCol },
|
|
67
|
+
end: { line: t.line ?? 1, column: endCol },
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
// ─── Parse entrypoint ─────────────────────────────────────────────────────────
|
|
73
|
+
const TRIGGER_RE = /^\s*(?:\/\/[^\n]*\n\s*)*trigger\b/i;
|
|
74
|
+
/** Synthetic root wrapping the real root so ESLint sees type="Program". */
|
|
75
|
+
function wrapAsProgram(root, tokens) {
|
|
76
|
+
return {
|
|
77
|
+
type: "Program",
|
|
78
|
+
body: [root],
|
|
79
|
+
loc: root.loc,
|
|
80
|
+
range: root.range,
|
|
81
|
+
_antlr: root._antlr,
|
|
82
|
+
tokens,
|
|
83
|
+
comments: [],
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* ESLint custom parser entrypoint.
|
|
88
|
+
* ESLint calls `parseForESLint(text, options)` first; falls back to `parse`.
|
|
89
|
+
*/
|
|
90
|
+
export function parse(text, _options) {
|
|
91
|
+
return parseForESLint(text, _options).ast;
|
|
92
|
+
}
|
|
93
|
+
const defaultExport = { parse, parseForESLint, meta: { name: "@cloudalgo/eslint-parser-apex", version: "0.1.0" } };
|
|
94
|
+
export default defaultExport;
|
|
95
|
+
export function parseForESLint(text, _options) {
|
|
96
|
+
const errors = [];
|
|
97
|
+
const parser = ApexParserFactory.createParser(text, false);
|
|
98
|
+
parser.removeErrorListeners();
|
|
99
|
+
parser.addErrorListener({
|
|
100
|
+
syntaxError: (_r, _o, line, column, msg) => {
|
|
101
|
+
errors.push({ line, column, message: msg });
|
|
102
|
+
},
|
|
103
|
+
reportAmbiguity: () => { },
|
|
104
|
+
reportAttemptingFullContext: () => { },
|
|
105
|
+
reportContextSensitivity: () => { },
|
|
106
|
+
});
|
|
107
|
+
const antlrRoot = TRIGGER_RE.test(text)
|
|
108
|
+
? parser.triggerUnit()
|
|
109
|
+
: parser.compilationUnit();
|
|
110
|
+
const tokens = extractTokens(parser, parser.vocabulary);
|
|
111
|
+
const ast = wrapAsProgram(convertNode(antlrRoot), tokens);
|
|
112
|
+
return { ast, visitorKeys, errors };
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAmB9D,yEAAyE;AACzE,iFAAiF;AACjF,MAAM,CAAC,MAAM,WAAW,GAA6B,IAAI,KAAK,CAC5D,EAA8B,EAC9B,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC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,EAAW,EACX,EAAW,EACX,IAAY,EACZ,MAAc,EACd,GAAW,EACX,EAAE;YACF,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;QAC9C,CAAC;QACD,eAAe,EAAE,GAAG,EAAE,GAAE,CAAC;QACzB,2BAA2B,EAAE,GAAG,EAAE,GAAE,CAAC;QACrC,wBAAwB,EAAE,GAAG,EAAE,GAAE,CAAC;KAC5B,CAAC,CAAC;IAEV,MAAM,SAAS,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC;QACrC,CAAC,CAAC,MAAM,CAAC,WAAW,EAAE;QACtB,CAAC,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;IAE7B,MAAM,MAAM,GAAG,aAAa,CAAC,MAAM,EAAG,MAAc,CAAC,UAAU,CAAC,CAAC;IACjE,MAAM,GAAG,GAAG,aAAa,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC,CAAC;IAC1D,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC;AACtC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cloudalgo/eslint-parser-apex",
|
|
3
|
+
"version": "0.1.5",
|
|
4
|
+
"description": "ESLint custom parser for Salesforce Apex — bridges @apexdevtools/apex-parser to ESTree",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@apexdevtools/apex-parser": "^5.0.0"
|
|
16
|
+
},
|
|
17
|
+
"license": "BSD-3-Clause",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/cloudalgo/apex-lint"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/cloudalgo/apex-lint#readme",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/cloudalgo/apex-lint/issues"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"salesforce",
|
|
28
|
+
"apex",
|
|
29
|
+
"eslint",
|
|
30
|
+
"parser"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=20"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsc -p tsconfig.json"
|
|
40
|
+
}
|
|
41
|
+
}
|