Comments (5)
I think we need some expression parsing anyway to handle ES6 default parameters:
function (opts = { foo: { done: (x) => console.log({ value: x }) } }) {
// ...
}
However, we don't actually need a full-fledged ES parser (like acorn) for that, and basic preparsing would allow us to skip default parameters and capture argument names very quickly.
I'm not familiar with snapdragon, but simple recursive descent would definitely work. For that we need to implement several simple tokenizers for identifiers, strings, numbers, punctuation, and comments.
Then I imagine the whole process to be something like this:
- Start tokenizing, see if the subject is an arrow function, generator, or plain ES5 function.
- Tokenize function name (an identifier).
- Parse argument list:
- Tokenize identifier (and save its value).
- Tokenize
,
or=
. - If
=
(argument has a default param), skip the following expression (just keep track of parentheses really, we don't need to actually parse anything).
- Save the rest of the string as the function body and stop.
This is some work, but not that hard, since we don't need to construct an AST. We can allow a much more loose grammar than EcmaScript defines:
function (opts = { foo foo:: { bar / } }) {
// ...
}
from parse-function.
Hmmm. Okey, I made some benchmarks tests and with regex is 40-60x faster, lol. So we will back to regex approach. I already have ready regex that handles regular and arrow functions.
from parse-function.
@tunnckoCore What I'm saying is that regexes won't ever support ES6 default parameters feature (see #8).
There are many modules that do regex-based parsing (js-args-names is one example), but they all are broken for ES6. We can do better and be the first module that works correctly with ES6.
> parseFunction('function (opts = { foo: { done: (x) => console.log({ value: x }) } }, cb) { /* ... */ }')
{ name: 'anonymous',
body: ' foo: { done: (x) => console.log({ value: x }) } }, cb) { /* ... */ ',
args: [ 'opts' ],
params: 'opts' }
from parse-function.
@eush77 you're right. PRs always welcome :)
from parse-function.
Again, very thanks! Hope this helps! Review the changelog.
from parse-function.
Related Issues (20)
- Error when parsing a native function HOT 2
- Bug with arguments default value consisting of a list of expressions HOT 11
- fake issue
- v5.1.3 doesn't have a `dist/index.js` file HOT 5
- Ensure dist builds exists
- When default value consists of a list of expressions, default value is not returned as a string
- 5.2.0 has no dist folder HOT 4
- module and main entry points are not exporting the same API HOT 8
- Weekly Digest (10 February, 2019 - 17 February, 2019)
- Weekly Digest (17 February, 2019 - 24 February, 2019)
- Weekly Digest (24 February, 2019 - 3 March, 2019)
- Weekly Digest (3 March, 2019 - 10 March, 2019)
- Arrow function with inner function fails to parse HOT 7
- Function with named parameters is not parsed correctly HOT 1
- V5.3.2 Error: Cannot find module './utils' HOT 4
- Announcement (a bit late): moved to a `tunnckoCore/opensource` monorepo
- add "Plugins Architecture" and "Result" sections
- Strange parser error when an `if` statement is at the top of an async arrow function HOT 8
- Parse bound functions HOT 8
- npm version and github release are not in sync HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parse-function.