Repository: glayzzle/php-parser
Pull request #82 (2017-07-15 18:35:58)

2.1.0


Patch:
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
deleted file mode 100644
index 8d4ec213..00000000
--- a/CODE_OF_CONDUCT.md
+++ /dev/null
@@ -1,46 +0,0 @@
-# Contributor Covenant Code of Conduct
-
-## Our Pledge
-
-In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
-
-## Our Standards
-
-Examples of behavior that contributes to creating a positive environment include:
-
-* Using welcoming and inclusive language
-* Being respectful of differing viewpoints and experiences
-* Gracefully accepting constructive criticism
-* Focusing on what is best for the community
-* Showing empathy towards other community members
-
-Examples of unacceptable behavior by participants include:
-
-* The use of sexualized language or imagery and unwelcome sexual attention or advances
-* Trolling, insulting/derogatory comments, and personal or political attacks
-* Public or private harassment
-* Publishing others' private information, such as a physical or electronic address, without explicit permission
-* Other conduct which could reasonably be considered inappropriate in a professional setting
-
-## Our Responsibilities
-
-Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
-
-Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
-
-## Scope
-
-This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
-
-## Enforcement
-
-Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at i.chiriac@yahoo.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
-
-Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
-
-## Attribution
-
-This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
-
-[homepage]: http://contributor-covenant.org
-[version]: http://contributor-covenant.org/version/1/4/
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
deleted file mode 100644
index c24acde5..00000000
--- a/CONTRIBUTING.md
+++ /dev/null
@@ -1,3 +0,0 @@
-Everyone is welcome to contribute, just use the same coding style, and cover with tests your code. 
-
-Any PR is welcomed :)
diff --git a/README.md b/README.md
index 8d2af883..a839f727 100644
--- a/README.md
+++ b/README.md
@@ -90,6 +90,7 @@ Documentation
 Related projects
 ----------------
 
+- [babel-preset-php](https://gitlab.com/kornelski/babel-preset-php) : Babel preset for converting PHP syntax to JavaScript. It can run subset of PHP in the browser or in Node.js. .
 - [php-unparser](https://github.com/chris-l/php-unparser) : Produce code that uses the style format recommended by PSR-1 and PSR-2.
 - [php-writer](https://github.com/glayzzle/php-writer) : Update PHP scripts from their AST
 - [ts-php-inspections](https://github.com/DaGhostman/ts-php-inspections) : Provide PHP code inspections written in typescript
diff --git a/package.json b/package.json
index 05cf04d4..837d1efa 100644
--- a/package.json
+++ b/package.json
@@ -12,7 +12,7 @@
     "url": "https://github.com/glayzzle/php-parser"
   },
   "bugs": {
-    "url": "https://github.com/glayzzle/php-parser/issues"
+    "url": "https://github.com/glayzze/php-parser/issues"
   },
   "homepage": "http://glayzzle.com/php-parser/",
   "keywords": [
diff --git a/src/parser/expr.js b/src/parser/expr.js
index 44505fd8..79873a13 100644
--- a/src/parser/expr.js
+++ b/src/parser/expr.js
@@ -353,9 +353,20 @@ module.exports = {
     if (this.is('VARIABLE')) {
       var result = this.node();
       expr = this.read_variable(false, false, false);
+
+      // https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L877
+      // should accept only a variable
+      var isConst = (
+        expr.kind === 'constref' || (
+          expr.kind === 'staticlookup' &&
+          expr.offset.kind === 'constref'
+        )
+      );
+
       // VARIABLES SPECIFIC OPERATIONS
       switch(this.token) {
         case '=':
+          if (isConst) this.error('VARIABLE');
           var right;
           if (this.next().token == '&') {
             if (this.next().token === this.tok.T_NEW) {
@@ -370,45 +381,59 @@ module.exports = {
 
         // operations :
         case this.tok.T_PLUS_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '+=');
 
         case this.tok.T_MINUS_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '-=');
 
         case this.tok.T_MUL_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '*=');
 
         case this.tok.T_POW_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '**=');
 
         case this.tok.T_DIV_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '/=');
 
         case this.tok.T_CONCAT_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '.=');
 
         case this.tok.T_MOD_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '%=');
 
         case this.tok.T_AND_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '&=');
 
         case this.tok.T_OR_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '|=');
 
         case this.tok.T_XOR_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '^=');
 
         case this.tok.T_SL_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign', expr, this.next().read_expr(), '<<=');
 
         case this.tok.T_SR_EQUAL:
+          if (isConst) this.error('VARIABLE');
           return result('assign',expr, this.next().read_expr(), '>>=');
 
         case this.tok.T_INC:
+          if (isConst) this.error('VARIABLE');
           this.next();
           return result('post', '+', expr);
         case this.tok.T_DEC:
+          if (isConst) this.error('VARIABLE');
           this.next();
           return result('post', '-', expr);
       }
diff --git a/src/parser/statement.js b/src/parser/statement.js
index 482f9f81..a9ceaa8f 100644
--- a/src/parser/statement.js
+++ b/src/parser/statement.js
@@ -322,18 +322,19 @@ module.exports = {
       case this.tok.T_STRING:
         var current = [this.token, this.lexer.getState()];
         var label = this.text();
+        // AST : https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L457
         if (this.next().token === ':') {
           var result = this.node('label');
           this.next();
           return result(label);
-        } else {
-          // default fallback expr
-          this.lexer.tokens.push(current);
-          var expr = this.next().read_expr();
-          this.expect([';', this.tok.T_CLOSE_TAG]) && this.nextWithComments();
-          return expr;
         }
 
+        // default fallback expr / T_STRING '::' (etc...)
+        this.lexer.tokens.push(current);
+        var expr = this.next().read_expr();
+        this.expectEndOfStatement();
+        return expr;
+
       case this.tok.T_GOTO:
         var result = this.node('goto'), label = null;
         if (this.next().expect(this.tok.T_STRING)) {


Test patch:
diff --git a/test/exprTests.js b/test/exprTests.js
index c9239f36..82c045e1 100644
--- a/test/exprTests.js
+++ b/test/exprTests.js
@@ -87,7 +87,7 @@ describe('Test expressions', function() {
       '$a > 5 ? true : false;',
       '$a ?: false;'
     ].join('\n'));
-    console.log(ast.children[1]);
+    //console.log(ast.children[1]);
     ast.children[1].kind.should.be.exactly('retif');
 
   });
@@ -209,6 +209,32 @@ describe('Test expressions', function() {
     ast.children[3].type.should.be.exactly('-');
   });
 
+  it('should fail to assign constants', function() {
+    var ast = parser.parseEval('a = 1;', {
+      parser: { debug: false, suppressErrors: true }
+    });
+    var msg = 'Parse Error : syntax error, unexpected \'=\' on line 1';
+    ast.errors.length.should.be.exactly(1);
+    ast.errors[0].message.should.be.exactly(msg);
+  });
+
+  it('should fail to assign class constants', function() {
+    var ast = parser.parseEval('foo::b = 1;', {
+      parser: { debug: false, suppressErrors: true }
+    });
+    var msg = 'Parse Error : syntax error, unexpected \'=\' on line 1';
+    ast.errors.length.should.be.exactly(1);
+    ast.errors[0].message.should.be.exactly(msg);
+  });
+
+  it('should assign class static', function() {
+    var ast = parser.parseEval('a::$b = 1;', {
+      parser: { debug: false, suppressErrors: true }
+    });
+    ast.errors.length.should.be.exactly(0);
+    ast.children[0].kind.should.be.exactly('assign');
+  });
+
   it('test new', function() {
     var ast = parser.parseEval([
       '$a = new \\foo();',