Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Minus operator confused with variable after new constructor #192

Closed
felixhao28 opened this issue Jan 18, 2019 · 2 comments · Fixed by #201 or #214
Closed

Minus operator confused with variable after new constructor #192

felixhao28 opened this issue Jan 18, 2019 · 2 comments · Fixed by #201 or #214

Comments

@felixhao28
Copy link

Current behavior

image

Expected:

image

Code:

class A {
    void f() {
        int a = new Date().getTime() - start.getTime();
    }
}

Fix:

Change this line from

'end': '(?=;|\\)|,|:|}|\\+)'

to

'end': '(?=;|\\)|,|:|}|\\+|\\-)'
                           ^^^
@felixhao28
Copy link
Author

felixhao28 commented Jan 18, 2019

Equal sign is also impacted for the same reason:

class A {
    void f() {
        new Integer(65535).floatValue() == 65535.0f
    }
}

The full expansion of this regex might be '(?=;|\\)|,|:|}|\\-|\\+|\\*|\\/|%|!|&|\\||=)';

@sadikovi
Copy link
Contributor

I will check, thanks.

sadikovi added a commit that referenced this issue Jun 24, 2019
### Requirements

* Filling out the template is required. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.
* All new code requires tests to ensure against regressions

### Description of the Change

This PR updates end pattern in `anonymous-classes-and-new` and patches variable scope after constructor, see example below:
```java
class A {
  void func() {
    long a = new Date().getTime() + start.getTime();
    long b = new Date().getTime() - start.getTime();
    long c = new Date().getTime() * start.getTime();
    long d = new Date().getTime() / start.getTime();
    long e = new Date().getTime() & start.getTime();
    long f = new Date().getTime() | start.getTime();
    boolean g = new Date().getTime() == start.getTime();
    boolean h = new Date().getTime() != start.getTime();
  }
}
```

Note that this is not exhaustive pattern, but should fix most of the cases.

### Alternate Designs

None were considered. Note that this issue will not exist once we switch to tree-sitter.

### Benefits

Enables correct scope in the case mentioned above.

### Possible Drawbacks

Could fix some other highlighting case that is not captured by our tests.

### Applicable Issues

Closes #192
sadikovi pushed a commit that referenced this issue Nov 28, 2019
…ttern

### Description of the Change

* Dot should also end  'anonymous-classes-and-new', in order to propery highlight method call of temporary variable created by 'new'.

* Question mark should also end 'anonymous-classes-and-new', in order to proper break out circumstances like `new Random() ? true : false`;

* Right square brace should also end 'anonymous-classes-and-new', for circumstances like `[new Random()]`.

* Xor should also end `anonymous-classes-and-new`, for circumstances like `new Integer(1) ^ new Integer(2)`.

### Alternate Designs

~~Non was considered.~~

Having to enumerate these much punctuations made me think of proposing a general and simple regex to end new expression. But soon I found out that I couldn't find a way to tell when `)` or `]` is met, whether there will be a `{}` block next to it.

### Benefits

* The meaning of dot method call by variable and new-variable is consistent.
* Every punctuation that could end a new expression is covered.

### Possible Drawbacks

No obvious drawback.

### Applicable Issues

Fix #213 -> dot
Fix #179 -> question mark
Fix #180 -> right square
Fix #192 -> xor
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants