@cparra/apex-reflection 0.1.1-alpha.9 → 1.1.1

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;
@@ -47,6 +47,7 @@ export interface MethodMirror {
47
47
  type: string;
48
48
  parameters: ParameterMirror[];
49
49
  docComment?: DocComment;
50
+ group?: string;
50
51
  }
51
52
  export interface PropertyMirror {
52
53
  access_modifier: string;
@@ -55,6 +56,7 @@ export interface PropertyMirror {
55
56
  memberModifiers: string[];
56
57
  type: string;
57
58
  docComment?: DocComment;
59
+ group?: string;
58
60
  }
59
61
  export interface FieldMirror {
60
62
  access_modifier: string;
@@ -63,12 +65,21 @@ export interface FieldMirror {
63
65
  memberModifiers: string[];
64
66
  type: string;
65
67
  docComment?: DocComment;
68
+ group?: string;
66
69
  }
67
70
  export interface ConstructorMirror {
68
71
  access_modifier: string;
69
72
  annotations: Annotation[];
70
73
  parameters: ParameterMirror[];
71
74
  docComment?: DocComment;
75
+ group?: string;
76
+ }
77
+ export interface ReflectionResult {
78
+ typeMirror?: Type;
79
+ error?: ParsingError;
80
+ }
81
+ export interface ParsingError {
82
+ message: string;
72
83
  }
73
84
  declare type TypeName = 'class' | 'interface' | 'enum';
74
85
  export declare type Type = InterfaceMirror | ClassMirror | EnumMirror;
@@ -78,6 +89,7 @@ export interface EnumMirror {
78
89
  type_name: TypeName;
79
90
  access_modifier: string;
80
91
  docComment?: DocComment;
92
+ group?: string;
81
93
  }
82
94
  export interface InterfaceMirror {
83
95
  annotations: Annotation[];
@@ -88,9 +100,9 @@ export interface InterfaceMirror {
88
100
  access_modifier: string;
89
101
  docComment?: DocComment;
90
102
  sharingModifier?: string;
103
+ group?: string;
91
104
  }
92
105
  export interface ClassMirror {
93
- string: any;
94
106
  annotations: Annotation[];
95
107
  name: string;
96
108
  type_name: TypeName;
@@ -107,5 +119,6 @@ export interface ClassMirror {
107
119
  classes: ClassMirror[];
108
120
  access_modifier: string;
109
121
  docComment?: DocComment;
122
+ group?: string;
110
123
  }
111
124
  export {};
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 {
@@ -60,6 +60,7 @@ export interface MethodMirror {
60
60
  type: string;
61
61
  parameters: ParameterMirror[];
62
62
  docComment?: DocComment;
63
+ group?: string;
63
64
  }
64
65
 
65
66
  export interface PropertyMirror {
@@ -69,6 +70,7 @@ export interface PropertyMirror {
69
70
  memberModifiers: string[];
70
71
  type: string;
71
72
  docComment?: DocComment;
73
+ group?: string;
72
74
  }
73
75
 
74
76
  export interface FieldMirror {
@@ -78,6 +80,7 @@ export interface FieldMirror {
78
80
  memberModifiers: string[];
79
81
  type: string;
80
82
  docComment?: DocComment;
83
+ group?: string;
81
84
  }
82
85
 
83
86
  export interface ConstructorMirror {
@@ -85,6 +88,16 @@ export interface ConstructorMirror {
85
88
  annotations: Annotation[];
86
89
  parameters: ParameterMirror[];
87
90
  docComment?: DocComment;
91
+ group?: string;
92
+ }
93
+
94
+ export interface ReflectionResult {
95
+ typeMirror?: Type;
96
+ error?: ParsingError;
97
+ }
98
+
99
+ export interface ParsingError {
100
+ message: string;
88
101
  }
89
102
 
90
103
  // Types
@@ -98,6 +111,7 @@ export interface EnumMirror {
98
111
  type_name: TypeName;
99
112
  access_modifier: string;
100
113
  docComment?: DocComment;
114
+ group?: string;
101
115
  }
102
116
 
103
117
  export interface InterfaceMirror {
@@ -109,10 +123,10 @@ export interface InterfaceMirror {
109
123
  access_modifier: string;
110
124
  docComment?: DocComment;
111
125
  sharingModifier?: string;
126
+ group?: string;
112
127
  }
113
128
 
114
129
  export interface ClassMirror {
115
- string;
116
130
  annotations: Annotation[];
117
131
  name: string;
118
132
  type_name: TypeName;
@@ -129,4 +143,5 @@ export interface ClassMirror {
129
143
  classes: ClassMirror[];
130
144
  access_modifier: string;
131
145
  docComment?: DocComment;
146
+ group?: string;
132
147
  }