palantir-java-format icon indicating copy to clipboard operation
palantir-java-format copied to clipboard

`error: expected token: '_'; generated instead`

Open koppor opened this issue 9 months ago • 1 comments

I am trying 2.58.0 on my project.

I get

Step 'palantir-java-format' found problem in 'src\main\java\org\jabref\gui\commonfxcontrols\CitationKeyPatternsPanel.java':
69:39: error: expected token: '_'; generated  instead
com.palantir.javaformat.java.FormatterException: 69:39: error: expected token: '_'; generated  instead
        at com.palantir.javaformat.java.Formatter.getFormatReplacements(Formatter.java:322)
        at com.palantir.javaformat.java.Formatter.formatSource(Formatter.java:297)
        at com.palantir.javaformat.java.Formatter.formatSource(Formatter.java:252)
        at com.palantir.javaformat.java.Formatter.formatSourceAndFixImports(Formatter.java:269)
        at com.palantir.javaformat.java.FormatterServiceImpl.formatSourceReflowStringsAndFixImports(FormatterServiceImpl.java:43)

The code is

        this.setOnSort(event ->
                viewModel.patternListProperty().sort(CitationKeyPatternsPanelViewModel.defaultOnTopComparator));

        patternColumn.setSortable(true);

Variable event is unused, but the error appears at true. When replacing event by _, the error persists.

Full case: https://github.com/JabRef/jabref-koppor/pull/672. Execute ./gradlew spotlessApply on the command line.

koppor avatar Mar 13 '25 11:03 koppor

Any update on this issue? We're unable to use spotless plugin while people begin to use '_' for unused variables.

chongkai avatar Jun 17 '25 02:06 chongkai

+1 on this, I came looking to replace Google formatter and found this did a much better job of formatting but failed with this error in a java 24 app.

StephenFlavin avatar Jun 19 '25 21:06 StephenFlavin

Same error for the following code

    static <T, R> Gatherer<T, ?, R> map(Function<T, R> f) {
        Integrator<Void, T, R> integrator = (_, e, ds) -> {
            var ir = f.apply(e);
            return ds.push(ir);
        };
        return Gatherer.of(integrator);
    }
Execution failed for task ':shared:spotlessJavaApply'.
> There were 1 lint error(s), they must be fixed or suppressed.
  src/.../Gatherers.java:L16 palantir-java-format(palantir-java-format) error: expected token: '_'; generated  instead
  Resolve these lints or suppress with `suppressLintsFor`

sureshg avatar Jul 14 '25 23:07 sureshg

Note that we dropped activities regarding planatir-java-format as prettier-java worked well on our Java 24 code. (We activated experimentalOperatorPosition: start (hint by https://github.com/jhipster/prettier-java/issues/534#issuecomment-3024231994) and tabWidth: 4 in .prettierrc.yaml). (Full case: https://github.com/JabRef/jabref-koppor/pull/717)

I leave the issue open for reference as it still seems to be unsolved.

koppor avatar Aug 19 '25 22:08 koppor

This issue has been reported in two other tickets, and given by the number of open issues, I don't think anyone cares about this repo anymore. @koppor This blog post discusses the pros and cons of various Java formatters, and Prettier seems to have its own problems including installation and inconsistency between versions.

asarkar avatar Sep 02 '25 20:09 asarkar

@koppor This blog post discusses the pros and cons of various Java formatters

Thank you for the link! The poster appears a bit too negative to me.

, and Prettier seems to have its own problems including installation

There is an IntelliJ plugin: https://plugins.jetbrains.com/plugin/10456-prettier

and inconsistency between versions.

I tend to like it. As user, I can choose which style to follow: And old style or an updated one. Updates can be fixes in the parser logic or learnings based on communnity feedback.

koppor avatar Sep 02 '25 21:09 koppor

Unnamed variables & patterns support was added in https://github.com/palantir/palantir-java-format/pull/1410

crogoz avatar Sep 25 '25 12:09 crogoz

Sadly, the problem now shifts to spotless (in my case, the spotless-maven-plugin), which doesn't recognise _ and flags the file to be excluded from formatting.

Darn catch-22's.

talios avatar Sep 26 '25 03:09 talios

@talios, it's strange. I don't see any problem with spotless-maven-plugin/2.44.5 + palantir-java-format/2.74.0.

chongkai avatar Sep 26 '25 08:09 chongkai

@chongkai weird, seemed to work fine now - did show up as a warning but worked fine, and I see spotless-maven-plugin 2.3.0 is out as well.

So all good.

talios avatar Sep 28 '25 23:09 talios