Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/boywithkeyboard-archive/js-imports
The most complete regular expressions for matching, parsing and validating JavaScript/TypeScript imports.
https://github.com/boywithkeyboard-archive/js-imports
exports imports javascript regex regular-expression regular-expressions typescript
Last synced: 12 days ago
JSON representation
The most complete regular expressions for matching, parsing and validating JavaScript/TypeScript imports.
- Host: GitHub
- URL: https://github.com/boywithkeyboard-archive/js-imports
- Owner: boywithkeyboard-archive
- License: mit
- Created: 2024-04-01T01:39:48.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-04-05T17:29:01.000Z (8 months ago)
- Last Synced: 2024-06-29T13:52:07.892Z (5 months ago)
- Topics: exports, imports, javascript, regex, regular-expression, regular-expressions, typescript
- Language: TypeScript
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
## js-imports
This library is **not perfect**. It attempts to **capture most common types of
imports and exports**. Some invalid import or export expressions might be seen
as valid and some valid ones might be seen invalid. This library is **not
meant** as a tool **for strict validation** of imports and rather as a naive way
to get all imports in a particular file.```ts
import {
denoTypesCommentRegex,
exportsRegex,
importsRegex,
isValidExport,
isValidImport,
rewriteIdentfiers,
tripleSlashCommentRegex,
walkImports,
} from 'https://deno.re/boywithkeyboard/[email protected]'
```### `rewriteIdentifiers()`
```ts
let fileContent = Deno.readTextFileSync('./foo.js')fileContent = rewriteImports(fileContent, (identifier, type) => {
return identifier.replace(...)
})
```### `walkImports()`
```ts
const fileContent = Deno.readTextFileSync('./foo.js')for (const i of walkImports(fileContent)) {
console.log(i)
// e.g.
// {
// input: 'import "foo"\n',
// identifier: 'foo',
// regexpArray: [],
// type: 'import'
// }
}
```### `isValidImport()`
**Valid:**
```ts
import 'bar'
import foo from 'bar'
import * as foo from 'bar'
import { foo } from 'bar'
import { type foo } from 'bar'
import { foo as bar } from 'bar'
import { type foo as bar } from 'bar'
import { default as foo } from 'bar'
import { type default as foo } from 'bar'
import type { foo } from 'bar'
import type { foo as bar } from 'bar'
import type { default as foo } from 'bar'
import foo, * as bar from 'bar'
import foo, { bar } from 'bar'
import { bar, foo } from 'bar'
import { bar as foobar, foo } from 'bar'
```### `isValidExport()`
**Valid:**
```ts
export * from 'bar'
export * as foo from 'bar'
export { foo } from 'bar'
export { foo as bar } from 'bar'
export { type foo } from 'bar'
export { type foo as bar } from 'bar'
export { default as foo } from 'bar'
export { type default as foo } from 'bar'
export type { foo } from 'bar'
export type { foo as bar } from 'bar'
export type { default as foo } from 'bar'
export { bar as foobar, foo } from 'bar'
```