cjs-module-lexer icon indicating copy to clipboard operation
cjs-module-lexer copied to clipboard

Export not detected

Open nicolo-ribaudo opened this issue 5 years ago • 2 comments

:warning: This is not real-world code. It's technically a bug, but no real program will notice it. Feel free to close this issue!

Consider this non-strict mode code: (GitHub's syntax highlighting is broken)

function fn() {
    yield / "/ //" /*
}
/*/
}

module.exports.foo = 2;

// */

The tokens are:

[function] [Identifier: fn] [paren left] [paren left] [brace left]
[Identifier: yield] [division] [String: "/ //"] [Comment: \n}\n/ ]
[brace right]

[Identifier: module] [dot] [Identifier: exports] [dot] [Identifier: foo] [equal] [Number: 2] [semicolon]

However, cjs-module-lexer returns { exports: [], reexports: [] }.

nicolo-ribaudo avatar Oct 27 '20 22:10 nicolo-ribaudo

Improving the lexing ambiguity cases is always a goal so thanks for posting, but yes there isn't a rush to fix this :)

I wasn't aware the this would be parsed as division in this expression position... what is it that makes yield act differently to return here?

guybedford avatar Oct 28 '20 20:10 guybedford

I've posted a potential fix in https://github.com/guybedford/cjs-module-lexer/pull/21, interestingly it's a negative diff and thankfully doesn't require any extra work :)

Would be grateful if you could review when you can.

guybedford avatar Oct 28 '20 20:10 guybedford