Skip to content

Commit

Permalink
fix: 'function' and default modifer emission, refine tests
Browse files Browse the repository at this point in the history
fix: 'function' for constructor/method declaration
fix: default property modifier to public since property a modifier in PHP is mandatory
chore: invalid namespace in test/features/Class.php
  • Loading branch information
harttle committed Apr 19, 2019
1 parent 024fa28 commit 21db4e8
Show file tree
Hide file tree
Showing 28 changed files with 42 additions and 32 deletions.
12 changes: 11 additions & 1 deletion src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,13 @@ export function emitFile(

function emitPropertyDeclaration(node: ts.PropertyDeclaration) {
// emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
if (node.modifiers) {
emitModifiers(node, node.modifiers);
}
else {
writeKeyword('public');
writeSpace();
}
emit(node.name);
// emit(node.questionToken);
// emit(node.exclamationToken);
Expand All @@ -784,6 +790,8 @@ export function emitFile(
emitModifiers(node, node.modifiers);
// emit(node.asteriskToken);
if (ts.isClassDeclaration(node.parent)) {
writeKeyword("function");
writeSpace();
emit(node.name);
}
else {
Expand All @@ -802,6 +810,8 @@ export function emitFile(

function emitConstructor(node: ts.ConstructorDeclaration) {
// emitModifiers(node, node.modifiers);
writeKeyword("function");
writeSpace();
writeKeyword("__construct");
emitSignatureAndBody(node, emitSignatureHead);
}
Expand Down
2 changes: 1 addition & 1 deletion test/features/Alias.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\Alias;
namespace test\case_Alias;
$a = "";
$a .= "123";
$b = 0;
Expand Down
2 changes: 1 addition & 1 deletion test/features/ArrayApi.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\ArrayApi;
namespace test\case_ArrayApi;
$a = count(array(1));
$b = array(1, "a");
$c = count($b);
Expand Down
2 changes: 1 addition & 1 deletion test/features/ArrayLiteralExpression.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace test\ArrayLiteralExpression;
namespace test\case_ArrayLiteralExpression;
$b = 4;
$a = array(1, 2, 3, $b);
8 changes: 4 additions & 4 deletions test/features/Class.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php
namespace test\Class;
namespace test\case_Class;
require_once("../some-utils");
use \Base;
class Article extends Base {
public $title;
$id;
public $id;
private $_x;
static $published = array();
__construct($options) {
function __construct($options) {
parent::__construct($options);
$this->title = $options["title"];
$this->publish(1);
}
private publish($id) {
private function publish($id) {
array_push(Article::$published, $id);
parent::dispose();
}
Expand Down
2 changes: 1 addition & 1 deletion test/features/ComputedPropertyName.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\ComputedPropertyName;
namespace test\case_ComputedPropertyName;
$a = "aaa";
$b = "bbb";
$c = array(
Expand Down
2 changes: 1 addition & 1 deletion test/features/ConditionalExpression.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace test\ConditionalExpression;
namespace test\case_ConditionalExpression;
$b = false;
$a = $b ? 1 : 2;
2 changes: 1 addition & 1 deletion test/features/Destructuring.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\Destructuring;
namespace test\case_Destructuring;
$tplData = array( "a" => 1 );
$difftime = isset($tplData["difftime"]) ? $tplData["difftime"] : 8;
$a = $tplData["a"];
Expand Down
2 changes: 1 addition & 1 deletion test/features/EnumDeclaration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\EnumDeclaration;
namespace test\case_EnumDeclaration;
$aaa = array( "a" => 1, "b" => 2, "c" => 3 );
$bbb = array( "a" => 0, "b" => 1, "c" => 2 );
$ccc = array( "a" => "a", "b" => "b", "c" => "c" );
Expand Down
2 changes: 1 addition & 1 deletion test/features/ForStatement.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\ForStatement;
namespace test\case_ForStatement;
$b = 1;
for ($i = 0; $i < 10; $i++) {
$b += 10;
Expand Down
2 changes: 1 addition & 1 deletion test/features/FunctionDeclaration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\FunctionDeclaration;
namespace test\case_FunctionDeclaration;
function aaa($a, $b, $c) {
$d = 123;
$e = $b . 123;
Expand Down
2 changes: 1 addition & 1 deletion test/features/GlobalApi.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\GlobalApi;
namespace test\case_GlobalApi;
$a = intval("1.2");
$b = floatval("1.2");
$c = \Ts2Php_Helper::typeof($a);
Expand Down
2 changes: 1 addition & 1 deletion test/features/IfStatement.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\IfStatement;
namespace test\case_IfStatement;
$a = true;
if (!$a) {
$b = 456;
Expand Down
2 changes: 1 addition & 1 deletion test/features/Math.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
namespace test\Math;
namespace test\case_Math;
$a = max(1, 2);
2 changes: 1 addition & 1 deletion test/features/NumberApi.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\NumberApi;
namespace test\case_NumberApi;
$a = 12.44;
$b = round($a, 1);
$c = round(($a + 1), 1);
Expand Down
2 changes: 1 addition & 1 deletion test/features/ObjectJSON.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\ObjectJSON;
namespace test\case_ObjectJSON;
$a = array_merge(array(), array( "a" => 1 ));
$b = array_keys($a);
$c = $a;
Expand Down
2 changes: 1 addition & 1 deletion test/features/ObjectLiteralExpression.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\ObjectLiteralExpression;
namespace test\case_ObjectLiteralExpression;
$b = array(
"a" => 123,
"b" => "456"
Expand Down
2 changes: 1 addition & 1 deletion test/features/ShorthandPropertyAssignment.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\ShorthandPropertyAssignment;
namespace test\case_ShorthandPropertyAssignment;
$b = 2;
$c = 1;
$a = array(
Expand Down
2 changes: 1 addition & 1 deletion test/features/SwitchStatement.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\SwitchStatement;
namespace test\case_SwitchStatement;
$b = 2;
$c = 1;
switch ($b) {
Expand Down
2 changes: 1 addition & 1 deletion test/features/WhileStatement.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\WhileStatement;
namespace test\case_WhileStatement;
$a = true;
$b;
while (!$a) {
Expand Down
2 changes: 1 addition & 1 deletion test/features/addDollar.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\addDollar;
namespace test\case_addDollar;
$a = "123";
$b = array();
$b[$a] = 123;
2 changes: 1 addition & 1 deletion test/features/elementAccessExpression.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\elementAccessExpression;
namespace test\case_elementAccessExpression;
$tplData = array();
$a = "aaa";
$tplData[$a] = 123;
Expand Down
2 changes: 1 addition & 1 deletion test/features/export.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\export;
namespace test\case_export;
function run($a) {
$b = "111";
var_dump($a, $b);
Expand Down
2 changes: 1 addition & 1 deletion test/features/import.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\import;
namespace test\case_import;
require_once("../some-utils");
use \Other_Utils as Util;
use \Some_Utils;
Expand Down
2 changes: 1 addition & 1 deletion test/features/stringProto.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\stringProto;
namespace test\case_stringProto;
$a = "wwa";
$b = \Ts2Php_Helper::str_replace_once("a", "b", $a);
$c = array( "s" => "aaa" );
Expand Down
2 changes: 1 addition & 1 deletion test/features/template.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\template;
namespace test\case_template;
$b = "123";
$c = "0" . $b . "45'6'\"789\"";
$s = "1" . $b . "2" . $b . "3" . $b . "4";
2 changes: 1 addition & 1 deletion test/features/vue.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\vue;
namespace test\case_vue;
$Vue["extend"](array(
"props" => array(
"b" => $String,
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {compile} = require('../src/index.ts');

const files = fs.readdirSync(path.resolve(__dirname, './features'));
const featureNames = files.reduce((res, file) => {
const m = file.match(/(.+)\.ts/);
const m = file.match(/(.+)\.ts$/);
if (m) {
res.push(m[1]);
}
Expand All @@ -39,7 +39,7 @@ describe('features', () => {
const phpContent = await readFile(path.resolve(__dirname, `./features/${featureName}.php`));
const tsPath = path.resolve(__dirname, `./features/${featureName}.ts`);
const res = compile(tsPath, {
namespace: `test\\${featureName}`,
namespace: `test\\case_${featureName}`,
modules: {
'vue': {
required: true
Expand Down

0 comments on commit 21db4e8

Please sign in to comment.