Skip to content

Commit

Permalink
docs: update readme for module specifier start/end index #9
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed May 12, 2024
1 parent 5ec8727 commit 0e234be
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
127 changes: 127 additions & 0 deletions playground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import parseImports from './src/index.js'

const code = `
import a from 'b'
import * as c from './d'
import { e as f, g as h, i } from '/j'
import k, { l as m } from 'n'
import o, * as p from "./q"
import r, { s as t, u } from "/v"
import fs from 'fs'
;(async () => {
await import("w")
await import("x" + "y")
})()
`

// Lazily iterate over iterable of imports
for (const $import of await parseImports(code)) {
console.log($import)
}

// Or get as an array of imports
const imports = [...(await parseImports(code))]

console.log(imports[0])
// =>
// {
// startIndex: 3,
// endIndex: 20,
// isDynamicImport: false,
// moduleSpecifier: {
// type: 'package',
// startIndex: 17,
// endIndex: 20,
// isConstant: true,
// code: `'b'`,
// value: 'b',
// resolved: undefined
// },
// importClause: {
// default: 'a',
// named: [],
// namespace: undefined
// }
// }

console.log(imports[1])
// =>
// {
// startIndex: 23,
// endIndex: 47,
// isDynamicImport: false,
// moduleSpecifier: {
// type: 'relative',
// startIndex: 42,
// endIndex: 47,
// isConstant: true,
// code: `'./d'`,
// value: './d',
// resolved: undefined
// },
// importClause: {
// default: undefined,
// named: [],
// namespace: 'c'
// }
// }

console.log(imports[5])
// =>
// {
// startIndex: 153,
// endIndex: 186,
// isDynamicImport: false,
// moduleSpecifier: {
// type: 'absolute',
// isConstant: true,
// code: '"/v"',
// value: '/v',
// resolved: undefined
// },
// importClause: {
// default: 'r',
// named: [
// { specifier: 's', binding: 't' },
// { specifier: 'u', binding: 'u' }
// ],
// namespace: undefined
// }
// }

console.log(imports[7])
// =>
// {
// startIndex: 238,
// endIndex: 249,
// isDynamicImport: true,
// moduleSpecifier: {
// type: 'package',
// startIndex: 245,
// endIndex: 248,
// isConstant: true,
// code: '"w"',
// value: 'w',
// resolved: undefined
// },
// importClause: undefined
// }

console.log(imports[8])
// =>
// {
// startIndex: 260,
// endIndex: 277,
// isDynamicImport: true,
// moduleSpecifier: {
// type: 'unknown',
// startIndex: 267,
// endIndex: 276,
// isConstant: false,
// code: '"x" + "y"',
// value: undefined,
// resolved: undefined
// },
// importClause: undefined
// }
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ console.log(imports[0])
// isDynamicImport: false,
// moduleSpecifier: {
// type: 'package',
// startIndex: 17,
// endIndex: 20,
// isConstant: true,
// code: `'b'`,
// value: 'b',
Expand All @@ -89,6 +91,8 @@ console.log(imports[1])
// isDynamicImport: false,
// moduleSpecifier: {
// type: 'relative',
// startIndex: 42,
// endIndex: 47,
// isConstant: true,
// code: `'./d'`,
// value: './d',
Expand All @@ -109,6 +113,8 @@ console.log(imports[5])
// isDynamicImport: false,
// moduleSpecifier: {
// type: 'absolute',
// startIndex: 182,
// endIndex: 186,
// isConstant: true,
// code: '"/v"',
// value: '/v',
Expand All @@ -132,6 +138,8 @@ console.log(imports[7])
// isDynamicImport: true,
// moduleSpecifier: {
// type: 'package',
// startIndex: 245,
// endIndex: 248,
// isConstant: true,
// code: '"w"',
// value: 'w',
Expand All @@ -148,6 +156,8 @@ console.log(imports[8])
// isDynamicImport: true,
// moduleSpecifier: {
// type: 'unknown',
// startIndex: 267,
// endIndex: 276,
// isConstant: false,
// code: '"x" + "y"',
// value: undefined,
Expand Down Expand Up @@ -208,6 +218,8 @@ type Import = {
isDynamicImport: boolean
moduleSpecifier: {
type: ModuleSpecifierType
startIndex: number
endIndex: number
isConstant: boolean
code: string
value?: string
Expand All @@ -225,6 +237,8 @@ type Import = {
`code.substring(startIndex, endIndex)` returns the full import statement or
expression.
`code.substring(moduleSpecifier.startIndex, moduleSpecifier.endIndex)` returns
the module specifier including quotes.
`moduleSpecifier.isConstant` is `true` when the import is not a dynamic import
(`isDynamicImport` is `false`), or when the import is a dynamic import where the
Expand Down

0 comments on commit 0e234be

Please sign in to comment.