Skip to content

Remove import attributes with emptied re-exports in --fix - #2192

Merged
webpro merged 2 commits into
webpro-nl:mainfrom
mkantautas:fix/fix-reexport-import-attributes
Oct 23, 2026
Merged

webpro merged 2 commits into
webpro-nl:mainfrom
mkantautas:fix/fix-reexport-import-attributes

Conversation

@mkantautas

@mkantautas mkantautas commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

When --fix removes 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' }; becomes with { type: 'json' };, which is a syntax error in a module.

The emptied re-export is now removed together with its with { … } clause (and the legacy assert { … } on the same line).

Added cases to remove-export.test.ts and a fix-import-attributes fixture with its own test. Both fail on main and pass now. The full test suite and pnpm run ci pass.

@pkg-pr-new

pkg-pr-new Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/knip@2192
npm i https://pkg.pr.new/@knip/language-server@2192
npm i https://pkg.pr.new/@knip/mcp@2192

commit: 52dc74a

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/knip/src/util/remove-export.ts Outdated
Comment on lines +10 to +46
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;
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@webpro

webpro commented Oct 23, 2026

Copy link
Copy Markdown
Member

Thanks Mantas! 🧡

@webpro
webpro merged commit 231026c into webpro-nl:main Oct 23, 2026
32 checks passed
@webpro

webpro commented Oct 24, 2026

Copy link
Copy Markdown
Member

🚀 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants