Skip to content

Commit

Permalink
fix: wrong float value for unsigned exponential in YAML 1.1 (#138)
Browse files Browse the repository at this point in the history
* fix: wrong float value for unsigned exponential in YAML 1.1

* Create tender-pans-own.md
  • Loading branch information
ota-meshi committed May 7, 2023
1 parent 455062e commit 4c7bac1
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-pans-own.md
@@ -0,0 +1,5 @@
---
"yaml-eslint-parser": patch
---

fix: wrong float value for unsigned exponential in YAML 1.1
6 changes: 3 additions & 3 deletions src/tags/tags1.1.ts
Expand Up @@ -90,9 +90,9 @@ export const FLOAT: TagResolver<number> = {
testString(str) {
// see https://yaml.org/type/float.html
return (
/^[+-]?(?:\d[\d_]*)?\.[\d_]*(?:[Ee][+-]\d+)?$/u.test(str) ||
/^[+-]?(?:\d[\d_]*)?\.[\d_]*(?:[Ee][+-]?\d+)?$/u.test(str) ||
// The previous regexp cannot handle "e" without dot. spec bug?
/^[+-]?(?:\d[\d_]*)?(?:[Ee][+-]\d+)?$/u.test(str)
/^[+-]?(?:\d[\d_]*)?(?:[Ee][+-]?\d+)?$/u.test(str)
);
},
resolveString(str) {
Expand Down Expand Up @@ -143,7 +143,7 @@ export const tagNodeResolvers = [OMAP, SET];
* Resolve int value
*/
function resolveInt(value: string, skip: number, radix: number) {
if ((skip > 0 && value.startsWith("-")) || value.startsWith("+")) {
if (value.startsWith("-") || value.startsWith("+")) {
return parseInt(value[0] + value.slice(skip + 1).replace(/_/gu, ""), radix);
}
return parseInt(value.slice(skip).replace(/_/gu, ""), radix);
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/parser/ast/yaml11-float-input.yaml
@@ -0,0 +1,4 @@
- 1.2
- 1.2e3
- 1.2e+3
- 1.2e-3
290 changes: 290 additions & 0 deletions tests/fixtures/parser/ast/yaml11-float-output.json
@@ -0,0 +1,290 @@
{
"type": "Program",
"body": [
{
"type": "YAMLDocument",
"directives": [],
"content": {
"type": "YAMLSequence",
"style": "block",
"entries": [
{
"type": "YAMLScalar",
"style": "plain",
"strValue": "1.2",
"value": 1.2,
"raw": "1.2",
"range": [
2,
5
],
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 5
}
}
},
{
"type": "YAMLScalar",
"style": "plain",
"strValue": "1.2e3",
"value": 1200,
"raw": "1.2e3",
"range": [
8,
13
],
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 7
}
}
},
{
"type": "YAMLScalar",
"style": "plain",
"strValue": "1.2e+3",
"value": 1200,
"raw": "1.2e+3",
"range": [
16,
22
],
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 8
}
}
},
{
"type": "YAMLScalar",
"style": "plain",
"strValue": "1.2e-3",
"value": 0.0012,
"raw": "1.2e-3",
"range": [
25,
31
],
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 8
}
}
}
],
"range": [
0,
31
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 8
}
}
},
"version": "1.2",
"range": [
0,
32
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 0
}
}
}
],
"comments": [],
"sourceType": "module",
"tokens": [
{
"type": "Punctuator",
"value": "-",
"range": [
0,
1
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
}
},
{
"type": "Numeric",
"value": "1.2",
"range": [
2,
5
],
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 5
}
}
},
{
"type": "Punctuator",
"value": "-",
"range": [
6,
7
],
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 1
}
}
},
{
"type": "Numeric",
"value": "1.2e3",
"range": [
8,
13
],
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 7
}
}
},
{
"type": "Punctuator",
"value": "-",
"range": [
14,
15
],
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
}
},
{
"type": "Numeric",
"value": "1.2e+3",
"range": [
16,
22
],
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 8
}
}
},
{
"type": "Punctuator",
"value": "-",
"range": [
23,
24
],
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
}
},
{
"type": "Numeric",
"value": "1.2e-3",
"range": [
25,
31
],
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 8
}
}
}
],
"range": [
0,
32
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 0
}
}
}
6 changes: 6 additions & 0 deletions tests/fixtures/parser/ast/yaml11-float-value.json
@@ -0,0 +1,6 @@
[
1.2,
1200,
1200,
0.0012
]

0 comments on commit 4c7bac1

Please sign in to comment.