Skip to content

Commit

Permalink
feat: add support for import assertions (#74)
Browse files Browse the repository at this point in the history
* feat: add support for import assertions

* dynamic import adjustments

* byte optimization

* fixup readme substring
  • Loading branch information
guybedford committed Jun 14, 2021
1 parent e883ae9 commit 770481e
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ lib/lexer.wasm: src/lexer.h src/lexer.c
@mkdir -p lib
../wasi-sdk-11.0/bin/clang src/lexer.c --sysroot=../wasi-sdk-11.0/share/wasi-sysroot -o lib/lexer.wasm -nostartfiles \
-Wl,-z,stack-size=13312,--no-entry,--compress-relocations,--strip-all,\
--export=parse,--export=sa,--export=e,--export=ri,--export=re,--export=is,--export=ie,--export=ss,--export=ip,--export=se,--export=id,--export=es,--export=ee,--export=f,--export=__heap_base \
--export=parse,--export=sa,--export=e,--export=ri,--export=re,--export=is,--export=ie,--export=ss,--export=ip,--export=se,--export=ai,--export=as,--export=ae,--export=id,--export=es,--export=ee,--export=f,--export=__heap_base \
-Wno-logical-op-parentheses -Wno-parentheses \
-Oz

Expand Down
48 changes: 31 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,47 +43,60 @@ import { init, parse } from 'es-module-lexer/dist/lexer.js';
await init;

const source = `
import { a } from 'asdf';
import { name } from 'mod';
import json from './json.json' assert { type: 'json' }
export var p = 5;
export function q () {
};
// Comments provided to demonstrate edge cases
import /*comment!*/ ('asdf');
import /*comment!*/ ('asdf', { assert: { type: 'json' }});
import /*comment!*/.meta.asdf;
`;

const [imports, exports] = parse(source, 'optional-sourcename');

// Returns "asdf"
// Returns "mod"
imports[0].n
source.substring(imports[0].s, imports[0].e);
// "s" is shorthand for "start"
// "e" is shorthand for "end"
// "s" = start
// "e" = end

// Returns "import { a } from 'asdf';"
// Returns "import { name } from 'mod';"
source.substring(imports[0].ss, imports[0].se);
// "ss" is shorthand for "statement start"
// "se" is shorthand for "statement end"
// "ss" = statement start
// "se" = statement end

// Returns "assert"
source.slice(imports[1].a, 6);
// "a" = assert

// Returns "{ type: 'json' }"
source.substring(imports[1].as, imports[1].ae);
// "as" = assert start
// "ae" = assert end

// Returns "p,q"
exports.toString();

// Dynamic imports are indicated by imports[1].d > -1
// Dynamic imports are indicated by imports[2].d > -1
// In this case the "d" index is the start of the dynamic import
// Returns true
imports[1].d > -1;
imports[2].d > -1;

// Returns "asdf"
imports[1].n
source.substring(imports[1].s, imports[1].e);
imports[2].n
// Returns "'asdf'"
source.substring(imports[2].s, imports[2].e);
// Returns "import /*comment!*/ ("
source.substring(imports[1].d, imports[1].s);
// Returns "import /*comment!*/ ('asdf')"
source.substring(imports[1].d, imports[1].e + 1);
// imports[1].ss and imports[1].se is not meaningful
// because dynamic import is not a statement
source.substring(imports[2].d, imports[2].s);
// Returns "import /*comment!*/ ('asdf', { assert: { type: 'json' } })"
source.substring(imports[2].d, imports[2].se + 1);
// Returns "{ assert: { type: 'json' } }"
source.substring(imports[2].a, imports[2].e);
// ss is the same as d
// as, ae not used for dynamic imports

// import.meta is indicated by imports[2].d === -2
// Returns true
Expand Down Expand Up @@ -193,3 +206,4 @@ MIT

[travis-url]: https://travis-ci.org/guybedford/es-module-lexer
[travis-image]: https://travis-ci.org/guybedford/es-module-lexer.svg?branch=master

Binary file modified lib/lexer.wasm
Binary file not shown.

0 comments on commit 770481e

Please sign in to comment.