Comparing version 1.0.0 to 1.1.0
@@ -1,17 +0,2 @@ | ||
## v2.5.0 | ||
* Corrected result annotations, more readme and support of error mocking for actions. | ||
## v2.4.0 | ||
* Added context support to decouple controllers/actions from express method signature and unify the way actions/controllers are declared and used. | ||
## v2.3.0 | ||
* Use operationId as a name for the handler. This will allow to map different routes to the same handler | ||
## v2.2.0 | ||
* Added spec to the route to be able to get any other arbitrary data from route spec | ||
## v2.1.0 | ||
* Added option to provide custom resolver for external type references | ||
## v2.0.0 | ||
* OpenAPI 3.0 support | ||
## 1.0.1 | ||
* Added more protection against code duplication |
23
code.js
const Assert = require('assert'); | ||
const Recast = require('recast'); | ||
const { Location } = require('./location'); | ||
class Code { | ||
constructor(snippet, location) { | ||
if (snippet instanceof Location) { | ||
location = snippet; | ||
snippet = undefined; | ||
} | ||
Assert.ok(!location || location instanceof Location); | ||
this.location = location; | ||
constructor(snippet, parent) { | ||
this.children = []; | ||
snippet !== undefined && this.add(String(snippet)); | ||
this.parent = parent; | ||
} | ||
add(code) { | ||
Assert.ok(code instanceof Code || typeof code === 'string'); | ||
Assert.ok(code instanceof Code || typeof code === 'string', `Actual value ${code}`); | ||
if (code instanceof Code) { | ||
code.location = this.location; | ||
code.parent = this; | ||
} | ||
@@ -27,6 +20,2 @@ this.children.push(code); | ||
getPath() { | ||
return this.location.getPath(); | ||
} | ||
toString() { | ||
@@ -36,5 +25,5 @@ return this.children.map(code => code.toString()).join(''); | ||
static pretty(code, tabWidth = 4) { | ||
static pretty(code, tabWidth = 4, quote = 'single') { | ||
const ast = Recast.parse(code.toString()); | ||
return Recast.prettyPrint(ast, { tabWidth }).code; | ||
return Recast.prettyPrint(ast, { tabWidth, quote }).code; | ||
} | ||
@@ -41,0 +30,0 @@ } |
const { Code } = require('./code'); | ||
const { Var } = require('./var'); | ||
const { Location } = require('./location'); | ||
const { Module } = require('./module'); | ||
const { createModule, ModuleLocation } = require('./module'); | ||
@@ -10,3 +10,4 @@ module.exports = { | ||
Location, | ||
Module | ||
createModule, | ||
ModuleLocation | ||
}; |
@@ -0,1 +1,2 @@ | ||
const Assert = require('assert'); | ||
const Path = require('path'); | ||
@@ -22,2 +23,3 @@ | ||
relative(path) { | ||
Assert.ok(path, 'Path must be provided'); | ||
const ret = new Location(this); | ||
@@ -27,4 +29,9 @@ ret.path = path; | ||
} | ||
set(location) { | ||
this.root = location.root; | ||
this.path = location.path; | ||
} | ||
} | ||
module.exports = { Location }; |
102
module.js
@@ -24,3 +24,2 @@ const Assert = require('assert'); | ||
this.importedCodeRef.getPath()); | ||
return `require('${/\./.test(path) ? path : `./${path}`}')`; | ||
@@ -33,3 +32,27 @@ } | ||
super(name, new Link(hostCode, importedCode)); | ||
this.reference = this.value; | ||
} | ||
unref() { | ||
const pos = this.value.hostCodeRef.imports.indexOf(this); | ||
this.value.hostCodeRef.imports.splice(pos, 1); | ||
return this; | ||
} | ||
inline() { | ||
const pos = this.value.hostCodeRef.imports.indexOf(this); | ||
const varDecl = new Var(this.name); | ||
varDecl.reference = this.value; | ||
varDecl.inline = () => this; | ||
this.value.hostCodeRef.imports.splice(pos, 1, varDecl); | ||
this.isInline = true; | ||
return this; | ||
} | ||
toString() { | ||
if (this.isInline) { | ||
return `${this.name} = ${this.value.toString()};`; | ||
} | ||
return super.toString(); | ||
} | ||
} | ||
@@ -46,16 +69,70 @@ | ||
} | ||
relative(path) { | ||
Assert.ok(path, 'path must be provided'); | ||
const ret = new ModuleLocation(this); | ||
ret.path = path; | ||
return ret; | ||
} | ||
} | ||
class Module extends Code { | ||
constructor(name, location) { | ||
super(location || new ModuleLocation(name)); | ||
this.name = name; | ||
this.external = !location; | ||
constructor(location) { | ||
super(); | ||
this.location = typeof location === 'string' ? new ModuleLocation(location) : location; | ||
this.imports = []; | ||
} | ||
get external() { | ||
let location = this.location; | ||
while (location instanceof Location) { | ||
if (location instanceof ModuleLocation) { | ||
return true; | ||
} | ||
location = location.root; | ||
} | ||
return false; | ||
} | ||
static create(location) { | ||
const newMod = new Module(location); | ||
return newMod; | ||
} | ||
import(name, mod) { | ||
Assert.ok(!this.external, `You cannot add import to external module ${this.getPath()}`); | ||
Assert.ok(mod instanceof Module); | ||
const imp = new Import(name, this, mod); | ||
let imp = new Import(name, this, mod); | ||
// check if we have dup imports or var name conflicts | ||
const existingImp = this.imports.find(im => | ||
String(im.reference) === String(imp.reference)); | ||
if (existingImp) { | ||
if (existingImp.name === name) { | ||
return existingImp; | ||
} | ||
// create a reference to the existing import | ||
imp = new Proxy({ | ||
name | ||
}, { | ||
get(target, prop) { | ||
if (typeof prop === 'string') { | ||
return target.hasOwnProperty(prop) ? target[prop] : existingImp[prop]; | ||
} | ||
return target[prop]; | ||
}, | ||
set(target, prop, value) { | ||
if (typeof prop === 'string' && target.hasOwnProperty(prop)) { | ||
target[prop] = value; | ||
} | ||
// change is not allowed for other properties | ||
} | ||
}); | ||
} | ||
// now check if we have var name cpnflict | ||
const nameConflictImp = this.imports.find(im => im.name === imp.name); | ||
if (nameConflictImp) { | ||
throw new Error(`The var ${name} with the same name already defined in module ${this.getPath()}`); | ||
} | ||
this.imports.push(imp); | ||
@@ -65,2 +142,6 @@ return imp; | ||
getPath() { | ||
return this.location.getPath(); | ||
} | ||
toString() { | ||
@@ -70,4 +151,11 @@ Assert.ok(!this.external, `You cannot serialize external module ${this.getPath()}`); | ||
} | ||
relative(path) { | ||
return this.location.relative(path); | ||
} | ||
} | ||
module.exports = { Module }; | ||
module.exports = { | ||
createModule: Module.create, | ||
ModuleLocation | ||
}; |
{ | ||
"name": "code-jinni", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Generic javascript code generator that uses actual code snippets and mantains relationship ebtween artifacts to simplify DOM construction and eventual code generation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,4 +20,4 @@ code-jinni | ||
```js | ||
const { Module } = require('code-generator/module'); | ||
const mod = new Module('foo'); | ||
const { createModule } = require('code-generator'); | ||
const mod = createModule('foo'); | ||
console.log(mod.getPath()); // foo | ||
@@ -29,6 +29,6 @@ ``` | ||
```js | ||
const { Module, Location } = require('code-generator/module'); | ||
const { createModule, Location } = require('code-generator'); | ||
const root = new Location('some/path/to/root'); | ||
const foo = new Module('foo', root.relative('foo')); | ||
const foo = createModule(root.relative('foo')); | ||
console.log(mod.getPath()); // some/path/to/root/foo | ||
@@ -40,7 +40,7 @@ ``` | ||
```js | ||
const { Module, Location } = require('code-generator'); | ||
const { createModule, Location } = require('code-generator'); | ||
const root = new Location('some/path/to/root'); | ||
const foo = new Module('foo', root.relative('foo')); | ||
const bar = new Module('bar', root.relative('other/bar')); | ||
const foo = createModule(root.relative('foo')); | ||
const bar = createModule(root.relative('other/bar')); | ||
@@ -54,7 +54,7 @@ foo.import('barvar', bar); | ||
```js | ||
const { Module, Location, Code, Var } = require('code-generator'); | ||
const { createModule, Location, Code, Var } = require('code-generator'); | ||
const root = new Location('some/path/to/root'); | ||
const foo = new Module('foo', root.relative('foo')); | ||
const bar = new Module('bar', root.relative('other/bar')); | ||
const foo = createModule(root.relative('foo')); | ||
const bar = createModule(root.relative('other/bar')); | ||
@@ -61,0 +61,0 @@ foo.import('barvar', bar); |
10
var.js
@@ -5,12 +5,16 @@ const Assert = require('assert'); | ||
class Var extends Code { | ||
constructor(name, value) { | ||
constructor(name, value, type = 'const') { | ||
super(); | ||
Assert.ok(value instanceof Code); | ||
Assert.ok(value === undefined || value instanceof Code); | ||
// var name | ||
this.name = name; | ||
this.value = value; | ||
this.type = type; | ||
} | ||
toString() { | ||
return `const ${this.name} = ${this.value.toString()};`; | ||
if (this.value === undefined) { | ||
return `let ${this.name};`; | ||
} | ||
return `${this.type} ${this.name} = ${this.value.toString()};`; | ||
} | ||
@@ -17,0 +21,0 @@ } |
11876
222