Commit 49b393e2 authored by Kornel's avatar Kornel

Merge branch 'babel7' into 'master'

babel7 compatibility

See merge request !18
parents 7cbbba61 ccec0967
Pipeline #122595086 passed with stage
in 1 minute and 11 seconds
const trans = require('./trans');
const Engine = require('php-parser');
module.exports = function() {
// How hell babel 6 pass options?!
const options = {
default_map: true,
encapsulate_ns: false,
map: {}
};
return {
manipulateOptions(opts, parserOpts) {
opts.parserOpts = {
parser(code, opts) {
const parser = new Engine({
parser: {
extractDoc: true
},
ast: {
withPositions: true
}
});
const ast = parser.parseCode(code, {filename: opts.sourceFileName});
return trans.translateProgram(ast, options);
},
};
},
};
};
{
"name": "babel-preset-php",
"version": "1.4.0",
"version": "2.0.0",
"description": "Convert PHP7 to JavaScript via Babel preset",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"test": "mocha"
},
......@@ -15,16 +15,22 @@
"keywords": [
"php",
"php2js",
"babel",
"babel-plugin",
"phpel"
],
"author": "Kornel Lesiński <[email protected]> (https://kornel.ski/)",
"contributors": [
"Raphaël Droz <[email protected]>"
],
"license": "MIT",
"devDependencies": {
"babel-core": "^6.26.0",
"mocha": "^3.5.3"
"@babel/cli": "^7.8.4",
"@babel/core": "^7.8.4",
"mocha": "^7.0.0"
},
"dependencies": {
"@babel/helper-plugin-utils": "^7.8.3",
"@babel/parser": "^7.8.4",
"php-parser": "^2.2.0"
}
}
const plugins = require('./lib/plugins');
const syntaxPlugin = require('./lib/syntax');
const plugins = require('./lib/plugins'),
syntaxPlugin = require('./lib/syntax');
module.exports = function() {
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* Support options (see lib/trans.js)
* - default_map = true
* Suffixes identifier among var,break,case,catch,class,default,extends,function,import,try
*
* - map = {}
* Additional identifiers to suffix using '_r'
*
* - encapsulate_ns = false
* Whether namespaced classes are enclosed inside a { } javascript block.
*/
module.exports = function(api, options) {
return {
plugins: [
syntaxPlugin,
[syntaxPlugin, options],
plugins.defineToConstant,
plugins.isDefined,
plugins.functionExists,
......@@ -14,6 +29,6 @@ module.exports = function() {
plugins.otherFunctions,
plugins.renameException,
plugins.superglobals,
],
].filter(Boolean),
};
}
};
......@@ -254,7 +254,7 @@ exports.stringFunctions = function stringFunctions(babel) {
regstr = "new Regexp('[' + _var_char + ']*$')";
break;
}
var template = require("babylon").parseExpression(regstr);
var template = require("@babel/parser").parseExpression(regstr);
regex = ast_recursive_replace(template, '_var_char', charListNode);
}
......@@ -459,7 +459,7 @@ exports.otherFunctions = function otherFunctions(babel) {
case 'range': {
// Array(Math.ceil((stop - start) / step)).fill(start).map((x, y) => x + y * step)
var [start, stop, step] = p.node.arguments;
var template = require("babylon").parseExpression("Array(Math.ceil((_var_stop - _var_start) / _var_step)).fill(_var_start).map((x, y) => x + y * _var_step)");
var template = require("@babel/parser").parseExpression("Array(Math.ceil((_var_stop - _var_start) / _var_step)).fill(_var_start).map((x, y) => x + y * _var_step)");
ast_recursive_replace(template, '_var_start', start);
ast_recursive_replace(template, '_var_stop', stop);
ast_recursive_replace(template, '_var_step', step);
......
const Engine = require('php-parser'),
translateProgram = require('./trans').default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function(api, options) {
api.assertVersion(7);
return {
name: "syntax-php",
manipulateOptions: (opts, parserOpts) => {
parserOpts.tokens = true;
},
parserOverride: (code, opts) => {
const parser = new Engine({
parser: {
extractDoc: true
},
ast: {
withPositions: true
}
});
const ast = parser.parseCode(code, {filename: opts.sourceFileName});
// console.log(`Parse with options ${JSON.stringify(options)}`);
return translateProgram(ast, options);
}
};
};
......@@ -17,11 +17,12 @@ const default_map = {
var map = {};
var encapsulate_ns;
exports.translateProgram = function translateProgram(program, options = {default_map: true, encapsulate_ns: false}) {
exports.default = function (program, options = {default_map: true, encapsulate_ns: false}) {
assert.equal(program.kind, "program");
map = {};
if (options) {
Object.assign(map,
options.default_map ? default_map : {},
options.default_map === false ? {} : default_map,
options.map || {});
encapsulate_ns = options.encapsulate_ns;
}
......@@ -36,7 +37,7 @@ exports.translateProgram = function translateProgram(program, options = {default
"body": [],
}, program.children, scope),
};
}
};
function mapIdent(name) {
if (name in map && name !== 'constructor') {
......
const assert = require('assert');
const babel = require("babel-core");
const phpPreset = require("../index");
const assert = require('assert'),
babel = require("@babel/core"),
phpPreset = require("../src/index");
module.exports = function translates(phpSrc, expected = undefined) {
module.exports = function translates(phpSrc, expected = undefined, preset_options = {}) {
let ignoreSemi = false;
if (undefined === expected) {
expected = phpSrc.replace(/\$/g, '').replace(/;/g, '');
......@@ -11,7 +11,7 @@ module.exports = function translates(phpSrc, expected = undefined) {
let out;
try {
out = babel.transform(`<?php ${phpSrc}`, {
presets: [phpPreset],
presets: [[phpPreset, preset_options]],
}).code;
} catch(e) {
e.message += `\nin ${phpSrc}`;
......
......@@ -203,13 +203,13 @@ describe('AST translation', function() {
function annotated($untyped = null, Cls\\Name $class = null, self $self = null, array $array = null,
callable $callable = null, bool $bool = null, float $float = null, int $int = null, string $string = null, iterable $iter = null) {}}`,
`class Foo {
annotated(untyped = undefined, class_r: ?Cls.Name = undefined, self: ?Foo = undefined, array: ?{} | any[] = undefined,
callable: ?Function = undefined, bool: ?boolean = undefined, float: ?number = undefined, int: ?number = undefined, string: ?string = undefined, iter: ?{} | any[] = undefined) {}};`);
annotated(untyped = undefined, class_r: ?Cls.Name = undefined, self: ?Foo = undefined, array: ?({} | any[]) = undefined,
callable: ?Function = undefined, bool: ?boolean = undefined, float: ?number = undefined, int: ?number = undefined, string: ?string = undefined, iter: ?({} | any[]) = undefined) {}};`);
translates(`class Foo {
function annotated($untyped, ?Cls\\Name $class, ?self $self, ?array $array,
?callable $callable, ?bool $bool, ?float $float, ?int $int, ?string $string, ?iterable $iter) {}}`,
`class Foo {
annotated(untyped, class_r: ?Cls.Name, self: ?Foo, array: ?{} | any[],
callable: ?Function, bool: ?boolean, float: ?number, int: ?number, string: ?string, iter: ?{} | any[]) {}};`);
annotated(untyped, class_r: ?Cls.Name, self: ?Foo, array: ?({} | any[]),
callable: ?Function, bool: ?boolean, float: ?number, int: ?number, string: ?string, iter: ?({} | any[])) {}};`);
});
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment