-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
Propagate variable types to generated code to allow statical analysis (master) #275
Closed
MartinMystikJonas
wants to merge
21
commits into
nette:master
from
MartinMystikJonas:types-for-static-analysis
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
33bcc0b
opened 3.0-dev
dg 80980c7
anonymous {block} create variable scope (possible BC break)
dg 90950cd
added macro n:nonce
dg b0a436b
added {cycle}
dg 67e124f
SnippetBridge: added typehints
dg 163fa75
Template: removed old accumulators $_l, $_g (deprecated in 2.4, BC br…
dg 0c208d5
removed aliases for old classes ILoader, IMacro, IHtmlString, IISnipp…
dg 028d2b8
Filters::escapeJS: invalid UTF-8 is replaced with Unicode Replacement…
dg 24d33f3
MacroTokens: correctly parses UTF-8 Combining character (TODO: normal…
dg 6a4e283
optional chaining: removed support for deprecated syntax
dg 29bee53
PhpWriter: supports named arguments in modifiers (in PHP 8)
dg 9790492
Colons as argument separator in modifiers are deprecated everywhere (…
dg 1dabdcf
Auto-empty is deprecated
dg 3279000
Empty closing macro {/} is deprecated
dg 814db41
phpdoc wip
dg 6a67fe6
BlockMacros: {include} is not passing all local variables by default …
dg 1031a05
compiler-related classes moved to namespace Latte\Compiler (BC break)
dg 63998bd
RootNode
dg a575495
Filters: compatibility with JS binding II.
dg 74c7abf
Propagate variable types to generated code to allow statical analysis
MartinMystikJonas a680f8a
Merge branch 'master' into types-for-static-analysis
MartinMystikJonas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,15 +325,20 @@ public function macroDefine(MacroNode $node, PhpWriter $writer): string | |
$tokens = $node->tokenizer; | ||
$params = []; | ||
while ($tokens->isNext()) { | ||
if ($tokens->nextToken($tokens::T_SYMBOL, '?', 'null', '\\')) { // type | ||
$tokens->nextAll($tokens::T_SYMBOL, '\\', '|', '[', ']', 'null'); | ||
$type = $tokens->nextValue($tokens::T_SYMBOL, '?', 'null', '\\'); | ||
if ($type) { | ||
$type .= $tokens->joinAll($tokens::T_SYMBOL, '\\', '|', '[', ']', 'null'); | ||
} | ||
$param = $tokens->consumeValue($tokens::T_VARIABLE); | ||
$default = $tokens->nextToken('=') | ||
? $tokens->joinUntilSameDepth(',') | ||
: 'null'; | ||
$mask ='%raw = $ʟ_args[%var] ?? $ʟ_args[%var] ?? %raw;'; | ||
if($type) { | ||
$mask = "/** @var $type $param */\n" . $mask; | ||
} | ||
$params[] = $writer->write( | ||
'%raw = $ʟ_args[%var] ?? $ʟ_args[%var] ?? %raw;', | ||
$mask, | ||
$param, | ||
count($params), | ||
substr($param, 1), | ||
|
@@ -547,7 +552,7 @@ private function addBlock(MacroNode $node, string $layer = null): Block | |
private function extractMethod(MacroNode $node, Block $block, string $params = null): void | ||
{ | ||
if (preg_match('#\$|n:#', $node->content)) { | ||
$node->content = '<?php extract(' . ($node->name === 'block' && $node->closest(['embed']) ? 'end($this->varStack)' : '$this->params') . ');' | ||
$node->content = '<?php ' . ($node->name === 'block' && $node->closest(['embed']) ? 'extract(end($this->varStack));' : $this->getCompiler()->paramsExtraction) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is necessary to keep params extraction inside block same as in top-level. Previous version always extracted all params inside block even when {parameters} is used. |
||
. ($params ?? 'extract($ʟ_args);') | ||
. 'unset($ʟ_args);?>' | ||
. $node->content; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
default values | ||
|
||
|
||
a) Variables 1, 123, 10 | ||
|
||
|
||
b) Variables 1, 123, 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
%A% | ||
final class Template%a% extends Latte\Runtime\Template | ||
{ | ||
protected const BLOCKS = [ | ||
['test' => 'blockTest'], | ||
]; | ||
|
||
|
||
public function main(): array | ||
{ | ||
extract($this->params); | ||
echo 'default values | ||
|
||
'; | ||
if ($this->getParentName()) { | ||
return get_defined_vars(); | ||
} | ||
echo ' | ||
a) '; | ||
$this->renderBlock('test', [1] + [], 'html') /* line %d% */; | ||
echo ' | ||
|
||
b) '; | ||
$this->renderBlock('test', ['var1' => 1] + [], 'html') /* line %d% */; | ||
return get_defined_vars(); | ||
} | ||
|
||
|
||
/** {define test $var1 = 0, array $var2 = [1, 2, 3], int $var3 = 10} on line %d% */ | ||
public function blockTest(array $ʟ_args): void | ||
{ | ||
extract($this->params); | ||
$var1 = $ʟ_args[0] ?? $ʟ_args['var1'] ?? 0; | ||
/** @var array $var2 */ | ||
$var2 = $ʟ_args[1] ?? $ʟ_args['var2'] ?? [1, 2, 3]; | ||
/** @var int $var3 */ | ||
$var3 = $ʟ_args[2] ?? $ʟ_args['var3'] ?? 10; | ||
unset($ʟ_args); | ||
echo ' Variables '; | ||
echo LR\Filters::escapeHtmlText($var1) /* line %d% */; | ||
echo ', '; | ||
echo LR\Filters::escapeHtmlText(($this->filters->implode)($var2)) /* line %d% */; | ||
echo ', '; | ||
echo LR\Filters::escapeHtmlText($var3) /* line %d% */; | ||
echo "\n"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
%A% | ||
public function main(): array | ||
{ | ||
/** @var string $a */ | ||
extract($this->params); | ||
%A% | ||
public function blockTest(array $ʟ_args): void | ||
{ | ||
/** @var string $a */ | ||
extract($this->params); | ||
%A% | ||
/** @var int $b */ | ||
$b = 5%a%; | ||
%A% |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default params extraction needs to be set in the beggining so macros can extend it with type information (prepend
@var
annotations)