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

Refactor/attach comments on node #278

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
db932f9
WIP: parser
clementdessoude Oct 3, 2019
2406982
edge case: comments only
clementdessoude Oct 4, 2019
03be6f9
attach non attached comments to tokens
clementdessoude Oct 4, 2019
3ce39a1
fix
clementdessoude Oct 4, 2019
298244a
WIP printer
clementdessoude Oct 3, 2019
57404b5
WIP printer
clementdessoude Oct 4, 2019
216c94a
WIP printer
clementdessoude Oct 4, 2019
d30c60a
fix parser
clementdessoude Oct 4, 2019
3fcb893
clean comment
clementdessoude Oct 4, 2019
6b4a450
fix prettier ignore print
clementdessoude Oct 4, 2019
464cd25
WIP printer
clementdessoude Oct 4, 2019
2c02d55
add unit tests
clementdessoude Oct 4, 2019
bb0db1d
fix rebase
clementdessoude Oct 23, 2019
8fdcc74
update after rebase
clementdessoude Oct 23, 2019
108f5b3
clean duplicated code
clementdessoude Oct 23, 2019
6b0f03b
attach comments to the correct node/token
clementdessoude Oct 23, 2019
7832163
wip reafficahge
clementdessoude Oct 23, 2019
8eff194
fix comments in else statements
clementdessoude Nov 16, 2019
9a623e6
improve tests for comments in if statements
clementdessoude Nov 16, 2019
8b78ad3
fix comments in labeled statements
clementdessoude Nov 17, 2019
81753d4
revert changes on single-print-run
clementdessoude Nov 17, 2019
40d7de0
clean
clementdessoude Nov 17, 2019
8959024
refactor: clean comments handling to remove duplicated code
clementdessoude Nov 18, 2019
596f5aa
refactor: clean and document handling comments in parser
clementdessoude Nov 18, 2019
4e8fe2b
docs: add docs on comments handling in the Java parser
clementdessoude Nov 18, 2019
ae624c9
fix typo
clementdessoude Nov 25, 2019
85e62ef
Merge remote-tracking branch 'upstream/master' into refactor/attach-c…
clementdessoude Nov 25, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ root: true
env:
es6: true
node: true
parserOptions:
ecmaVersion: 2018
rules:
no-fallthrough: off
curly: error
Expand Down
58 changes: 58 additions & 0 deletions packages/java-parser/docs/comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Handling comments

## Objective

Attach each comment to a CST node or token as either leading or trailing comment.

### Attach comment to the highest node possible

A comment could often be attached at several nodes or token. For instance, in the following code snippet:

```java
// comment
public void t() {}
```

`// comment` could be attached to:

- the `methodDeclaration` node
- the `methodModifier` node
- the `public` token

We have made the decision to attach the comment to the highest node possible (the most enclosive one). It makes the more sense to us, and it ease handling comments in the prettier-plugin.

### Trailing comments

A comment should be considered as a trailing comment in very specific cases:

- If it is on the same line as the node/token it followed, and not on the same line as the node/token it preceded. For instance, `//comment` is considered as a trailing comment in the following code snippet:

```java
int i = 1; // comment
int j = 2;
```

- If it is at the end of the file

### Consecutives comments

If there are consecutive comments, each comment will be considered separately. For instance:

```java
int i = 1;
// comment1
// comment2
int j = 2;
```

`// comment1` and `// comment2` will be both attached as leading comments to the second `localVariableDeclarationStatement` node.

When in:

```java
int i = 1; // comment1
// comment2
int j = 2;
```

`// comment1` will be attached as a trailing comment to the first `localVariableDeclarationStatement` node and `// comment2` will be attached as a leading comment to the second one.
Loading