1.0.0 • Published 1 year ago

@tokey/imports-parser v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@tokey/imports-parser

Parser for esm like import syntax with support for "functional" tagged imports

import Name1, { Name2, Name3 as Name4, Tag(Name5) } from "request";

The output of parsing an import is represented with the following type:

interface ImportValue {
  star: boolean;
  defaultName: string | undefined;
  named: Record<string, string> | undefined;
  tagged: Record<string, Record<string, string>> | undefined;
  from: string | undefined;
  errors: string[];
  start: number;
  end: number;
}

Notice that this parser can parse broken/incomplete syntax and all the errors will be available in the "error" field.

The parser parses multiple imports and the result is an ImportValue[]

API

import { parseImports, ImportValue } from "@tokey/imports-parser";

const code = `
    import { a } from "request/a";
    import "request/b";
`;
const startNamedBlock = '{'; // default
const endNamedBlock = '}'; // default
const shouldParseTags = false // default

const results: ImportValue[] = parseImports(code, startNamedBlock, endNamedBlock, shouldParseTags);

const firstImport = results[0];
firstImport.from === 'request/a'; // true
firstImport.named['a'] === 'a'; // true
1.0.0

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

2 years ago

0.0.2

3 years ago

0.0.1

3 years ago