-
Notifications
You must be signed in to change notification settings - Fork 72
Improve NpmSpec parsing #116
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
f4ce03c
7b00926
77ef19a
2fc126d
4f72d2f
3afef0d
912cc8a
2e21f5a
12d67c8
34320c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1230,15 +1230,21 @@ class Parser: | |
|
|
||
| NUMBER = r'x|X|\*|0|[1-9][0-9]*' | ||
| PART = r'[a-zA-Z0-9.-]*' | ||
| OP = r'<|<=|=|>=|>|\^|~|~>' | ||
| NPM_SPEC_BLOCK = re.compile(r""" | ||
| ^(?:v)? # Strip optional initial v | ||
| (?P<op><|<=|>=|>|=|\^|~|) # Operator, can be empty | ||
| (?P<op>{op}|) # Operator, can be empty | ||
| (?P<major>{nb})(?:\.(?P<minor>{nb})(?:\.(?P<patch>{nb}))?)? | ||
| (?:-(?P<prerel>{part}))? # Optional re-release | ||
| (?:\+(?P<build>{part}))? # Optional build | ||
| $""".format(nb=NUMBER, part=PART), | ||
| $""".format(nb=NUMBER, part=PART, op=OP), | ||
| re.VERBOSE, | ||
| ) | ||
| OP_RE = re.compile(r""" | ||
| ^(?:{op})$ # A standalone operator, cannot be empty | ||
| """.format(op=OP), | ||
| re.VERBOSE, | ||
| ) | ||
|
|
||
| @classmethod | ||
| def range(cls, operator, target): | ||
|
|
@@ -1256,15 +1262,21 @@ def parse(cls, expression): | |
| subclauses = [] | ||
| if cls.HYPHEN in group: | ||
| low, high = group.split(cls.HYPHEN, 2) | ||
| subclauses = cls.parse_simple('>=' + low) + cls.parse_simple('<=' + high) | ||
| subclauses = cls.parse_simple('>=' + low.strip()) + cls.parse_simple('<=' + high.strip()) | ||
|
|
||
| else: | ||
| blocks = group.split(' ') | ||
| blocks = group.split() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This now splits across multiple whitespace, but also allows for newlines (so it would allow
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO this is not worth
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume you're referring to "not worth raising an error for newlines", as opposed to allowing multiple spaces between the subclauses?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Correct. How many times are there new lines across all the package.json anyway?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I wouldn't expect it as an issue, just a slight nitpick of my own code where it's different to the current npm implementation. |
||
| maybe_prepend_op = None | ||
| for block in blocks: | ||
| if not cls.NPM_SPEC_BLOCK.match(block): | ||
| block = maybe_prepend_op + block if maybe_prepend_op else block | ||
| if cls.NPM_SPEC_BLOCK.match(block): | ||
| subclauses.extend(cls.parse_simple(block)) | ||
| maybe_prepend_op = None | ||
| elif maybe_prepend_op is None and cls.OP_RE.match(block): | ||
| maybe_prepend_op = block | ||
| else: | ||
| raise ValueError("Invalid NPM block in %r: %r" % (expression, block)) | ||
|
|
||
| subclauses.extend(cls.parse_simple(block)) | ||
|
|
||
| prerelease_clauses = [] | ||
| non_prerel_clauses = [] | ||
|
|
@@ -1314,6 +1326,7 @@ def parse(cls, expression): | |
|
|
||
| PREFIX_ALIASES = { | ||
| '': PREFIX_EQ, | ||
| '~>': PREFIX_TILDE, | ||
| } | ||
|
|
||
| PREFIX_TO_OPERATOR = { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.