IanVS / prettier-plugin-sort-imports

An opinionated but flexible prettier plugin to sort import statements
Apache License 2.0
1.02k stars 25 forks source link

Please allow overriding `importAttributesKeyword` with `assert` #180

Closed ifeltsweet closed 2 months ago

ifeltsweet commented 2 months ago

Our build system requires output to be generated with assert instead of with on the import statements. Please allow us to override babel format option importAttributesKeyword.

IanVS commented 2 months ago

Why does your build system enforce the use of a deprecated syntax?

fbartho commented 2 months ago

@ifeltsweet I believe you can override attributes using this example: https://github.com/IanVS/prettier-plugin-sort-imports/blob/fef70a61af77f70919742cd4ae335af3edd2a027/tests/Babel/ppsi.spec.ts#L5

Does that solve what you need?

IanVS commented 2 months ago

@fbartho that will allow parsing the deprecated syntax, but we don't allow changing the way that we re-generate it.

vincerubinetti commented 2 months ago

Related issue on upstream repo, in case it's relevant/helpful:

https://github.com/trivago/prettier-plugin-sort-imports/issues/270


Also, I'm not using the assert syntax, I'm using with { type: "json" }, and I'm finding that this plugin (removing just this plugin makes everything work) is breaking the parsing of some playwright tests I have:

SyntaxError: This experimental syntax requires enabling the parser plugin: "importAttributes". (3:53)
    at constructor (/Users/vincerubinetti/Desktop/molevolvr-new/app/node_modules/@babel/parser/lib/index.js:352:19)
    at TypeScriptParserMixin.raise (/Users/vincerubinetti/Desktop/molevolvr-new/app/node_modules/@babel/parser/lib/index.js:3250:19)
    at TypeScriptParserMixin.expectPlugin (/Users/vincerubinetti/Desktop/molevolvr-new/app/node_modules/@babel/parser/lib/index.js:3278:16)
    at TypeScriptParserMixin.expectImportAttributesPlugin (/Users/vincerubinetti/Desktop/molevolvr-new/app/node_modules/@babel/parser/lib/index.js:10704:12)
    at TypeScriptParserMixin.maybeParseImportAttributes (/Users/vincerubinetti/Desktop/molevolvr-new/app/node_modules/@babel/parser/lib/index.js:13783:16)
    at TypeScriptParserMixin.parseImportSourceAndAttributes (/Users/vincerubinetti/Desktop/molevolvr-new/app/node_modules/@babel/parser/lib/index.js:13696:10)
    at TypeScriptParserMixin.parseImportSpecifiersAndAfter (/Users/vincerubinetti/Desktop/molevolvr-new/app/node_modules/@babel/parser/lib/index.js:13690:17)
    at TypeScriptParserMixin.parseImport (/Users/vincerubinetti/Desktop/molevolvr-new/app/node_modules/@babel/parser/lib/index.js:13681:17)
    at TypeScriptParserMixin.parseImport (/Users/vincerubinetti/Desktop/molevolvr-new/app/node_modules/@babel/parser/lib/index.js:9048:26)
    at TypeScriptParserMixin.parseStatementContent (/Users/vincerubinetti/Desktop/molevolvr-new/app/node_modules/@babel/parser/lib/index.js:12328:27)

Any way to at least get this to not cause a critical error that completely halts all of prettier formatting?

EDIT: Nevermind, putting this in my prettier config seems to work: "importOrderParserPlugins": ["typescript", "importAssertions"]. I was already trying importAssertions as the error message said, but then I was getting a different error. I guess it was due to it not being able to understand the TS syntax in the file.

fbartho commented 2 months ago

Nevermind, putting this in my prettier config seems to work: "importOrderParserPlugins": ["typescript", "importAssertions"]. I was already trying importAssertions as the error message said, but then I was getting a different error. I guess it was due to it not being able to understand the TS syntax in the file.

Ah! That's good news!

I agree, I don't love the importOrderParserPlugins option. I think it's a bit clunky, and hard to prevent problems between different plugins clobbering each other -- but so far, this is a pinch point a bit outside the control of this plugin! (babel/prettier take the blame for the fundamental underlying issue there!)

IanVS commented 2 months ago

Hah, I guess you had the right suggestion after all, @fbartho!

Glad it sounds like everything's sorted out. Thanks ya'll.

vincerubinetti commented 2 months ago

FYI I also ended up needing to add jsx along with typescript, since I'm using the same prettier config for my tests (pure ts) and my app source (TSX).

Also FYI, I tested the importOrderParserPlugins solution using assert instead of with (per OP's original request), and it seems to work fine.... however, either Prettier or this plugin seems to be automatically change assert to with, which may mess things up in OP's case. At least it's not a critical format-halting error though.