Skip to content

Commit

Permalink
Making Context easier to use. Moving module and local templates here.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkedy committed Aug 4, 2022
1 parent 3a2ab31 commit c51bad2
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 160 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apexlang/core",
"version": "0.0.20",
"version": "0.0.21",
"description": "Apex language JavaScript support",
"keywords": [
"apex",
Expand Down
112 changes: 82 additions & 30 deletions src/model/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ class ErrorHolder {
}
}

function dummyValue<T>(fieldName: string): T {
return new Proxy(
{},
{
get: function (target, name, receiver) {
throw new Error(`${fieldName} is not available in this scope`);
},
set: function (target, name, value, receiver) {
throw new Error(`${fieldName} is not available in this scope`);
},
}
) as T;
}

export class Context {
config: ObjectMap;
document?: Document;
Expand All @@ -119,26 +133,26 @@ export class Context {
// Drill-down definitions
namespace: Namespace;
namespacePos: number = 9999;
directive?: Directive;
alias?: Alias;
interface?: Interface;
role?: Role;
type?: MObject;
operations?: Operation[];
operation?: Operation;
parameters?: Parameter[];
parameter?: Parameter;
parameterIndex?: number;
fields?: Field[];
field?: Field;
fieldIndex?: number;
enum?: Enum;
enumValues?: EnumValue[];
enumValue?: EnumValue;
union?: Union;

annotations?: Annotation[];
annotation?: Annotation;
directive: Directive = dummyValue<Directive>("directive");
alias: Alias = dummyValue<Alias>("alias");
interface: Interface = dummyValue<Interface>("interface");
role: Role = dummyValue<Role>("role");
type: MObject = dummyValue<MObject>("type");
operations: Operation[] = dummyValue<Operation[]>("operations");
operation: Operation = dummyValue<Operation>("operation");
parameters: Parameter[] = dummyValue<Parameter[]>("parameters");
parameter: Parameter = dummyValue<Parameter>("parameter");
parameterIndex: number = 9999;
fields: Field[] = dummyValue<Field[]>("fields");
field: Field = dummyValue<Field>("fields");
fieldIndex: number = 9999;
enum: Enum = dummyValue<Enum>("enum");
enumValues: EnumValue[] = dummyValue<EnumValue[]>("enumValues");
enumValue: EnumValue = dummyValue<EnumValue>("enumValue");
union: Union = dummyValue<Union>("union");

annotations: Annotation[] = dummyValue<Annotation[]>("annotations");
annotation: Annotation = dummyValue<Annotation>("annotation");

private errors: ErrorHolder;
private typeMap: {
Expand Down Expand Up @@ -270,7 +284,7 @@ export class Context {
throw new Error("namespace not found");
}

this.document!.definitions.forEach((value, index) => {
this.document!.definitions.forEach((value) => {
switch (value.getKind()) {
case Kind.DirectiveDefinition:
const directiveDef = value as DirectiveDefinition;
Expand Down Expand Up @@ -328,6 +342,44 @@ export class Context {
break;
}
});

// Reorder all types per the contents of the spec document.
this.document!.definitions.forEach((value) => {
switch (value.getKind()) {
case Kind.AliasDefinition:
const aliasDef = value as AliasDefinition;
const a = this.namespace.aliases[aliasDef.name.value];
delete this.namespace.aliases[a.name];
delete this.namespace.allTypes[a.name];
this.namespace.aliases[a.name] = a;
this.namespace.allTypes[a.name] = a;
break;
case Kind.TypeDefinition:
const typeDef = value as TypeDefinition;
const t = this.namespace.types[typeDef.name.value];
delete this.namespace.types[t.name];
delete this.namespace.allTypes[t.name];
this.namespace.types[t.name] = t;
this.namespace.allTypes[t.name] = t;
break;
case Kind.EnumDefinition:
const enumDef = value as EnumDefinition;
const e = this.namespace.enums[enumDef.name.value];
delete this.namespace.enums[e.name];
delete this.namespace.allTypes[e.name];
this.namespace.enums[e.name] = e;
this.namespace.allTypes[e.name] = e;
break;
case Kind.UnionDefinition:
const unionDef = value as UnionDefinition;
const u = this.namespace.unions[unionDef.name.value];
delete this.namespace.unions[u.name];
delete this.namespace.allTypes[u.name];
this.namespace.unions[u.name] = u;
this.namespace.allTypes[u.name] = u;
break;
}
});
}

reportError(error: ApexError): void {
Expand Down Expand Up @@ -363,37 +415,37 @@ export class Context {
this.getType.bind(this),
anyTypeDef as AliasDefinition
);
this.namespace!.aliases[name] = alias;
this.namespace!.allTypes[name] = alias;
this.namespace.aliases[name] = alias;
this.namespace.allTypes[name] = alias;
return alias;
case ASTKind.TypeDefinition:
const type = new MObject(
this.getType.bind(this),
anyTypeDef as TypeDefinition
);
this.namespace!.types[name] = type;
this.namespace!.allTypes[name] = type;
this.namespace.types[name] = type;
this.namespace.allTypes[name] = type;
return type;
case ASTKind.UnionDefinition:
const union = new Union(
this.getType.bind(this),
anyTypeDef as UnionDefinition
);
this.namespace!.unions[name] = union;
this.namespace!.allTypes[name] = union;
this.namespace.unions[name] = union;
this.namespace.allTypes[name] = union;
return union;
case ASTKind.EnumDefinition:
const enumDef = (namedType = new Enum(
this.getType.bind(this),
anyTypeDef as EnumDefinition
));
this.namespace!.enums[name] = enumDef;
this.namespace!.allTypes[name] = enumDef;
this.namespace.enums[name] = enumDef;
this.namespace.allTypes[name] = enumDef;
return enumDef;
}

if (namedType != undefined) {
this.namespace!.allTypes[name] = namedType;
this.namespace.allTypes[name] = namedType;
return namedType;
}

Expand Down
2 changes: 2 additions & 0 deletions templates/local/.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: local
description: Add custom generators to a project by running `apex init local`
3 changes: 3 additions & 0 deletions templates/local/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["apexlang.apexlang"]
}
32 changes: 32 additions & 0 deletions templates/local/codegen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "codegen",
"type": "module",
"engines": {
"node": ">=14.0"
},
"sideEffects": false,
"scripts": {
"prebuild": "npm run clean",
"build": "tsc",
"watch": "tsc -w",
"clean": "shx rm -rf dist",
"test": "echo todo",
"style": "npm run format -- --list-different && npm run lint",
"style:fix": "npm run format:fix && npm run lint:fix",
"format": "prettier \"src/**/*.{ts,tsx,js,jsx,css,scss,sass,less,md}\"",
"format:fix": "npm run format -- --write",
"lint": "eslint -c ./config/.eslintrc.json src --ext .ts",
"lint:fix": "npm run lint -- --fix"
},
"dependencies": {
"@apexlang/core": "^0.0.20"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"eslint": "^7.22.0",
"prettier": "^2.2.1",
"shx": "0.3.3",
"typescript": "^4.5.5"
}
}
1 change: 1 addition & 0 deletions templates/local/codegen/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Module code here
31 changes: 31 additions & 0 deletions templates/local/codegen/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"include": ["src/**/*"],
"exclude": ["dist"],
"compilerOptions": {
"target": "ES2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "ES2020" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": [
"es2020",
"dom"
] /* Specify library files to be included in the compilation. */,
"declaration": true /* Generates corresponding '.d.ts' file. */,
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
"sourceMap": true /* Generates corresponding '.map' file. */,
"outDir": "./dist" /* Redirect output structure to the directory. */,
"rootDir": "." /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"strict": true /* Enable all strict type-checking options. */,
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"typeRoots": ["./types", "./node_modules/@types"],
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
"stripInternal": true,
"importHelpers": true,
"plugins": [
{
"name": "typescript-tslint-plugin",
"alwaysShowRuleFailuresAsWarnings": false
}
]
},
"compileOnSave": false
}
2 changes: 1 addition & 1 deletion templates/module/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
./node_modules
dist
docs
coverage
2 changes: 1 addition & 1 deletion templates/module/.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: module
description: A Apex codegen project
description: An Apex codegen project
variables:

- name: module
Expand Down
3 changes: 3 additions & 0 deletions templates/module/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["apexlang.apexlang"]
}
27 changes: 0 additions & 27 deletions templates/module/config/jest.config.js

This file was deleted.

25 changes: 0 additions & 25 deletions templates/module/config/rollup.config.js

This file was deleted.

4 changes: 0 additions & 4 deletions templates/module/config/setup-tests.js

This file was deleted.

16 changes: 0 additions & 16 deletions templates/module/config/tsconfig.json

This file was deleted.

Loading

0 comments on commit c51bad2

Please sign in to comment.