Skip to content
Draft
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 src/__tests__/runBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const replacedImports: CliOptions['replaceImports'] = {
};
const compilerOptions = {
baseUrl: baseDir,
strictNullChecks: true,
paths: {
'#specimens/*': ['specimens/'],
},
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/specimens/byType/CallExpressionModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function getInstance(): CallExpressionModule {
}

/**
* @var mixed[] $c
* @var mixed|mixed[] $c
*/
public $c;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {useState} = React;

interface Props {
count: number;
actionText?: string;
handleScroll: () => void;
}

Expand All @@ -12,14 +13,14 @@ interface Props {
* @param props
* @constructor
*/
export function BasicComponentWithProps({count: cnt, handleScroll}: Props) {
export function BasicComponentWithProps({count: cnt, actionText, handleScroll}: Props) {
const [count, setCount] = useState(cnt);

return (
<div onScroll={handleScroll}>
<p>You clicked {count} times starting at {cnt}.</p>
<button onClick={() => setCount(count + 1)}>
Click me
{actionText || 'Click me'}
</button>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private function __construct() {
*/
public function render(array $anon_deref_1, array $children) {
$cnt = (float) $anon_deref_1["count"];
$action_text = $anon_deref_1["actionText"];
$anon_2c03773 = [$cnt];
$count = (float) $anon_2c03773[0];
return \VK\Elephize\Builtins\IntrinsicElement::get("div")->render(
Expand All @@ -35,7 +36,10 @@ public function render(array $anon_deref_1, array $children) {
[],
["You clicked ", $count, " times starting at ", $cnt, "."]
),
\VK\Elephize\Builtins\IntrinsicElement::get("button")->render([], [" Click me "]),
\VK\Elephize\Builtins\IntrinsicElement::get("button")->render(
[],
[\VK\Elephize\Builtins\IntrinsicElement::escape($action_text ?: "Click me")]
),
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public function render(array $props, array $children) {
["You clicked ", $props["count"], " times"]
),
\VK\Elephize\Builtins\IntrinsicElement::get("button")->render([], [" Click me "]),
$input_value
? \VK\Elephize\Builtins\IntrinsicElement::get("span")->render(
["className" => "someSpan"],
[\VK\Elephize\Builtins\IntrinsicElement::escape($input_value)]
)
: $input_value,
\VK\Elephize\Builtins\IntrinsicElement::escape(
$input_value
? \VK\Elephize\Builtins\IntrinsicElement::get("span")->render(
["className" => "someSpan"],
[\VK\Elephize\Builtins\IntrinsicElement::escape($input_value)]
)
: $input_value
),
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ public function render(array $input_props, array $children) {
[]
),
\VK\Elephize\Builtins\IntrinsicElement::get("span")->render(["className" => "Radio__control"], []),
$children
? \VK\Elephize\Builtins\IntrinsicElement::get("span")->render(
["className" => "Radio__text"],
[\VK\Elephize\Builtins\IntrinsicElement::escape($children)]
)
: $children,
\VK\Elephize\Builtins\IntrinsicElement::escape(
$children
? \VK\Elephize\Builtins\IntrinsicElement::get("span")->render(
["className" => "Radio__text"],
[\VK\Elephize\Builtins\IntrinsicElement::escape($children)]
)
: $children
),
]
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/specimens/fixes/CheckBox/CheckBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private function __construct() {
*/
public function render(array $props, array $children) {
$checked = $props["checked"];
$disabled = (bool) $props["disabled"];
$disabled = $props["disabled"];
$name = $props["name"];
$id = $props["id"];
$native_props = Stdlib::objectOmit($props, ["children", "checked", "disabled", "indeterminate", "name", "id"]);
Expand All @@ -44,7 +44,7 @@ public function render(array $props, array $children) {
"type" => "checkbox",
"checked" => !!$checked,
"name" => \VK\Elephize\Builtins\IntrinsicElement::escape((string) $name),
"disabled" => $disabled,
"disabled" => \VK\Elephize\Builtins\IntrinsicElement::escape($disabled),
]),
[]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private function __construct() {
* @return ?string
*/
public function render(array $anon_deref_1, array $children) {
$align = (string) $anon_deref_1["align"];
$align = $anon_deref_1["align"];
$class_names = ["content-text--container"];
if ($align) {
array_push($class_names, "content-text--container__align-" . $align);
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/watcherTestEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const ignoreImportRules: Set<string> = new Set();
const replaceImportRules = { };
const compilerOptions = {
baseUrl: baseDir,
strictNullChecks: true,
paths: {
'#specimens/*': ['specimens'],
},
Expand Down
1 change: 1 addition & 0 deletions src/ts2php/components/cli/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function transpile(options: CliOptions, baseDir: string, outDir: string,
const compilerOptions = {
baseUrl: baseDir,
paths: options.tsPaths || {},
strictNullChecks: true,
};

(options.watch ? translateCodeAndWatch : translateCode)(
Expand Down
1 change: 1 addition & 0 deletions src/ts2php/components/codegen/defaultCompilerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export const defaultCompilerOptions: ts.CompilerOptions = {
allowUnreachableCode: true,
allowJs: true,
resolveJsonModule: true,
strictNullChecks: true,
};
38 changes: 27 additions & 11 deletions src/ts2php/components/typeInference/basicTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ const transformTypeName = (type: ts.Type, node: ts.Node, checker: ts.TypeChecker

function describeNodeType(node: ts.Node | undefined, type: ts.Type, checker: ts.TypeChecker, log: LogObj) {
const nodeIdentForLog = node?.getText();
const optionalMark = (node?.parent.kind === ts.SyntaxKind.Parameter && (
let optionalMark = (node?.parent.kind === ts.SyntaxKind.Parameter && (
(node.parent as ts.ParameterDeclaration).initializer ||
(node.parent as ts.ParameterDeclaration).questionToken
)) ? '?' : '';
Expand All @@ -274,16 +274,32 @@ function describeNodeType(node: ts.Node | undefined, type: ts.Type, checker: ts.

const customTypehints = checkCustomTypehints(type, checker);
if (customTypehints) {
const types = customTypehints.foundTypes.map((t) => {
if (typeof t === 'string') {
return optionalMark + t;
}
// Some of union members may be literal types
return optionalMark + describeAsApparentType(t, node!, checker, log, nodeIdentForLog);
}).filter((t) => !customTypehints.typesToDrop.includes(t));
const typehint = Array.from(new Set((<string[]>[])
.concat(types)))
.join('|');
if (!optionalMark) {
optionalMark = customTypehints.foundTypes.find(
(t) => typeof t !== 'string' && t.getFlags() & ts.TypeFlags.Undefined
)
? '?'
: '';
}

const types = customTypehints.foundTypes
.filter(
(t) => typeof t === 'string' || !(t.getFlags() & ts.TypeFlags.Undefined)
)
.map((t) => {
if (typeof t === 'string') {
return optionalMark + t;
}
// Some of union members may be literal types
return (
optionalMark +
describeAsApparentType(t, node!, checker, log, nodeIdentForLog)
);
})
.filter((t) => !customTypehints.typesToDrop.includes(t));
const typehint = Array.from(new Set((<string[]>[]).concat(types))).join(
'|'
);
log.typehint('Inferred type of node: %s -> %s [1]', [nodeIdentForLog || '', typehint]);
return typehint;
}
Expand Down