AustP / Stylus.php

A Stylus parser for PHP
49 stars 17 forks source link

isVariableDeclaration-method is trying to use non-existent variable $intent #16

Open henry-kiho opened 7 years ago

henry-kiho commented 7 years ago
PHP Notice:  Undefined offset: 1 in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 293
PHP Notice:  Undefined variable: indent in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 115
PHP Notice:  Undefined variable: indent in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 116
PHP Notice:  Undefined variable: indent in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 117
PHP Notice:  Undefined variable: indent in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 115
PHP Notice:  Undefined variable: indent in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 116
PHP Notice:  Undefined variable: indent in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 117
PHP Notice:  Undefined offset: 1 in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 293
PHP Notice:  Undefined variable: indent in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 115
PHP Notice:  Undefined variable: indent in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 116
PHP Notice:  Undefined variable: indent in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 117
PHP Warning:  Invalid argument supplied for foreach() in .../vendor/neemzy/stylus/src/Stylus/Stylus.php on line 350
kylekatarnls commented 7 years ago

Hi, could you give me the Stylus code with which it happens?

The probable cause would be a semi-colon in a variable declaration:

@foo: ";"

I know this should be valid Stylus code but the pattern in the RegExp : [^;]+ does not allow it.

If you can confirm the problem, I can fix it in my fork: https://github.com/kylekatarnls/Stylus.php (composer require kylekatarnls/stylus) and propose here a pull-request.

LinusU commented 7 years ago

Seems to be three different issues, all of which should be addressed.

1) Undefined offset 1 in the function addVariable, most likely because the regex failed to match. This seems to be what @kylekatarnls describes and is a bit tricky. It should probably check if the string is enclosed in quotes and parse that differently...

2) The missing variable $indent in the function isVariableDeclaration, this should probably be set to an empty string, or maybe just removed...

3) The invalid value of $this->blocks inside convertBlocksToCSS, I think that this should be initialized into an empty array in the constructor...

PRs welcome for any or all of the problems 💌

kylekatarnls commented 7 years ago

All this is fixed here: https://github.com/AustP/Stylus.php/compare/master...kylekatarnls:master (+ the @ rules implemented) so I can cherry-pick on a branch and propose the pull request. @LinusU do you have write access on https://github.com/AustP/Stylus.php?

LinusU commented 7 years ago

Hmm, I believe that preg_match('~^([\$a-zA-Z0-9_-]+)\s*=\s*(.*[^;]);*$~', $line, $matches); wouldn't quite cut it. I think that all of the following examples needs to pass:

kylekatarnls commented 7 years ago

And they all pass.

LinusU commented 7 years ago

Maybe I'm missing something here, but won't e.g. @foo: 1;@bar: 2; get added as { "foo": "1;@bar: 2" } whereas before it wouldn't? The correct behaviour would be { "foo": "1", "bar": "2" }?

kylekatarnls commented 7 years ago

Note: @ is not valid in Stylus syntax.

The current case : [^;]+ so ; is completly forbidden, you can have only one variable declaration per line.

My RegExp, just allow anything and trim semi-colon(s) at the end. But it still can handle only one declaration per line.

Multiple declaration are another thing that never worked. For that, we need a stronger string parsing like this for example: ~^(([\$a-zA-Z0-9_-]+)\s*=\s*([^;]*|"(\\\\.|[^\\\\"])*"|\'(\\\\.|[^\\\\\'])*\');*)*~

But you get in another problem: multiple lines for one value:

var = "
my text
"

body
  content var

The code above is valid Stylus and here, it's no longer about the good RegExp to parse a line, but replace the line-by-line parser with a real lexer/parser/compiler system.