tilgovi commented on a change in pull request #2345: port to spidermonkey 60 URL: https://github.com/apache/couchdb/pull/2345#discussion_r359161513
########## File path: share/server/60/rewrite_fun.js ########## @@ -0,0 +1,56 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +// +// Based on the normalizeFunction which can be +// found here: +// +// https://github.com/dmunch/couch-chakra/blob/master/js/normalizeFunction.js + +function rewriteFunInt(fun) { + const ast = esprima.parse(fun); + let idx = ast.body.length - 1; + let decl = {}; + + // Search for the first FunctionDeclaration beginning from the end + do { + decl = ast.body[idx--]; + } while (idx >= 0 && decl.type !== "FunctionDeclaration"); + idx++; + + // If we have a function declaration without an Id, wrap it + // in an ExpressionStatement and change it into + // a FuntionExpression + if (decl.type == "FunctionDeclaration" && decl.id == null) { Review comment: I'm trying to reproduce such a parse tree with the esprima online parser, and I just get errors for things like this: ``` js function(doc) {} ``` Here's a link: https://esprima.org/demo/parse.html?code=function(doc)%20%7B%7D Does this actually parse both forms we want it to? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
