@cparra/apex-reflection 0.1.1-alpha.9 → 1.0.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 ADDED
@@ -0,0 +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.
@@ -3,7 +3,7 @@ import {ClassMirror, InterfaceMirror, reflect} from '../index';
3
3
  describe('Enum Reflection', () => {
4
4
  test('Simple, single line declaration', () => {
5
5
  const enumBody = 'enum MyEnumName {}';
6
- const result = reflect(enumBody);
6
+ const result = reflect(enumBody).typeMirror;
7
7
  expect(result.type_name).toBe('enum');
8
8
  expect(result.name).toBe('MyEnumName');
9
9
  });
@@ -15,7 +15,7 @@ describe('Enum Reflection', () => {
15
15
  VALUE2
16
16
  }
17
17
  `;
18
- const result = reflect(enumBody);
18
+ const result = reflect(enumBody).typeMirror;
19
19
  expect(result.type_name).toBe('enum');
20
20
  expect(result.name).toBe('MyEnumName');
21
21
  });
@@ -30,7 +30,7 @@ describe('Enum Reflection', () => {
30
30
  VALUE2
31
31
  }
32
32
  `;
33
- const result = reflect(enumBody);
33
+ const result = reflect(enumBody).typeMirror;
34
34
  expect(result.docComment.description).toBe('My enum description');
35
35
  });
36
36
  });
@@ -38,20 +38,20 @@ describe('Enum Reflection', () => {
38
38
  describe('Interface Reflection', () => {
39
39
  test('Single line interface definition', () => {
40
40
  const interfaceBody = 'interface MyInterface{}';
41
- const result = reflect(interfaceBody);
41
+ const result = reflect(interfaceBody).typeMirror;
42
42
  expect(result.type_name).toBe('interface');
43
43
  expect(result.name).toBe('MyInterface');
44
44
  });
45
45
 
46
46
  test('When no access modifier is defined it is private', () => {
47
47
  const interfaceBody = 'interface MyInterface{}';
48
- const result = reflect(interfaceBody);
48
+ const result = reflect(interfaceBody).typeMirror;
49
49
  expect(result.access_modifier).toBe('private');
50
50
  });
51
51
 
52
52
  test('Can have access modifier', () => {
53
53
  const interfaceBody = 'public interface MyInterface{}';
54
- const result = reflect(interfaceBody);
54
+ const result = reflect(interfaceBody).typeMirror;
55
55
  expect(result.access_modifier).toBe('public');
56
56
  });
57
57
 
@@ -99,20 +99,20 @@ describe('Interface Reflection', () => {
99
99
  describe('Class reflection', () => {
100
100
  test('Single line class definition', () => {
101
101
  const classBody = 'class MyClass{}';
102
- const result = reflect(classBody);
102
+ const result = reflect(classBody).typeMirror;
103
103
  expect(result.type_name).toBe('class');
104
104
  expect(result.name).toBe('MyClass');
105
105
  });
106
106
 
107
107
  test('When no access modifier is defined it is private', () => {
108
108
  const classBody = 'class MyClass{}';
109
- const result = reflect(classBody);
109
+ const result = reflect(classBody).typeMirror;
110
110
  expect(result.access_modifier).toBe('private');
111
111
  });
112
112
 
113
113
  test('Can have access modifier', () => {
114
114
  const interfaceBody = 'public class MyClass{}';
115
- const result = reflect(interfaceBody);
115
+ const result = reflect(interfaceBody).typeMirror;
116
116
  expect(result.access_modifier).toBe('public');
117
117
  });
118
118
 
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare function reflect(declarationBody: string): Type;
1
+ export declare function reflect(declarationBody: string): ReflectionResult;
2
2
  export interface ParamAnnotation {
3
3
  bodyLines: string[];
4
4
  paramName: string;
@@ -70,6 +70,13 @@ export interface ConstructorMirror {
70
70
  parameters: ParameterMirror[];
71
71
  docComment?: DocComment;
72
72
  }
73
+ export interface ReflectionResult {
74
+ typeMirror?: Type;
75
+ error?: ParsingError;
76
+ }
77
+ export interface ParsingError {
78
+ message: string;
79
+ }
73
80
  declare type TypeName = 'class' | 'interface' | 'enum';
74
81
  export declare type Type = InterfaceMirror | ClassMirror | EnumMirror;
75
82
  export interface EnumMirror {
package/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  const lib = require('./out');
2
2
 
3
- export function reflect(declarationBody: string): Type {
4
- return JSON.parse(lib.reflect(declarationBody)) as Type;
3
+ export function reflect(declarationBody: string): ReflectionResult {
4
+ return JSON.parse(lib.reflect(declarationBody)) as ReflectionResult;
5
5
  }
6
6
 
7
7
  export interface ParamAnnotation {
@@ -87,6 +87,15 @@ export interface ConstructorMirror {
87
87
  docComment?: DocComment;
88
88
  }
89
89
 
90
+ export interface ReflectionResult {
91
+ typeMirror?: Type;
92
+ error?: ParsingError;
93
+ }
94
+
95
+ export interface ParsingError {
96
+ message: string;
97
+ }
98
+
90
99
  // Types
91
100
 
92
101
  type TypeName = 'class' | 'interface' | 'enum';
@@ -112,7 +121,6 @@ export interface InterfaceMirror {
112
121
  }
113
122
 
114
123
  export interface ClassMirror {
115
- string;
116
124
  annotations: Annotation[];
117
125
  name: string;
118
126
  type_name: TypeName;