Repository navigation
Remove import attributes with emptied re-exports in --fix - #2192
Conversation
commit: |
webpro
left a comment
There was a problem hiding this comment.
Thanks for the pull request, Mantas! The leftover with { type: 'json' } is a real syntax error after --fix, and the test cases are thorough. One request inline: I think a single pattern can replace the scanner.
| const lineTerminatorMatcher = /[\n\r\u2028\u2029]/; | ||
|
|
||
| const skipTrivia = (text: string, index: number) => { | ||
| let i = index; | ||
| while (i < text.length) { | ||
| if (/\s/.test(text[i])) i++; | ||
| else if (text.startsWith('/*', i)) { | ||
| const end = text.indexOf('*/', i + 2); | ||
| i = end === -1 ? text.length : end + 2; | ||
| } else if (text.startsWith('//', i)) { | ||
| const end = text.slice(i).search(lineTerminatorMatcher); | ||
| i = end === -1 ? text.length : i + end; | ||
| } else break; | ||
| } | ||
| return i; | ||
| }; | ||
|
|
||
| const getImportAttributesLength = (text: string) => { | ||
| const start = skipTrivia(text, 0); | ||
| const isWith = text.startsWith('with', start); | ||
| const isAssert = !isWith && text.startsWith('assert', start) && !lineTerminatorMatcher.test(text.slice(0, start)); | ||
| if (!isWith && !isAssert) return 0; | ||
| let i = skipTrivia(text, start + (isWith ? 4 : 6)); | ||
| if (text[i] !== '{') return 0; | ||
| i++; | ||
| while (i < text.length) { | ||
| const char = text[i]; | ||
| if (char === '}') return i + 1; | ||
| if (char === "'" || char === '"') { | ||
| i++; | ||
| while (i < text.length && text[i] !== char) i += text[i] === '\\' ? 2 : 1; | ||
| i++; | ||
| } else if (text.startsWith('/*', i) || text.startsWith('//', i)) i = skipTrivia(text, i); | ||
| else i++; | ||
| } | ||
| return 0; | ||
| }; |
There was a problem hiding this comment.
Could this be a single pattern instead? Something like:
const importAttributesMatcher = /^(?:\s*with|[ \t]*assert)\s*\{[^}]*\}/;const afterSpecifier = afterEnd.substring(fromSpecifierLength).replace(importAttributesMatcher, '');
return beforeBracket.substring(0, exportKeywordOffset) + afterSpecifier;With this, the new cases in remove-export.test.ts pass except the three with comments or a } inside the clause. That includes with on the next line, assert, assert on the next line (left alone), resolution-mode and within(. Modules are always strict, so with can't start a statement there, and with { right after the specifier is always the attributes clause.
The rest of removeExport doesn't handle comments either, so a commented statement still doesn't come out clean:
| Input | Result with this PR |
|---|---|
export { AB } /* c */ from './data.json' with { type: 'json' }; |
/* c */ from './data.json' with { type: 'json' }; |
export { AB /* c */ } from './data.json' with { type: 'json' }; |
export { /* c */ } from './data.json' with { type: 'json' }; |
export { AB } from './data.json' /* c */ with { type: 'json' }; |
; |
Attribute values come from a short list ('json', 'css', 'import'), so a } inside one is unlikely too. With the pattern, those three cases and the /* JSON */ line in the fixture would go.
|
Thanks Mantas! 🧡 |
|
🚀 This pull request is included in v6.43.0. See Release 6.43.0 for release notes. Using Knip in a commercial project? Please consider becoming a sponsor. |
When
--fixremoves the last specifier of a re-export, it cuts the statement at the end of the module specifier, so an import attributes clause stays behind:export { default as settings } from './settings.json' with { type: 'json' };becomeswith { type: 'json' };, which is a syntax error in a module.The emptied re-export is now removed together with its
with { … }clause (and the legacyassert { … }on the same line).Added cases to
remove-export.test.tsand afix-import-attributesfixture with its own test. Both fail on main and pass now. The full test suite andpnpm run cipass.