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

Fix NPM loose comparisons #36

Merged
merged 4 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.idea
.classpath
.project
.settings
.vscode
*.iml
target
16 changes: 9 additions & 7 deletions src/main/java/com/vdurmont/semver4j/Requirement.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,22 +280,24 @@ private static List<Tokenizer.Token> toReversePolishNotation(List<Tokenizer.Toke
Tokenizer.Token token = tokens.get(i);
switch (token.type) {
case VERSION:
queue.add(token);
queue.push(token);
break;
case CLOSING:
while (stack.peek().type != Tokenizer.TokenType.OPENING) {
queue.add(stack.pop());
queue.push(stack.pop());
}
stack.pop();
if (stack.size() > 0 && stack.peek().type.isUnary()) {
queue.add(stack.pop());
queue.push(stack.pop());
}
break;
default:
if (token.type.isUnary()) {
queue.push(token);
// Push the operand first
i++;
queue.push(tokens.get(i));
// Then the operator
queue.push(token);
} else {
stack.push(token);
}
Expand All @@ -304,10 +306,9 @@ private static List<Tokenizer.Token> toReversePolishNotation(List<Tokenizer.Toke
}

while (!stack.isEmpty()) {
queue.add(stack.pop());
queue.push(stack.pop());
}

Collections.reverse(queue);
return queue;
}

Expand Down Expand Up @@ -366,8 +367,9 @@ private static Requirement evaluateReversePolishNotation(Iterator<Tokenizer.Toke
Range range = new Range(token2.value, rangeOp);
return new Requirement(range, null, null, null);
} else {
Requirement req1 = evaluateReversePolishNotation(iterator, type);
// They don't call it "reverse" for nothing
Requirement req2 = evaluateReversePolishNotation(iterator, type);
Requirement req1 = evaluateReversePolishNotation(iterator, type);

RequirementOperator requirementOp;
switch (token.type) {
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/vdurmont/semver4j/Semver.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,14 @@ public boolean isGreaterThan(Semver version) {
if (this.getMajor() > version.getMajor()) return true;
else if (this.getMajor() < version.getMajor()) return false;

if (this.type == SemverType.NPM && version.getMinor() == null) return false;

int otherMinor = version.getMinor() != null ? version.getMinor() : 0;
if (this.getMinor() != null && this.getMinor() > otherMinor) return true;
else if (this.getMinor() != null && this.getMinor() < otherMinor) return false;

if (this.type == SemverType.NPM && version.getPatch() == null) return false;

int otherPatch = version.getPatch() != null ? version.getPatch() : 0;
if (this.getPatch() != null && this.getPatch() > otherPatch) return true;
else if (this.getPatch() != null && this.getPatch() < otherPatch) return false;
Expand Down Expand Up @@ -350,6 +354,12 @@ public boolean isEqualTo(String version) {
* @return true if the current version equals the provided version
*/
public boolean isEqualTo(Semver version) {
if (this.type == SemverType.NPM) {
if (this.getMajor() != version.getMajor()) return false;
if (version.getMinor() == null) return true;
if (version.getPatch() == null) return true;
}

return this.equals(version);
}

Expand Down Expand Up @@ -456,11 +466,11 @@ public Semver withClearedBuild() {
public Semver withClearedSuffixAndBuild() {
return with(this.major, this.minor, this.patch, false, false);
}

public Semver withSuffix(String suffix) {
return with(this.major, this.minor, this.patch, suffix.split("\\."), this.build);
}

public Semver withBuild(String build) {
return with(this.major, this.minor, this.patch, this.suffixTokens, build);
}
Expand All @@ -484,7 +494,7 @@ private Semver with(int major, Integer minor, Integer patch, boolean suffix, boo
String[] suffixTokens = suffix ? this.suffixTokens : null;
return Semver.create(this.type, major, minor, patch, suffixTokens, buildStr);
}

private Semver with(int major, Integer minor, Integer patch, String[] suffixTokens, String build) {
minor = this.minor != null ? minor : null;
patch = this.patch != null ? patch : null;
Expand Down Expand Up @@ -524,7 +534,6 @@ private static Semver create(SemverType type, int major, Integer minor, Integer
if (!(o instanceof Semver)) return false;
Semver version = (Semver) o;
return value.equals(version.value);

}

@Override public int hashCode() {
Expand Down
Loading