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.

Derive name for `const` and `forEach` from expression

See original GitHub issue

Hey! I have awesome idea to derive variable names for builtin const and forEach snippets.

const

Improve variable name for const, let (and var?) snippets.

I simply use regexs to derive const name from expression.

We can use the part after get|read|create|retrieve|select|modify|update|use

Some examples:

someObject.getUser().const         ; const user = someObject.getUser()
// react or vue
useRouter().const                  ; const router = useRouter()
fs.readFile().const                ; const file = fs.readFile()
await updateRect(...anyArgs).const ; const rect = await updateRect(...anyArgs)

UPDATE: special handling of expression starting with new:

  • just lowercase variable (if class starts with uppercase)
const mutationObserver = new MutationObserver().const

forEach

Also for forof snippet.

Just use singular form of last part of expression (if appliable).

  1. Skip if identifier doesn’t end with s or sList (or just List?)
  2. Remove List part if appliable
  3. Use pluralize with count 1 to get singular form

Example:

cookies.forEach(cookie => )

I’m using it for a while and can say its pretty neat.

Anyway I think it’s much better than just name or item variables that should be renamed in 99% cases.

What do you think? Should we make this behaviour disableable?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
zardoycommented, Sep 24, 2022

Some of the postfixes could probably be implemented this way

You’re right! I’d make it work in the web, reduce ext size (and probably parsing time, as it’d reuse sourcefiles). I think I’m going to give more details in https://github.com/ipatalas/vscode-postfix-ts/issues/13 within a few days.

it’s more effort to install a TS plugin than VSCode extension (which you do only once, not per workspace). Do you have this plugin released officially or still WIP?

Of course! You can look at repository URL. I’m using it on daily bases (also that’s why I don’t use webstorm anymore).

Actually I don’t have plugin published, because for now it’s not possible to install it in some workspace, because it relies on the extension itself. This is primarily a VSCode extension, that as you said you install only once.

1reaction
zardoycommented, Sep 24, 2022

No, its just disabled by default: "editor.suggest.preview": true

Read more comments on GitHub >

github_iconTop Results From Across the Web

constant declarations with forEach loop - javascript
Why is my hard drive still working long after I finished the file transfer? What is the meaning of the equestrian term “Undermounted”?...
Read more >
for...of - JavaScript | MDN - MDN Web Docs
Receives a value from the sequence on each iteration. May be either a declaration with const , let , or var , or...
Read more >
Guide to JavaScript's forEach() Method - Stack Abuse
In this guide, learn everything you need to know about JavaScript's forEach() Method - loop through array, set or map, sum elements or...
Read more >
Which is faster: for, for…of, or forEach loops in JavaScript
The JavaScript for loop iterates the elements for the fixed number of times. It should be used if the number of iteration is...
Read more >
for..in versus for..of Loops - bitsofcode
The for..in method provides us the most straightforward way to loop over Object keys and values, since Objects do not have access to...
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