@ts-bridge/cli
Version:
Bridge the gap between ES modules and CommonJS modules with an easy-to-use alternative to `tsc`.
160 lines • 6.49 kB
TypeScript
import type { TypeChecker, Node, SourceFile, ImportDeclaration, Statement, System, ExportDeclaration, NodeArray, ImportSpecifier } from 'typescript';
import typescript from 'typescript';
/**
* Check if a symbol name is unique in the scope of the given node.
*
* @param typeChecker - The type checker to use.
* @param node - The node to check.
* @param symbolName - The name of the symbol to check.
* @returns `true` if the symbol name is unique, or `false` otherwise.
*/
export declare function isUnique(typeChecker: TypeChecker, node: Node, symbolName: string): boolean;
/**
* Check if a symbol is global.
*
* @param typeChecker - The type checker to use.
* @param node - The node to check.
* @param symbolName - The name of the symbol to check.
* @returns `true` if the symbol is global, or `false` otherwise.
*/
export declare function isGlobal(typeChecker: TypeChecker, node: Node, symbolName: string): boolean;
/**
* Get a unique identifier given a source file and (expected) name. This will
* try to get a name as close to the expected name as possible, and prepend an
* underscore if necessary.
*
* @param typeChecker - The type checker to use.
* @param sourceFile - The source file to get the unique identifier for.
* @param name - The (expected) name to use.
* @returns The unique identifier.
*/
export declare function getUniqueIdentifier(typeChecker: TypeChecker, sourceFile: SourceFile, name: string): string;
/**
* An import declaration, containing the name of the import and an optional
* property name.
*/
export type Import = {
/**
* The name of the import. This is the name that is available the scope of the
* file containing the import. If the `propertyName` is not set, this is the
* name that is exported by the module as well.
*/
name: string;
/**
* The property name of the import. If this is set, this is the name that is
* exported by the module.
*/
propertyName?: string;
};
/**
* An object with the detected and undetected imports.
*/
export type Imports = {
/**
* The detected imports, i.e., the named imports that are detected by
* `cjs-module-lexer` and can be used as named imports in ES modules.
*/
detected: Import[];
/**
* The other imports, i.e., the named imports that are not detected by
* `cjs-module-lexer` and need to be imported as a default import and
* destructured.
*/
undetected: Import[];
};
/**
* Get the detected and undetected imports for the given package specifier.
* This function uses `cjs-module-lexer` to parse the CommonJS module and
* extract the exports, and then compares the named imports to the exports to
* determine which imports are detected and which are not.
*
* @param packageSpecifier - The specifier for the package.
* @param system - The TypeScript system.
* @param parentUrl - The URL of the parent module.
* @param imports - The named imports from the import declaration.
* @returns The "exported" imports and other imports.
*/
export declare function getImports(packageSpecifier: string, system: System, parentUrl: string, imports: NodeArray<ImportSpecifier>): Imports;
/**
* Get the named import node(s) for the given import declaration. This function
* transforms named imports for CommonJS modules to a default import and a
* variable declaration, so that the named imports can be used in ES modules.
*
* For example, the following import from a CommonJS module:
* ```js
* import { namedImport1, namedImport2 } from 'some-module';
* ```
*
* will be transformed to:
* ```js
* import somemodule from 'some-module';
* const { namedImport1, namedImport2 } = somemodule;
* ```
*
* @param typeChecker - The type checker to use.
* @param sourceFile - The source file to use.
* @param node - The import declaration node.
* @param system - The compiler system to use.
* @returns The new node(s) for the named import.
*/
export declare function getNamedImportNodes(typeChecker: TypeChecker, sourceFile: SourceFile, node: ImportDeclaration, system: System): Statement | Statement[];
/**
* Get the import declaration without type-only imports.
*
* @param typeChecker - The type checker to use.
* @param node - The import declaration node.
* @returns The import declaration without type-only imports. If there are no
* type-only imports, the node is returned as is.
*/
export declare function getNonTypeImports(typeChecker: TypeChecker, node: ImportDeclaration): ImportDeclaration | undefined;
/**
* Get the export declaration without type-only exports.
*
* @param typeChecker - The type checker to use.
* @param node - The export declaration node.
* @returns The export declaration without type-only exports. If there are no
* type-only exports, the node is returned as is.
*/
export declare function getNonTypeExports(typeChecker: TypeChecker, node: ExportDeclaration): ExportDeclaration | undefined;
/**
* Create a namespace import, e.g.:
*
* ```
* import * as name from 'module';
* ```
*
* @param name - The name of the namespace import.
* @param module - The module to import.
* @returns The namespace import.
*/
export declare function getNamespaceImport(name: string, module: string): ImportDeclaration;
/**
* Get `import.meta.url`. This is extracted into a function to reduce code
* duplication.
*
* @returns The `import.meta.url` expression.
*/
export declare function getImportMetaUrl(): typescript.PropertyAccessExpression;
/**
* Check if the TypeScript version supports import attributes.
*
* @returns `true` if the TypeScript version supports import attributes, or
* `false` otherwise.
*/
export declare function hasImportAttributes(): boolean;
/**
* Get the import attributes for the given name and value.
*
* This function supports older versions of TypeScript by using import
* assertions rather than import attributes, if necessary. Import assertions are
* deprecated though, so it is recommended to use a recent version of TypeScript
* that supports import attributes.
*
* @param name - The name of the attribute.
* @param value - The value of the attribute.
* @param useAttributes - Whether to use import attributes. By default, this is
* determined by the {@link hasImportAttributes} function.
* @returns The import attributes.
*/
export declare function getImportAttribute(name: string, value: string, useAttributes?: boolean): typescript.AssertClause;
//# sourceMappingURL=generator.d.ts.map