Skip to content
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

Javascript regular expression literal not recognized after comparison operator #1170

Closed
cheuer opened this issue Sep 22, 2016 · 4 comments
Closed

Comments

@cheuer
Copy link

cheuer commented Sep 22, 2016

Our coding style enforces strict comparisons with the literal preceding the variable/function to check. Here is part of a function that checks for numeric input:
if( true === /^\d*\.?\d*$/.test( input ) ) return true;

PHPCS does not recognize the regular expression literal when tokenizing the above code.

@aik099
Copy link
Contributor

aik099 commented Sep 22, 2016

Which error code you'll get for that error (sniff name + error code)?

@cheuer
Copy link
Author

cheuer commented Sep 22, 2016

I get two errors:

----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------
 1 | ERROR | [x] Expected 1 space before logical operator; 0 found
   |       |     (Squiz.WhiteSpace.LogicalOperatorSpacing.NoSpaceBefore)
 1 | ERROR | [x] Expected 1 space after logical operator; 0 found
   |       |     (Squiz.WhiteSpace.LogicalOperatorSpacing.NoSpaceAfter)
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

But the issue is not in the LogicalOperatorSpacing sniff, it's in the token processing. The above code generates these tokens:

Process token 0: T_OPEN_TAG =>
Process token 1: T_IF => if
Process token 2: T_OPEN_PARENTHESIS => (
Process token 3: T_WHITESPACE =>
Process token 4: T_TRUE => true
Process token 5: T_WHITESPACE =>
Process token 6: T_IS_IDENTICAL => ===
Process token 7: T_WHITESPACE =>
Process token 8: T_DIVIDE => /
Process token 9: T_LOGICAL_XOR => ^
Process token 10: T_STRING => \d
Process token 11: T_MULTIPLY => *
Process token 12: T_STRING => \
Process token 13: T_OBJECT_OPERATOR => .
Process token 14: T_STRING => ?
Process token 15: T_STRING => \d
Process token 16: T_MULTIPLY => *
Process token 17: T_STRING => $
Process token 18: T_DIVIDE => /
Process token 19: T_OBJECT_OPERATOR => .
Process token 20: T_STRING => test
Process token 21: T_OPEN_PARENTHESIS => (
Process token 22: T_WHITESPACE =>
Process token 23: T_STRING => input
Process token 24: T_WHITESPACE =>
Process token 25: T_CLOSE_PARENTHESIS => )
Process token 26: T_WHITESPACE =>
Process token 27: T_CLOSE_PARENTHESIS => )
Process token 28: T_WHITESPACE =>
Process token 29: T_RETURN => return
Process token 30: T_WHITESPACE =>
Process token 31: T_TRUE => true
Process token 32: T_SEMICOLON => ;

If I remove the "true ===" part, I get this:

Process token 0: T_OPEN_TAG =>
Process token 1: T_IF => if
Process token 2: T_OPEN_PARENTHESIS => (
Process token 3: T_WHITESPACE =>
Process token 4: T_REGULAR_EXPRESSION => /^\d*\.?\d*$/
Process token 5: T_OBJECT_OPERATOR => .
Process token 6: T_STRING => test
Process token 7: T_OPEN_PARENTHESIS => (
Process token 8: T_WHITESPACE =>
Process token 9: T_STRING => input
Process token 10: T_WHITESPACE =>
Process token 11: T_CLOSE_PARENTHESIS => )
Process token 12: T_WHITESPACE =>
Process token 13: T_CLOSE_PARENTHESIS => )
Process token 14: T_WHITESPACE =>
Process token 15: T_RETURN => return
Process token 16: T_WHITESPACE =>
Process token 17: T_TRUE => true
Process token 18: T_SEMICOLON => ;

I actually ran into this bug because I ran PHPCBF on the file containing this code and it changed the regex to / ^ \d*\.?\d*$/.

@aik099
Copy link
Contributor

aik099 commented Sep 22, 2016

Wow, that's interesting.

gsherwood added a commit that referenced this issue Sep 23, 2016
@gsherwood
Copy link
Member

Thanks for the detailed report. I've fixed this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants