Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Jan 21, 2017
0 parents commit 35c0087
Show file tree
Hide file tree
Showing 39 changed files with 2,549 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.DS_Store
coverage
publish
lib
node_modules/*
npm-debug.log
src/*.js
src/*.js.map
src/*.d.ts
test/*.js
test/*.js.map
test/*.d.ts
src/**/*.js
src/**/*.js.map
src/**/*.d.ts
test/**/*.js
test/**/*.js.map
test/**/*.d.ts
11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
src
test
coverage
publish
tsconfig.json
tsd.json
gulpfile.js
.travis.yml
.gitignore
.vscode
.publishrc
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js

node_js:
- stable
- 6.0.0
- 5.0.0
- 4.1.0
- '0.12'
- '0.10'

after_script: "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
41 changes: 41 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Mocha",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
"--no-timeouts",
"--recursive",
"--colors"
],
"cwd": "${workspaceRoot}",
"runtimeExecutable": null,
"env": {
"NODE_ENV": "testing"
}
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/index.js",
"cwd": "${workspaceRoot}",
"outFiles": [],
"sourceMaps": true
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858,
"outFiles": [],
"sourceMaps": true
}
]
}
9 changes: 9 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "0.1.0",
"command": "${cwd}/node_modules/.bin/tsc",
"isShellCommand": true,
"args": ["-w", "-p", "."],
"showOutput": "silent",
"isWatching": true,
"problemMatcher": "$tsc-watch"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 kambojajs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#Kenanga

[![Build Status](https://travis-ci.org/kambojajs/kecubung.svg?branch=master)](https://travis-ci.org/kambojajs/kecubung)
[![Coverage Status](https://coveralls.io/repos/github/kambojajs/kecubung/badge.svg?branch=master)](https://coveralls.io/github/kambojajs/kecubung?branch=master)

A javascript transformer to Type Metadata.

###About

Kenanga is javascript syntax transformer into Type information
such as Class, Method, Constructor, Module etc.
86 changes: 86 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"use strict";


var gulp = require("gulp"),
tsc = require("gulp-typescript"),
runSequence = require("run-sequence"),
mocha = require("gulp-mocha"),
istanbul = require("gulp-istanbul");

//******** BUILD *************

var tsProject = tsc.createProject("tsconfig.json", {
declaration: true,
noResolve: false,
typescript: require("typescript")
});

gulp.task("build-source", function() {
return gulp.src([
"src/**/**.ts"
])
.pipe(tsProject())
.on("error", function (err) {
process.exit(1);
})
.pipe(gulp.dest("src/"));
});

var tsTestProject = tsc.createProject("tsconfig.json", {
declaration: false,
noResolve: false,
typescript: require("typescript")
});

gulp.task("build-test", function() {
return gulp.src([
"test/**/**.ts"
])
.pipe(tsTestProject())
.on("error", function (err) {
process.exit(1);
})
.pipe(gulp.dest("test"));
});


gulp.task("build", function(cb) {
runSequence("build-source", "build-test", cb);
});


//******** TEST *************
gulp.task("mocha", function() {
return gulp.src([
"test/**/**.js"
])
.pipe(mocha({ui: "bdd"}))
.pipe(istanbul.writeReports());
});

gulp.task("istanbul:hook", function() {
return gulp.src(["src/**/**.js"])
// Covering files
.pipe(istanbul())
// Force `require` to return covered files
.pipe(istanbul.hookRequire());
});

gulp.task("test", function(cb) {
runSequence("istanbul:hook", "mocha", cb);
});

//******** DISTRIBUTION *************
gulp.task("dist", function() {
return gulp.src(["src/**/*.js", "src/**/*.d.ts"])
.pipe(gulp.dest("lib/"));
});

//******** DEFAULT *************
gulp.task("default", function (cb) {
runSequence(
"build",
"test",
"dist",
cb);
});
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "kecubung",
"version": "0.0.1-dev001",
"description": "Javascript transformer to Type Metadata",
"main": "lib/index.js",
"scripts": {
"test": "gulp"
},
"keywords": [],
"author": "Ketut Sandiarsa <ktutnik@gmail.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/kambojajs/kenanga"
},
"typings": "./lib/index.d.ts",
"dependencies": {
"reflect-metadata": "^0.1.9",
"tslib": "^1.4.0"
},
"devDependencies": {
"@types/babylon": "^6.7.15",
"@types/chai": "^3.4.34",
"@types/express": "^4.0.34",
"@types/mocha": "^2.2.33",
"@types/node": "^6.0.52",
"babylon": "^6.14.1",
"chai": "^3.5.0",
"coveralls": "^2.11.15",
"gulp": "^3.9.1",
"gulp-istanbul": "^1.1.1",
"gulp-mocha": "^3.0.1",
"gulp-typescript": "^3.1.3",
"mocha": "^3.2.0",
"run-sequence": "^1.2.2",
"typescript": "^2.0.10"
}
}
49 changes: 49 additions & 0 deletions src/analyzers/class-analyzer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { SyntaxKind } from "../core"
import * as HP from "./helper"

export class ClassAnalyzer {

/**
* expect AssignmentExpression, VariableDeclaration
*/
constructor(private node) { }

/**
* expect AssignmentExpression
*/
isExported(name, parentName) {
return this.isExportedStatement()
&& (this.node.left.object.name == parentName || this.node.left.object.name == "exports")
&& this.node.right.name == name
}

/**
* expect AssignmentExpression
*/
private isExportedStatement() {
return this.node.type == SyntaxKind.AssignmentExpression
&& this.node.left.type == SyntaxKind.MemberExpression
&& this.node.right.type == SyntaxKind.Identifier
}

getName() {
if (this.isExportedStatement())
return this.node.right.name;
else if (this.isCandidate())
return this.node.declarations[0].id.name
else return null;
}

/**
* expect VariableDeclaration
*/
isCandidate() {
return this.node.type == SyntaxKind.VariableDeclaration
&& this.node.declarations[0].type == SyntaxKind.VariableDeclarator
&& this.node.declarations[0].init
&& this.node.declarations[0].init.type == SyntaxKind.CallExpression
&& this.node.declarations[0].init.callee.type == SyntaxKind.FunctionExpression
&& this.node.declarations[0].init.callee.id == null
}
}

31 changes: 31 additions & 0 deletions src/analyzers/class-decorator-analyzer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { SyntaxKind, MetaData } from "../core"
import * as HP from "./helper"


export class ClassDecoratorAnalyzer{

/**
* expect AssignmentExpression
*/
constructor(private node){}

getClassName(){
if (this.isDecorator()){
return this.node.left.name;
}
else return null;
}

isDecorator() {
return this.node.type == SyntaxKind.AssignmentExpression
&& this.node.left.type == SyntaxKind.Identifier
&& this.node.right.type == SyntaxKind.CallExpression
&& HP.getMethodNameFromCallee(this.node.right.callee) == "__decorate"
}

getDecorators():MetaData[]{
if(this.isDecorator())
return HP.getDecorators(this.node.right.arguments[0])
else return null;
}
}
53 changes: 53 additions & 0 deletions src/analyzers/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { SyntaxKind, MetaData, AnalysisType } from "../core"

/**
* require CallExpression.callee
*/
export function getMethodNameFromCallee(callee) {
if (!callee) return "";
if (callee.type == SyntaxKind.MemberExpression) return callee.property.name;
else return callee.name;
}

/**
* require ArrayExpression
*/
export function getDecorators(arrayExpression) {
let result = [];
for (let x of arrayExpression.elements) {
if (isReservedDecorator(x)) continue;
result.push(<MetaData>{
type: "Decorator",
name: getMethodNameFromCallee(x.callee),
analysis: AnalysisType.Valid,
children: x.arguments.map(arg => <MetaData>{
type: "Parameter",
name: getDecoratorParameterName(arg),
analysis: AnalysisType.Valid,
children: []
})
})
}
return result;
}

/**
* require CallExpression
*/
export function isReservedDecorator(callExpression) {
return getMethodNameFromCallee(callExpression.callee) == "__metadata"
|| getMethodNameFromCallee(callExpression.callee) == "__param"
}

export function getDecoratorParameterName(param) {
switch (param.type) {
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
case SyntaxKind.BooleanLiteral:
return param.value;
case SyntaxKind.Identifier:
return param.name;
default:
return "Unknown";
}
}
Loading

0 comments on commit 35c0087

Please sign in to comment.