Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
feat(typescript): TrextMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Oct 10, 2021
1 parent ef6b6e0 commit 8cdaaf0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export type { default as RegexString } from './regex-string.type'
export type { default as SourceMapComment } from './source-map-comment.type'
export type { default as TrextDefaults } from './trext-defaults.type'
export type { default as TrextFileResult } from './trext-file-result.type'
export type { default as TrextMatch } from './trext-match.type'
export type { default as TrextToFn } from './trext-to-fn.type'
export type { default as TrextTo } from './trext-to.type'
21 changes: 21 additions & 0 deletions src/types/trext-match.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { NodePath } from '@babel/core'
import type { CallExpression, ImportDeclaration } from '@babel/types'
import type RegexString from './regex-string.type'

/**
* @file Type Definitions - TrextMatch
* @module trext/types/TrextMatch
*/

/**
* The matched substring when [replacing a file extension][1], or when used
* within `Trextel`, a custom Babel plugin, a type of {@link NodePath} object:
*
* - `NodePath<CallExpression>`; see {@link CallExpression}
* - `NodePath<ImportDeclaration>`; see {@link ImportDeclaration}
*
* [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_function_as_a_parameter
*/
type TrextMatch = RegexString | NodePath<CallExpression | ImportDeclaration>

export default TrextMatch
25 changes: 13 additions & 12 deletions src/types/trext-to-fn.type.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import type { NodePath } from '@babel/core'
import type { CallExpression, ImportDeclaration } from '@babel/types'
import type FileExtension from './file-extension.type'
import type RegexString from './regex-string.type'
import type TrextMatch from './trext-match.type'

/**
* @file Type Definitions - TrextToFn
* @module trext/types/TrextToFn
*/

/**
* [Function that returns a file extension][1].
* Function that returns a file extension.
*
* When used in `Trext.plugin`, a custom Babel plugin, the only argument passed
* will be one of the following {@link NodePath} types:
* The exact number of arguments passed depends on the context in which the
* function was invoked, and then on if the first argument is a `RegExp` object.
*
* - `NodePath<CallExpression>`; see {@link CallExpression}
* - `NodePath<ImportDeclaration>`; see {@link ImportDeclaration}
* See [Specifying a function as a parameter][1] from MDN documentation for
* arguments passed when using `String.prototype.replace`.
*
* When used within `Trextel`, our custom Babel plugin, the only argument passed
* will be a type of {@link NodePath} object:
*
* - `NodePath<CallExpression>`
* - `NodePath<ImportDeclaration>`
*
* @template T - File extension name(s)
*
* [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_function_as_a_parameter
*/
type TrextToFn<T extends string = string> = {
(
match: RegexString | NodePath<CallExpression | ImportDeclaration>,
...args: any[]
): FileExtension<T>
(match: TrextMatch, ...args: any[]): FileExtension<T>
}

export default TrextToFn

0 comments on commit 8cdaaf0

Please sign in to comment.