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

Java14 Full Records Support Check Validation: WhitespaceAroundCheck #8528

Closed
nrmancuso opened this issue Jul 16, 2020 · 7 comments
Closed

Java14 Full Records Support Check Validation: WhitespaceAroundCheck #8528

nrmancuso opened this issue Jul 16, 2020 · 7 comments

Comments

@nrmancuso
Copy link
Member

Child of #8452
Check documentation: https://checkstyle.sourceforge.io/config_whitespace.html#WhitespaceAround

From check documentation:
Checks that a token is surrounded by whitespace. Empty constructor, method, class, enum, interface, loop bodies (blocks), lambdas of the form...

➜  full-record-grammar /usr/lib/jvm/java-14-openjdk/bin/javac --enable-preview --source 14 TestClass.java
Note: TestClass.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
➜  full-record-grammar cat config.xml    
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
      <module name="WhitespaceAround"/>
  </module>
</module>
➜  full-record-grammar cat TestClass.java
class MyClass {} // violation 2x
interface Foo {} // violation 2x

//simple record def
record MyRecord() {} // violation 2x

//simple record def
record MyRecord1() { } // ok

//nested constructs
record MyRecord2() {
    class MyClass {} // violation 2x
    interface Foo {} // violation 2x
    record MyRecord() {} // violation 2x
}

//method
record MyRecord3() {
    void method(){ // violation
        final int a = 1;
        int b= 1; // violation
        b=1; // violation 2x
    }

}

//ctor
record MyRecord4() {
    public MyRecord4() {
        final int a = 1;
        int b= 1; // violation
        b=1; // violation 2x
    }
}

//compact ctor
record MyRecord5() {
    public MyRecord5 {
        final int a = 1;
        int b= 1; // violation
        b=1; // violation 2x
    }
}

//static fields
record MyRecord6() {
    static final int a = 1;
    static int b= 1; // violation
}
➜  full-record-grammar java $RUN_LOCALE -jar ~/IdeaProjects/checkstyle/target/checkstyle-8.35-SNAPSHOT-all.jar -c config.xml TestClass.java
Starting audit...
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:1:15: '{' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:1:16: '}' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:2:15: '{' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:2:16: '}' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:5:19: '{' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:5:20: '}' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:12:19: '{' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:12:20: '}' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:13:19: '{' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:13:20: '}' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:14:23: '{' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:14:24: '}' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:19:18: '{' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:21:14: '=' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:22:10: '=' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:22:10: '=' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:31:14: '=' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:32:10: '=' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:32:10: '=' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:40:14: '=' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:41:10: '=' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:41:10: '=' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:48:17: '=' is not preceded with whitespace. [WhitespaceAround]
Audit done.
Checkstyle ends with 23 errors.

This check works as intended with the new records syntax, so I think we can close this issue.

@pbludov
Copy link
Member

pbludov commented Jul 18, 2020

This check should handle compact record ctors too:

<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="WhitespaceAround">
            <property name="allowEmptyConstructors" value="true"/>
        </module>
    </module>
</module>
class TestClass {
    public TestClass() {} // ok
}

record TestRecord() {
    public TestRecord {} // unexpected violation x2
}

The method WhitespaceAroundCheck.isEmptyCtorBlock should be ware of TokenTypes.COMPACT_CTOR_DEF.
Sure, this looks ridicules, empty compact ctor makes no sense, but who knows? It may be necessary for an annotation or something like that.

@nrmancuso
Copy link
Member Author

@pbludov This should be a violation too, right?

record TestRecord () {
              // ^
    public TestRecord {} 
}

Also, what about allowing violation of empty component list, allowEmptyComponentList option?

@pbludov
Copy link
Member

pbludov commented Jul 28, 2020

@pbludov This should be a violation too, right?

Yes, it should be more like a ctor declaration.

class TestClass {
  TestClass () { // if there is a violation...
  }
}
class TestRecord () { // ...then there must be the same violation here
}

Also, what about allowing violation of empty component list, allowEmptyComponentList option?

I'm not sure. There is already allowEmptyTypes. Also allowEmptyConstructors for compact ctors.

@nrmancuso
Copy link
Member Author

Actually, there is no violation for this:

    class MyClass2 {
        MyClass2 () {
             // ^ not a violation
        }
    }

@pbludov
Copy link
Member

pbludov commented Jul 28, 2020

Actually, there is no violation for this:

Both MyClass2 () and MyClass2() are valid? If so, there is no need to report a violation for records here.

@nrmancuso
Copy link
Member Author

nrmancuso commented Jul 28, 2020

I'm adding test cases to show this.

@strkkk
Copy link
Member

strkkk commented Jul 29, 2020

Fix is merged

@strkkk strkkk closed this as completed Jul 29, 2020
@strkkk strkkk added this to the 8.36 milestone Jul 29, 2020
tobiasbaum pushed a commit to tobiasbaum/checkstyle that referenced this issue Aug 1, 2020
shiliyu pushed a commit to shiliyu/checkstyle that referenced this issue Sep 1, 2020
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