Merge pull request 'fix(compiler): 修复命名空间尾部有注释导致编译不通过' (#29) from compiler-fix-namespace-trailing-comment into master

Reviewed-on: #29
pull/40/head
韩天峰 1 month ago
commit e3f55fe69c
  1. 14
      phpunit/code/preprocessor/namespace_ending_comment_unbracketed.php
  2. 19
      phpunit/src/PreprocessorTest.php
  3. 2
      src/Preprocessor.php
  4. 2
      src/Translator.php
  5. 22
      tests/compiler/namespace/namespace-ending-comment.phpt

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace NamespaceEndingComment;
const VALUE = 42;
function value(): int
{
return VALUE;
}
// unbracketed namespace trailing comment

@ -321,6 +321,25 @@ class PreprocessorTest extends TestCase
$this->assertSame('App\\VERSION', $constants['_const_var_App__VERSION']->name);
}
public function testNamespaceEndingCommentWithUnbracketedSyntaxIsIgnored(): void
{
global $translator;
$file = __DIR__ . '/../code/preprocessor/namespace_ending_comment_unbracketed.php';
$previousTranslator = $translator ?? null;
$translator = $this->compiler;
try {
$this->compiler->addFiles([$file]);
$this->compiler->prepareFile($file);
$this->compiler->convertFile($file);
} finally {
$translator = $previousTranslator;
}
$constants = $this->getProperty('constants');
$this->assertArrayHasKey('_const_var_NamespaceEndingComment__VALUE', $constants);
}
public function testSortFilesUsesImplementsAndTraitDependencies(): void
{
$classFile = realpath(__DIR__ . '/../code/preprocessor/deps_class_implements.php');

@ -321,6 +321,8 @@ class Preprocessor extends CompilerBase
case 'Stmt_Interface':
$this->parseInterface($v2);
break;
case 'Stmt_Nop':
break;
default:
$this->foundStrayCode($v2);
break;

@ -2514,6 +2514,8 @@ CODE;
case 'Stmt_Interface':
$this->validateInterfaceOverrideAttributes($v2);
break;
case 'Stmt_Nop':
break;
default:
abort($v2);
break;

@ -0,0 +1,22 @@
--TEST--
A namespace block ending with a comment must not be treated as stray code
--FILE--
<?php
declare(strict_types=1);
namespace Test {
/* named namespace trailing block comment */
}
namespace {
function main()
{
var_dump('done');
}
// global namespace trailing line comment
}
?>
--EXPECT--
string(4) "done"
Loading…
Cancel
Save