Skip to content

Commit

Permalink
Fixed bug where a semicolon was incorrectly inserted after a class de…
Browse files Browse the repository at this point in the history
…claration if the brackets started on a new line.

Added tests.
  • Loading branch information
hexydec committed Jan 9, 2024
1 parent b265b2b commit 6550e27
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/tokens/expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ protected function isEol(tokenise $tokens, object $prev, ?object $beforeprev = n
$key = __NAMESPACE__.'\\keyword';
$bra = __NAMESPACE__.'\\brackets';
$op = __NAMESPACE__.'\\operator';
$var = __NAMESPACE__.'\\variable';

// check for kewords
$keywords = ['debugger', 'continue', 'break', 'throw', 'return'];
Expand All @@ -238,6 +239,9 @@ protected function isEol(tokenise $tokens, object $prev, ?object $beforeprev = n
} elseif ($prevtype === $bra && $prev->bracket === 'curly' && $beforeprevtype !== $op) {
return $assignment;

// if class declaration
} elseif ($beforeprevtype === $key && $prevtype === $var && $beforeprev->content === 'class') {

// get next token
} elseif (($next = $this->getNextSignificantToken($tokens)) === null) {

Expand Down
22 changes: 22 additions & 0 deletions tests/jsliteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,21 @@ public function testHandleDifficultJavascript() {
}',
'output' => 'class ClassWithGetSet{#msg="hello world";get msg(){return this.#msg}set msg(x){this.#msg=`hello ${x}`}}'
],
[
'input' => 'class TypicalClass
{
constructor() {
this.msg = "Typical";
}
get msg() {
return this.msg;
}
update(newmsg) {
this.msg = newmsg;
}
}',
'output' => 'class TypicalClass{constructor(){this.msg="Typical"}get msg(){return this.msg}update(newmsg){this.msg=newmsg}}'
],
[
'input' => 'var get = "foo";
function get(val) {
Expand Down Expand Up @@ -790,6 +805,13 @@ function () {
+
6;',
'output' => 'var val=5+6;'
],
[
'input' => 'class MyClass
{
}',
'output' => 'class MyClass{}'
]
];
$this->compareMinify($tests, ['semicolons' => false]);
Expand Down

0 comments on commit 6550e27

Please sign in to comment.