Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve helper-module-imports typings (#14623)
* refactor: simplify ImportInjector._applyDefaults * helper-module-imports * map globals to the one used in Babel 8
- Loading branch information
Showing
5 changed files
with
130 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,77 @@ | ||
import ImportInjector from "./import-injector"; | ||
import ImportInjector, { type ImportOptions } from "./import-injector"; | ||
import type { NodePath } from "@babel/traverse"; | ||
import type * as t from "@babel/types"; | ||
|
||
export { ImportInjector }; | ||
|
||
export { default as isModule } from "./is-module"; | ||
|
||
export function addDefault(path, importedSource, opts?) { | ||
export function addDefault( | ||
path: NodePath, | ||
importedSource: string, | ||
opts?: Partial<ImportOptions>, | ||
) { | ||
return new ImportInjector(path).addDefault(importedSource, opts); | ||
} | ||
|
||
export function addNamed(path, name, importedSource, opts?) { | ||
function addNamed( | ||
path: NodePath, | ||
name: string, | ||
importedSource: string, | ||
opts?: Omit< | ||
Partial<ImportOptions>, | ||
"ensureLiveReference" | "ensureNoContext" | ||
>, | ||
): t.Identifier; | ||
function addNamed( | ||
path: NodePath, | ||
name: string, | ||
importedSource: string, | ||
opts?: Omit<Partial<ImportOptions>, "ensureLiveReference"> & { | ||
ensureLiveReference: true; | ||
}, | ||
): t.MemberExpression; | ||
function addNamed( | ||
path: NodePath, | ||
name: string, | ||
importedSource: string, | ||
opts?: Omit<Partial<ImportOptions>, "ensureNoContext"> & { | ||
ensureNoContext: true; | ||
}, | ||
): t.SequenceExpression; | ||
/** | ||
* add a named import to the program path of given path | ||
* | ||
* @export | ||
* @param {NodePath} path The starting path to find a program path | ||
* @param {string} name The name of the generated binding. Babel will prefix it with `_` | ||
* @param {string} importedSource The source of the import | ||
* @param {Partial<ImportOptions>} [opts] | ||
* @returns {t.Identifier | t.MemberExpression | t.SequenceExpression} If opts.ensureNoContext is true, returns a SequenceExpression, | ||
* else if opts.ensureLiveReference is true, returns a MemberExpression, else returns an Identifier | ||
*/ | ||
function addNamed( | ||
path: NodePath, | ||
name: string, | ||
importedSource: string, | ||
opts?: Partial<ImportOptions>, | ||
) { | ||
return new ImportInjector(path).addNamed(name, importedSource, opts); | ||
} | ||
export { addNamed }; | ||
|
||
export function addNamespace(path, importedSource, opts?) { | ||
export function addNamespace( | ||
path: NodePath, | ||
importedSource: string, | ||
opts?: Partial<ImportOptions>, | ||
) { | ||
return new ImportInjector(path).addNamespace(importedSource, opts); | ||
} | ||
|
||
export function addSideEffect(path, importedSource, opts?) { | ||
export function addSideEffect( | ||
path: NodePath, | ||
importedSource: string, | ||
opts?: Partial<ImportOptions>, | ||
) { | ||
return new ImportInjector(path).addSideEffect(importedSource, opts); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters