Skip to content

flex-development/export-regex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

export-regex

github release npm codecov module type: esm license conventional commits typescript vitest yarn

export statement regex.

Contents

What is this?

This package contains regular expressions for matching export statements.

When should I use this?

Use this package when you need to match export statements.

Note:

  • Statements in docblock (/** */), multiline (/* */), and single-line (//) comments are ignored
  • Expressions are ECMAScript-compatible. They have not been tested with other flavors (PCRE, PCRE2, etc)

Install

This package is ESM only.

yarn add @flex-development/export-regex

From Git:

yarn add @flex-development/export-regex@flex-development/export-regex
See Git - Protocols | Yarn  for details on requesting a specific branch, commit, or tag.

Use

Suppose we have the following module:

import * as regexp from '@flex-development/export-regex'
import { at, omit, select, type Times } from '@flex-development/tutils'
import { dedent } from 'ts-dedent'

const code: Times<4, string> = [
  dedent`
    export { defineBuildConfig, type BuildConfig } from "#src"
    export type {
      JsonObject,
      LiteralUnion,
      Nullable
    } from '@flex-development/tutils'
    export * as constants from "./constants"
    export * from './interfaces'
  `,
  dedent`
    export const ESM_SYNTAX_REGEX: RegExp = /pattern/gm;
    export declare const RESOLVE_EXTENSIONS: string[]
    export enum Snack{}
    export interface User {}
    export class House {}
    export declare abstract class House extends Building {}
    export abstract class House {}
    export async function run() {}
    export function* foo() {}
    export namespace Foo {}
    export type Foo = 'foo'
    export function functionName() {}
    export class ClassName {}
    export const { name1, name2: bar } = o;
    export const [ name1, name2 ] = array;
  `,
  dedent`
    export default function () {}
    export default async function() {}
    export default class {}

    export default function functionName() {}
    export default function* generatorName() {}
    export default class ClassName {}
    export default abstract class ClassName {}

    export default async () => {}
    export default () => {}
    export default arg => {}

    export default foo
    export default 1 + 1;
  `,
  dedent`
    export { defineBuildConfig, type BuildConfig }
    export type {
      JsonObject,
      LiteralUnion,
      Nullable
    }
  `
]

const print = (matches: IterableIterator<RegExpMatchArray>): void => {
  console.debug(select([...matches], null, match => omit(match, ['input'])))
}

print(at(code, 0).matchAll(regexp.EXPORT_AGGREGATE_REGEX))
print(at(code, 1).matchAll(regexp.EXPORT_DECLARATION_REGEX))
print(at(code, 2).matchAll(regexp.EXPORT_DEFAULT_REGEX))
print(at(code, 3).matchAll(regexp.EXPORT_LIST_REGEX))

...running that yields:

[
  {
    '0': 'export { defineBuildConfig, type BuildConfig } from "#src"',
    '1': undefined,
    '2': '{ defineBuildConfig, type BuildConfig }',
    '3': '#src',
    index: 0,
    groups: [Object: null prototype] {
      type: undefined,
      exports: '{ defineBuildConfig, type BuildConfig }',
      specifier: '#src'
    }
  },
  {
    '0': 'export type {\n' +
      '  JsonObject,\n' +
      '  LiteralUnion,\n' +
      '  Nullable\n' +
      "} from '@flex-development/tutils'",
    '1': 'type',
    '2': '{\n  JsonObject,\n  LiteralUnion,\n  Nullable\n}',
    '3': '@flex-development/tutils',
    index: 59,
    groups: [Object: null prototype] {
      type: 'type',
      exports: '{\n  JsonObject,\n  LiteralUnion,\n  Nullable\n}',
      specifier: '@flex-development/tutils'
    }
  },
  {
    '0': 'export * as constants from "./constants"',
    '1': undefined,
    '2': '* as constants',
    '3': './constants',
    index: 148,
    groups: [Object: null prototype] {
      type: undefined,
      exports: '* as constants',
      specifier: './constants'
    }
  },
  {
    '0': "export * from './interfaces'",
    '1': undefined,
    '2': '*',
    '3': './interfaces',
    index: 189,
    groups: [Object: null prototype] {
      type: undefined,
      exports: '*',
      specifier: './interfaces'
    }
  }
]
[
  {
    '0': 'export const ESM_SYNTAX_REGEX',
    '1': undefined,
    '2': 'const',
    '3': 'ESM_SYNTAX_REGEX',
    index: 0,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'const',
      exports: 'ESM_SYNTAX_REGEX'
    }
  },
  {
    '0': 'export declare const RESOLVE_EXTENSIONS',
    '1': 'declare',
    '2': 'const',
    '3': 'RESOLVE_EXTENSIONS',
    index: 53,
    groups: [Object: null prototype] {
      modifiers: 'declare',
      declaration: 'const',
      exports: 'RESOLVE_EXTENSIONS'
    }
  },
  {
    '0': 'export enum Snack',
    '1': undefined,
    '2': 'enum',
    '3': 'Snack',
    index: 103,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'enum',
      exports: 'Snack'
    }
  },
  {
    '0': 'export interface User',
    '1': undefined,
    '2': 'interface',
    '3': 'User',
    index: 123,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'interface',
      exports: 'User'
    }
  },
  {
    '0': 'export class House',
    '1': undefined,
    '2': 'class',
    '3': 'House',
    index: 148,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'class',
      exports: 'House'
    }
  },
  {
    '0': 'export declare abstract class House',
    '1': 'declare abstract',
    '2': 'class',
    '3': 'House',
    index: 170,
    groups: [Object: null prototype] {
      modifiers: 'declare abstract',
      declaration: 'class',
      exports: 'House'
    }
  },
  {
    '0': 'export abstract class House',
    '1': 'abstract',
    '2': 'class',
    '3': 'House',
    index: 226,
    groups: [Object: null prototype] {
      modifiers: 'abstract',
      declaration: 'class',
      exports: 'House'
    }
  },
  {
    '0': 'export async function run',
    '1': 'async',
    '2': 'function',
    '3': 'run',
    index: 257,
    groups: [Object: null prototype] {
      modifiers: 'async',
      declaration: 'function',
      exports: 'run'
    }
  },
  {
    '0': 'export function* foo',
    '1': undefined,
    '2': 'function*',
    '3': 'foo',
    index: 288,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'function*',
      exports: 'foo'
    }
  },
  {
    '0': 'export namespace Foo',
    '1': undefined,
    '2': 'namespace',
    '3': 'Foo',
    index: 314,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'namespace',
      exports: 'Foo'
    }
  },
  {
    '0': 'export type Foo',
    '1': undefined,
    '2': 'type',
    '3': 'Foo',
    index: 338,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'type',
      exports: 'Foo'
    }
  },
  {
    '0': 'export function functionName',
    '1': undefined,
    '2': 'function',
    '3': 'functionName',
    index: 362,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'function',
      exports: 'functionName'
    }
  },
  {
    '0': 'export class ClassName',
    '1': undefined,
    '2': 'class',
    '3': 'ClassName',
    index: 396,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'class',
      exports: 'ClassName'
    }
  },
  {
    '0': 'export const { name1, name2: bar } = o',
    '1': undefined,
    '2': 'const',
    '3': '{ name1, name2: bar }',
    index: 422,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'const',
      exports: '{ name1, name2: bar }'
    }
  },
  {
    '0': 'export const [ name1, name2 ] = array',
    '1': undefined,
    '2': 'const',
    '3': '[ name1, name2 ]',
    index: 462,
    groups: [Object: null prototype] {
      modifiers: undefined,
      declaration: 'const',
      exports: '[ name1, name2 ]'
    }
  }
]
[
  {
    '0': 'export default function',
    '1': undefined,
    '2': 'function',
    '3': undefined,
    index: 0,
    groups: [Object: null prototype] {
      modifiers: undefined,
      kind: 'function',
      exports: undefined
    }
  },
  {
    '0': 'export default async function',
    '1': 'async',
    '2': 'function',
    '3': undefined,
    index: 30,
    groups: [Object: null prototype] {
      modifiers: 'async',
      kind: 'function',
      exports: undefined
    }
  },
  {
    '0': 'export default class',
    '1': undefined,
    '2': 'class',
    '3': undefined,
    index: 65,
    groups: [Object: null prototype] {
      modifiers: undefined,
      kind: 'class',
      exports: undefined
    }
  },
  {
    '0': 'export default function functionName',
    '1': undefined,
    '2': 'function',
    '3': 'functionName',
    index: 90,
    groups: [Object: null prototype] {
      modifiers: undefined,
      kind: 'function',
      exports: 'functionName'
    }
  },
  {
    '0': 'export default function* generatorName',
    '1': undefined,
    '2': 'function*',
    '3': 'generatorName',
    index: 132,
    groups: [Object: null prototype] {
      modifiers: undefined,
      kind: 'function*',
      exports: 'generatorName'
    }
  },
  {
    '0': 'export default class ClassName',
    '1': undefined,
    '2': 'class',
    '3': 'ClassName',
    index: 176,
    groups: [Object: null prototype] {
      modifiers: undefined,
      kind: 'class',
      exports: 'ClassName'
    }
  },
  {
    '0': 'export default abstract class ClassName',
    '1': 'abstract',
    '2': 'class',
    '3': 'ClassName',
    index: 210,
    groups: [Object: null prototype] {
      modifiers: 'abstract',
      kind: 'class',
      exports: 'ClassName'
    }
  },
  {
    '0': 'export default async',
    '1': 'async',
    '2': undefined,
    '3': undefined,
    index: 254,
    groups: [Object: null prototype] {
      modifiers: 'async',
      kind: undefined,
      exports: undefined
    }
  },
  {
    '0': 'export default',
    '1': undefined,
    '2': undefined,
    '3': undefined,
    index: 284,
    groups: [Object: null prototype] {
      modifiers: undefined,
      kind: undefined,
      exports: undefined
    }
  },
  {
    '0': 'export default',
    '1': undefined,
    '2': undefined,
    '3': undefined,
    index: 308,
    groups: [Object: null prototype] {
      modifiers: undefined,
      kind: undefined,
      exports: undefined
    }
  },
  {
    '0': 'export default foo',
    '1': undefined,
    '2': undefined,
    '3': 'foo',
    index: 334,
    groups: [Object: null prototype] {
      modifiers: undefined,
      kind: undefined,
      exports: 'foo'
    }
  },
  {
    '0': 'export default',
    '1': undefined,
    '2': undefined,
    '3': undefined,
    index: 353,
    groups: [Object: null prototype] {
      modifiers: undefined,
      kind: undefined,
      exports: undefined
    }
  }
]
[
  {
    '0': 'export { defineBuildConfig, type BuildConfig }',
    '1': undefined,
    '2': '{ defineBuildConfig, type BuildConfig }',
    index: 0,
    groups: [Object: null prototype] {
      type: undefined,
      exports: '{ defineBuildConfig, type BuildConfig }'
    }
  },
  {
    '0': 'export type {\n  JsonObject,\n  LiteralUnion,\n  Nullable\n}',
    '1': 'type',
    '2': '{\n  JsonObject,\n  LiteralUnion,\n  Nullable\n}',
    index: 47,
    groups: [Object: null prototype] {
      type: 'type',
      exports: '{\n  JsonObject,\n  LiteralUnion,\n  Nullable\n}'
    }
  }
]

API

This package exports the following identifiers:

There is no default export.

EXPORT_AGGREGATE_REGEX

Aggregate export statement regex. Ignores matches in comments.

EXPORT_DECLARATION_REGEX

Declaration export statement regex. Ignores matches in comments.

Requires unicode support (flag u).

EXPORT_DEFAULT_REGEX

Default export statement regex. Ignores matches in comments.

Declaration bodies and expressions that are not identifiers are not captured.

Requires unicode support (flag u).

EXPORT_LIST_REGEX

List export statement regex. Ignores matches in comments.

Types

This package is fully typed with TypeScript.

Related

Contribute

See CONTRIBUTING.md.