Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] bin doesnt work when incrementing arg with equals sign #431

Closed
gonza7aav opened this issue Feb 19, 2022 · 3 comments · Fixed by #449 or #451 · May be fixed by OutSystems/cordova-plugin-add-swift-support#3
Closed

[BUG] bin doesnt work when incrementing arg with equals sign #431

gonza7aav opened this issue Feb 19, 2022 · 3 comments · Fixed by #449 or #451 · May be fixed by OutSystems/cordova-plugin-add-swift-support#3
Assignees

Comments

@gonza7aav
Copy link

What / Why

Why it unshift an empty string into argv when an argument contains a "="?

// line 35 @ /bin/semver.js
let a = argv.shift()
const indexOfEqualSign = a.indexOf('=')
if (indexOfEqualSign !== -1) {
  a = a.slice(0, indexOfEqualSign)
  argv.unshift(a.slice(indexOfEqualSign + 1))
}

Let's suppose a contains an "=", then indexOfEqualSign won't be -1. Inside the if statement, it overwrites a with a slice of it (removing whatever is after the "="). After that, it inserts into argv another slice of a. But this slice starts at indexOfEqualSign + 1 (the part we just remove), so an empty string ("") will be returned by the slice and inserted by the unshift.

As I understood, the empty string doesn't affect the execution because it is filtered out at line 92. But, Why it bother to add an "", loop with it, add it to versions to finally remove from versions? I think I am missing something. Can someone explain it to me, please?

@lukekarrys
Copy link
Member

Good catch! This is a bug:

❯ ./bin/semver.js --increment major 1.0.0
2.0.0

❯ ./bin/semver.js --increment=major 1.0.0
1.0.1

@lukekarrys lukekarrys changed the title [QUESTION] Why the main function inserts an empty string into argv? [BUG] bin doesnt work when incrementing arg with equals sign Apr 9, 2022
@ljharb
Copy link

ljharb commented Apr 9, 2022

An equals sign should be the preferred form with long options; it’d be great to audit all long options for this.

@lukekarrys
Copy link
Member

I believe this is the fix. It should be getting the value before overwriting a. @ljharb I think this should fix all the long options, and I can add a test for this.

diff --git a/bin/semver.js b/bin/semver.js
index 779b8b0..8d1b557 100755
--- a/bin/semver.js
+++ b/bin/semver.js
@@ -37,8 +37,9 @@ const main = () => {
     let a = argv.shift()
     const indexOfEqualSign = a.indexOf('=')
     if (indexOfEqualSign !== -1) {
+      const value = a.slice(indexOfEqualSign + 1)
       a = a.slice(0, indexOfEqualSign)
-      argv.unshift(a.slice(indexOfEqualSign + 1))
+      argv.unshift(value)
     }
     switch (a) {
       case '-rv': case '-rev': case '--rev': case '--reverse':

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