sebmarkbage / link.js

Link.js is a module loader and conversion tool. It supports Labeled Modules, CommonJS and Asynchronous Module Definitions (AMD).
Other
29 stars 9 forks source link

Simple Function parse fails (browser) #4

Closed jakobo closed 11 years ago

jakobo commented 11 years ago
function() {
  return 'a';
}
LinkJS.parse(theAboveFile);

The tokenizer barfs on the expect("(") call, as it's already lex()-ed to ) in the string. Workaround is to rewrite the function with a function name, which gets past the lex problems.

Sample of us using our workaround: https://github.com/Jakobo/inject/blob/test_restructure/src/requirecontext.js#L327

sebmarkbage commented 11 years ago

Yea, LinkJS expects a JavaScript source file. Your example is not a valid JavaScript statement. I'm hesitant to support it since there may be other corner cases that could be contradictory. I guess I could supply an API to specifically parse an expression but a much simpler solution is to wrap it in parenthesis. That should work whether it's a function or literal.

(function() {
  return 'a';
})
('a')

If you want to parse an expression you can simply do:

linkjs.parse('(' + expression + ')')
jakobo commented 11 years ago

That's a simple enough explanation for me. Thanks for the update. On Nov 6, 2012 5:49 PM, "Sebastian Markbåge" notifications@github.com wrote:

Yea, LinkJS expects a JavaScript source file. Your example is not a valid JavaScript statement. I'm hesitant to support it since there may be other corner cases that could be contradictory. I guess I could supply an API to specifically parse an expression but a much simpler solution is to wrap it in parenthesis. That should work whether it's a function or literal.

(function() { return 'a';})

('a')

If you want to parse en expression you can simply do:

linkjs.parse('(' + expression + ')')

— Reply to this email directly or view it on GitHubhttps://github.com/sebmarkbage/link.js/issues/4#issuecomment-10135489.