Comparing version 0.1.5 to 0.1.6
61
index.js
@@ -0,1 +1,14 @@ | ||
exports.dialects = require('fs').readdirSync(require('path').resolve(__dirname, 'parsers')).map(function (s) { | ||
return (s !== ('js.js') && s.match(/(.+)\.js$/) || [0, 0])[1] | ||
}).filter(isNaN); | ||
exports.register = function (ext) { | ||
ext = [].concat(ext); | ||
ext.forEach(function (ext) { | ||
require.extensions['.' + ext] = function (module, path) { | ||
return module._compile(require('./').read({ | ||
"dialect": ext | ||
}, require('fs').readFileSync(path, 'utf-8')), path) | ||
} | ||
}) | ||
}; | ||
exports.read = function (opts, input) { | ||
@@ -5,3 +18,5 @@ return compile(opts, parse(opts, input)) | ||
var parse = exports.parse = function (opts, input) { | ||
return require('./parsers/' + opts.language).parse(input) | ||
var ast = require('./parsers/' + opts.dialect).parse(input); | ||
if (opts.ast) return console.log(JSON.stringify(ast, null, 4)) && ''; | ||
return ast | ||
}; | ||
@@ -13,5 +28,9 @@ var compile = exports.compile = function (opts, ast) { | ||
var inlineElements = { | ||
"Program": 0, | ||
"GroupedExpression": 0, | ||
"AssignmentExpression": 0, | ||
"FunctionCall": 0, | ||
"ForInStatement": 0, | ||
"ForStatement": 0, | ||
"IfStatement": 0, | ||
"VariableDeclaration": 0, | ||
@@ -21,2 +40,6 @@ "PropertyAccess": 0, | ||
}; | ||
var ifStatementInline = { | ||
"IfStatement": 0, | ||
"Block": 0 | ||
}; | ||
var notTerminatedElements = { | ||
@@ -28,12 +51,5 @@ "IfStatement": 0, | ||
"WhileStatement": 0, | ||
"DoWhileStatement": 0, | ||
"WithStatement": 0, | ||
"SwitchStatement": 0 | ||
}; | ||
var bracketAccess = { | ||
"Expression": 0, | ||
"Variable": 0, | ||
"NumericLiteral": 0, | ||
"PropertyAccess": 0 | ||
}; | ||
var ind = function (il) { | ||
@@ -87,3 +103,3 @@ if (!opts.tab) return ''; | ||
}; | ||
return function compile (il, par) { | ||
return function compile(il, par) { | ||
il = il || 0; | ||
@@ -95,3 +111,3 @@ return function (node) { | ||
"initial": (!(par in inlineElements) ? inl(il) : ''), | ||
"name": node.name ? ' ' + node.name + sp() : sp(), | ||
"name": node.name ? ' ' + node.name : sp(), | ||
"params": node.params.join(',' + sp()), | ||
@@ -109,3 +125,3 @@ "elements": (node.elements && node.elements.length ? nli(il + 1) + node.elements.map(compile(il + 1, node.type)).reduce(ej(nli(il + 1), nli(il)), '') : '') | ||
"PropertyAccess": function () { | ||
return Element(node.type, tmpl(il, (node.name.type in bracketAccess ? '{base}[{name}]' : '{base}.{name}'), { | ||
return Element(node.type, tmpl(il, (!node.name.type ? '{base}.{name}' : '{base}[{name}]'), { | ||
"base": compile(il, node.type)(node.base), | ||
@@ -220,3 +236,3 @@ "name": compile(il, node.type)(node.name) | ||
return Element(node.type, tmpl(il, 'var {declarations}', { | ||
"declarations": (node.declarations || []).map(compile(il, node.type)).join(';' + nli(il) + 'var ') | ||
"declarations": (node.declarations || []).map(compile(il, node.type)).join(', ' + (!(par in inlineElements) ? nli(il + 1) : '')) | ||
})) | ||
@@ -235,4 +251,5 @@ }, | ||
"left": compile(il, node.type)(node.left), | ||
"operator": tmpl(il, (node.operator === ',' ? '{operator}{nli}' : '{sp}{operator}{sp}'), { | ||
"operator": node.operator | ||
"operator": tmpl(il, (node.operator === ',' ? '{operator}{union}' : '{sp}{operator}{sp}'), { | ||
"operator": node.operator, | ||
"union": !(par in inlineElements) ? nli(il + 1) : sp() | ||
}), | ||
@@ -250,3 +267,3 @@ "right": compile(il, node.type)(node.right) | ||
return Element(node.type, tmpl(il, '{initial}if{sp}({condition}){ifStatement}{elseStatement}', { | ||
"initial": (par !== 'IfStatement' ? inl(il) : ''), | ||
"initial": !(par in ifStatementInline) ? inl(il) : '', | ||
"condition": compile(il, node.type)(node.condition), | ||
@@ -266,3 +283,3 @@ "ifStatement": tmpl(il, (ifStatementBlock ? '{sp}{{nli1}{ifStatement}{nli}}' : ' {ifStatement};'), { | ||
var statementBlock = node.statement && node.statement.elements && node.statement.elements.length; | ||
return Element(node.type, tmpl(il, '{initial}do{condition}{sp}while{sp}{statement}', { | ||
return Element(node.type, tmpl(il, '{initial}do{sp}{statement}{sp}while{sp}({condition})', { | ||
"initial": inl(il), | ||
@@ -332,4 +349,4 @@ "condition": compile(il, node.type)(node.condition), | ||
"expression": compile(il, node.type)(node.expression), | ||
"clauses": node.clauses.length ? tmpl(il, '{nl1}{clauses}{nl1}', { | ||
"clauses": node.clauses.map(compile(il + 1, node.type)) | ||
"clauses": node.clauses.length ? tmpl(il, '{nli1}{clauses}{nli}', { | ||
"clauses": node.clauses.map(compile(il + 1, node.type)).join(';' + nli(il + 1)) | ||
}) : '' | ||
@@ -339,5 +356,5 @@ })) | ||
"CaseClause": function () { | ||
return Element(node.type, tmpl(il, 'case {selector}:{nli1}{elements}', { | ||
return Element(node.type, tmpl(il, 'case {selector}:{elements}', { | ||
"selector": compile(il, node.type)(node.selector) || '""', | ||
"elements": node.elements.length ? tmpl(il, '{nl1}{elements}', { | ||
"elements": node.elements.length ? tmpl(il, '{nli1}{elements}', { | ||
"elements": node.elements.map(compile(il + 1, node.type)).join(';' + nli(il + 1)) | ||
@@ -348,4 +365,4 @@ }) : '' | ||
"DefaultClause": function () { | ||
return Element(node.type, tmpl(il, 'default:{nli1}{elements}', { | ||
"elements": node.elements.length ? tmpl(il, '{nl1}{elements}', { | ||
return Element(node.type, tmpl(il, 'default:{elements}', { | ||
"elements": node.elements.length ? tmpl(il, '{nli1}{elements}', { | ||
"elements": node.elements.map(compile(il + 1, node.type)).join(';' + nli(il + 1)) | ||
@@ -352,0 +369,0 @@ }) : '' |
{ | ||
"name": "node-jsjs", | ||
"description": "Javascript beautifier", | ||
"description": "Javascript dialectic transpiler", | ||
"keywords": [ | ||
@@ -10,3 +10,3 @@ "javascript", | ||
"author": "aynik", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"repository": { | ||
@@ -13,0 +13,0 @@ "type": "git", |
# Jsjs | ||
## A handy javascript to javascript transpiler | ||
## A handy javascript dialectic transpiler | ||
@@ -30,2 +30,3 @@ [![Build Status via Travis CI](https://travis-ci.org/aynik/jsjs.svg?branch=master)](https://travis-ci.org/aynik/jsjs) | ||
* [`--compress, -c`](#compress) | ||
* [`--dialect, -l [dialect]`](#dialect) | ||
@@ -50,2 +51,52 @@ --- | ||
<a name="dialect" /> | ||
### jsjs --dialect [dialect] | -l [dialect] | ||
Use another input dialect instead of javascript. | ||
Javascript dialects are basically different languages which follow the style and the | ||
semantics of javascript. | ||
This library includes the following dialects: | ||
### Standard Javascript (js): Common ECMAScript 5. | ||
```js | ||
function pow(a, b){ | ||
for (var r = a, n = 0; n < b; n++) { | ||
r = r * a | ||
} | ||
return r | ||
} | ||
function head(arr){ | ||
return arr.slice(0, 1); | ||
} | ||
``` | ||
### Go-Script (gs): A clone of [Go](http://golang.com) syntax without the type stuff. | ||
```go | ||
func pow(a, b){ | ||
for r := a, n := 0; n < b; n++ { | ||
r = r * a | ||
} | ||
return r | ||
} | ||
func head(arr){ | ||
return arr[0:1] | ||
} | ||
``` | ||
To register dialects as Node's require.extensions, you can use *jsjs.register*: | ||
```js | ||
var jsjs = require('jsjs'); | ||
jsjs.register('gs'); // Go-Script registered | ||
var gos = require('./go-test.gs'); | ||
jsjs.register(jsjs.dialects); // registers all supported jsjs dialects | ||
``` | ||
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
1539629
11
32942
101
1
4