@cparra/apex-reflection 2.8.0 → 2.9.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 +96 -96
- package/__tests__/end-to-end.test.ts +374 -346
- package/index.d.ts +157 -152
- package/index.js +8 -8
- package/index.ts +187 -181
- package/jest.config.js +11 -11
- package/out.js +3638 -3618
- package/package.json +28 -28
- package/tsconfig.json +10 -10
package/README.md
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
# Apex Reflection
|
|
2
|
-
|
|
3
|
-
Provides basic reflection for the Apex programming language.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
npm i @cparra/apex-reflection
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Usage
|
|
12
|
-
|
|
13
|
-
This library exposes a single function that handles parsing the body of an Apex top level type (class, interface, or
|
|
14
|
-
enum)
|
|
15
|
-
and returns the result.
|
|
16
|
-
|
|
17
|
-
```typescript
|
|
18
|
-
import {reflect} from '@cparra/apex-reflection';
|
|
19
|
-
|
|
20
|
-
const classBody = 'public with sharing class ExampleClass {}';
|
|
21
|
-
const response = reflect(classBody);
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
If you wish to parse an Apex type that comes from a file, you can read the file contents and use that as the source to
|
|
25
|
-
reflect
|
|
26
|
-
|
|
27
|
-
```typescript
|
|
28
|
-
import * as fs from 'fs';
|
|
29
|
-
import {reflect} from '@cparra/apex-reflection';
|
|
30
|
-
|
|
31
|
-
const path = './MyClass.cls';
|
|
32
|
-
const rawFile = fs.readFileSync(path);
|
|
33
|
-
const response = reflect(rawFile.toString());
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
The `reflect` function returns a `ReflectionResult` which contains either the results of the parsed `Type`
|
|
37
|
-
(which will either be a `ClassMirror`, an `InterfaceMirror` or an `EnumMirror`) or a `ParsingError` if the passed in
|
|
38
|
-
body was not parsed successfully, with a message indicating where the error occurred.
|
|
39
|
-
|
|
40
|
-
## Contributing
|
|
41
|
-
|
|
42
|
-
Even though this library is exposed as a Node.js library, the project's source code is written in Dart. The source can
|
|
43
|
-
be found in the `lib/src` directory.
|
|
44
|
-
|
|
45
|
-
The Dart source code is transpiled to JS by `dart2js` through the default [grinder](https://pub.dev/packages/grinder)
|
|
46
|
-
workflow within `tool/grind.dart`.
|
|
47
|
-
|
|
48
|
-
To generate the JS files first set up `grinder` locally by following that package's instructions through its pub.dev
|
|
49
|
-
listing, and then you can simply run `grind`. That build takes care of combining the output with `preamble/preamble.js`
|
|
50
|
-
to achieve compatibility with Node.js. The resulting file is `js/apex-reflection-node/out.js`.
|
|
51
|
-
|
|
52
|
-
### Tests
|
|
53
|
-
|
|
54
|
-
Both the Dart source code and the JS output must be tested.
|
|
55
|
-
|
|
56
|
-
The Dart tests live in the `test` directory. The Dart source code must have unit tests testing each individual Dart file
|
|
57
|
-
as well as end-to-end tests that verify the overall parsing functionality.
|
|
58
|
-
|
|
59
|
-
The JS tests live in `js/apex-reflection-node/__tests__`. These are end-to-end tests that ensure that the transpiled JS
|
|
60
|
-
code is working as intended.
|
|
61
|
-
|
|
62
|
-
### JSON serialization
|
|
63
|
-
|
|
64
|
-
The reflection operation outputs a JSON representation of the Apex type, which is then deserialized on the JS side to
|
|
65
|
-
return typed objects.
|
|
66
|
-
|
|
67
|
-
Serialization is handled through the [json_serializable](https://pub.dev/packages/json_serializable) package, which
|
|
68
|
-
helps automatically create the round-trip code for serialization and de-serialization.
|
|
69
|
-
|
|
70
|
-
When changing any of the model classes with serialization support, to re-build the serialization code run
|
|
71
|
-
|
|
72
|
-
```
|
|
73
|
-
pub run build_runner build
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
### Parsing
|
|
77
|
-
|
|
78
|
-
The parsing algorithm relies on using ANTLR4 and its Dart target. Currently `dart2js` is not able to transpile the
|
|
79
|
-
source from the `antrl4` library hosted in pub.dev, so we rely on a local copy that fixes the transpilation issues,
|
|
80
|
-
which lives in `lib/antrl4-4.9.2`.
|
|
81
|
-
|
|
82
|
-
To generate the Antlr4 Apex output run:
|
|
83
|
-
|
|
84
|
-
```
|
|
85
|
-
antlr4 -Dlanguage=Dart lib/src/antlr/grammars/apex/ApexLexer.g4 lib/src/antlr/grammars/apex/ApexParser.g4 -o lib/src/antlr/lib/apex/
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
To generate the Antlr4 Apexdoc output run:
|
|
89
|
-
|
|
90
|
-
```
|
|
91
|
-
antlr4 -Dlanguage=Dart lib/src/antlr/grammars/apexdoc/ApexdocLexer.g4 lib/src/antlr/grammars/apexdoc/ApexdocParser.g4 -o lib/src/antlr/lib/apexdoc/
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
## Typescript
|
|
95
|
-
|
|
96
|
-
This library provides its own TS type definition.
|
|
1
|
+
# Apex Reflection
|
|
2
|
+
|
|
3
|
+
Provides basic reflection for the Apex programming language.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm i @cparra/apex-reflection
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
This library exposes a single function that handles parsing the body of an Apex top level type (class, interface, or
|
|
14
|
+
enum)
|
|
15
|
+
and returns the result.
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import {reflect} from '@cparra/apex-reflection';
|
|
19
|
+
|
|
20
|
+
const classBody = 'public with sharing class ExampleClass {}';
|
|
21
|
+
const response = reflect(classBody);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
If you wish to parse an Apex type that comes from a file, you can read the file contents and use that as the source to
|
|
25
|
+
reflect
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import * as fs from 'fs';
|
|
29
|
+
import {reflect} from '@cparra/apex-reflection';
|
|
30
|
+
|
|
31
|
+
const path = './MyClass.cls';
|
|
32
|
+
const rawFile = fs.readFileSync(path);
|
|
33
|
+
const response = reflect(rawFile.toString());
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The `reflect` function returns a `ReflectionResult` which contains either the results of the parsed `Type`
|
|
37
|
+
(which will either be a `ClassMirror`, an `InterfaceMirror` or an `EnumMirror`) or a `ParsingError` if the passed in
|
|
38
|
+
body was not parsed successfully, with a message indicating where the error occurred.
|
|
39
|
+
|
|
40
|
+
## Contributing
|
|
41
|
+
|
|
42
|
+
Even though this library is exposed as a Node.js library, the project's source code is written in Dart. The source can
|
|
43
|
+
be found in the `lib/src` directory.
|
|
44
|
+
|
|
45
|
+
The Dart source code is transpiled to JS by `dart2js` through the default [grinder](https://pub.dev/packages/grinder)
|
|
46
|
+
workflow within `tool/grind.dart`.
|
|
47
|
+
|
|
48
|
+
To generate the JS files first set up `grinder` locally by following that package's instructions through its pub.dev
|
|
49
|
+
listing, and then you can simply run `grind`. That build takes care of combining the output with `preamble/preamble.js`
|
|
50
|
+
to achieve compatibility with Node.js. The resulting file is `js/apex-reflection-node/out.js`.
|
|
51
|
+
|
|
52
|
+
### Tests
|
|
53
|
+
|
|
54
|
+
Both the Dart source code and the JS output must be tested.
|
|
55
|
+
|
|
56
|
+
The Dart tests live in the `test` directory. The Dart source code must have unit tests testing each individual Dart file
|
|
57
|
+
as well as end-to-end tests that verify the overall parsing functionality.
|
|
58
|
+
|
|
59
|
+
The JS tests live in `js/apex-reflection-node/__tests__`. These are end-to-end tests that ensure that the transpiled JS
|
|
60
|
+
code is working as intended.
|
|
61
|
+
|
|
62
|
+
### JSON serialization
|
|
63
|
+
|
|
64
|
+
The reflection operation outputs a JSON representation of the Apex type, which is then deserialized on the JS side to
|
|
65
|
+
return typed objects.
|
|
66
|
+
|
|
67
|
+
Serialization is handled through the [json_serializable](https://pub.dev/packages/json_serializable) package, which
|
|
68
|
+
helps automatically create the round-trip code for serialization and de-serialization.
|
|
69
|
+
|
|
70
|
+
When changing any of the model classes with serialization support, to re-build the serialization code run
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
pub run build_runner build
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Parsing
|
|
77
|
+
|
|
78
|
+
The parsing algorithm relies on using ANTLR4 and its Dart target. Currently `dart2js` is not able to transpile the
|
|
79
|
+
source from the `antrl4` library hosted in pub.dev, so we rely on a local copy that fixes the transpilation issues,
|
|
80
|
+
which lives in `lib/antrl4-4.9.2`.
|
|
81
|
+
|
|
82
|
+
To generate the Antlr4 Apex output run:
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
antlr4 -Dlanguage=Dart lib/src/antlr/grammars/apex/ApexLexer.g4 lib/src/antlr/grammars/apex/ApexParser.g4 -o lib/src/antlr/lib/apex/
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
To generate the Antlr4 Apexdoc output run:
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
antlr4 -Dlanguage=Dart lib/src/antlr/grammars/apexdoc/ApexdocLexer.g4 lib/src/antlr/grammars/apexdoc/ApexdocParser.g4 -o lib/src/antlr/lib/apexdoc/
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Typescript
|
|
95
|
+
|
|
96
|
+
This library provides its own TS type definition.
|