question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can I use this to extend JavaScript functionalities?

See original GitHub issue

I tried the live editor but I’m not sure if what I’m doing it’s correct or not: I would like, for example, use “func” instead of “function” into my code, so parse the token and replace with the valid JS one, but it outs always syntax error, also using “acorn” with “loose” checked.

I tried with this

module.exports.replace = () => ({
    'func': 'function',
});

Is what I would like to do buildable or not?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
coderaisercommented, Jun 21, 2022

Just created the repository, we can move there and continue to add new keywords 😃.

It already has ability to write tests, and extend in any way using TDD.

1reaction
coderaisercommented, Jun 16, 2022

If you want to use func inside your code, like this:

func hello() {
    return 'world';
}

This will not be correct JavaScript, so @babel/parser, and any other ESTree-parser cannot parse it to FunctionDeclaration node.

Anyways you can extend acorn to have support of func, and produce FunctionDeclaration, and use it in 🐊Putout with changes in .putout.json:

{
    "parser": "my-parser-with-func-support"
}

This will work!

But not in 🐊Putout Editor, since it has preconfigured set of parsers.

Also you can use API:

const source = `
    func hello() {
        return 'world';
    }
`;
const {code} = putout(source, {
    parser: 'my-parser-with-func-support'
});

But remember, that you cannot use Replacer for this purpose, because it uses Babel parser inside. Anyways Traverser can handle it 😃.

Why do you need it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript: Extend a Function - Stack Overflow
Use extendFunction.js ... init = extendFunction(init, function(args) { doSomethingHereToo(); });. But in your specific case, it's easier to extend ...
Read more >
extends - JavaScript - MDN Web Docs - Mozilla
The extends keyword can be used to subclass custom classes as well as built-in objects. Any constructor that can be called with new...
Read more >
How to Extend Legacy Functions in JavaScript Without ...
Let's fix that and we will see how we can support both types of parameters at once: the legacy single input parameters for...
Read more >
6 Ways to Implement the JavaScript Extends Keyword
The extends keyword is mainly used in class declarations or class expressions to create a class that is a subclass of another class....
Read more >
Extending a JavaScript Function - Corey Maynard
Wrapper/Extension Function ... // Create an anonymous function to wrap everything in. This gets called immediately // on script load. ... log("Before"); //...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found