嘗試做一個多行 NamedImports。
我的代碼:
const importClause = factory?.createImportClause(
Boolean(node.importClause?.isTypeOnly),
node.importClause?.name,
factory?.createNamedImports(uniqImports as ImportSpecifier[]),
);
const importProps: ts.ImportDeclaration = factory?.createImportDeclaration(
node.decorators,
node.modifiers,
importClause,
node.moduleSpecifier,
node.assertClause,
);
我的結果:
import { One, Two } from 'something';
預期結果:
import {
One,
Two,
} from 'something';
我應該怎么辦?
uj5u.com熱心網友回復:
TypeScript 中的列印機(發射器)在源代碼中發出命名的匯入和匯出,而沒有多行。你可以在這里看到:
- https://github.com/microsoft/TypeScript/blob/8efa88f180eed88b197c509134d60a6313e8f1fd/src/compiler/types.ts#L9069
- https://github.com/microsoft/TypeScript/blob/8efa88f180eed88b197c509134d60a6313e8f1fd/src/compiler/emitter.ts#L3570
- https://github.com/microsoft/TypeScript/blob/8efa88f180eed88b197c509134d60a6313e8f1fd/src/compiler/emitter.ts#L4412
從我對源代碼的閱讀中,我看不出有任何方法可以使這些多行。
請記住,列印機不是為修改要維護的代碼而設計的,而是為輸出要使用的代碼而設計的,因此輸出不是很靈活。根據具體情況,不使用轉換 api/printer 而是直接對源檔案文本進行編輯可能會更好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/524773.html
