core-annotations
Advanced tools
Comparing version 0.0.1 to 1.0.1
@@ -0,1 +1,12 @@ | ||
class Annotation { | ||
constructor({ annotation, clz, type, target, values }) { | ||
this.annotation = annotation | ||
this.clz = clz | ||
this.type = type | ||
this.target = target | ||
this.values = values | ||
} | ||
} | ||
module.exports = { | ||
@@ -6,11 +17,14 @@ | ||
let generated = this._generateAnnotations(annotations, src) | ||
injected += this._injectClasses(generated) | ||
injected += this._injectMethods(generated) | ||
injected += this._injectFields(generated) | ||
src = this._cleanup(annotations, src) | ||
src = this._inject(src, injected) | ||
src = this._fixExport(src) | ||
let classAnnotations = this._getClassAnnotations(annotations, src, generated) | ||
let methodAnnotations = this._getMethodAnnotations(annotations, src, generated) | ||
let fieldAnnotations = this._getFieldAnnotations(annotations, src, generated) | ||
let annos = [...classAnnotations, ...methodAnnotations, ...fieldAnnotations] | ||
if (annos.length > 0) { | ||
src = this._cleanup(annotations, src) | ||
src = this._inject(src, annos) | ||
src = this._fixExport(src) | ||
} | ||
return { | ||
src: src, | ||
compiled: injected.length > 0 | ||
compiled: annos.length > 0 | ||
} | ||
@@ -20,8 +34,11 @@ }, | ||
_cleanup: function(annotations, src) { | ||
let key = new Date().getTime() | ||
src = src.replace(/\@(\w+)\s*(\([^\(\)]+\)|\s)/g, (all, $annotation, $values) => { | ||
if (!!annotations[$annotation]) { | ||
return '' | ||
if (!annotations[$annotation]) { | ||
return all | ||
} | ||
return all | ||
return ` @[${key++}] ` | ||
}) | ||
src = src.replace(/\]\s*\@\[/g, ',') | ||
src = src.replace(/\@\[([\d,]+)\](\s+\w+(\r|\n)+|)/g, '') | ||
return src | ||
@@ -32,9 +49,11 @@ }, | ||
let clz = null | ||
src = src.replace(/module\.exports\s*=(\s*class\s+\w+\s+[\w\s]*)\{/g, (all, $clz) => { | ||
clz = $clz | ||
return $clz | ||
src = src.replace(/module\.exports\s*=(\s*class\s+\w+\s+[\w\s]*\{)/g, (all, $clz) => { | ||
clz = $clz.trim() | ||
return `${$clz}` | ||
}) | ||
if (!!clz) { | ||
clz = clz.replace(/class\s+(\w+)\s+.*/g, (all, $clz) => $clz.trim()) | ||
src = `${src}\r\nmodule.exports = ${clz}` | ||
} | ||
return src | ||
}, | ||
@@ -83,16 +102,7 @@ | ||
_inject: function(src, injected) { | ||
src += `\r\n | ||
var _annotations = [] | ||
\r\n | ||
${injected} | ||
\r\n | ||
_annotations.sort((a, b) => b.getPriority() - a.getPriority()) | ||
_annotations.forEach(annotation => annotation.compile()) | ||
\r\n` | ||
return src | ||
}, | ||
_injectClasses: function({ src, targets }) { | ||
let injected = '' | ||
_getClassAnnotations: function(annotations, src, generated) { | ||
generated = !!generated ? generated : this._generateAnnotations(annotations, src) | ||
src = generated.src | ||
let targets = generated.targets | ||
let classAnnotations = [] | ||
src = src.replace(/\@\[([\d,]+)\][\w\s.=]*class\s+(\w+)\s+[\w\s]*\{/g, (all, $targets, $class) => { | ||
@@ -102,18 +112,20 @@ $targets = $targets.split(',') | ||
var target = targets[$target] | ||
injected += `\r\n | ||
var _annotation = new Annotation._annotations['${target.$annotation}']() | ||
_annotation._clz = ${$class} | ||
_annotation._target = '${$class}' | ||
_annotation._type = 'class' | ||
_annotation._values = ${target.$values} | ||
_annotations.push(_annotation) | ||
\r\n` | ||
classAnnotations.push(new Annotation({ | ||
annotation: target.$annotation, | ||
clz: $class, | ||
type: 'class', | ||
target: $class, | ||
values: target.$values | ||
})) | ||
}) | ||
return all | ||
}) | ||
return injected | ||
return classAnnotations | ||
}, | ||
_injectFields: function({ src, targets }) { | ||
let injected = '' | ||
_getFieldAnnotations: function(annotations, src, generated) { | ||
generated = !!generated ? generated : this._generateAnnotations(annotations, src) | ||
src = generated.src | ||
let targets = generated.targets | ||
let fieldAnnotations = [] | ||
src = src.replace(/\@\[([\d,]+)\]\s*(\w+)\s+(\r|\n)/g, (all, $targets, $field) => { | ||
@@ -125,10 +137,9 @@ var clz = this._getClass(src, $targets) | ||
var target = targets[$target] | ||
injected += `\r\n | ||
var _annotation = new Annotation._annotations['${target.$annotation}']() | ||
_annotation._clz = ${clz} | ||
_annotation._target = '${$field}' | ||
_annotation._type = 'field' | ||
_annotation._values = ${target.$values} | ||
_annotations.push(_annotation) | ||
\r\n` | ||
fieldAnnotations.push(new Annotation({ | ||
annotation: target.$annotation, | ||
clz: clz, | ||
type: 'field', | ||
target: $field, | ||
values: target.$values | ||
})) | ||
}) | ||
@@ -138,7 +149,10 @@ } | ||
}) | ||
return injected | ||
return fieldAnnotations | ||
}, | ||
_injectMethods: function({ src, targets }) { | ||
let injected = '' | ||
_getMethodAnnotations: function(annotations, src, generated) { | ||
generated = !!generated ? generated : this._generateAnnotations(annotations, src) | ||
src = generated.src | ||
let targets = generated.targets | ||
let methodAnnotations = [] | ||
src = src.replace(/\@\[([\d,]+)\]\s*(\w*[\t ]+\w+|\w+)\s*\([^\(\)]*\)\s*\{/g, (all, $targets, $method) => { | ||
@@ -150,10 +164,9 @@ var clz = this._getClass(src, $targets) | ||
var target = targets[$target] | ||
injected += `\r\n | ||
var _annotation = new Annotation._annotations['${target.$annotation}']() | ||
_annotation._clz = ${clz} | ||
_annotation._target = '${$method}' | ||
_annotation._type = 'method' | ||
_annotation._values = ${target.$values} | ||
_annotations.push(_annotation) | ||
\r\n` | ||
methodAnnotations.push(new Annotation({ | ||
annotation: target.$annotation, | ||
clz: clz, | ||
type: 'method', | ||
target: $method, | ||
values: target.$values | ||
})) | ||
}) | ||
@@ -163,5 +176,29 @@ } | ||
}) | ||
return injected | ||
return methodAnnotations | ||
}, | ||
_inject: function(src, annos) { | ||
annos = annos || [] | ||
if (annos.length == 0) { | ||
return src | ||
} | ||
src += '\r\n' | ||
src += 'var _annotations = []' | ||
annos.forEach(annotation => { | ||
src += `\r\n | ||
var _annotation = new Annotation._annotations['${annotation.annotation}']() | ||
_annotation._clz = ${annotation.clz} | ||
_annotation._target = '${annotation.target}' | ||
_annotation._type = '${annotation.type}' | ||
_annotation._values = ${annotation.$values} | ||
_annotations.push(_annotation) | ||
\r\n` | ||
}) | ||
src += `\r\n | ||
_annotations.sort((a, b) => b.getPriority() - a.getPriority()) | ||
_annotations.forEach(annotation => annotation.compile()) | ||
\r\n` | ||
return src | ||
} | ||
} | ||
const fse = require('fs-extra') | ||
const hook = require('node-hook') | ||
const path = require('path') | ||
@@ -12,2 +13,6 @@ const compiler = require('./compiler') | ||
getAnnotation() { | ||
return this._annotation | ||
} | ||
getClass() { | ||
@@ -79,3 +84,3 @@ return this._clz | ||
if (result.compiled && Annotation._debuggable) { | ||
fse.outputFileSync(Annotation._logDir, result.src); | ||
fse.outputFileSync(path.join(Annotation._logDir, dir.replace(process.cwd(), '')), result.src); | ||
} | ||
@@ -85,14 +90,2 @@ return result.src | ||
Annotation._getClass = (src, key) => { | ||
let clz = null; | ||
src = src.replace(key, '[@@@@@]'); | ||
src = src.replace(/[\r\n]*/g, ''); | ||
src = src.replace(/class\s+([a-zA-Z]+).*\{/g, all => `\r\n${all}`); | ||
src = src.replace(/class\s+([a-zA-Z]+).*\{.*\[\@\@\@\@\@\]/g, (all, $clz) => { | ||
clz = $clz; | ||
return all; | ||
}); | ||
return clz; | ||
} | ||
hook.hook('.js', (src, dir) => Annotation._compile(src, dir.replace(process.cwd(), ''))) | ||
@@ -99,0 +92,0 @@ |
{ | ||
"name": "core-annotations", | ||
"version": "0.0.1", | ||
"version": "1.0.1", | ||
"description": "Flexible annotation parser for Node", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "gulp test" | ||
}, | ||
@@ -27,3 +27,9 @@ "repository": { | ||
"node-hook": "^0.4.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"gulp": "^3.9.1", | ||
"gulp-mocha": "^4.0.1", | ||
"mocha": "^3.2.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
21360
4
8
282
1