Skip to content

doc: add esm examples to node:repl - #55432

Merged
nodejs-github-bot merged 2 commits into
nodejs:mainfrom
mfdebian:docs-repl-esm
Dec 14, 2024
Merged

nodejs-github-bot merged 2 commits into
nodejs:mainfrom
mfdebian:docs-repl-esm

Conversation

@mfdebian

Copy link
Copy Markdown
Contributor

This PR adds the ESM counterparts of the CJS examples for the REPL documentation.

I've tested every single example and they all work/behave as expected.

Best regards!

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to Node.js documentation. repl Issues and PRs related to the REPL subsystem. labels Oct 17, 2024
Comment thread doc/api/repl.md
Comment on lines +309 to +320
```mjs
import repl from 'node:repl';
import { Translator } from 'translator';

const myTranslator = new Translator('en', 'fr');

function myEval(cmd, context, filename, callback) {
callback(null, myTranslator.translate(cmd));
}

repl.start({ prompt: '> ', eval: myEval });
```

ghost Oct 17, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code block adds a mock dependency to a module that actually exists and is very dormant (translator). IMO we should try to avoid that and the alternative of using a module that doesn't exist.

While this is also true of the previously existing example, since it's being updated now with a totally new example I think it's a good time to adjust it.

ghost Oct 17, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! I also thought about that example since it throws an error when you try to execute it as is and new users can be frustrated by that, I'll come up with a good alternative and update as soon as I can! Thanks for the feedback! 🙌

ghost Oct 17, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the example, tell me what you think! 😊 And again, thank you for taking the time to provide feedback! 🙏

Comment thread doc/api/repl.md
Comment on lines +486 to +497
```mjs
import repl from 'node:repl';

function initializeContext(context) {
context.m = 'test';
}

const r = repl.start({ prompt: '> ' });
initializeContext(r.context);

r.on('reset', initializeContext);
```

ghost Oct 17, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this one is working as it's described? Maybe I'm misunderstanding what's supposed to happen here?
image

ghost Oct 17, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It resets the context, m equals to 'test', but if you were to do something like m = 'other thing' and then you call .clear it will reset the context, so when you print the value of m it would be reset back to 'test' 😊

full example:

$ ./node index.js
> m
'test'
> m = 1
1
> m
1
> .clear
Clearing context...
> m
'test'
> 

Comment thread doc/api/repl.md
help: 'Say hello',
action(name) {
this.clearBufferedCommand();
console.log(`Hello, ${name}!`);

ghost Oct 17, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this output doesn't quite make sense - where would ${name} ever come from?

ghost Oct 17, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's passed when you execute the .sayhello command, like so:

> .sayhello bnb
Hello, bnb!

ghost left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all really good additions! I'd like to see the comments I left addressed - specifically, for ${name} and the r.on('reset', initializeContext); example, I'd like to make sure we improve them to be fully understandable/usable examples, if possible.

@mfdebian

ghost commented Dec 5, 2024

Copy link
Copy Markdown
Contributor Author

hello @bnb I've made the changes you requested, can I kindly ask you to review once more and if everything looks good to you you can approve the PR? 😊 if not, let me know what other changes you'd like me to make!

I generally try to not change everything too much just so there's a lot of consistency between the CJS and ESM examples for new users but let me know your thoughts!

Thanks in advance! 🙌

@lpinca lpinca added commit-queue PRs queued for automated landing through the Commit Queue. commit-queue-squash PRs the Commit Queue should land as one squashed commit. labels Dec 14, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Dec 14, 2024
@nodejs-github-bot
nodejs-github-bot merged commit 2d4e35c into nodejs:main Dec 14, 2024
@nodejs-github-bot

ghost commented Dec 14, 2024

Copy link
Copy Markdown
Collaborator

Landed in 2d4e35c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit-queue-squash PRs the Commit Queue should land as one squashed commit. doc Issues and PRs related to Node.js documentation. repl Issues and PRs related to the REPL subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants