Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Transpiling #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
10 changes: 10 additions & 0 deletions transpile/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scripts": {
"debug": "yarn jscodeshift test.js --dry --print"
},
"dependencies": {
"acorn": "^5.5.0",
"jscodeshift": "^0.4.1",
"recast": "^0.14.4"
}
}
77 changes: 77 additions & 0 deletions transpile/parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Modified https://github.com/angelozerr/acorn-es7/blob/master/acorn-es7.js to do the same thing
* but for function declarations instead of class declarations
*/

const acorn = require('acorn')

function getTokenType(p, loose) {
return loose ? p.tok.type : p.type;
}

var extendsAcorn = function (pp) {
var loose = pp == (acorn.LooseParser && acorn.LooseParser.prototype);

pp.parseDecorator = function() {
var node = this.startNode();
this.next();
node.expression = this.parseMaybeAssign();
return this.finishNode(node, "Decorator");
}

return function(instance) {
instance.decorators = [];

instance.extend('getTokenFromCode', function(inner) {
return function(code) {
if (code == 64) {
++this.pos; return this.finishToken(tt.at);
}
return inner.call(this, code);
};
});

instance.extend('parseStatement', function(inner) {
return function(declaration, topLevel) {
switch(getTokenType(this, loose)) {
case tt.at:
while (getTokenType(this, loose) === tt.at) {
this.decorators.push(this.parseDecorator());
}
if (!loose && getTokenType(this, loose) !== tt._function) {
this.raise(this.start, "Leading decorators must be attached to a function declaration");
}
case tt._function:
if (!loose && !declaration) this.unexpected()
if (this.decorators.length) {
var node = inner.call(this, declaration, topLevel);
node.decorators = this.decorators;
// adjust start of FunctionDeclaration with start of the first decorator (helpful for ES7 walk).
node.start = node.decorators[0].start;
this.decorators = [];
return node;
}
}
return inner.call(this, declaration, topLevel);
};
});
}
}

var tt = acorn.tokTypes;
tt.at = new acorn.TokenType('@');
// acorn
acorn.plugins.es7 = extendsAcorn(acorn.Parser.prototype);
// acorn loose
if(acorn.LooseParser) acorn.pluginsLoose.es7 = extendsAcorn(acorn.LooseParser.prototype);

module.exports = {
parse(source) {
return acorn.parse(source, {
// Specify use of the plugin
plugins:{es7:true},
// Specify the ecmaVersion
ecmaVersion:7
})
}
}
41 changes: 41 additions & 0 deletions transpile/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/** Classic decorator */
function memoize(func) {
const results = new Map();
return (arg, ...rest) => {
if (results.has(arg)) {
return results.get(arg);
}
const value = func(arg, ...rest);
results.set(arg, value);
return value;
}
}

/** Parametrized decorator */
function partial(...preargs) {
return func => (...args) => func(...preargs, ...args);
}

const USERS = {
42: {
name: "Dan"
}
}

/** Wrap in block */
function init() {
/** Hoisted call */
search(42);

/** Classic decorator appliance */
@memoize
/** Decorator factory appliance */
@partial(USERS)
function search(users, id) {
return users[id];
}

return search;
}

init();
29 changes: 29 additions & 0 deletions transpile/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const parser = require('./parser')

module.exports = function (fileInfo, api, options) {
const { j } = api
return j(fileInfo.source)
.find(j.FunctionDeclaration)
.filter(path => path.node.decorators)
.map(path => {
const { node } = path
const functionExpression = j.functionExpression(
node.id,
node.params,
node.body
)
const resolved = node.decorators.reduceRight((acc, decorator) => {
return j.callExpression(decorator.expression, [acc])
}, functionExpression)
path.replace(j.variableDeclaration('var', [
j.variableDeclarator(
node.id,
resolved,
)
]))
return path
})
.toSource();
}

module.exports.parser = parser
Loading