Skip to content

Commit

Permalink
Fixed AST locations (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Aug 3, 2020
1 parent 0c7a51d commit 3319035
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 135 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "yaml-eslint-parser",
"version": "0.0.6",
"version": "0.0.7",
"description": "A YAML parser that produces output compatible with ESLint",
"main": "lib/index.js",
"files": [
Expand Down
46 changes: 29 additions & 17 deletions src/convert.ts
Expand Up @@ -682,11 +682,6 @@ function convertBlockLiteral(
break
}
column++
if (c === "\n") {
line++
column = 0
break
}
index++
}
const punctuatorLoc: Locations = {
Expand All @@ -700,15 +695,21 @@ function convertBlockLiteral(
},
}
addToken(tokens, "Punctuator", punctuatorLoc, code)
let lineFeed = false
while (index < text.length) {
const c = text[index]
if (c.trim()) {
break
}
column++
if (c === "\n") {
if (lineFeed) {
break
}
line++
column = 0
lineFeed = true
} else {
column++
}
index++
}
Expand All @@ -722,7 +723,9 @@ function convertBlockLiteral(
end: clone(loc.loc.end),
},
}
addToken(tokens, "BlockLiteral", tokenLoc, code)
if (tokenLoc.range[0] < tokenLoc.range[1]) {
addToken(tokens, "BlockLiteral", tokenLoc, code)
}
} else {
// ??
addToken(tokens, "BlockLiteral", clone(loc), code)
Expand Down Expand Up @@ -763,11 +766,6 @@ function convertBlockFolded(
break
}
column++
if (c === "\n") {
line++
column = 0
break
}
index++
}
const punctuatorLoc: Locations = {
Expand All @@ -781,15 +779,21 @@ function convertBlockFolded(
},
}
addToken(tokens, "Punctuator", punctuatorLoc, code)
let lineFeed = false
while (index < text.length) {
const c = text[index]
if (c.trim()) {
break
}
column++
if (c === "\n") {
if (lineFeed) {
break
}
line++
column = 0
lineFeed = true
} else {
column++
}
index++
}
Expand All @@ -803,7 +807,9 @@ function convertBlockFolded(
end: clone(loc.loc.end),
},
}
addToken(tokens, "BlockFolded", tokenLoc, code)
if (tokenLoc.range[0] < tokenLoc.range[1]) {
addToken(tokens, "BlockFolded", tokenLoc, code)
}
} else {
// ??
addToken(tokens, "BlockFolded", clone(loc), code)
Expand Down Expand Up @@ -849,7 +855,9 @@ function convertAlias(
end: clone(loc.loc.end),
},
}
addToken(tokens, "Identifier", tokenLoc, code)
if (tokenLoc.range[0] < tokenLoc.range[1]) {
addToken(tokens, "Identifier", tokenLoc, code)
}
} else {
// ??
addToken(tokens, "Identifier", clone(loc), code)
Expand Down Expand Up @@ -944,7 +952,9 @@ function convertAnchor(
end: clone(loc.loc.end),
},
}
addToken(tokens, "Identifier", tokenLoc, code)
if (tokenLoc.range[0] < tokenLoc.range[1]) {
addToken(tokens, "Identifier", tokenLoc, code)
}
} else {
// ??
addToken(tokens, "Identifier", clone(loc), code)
Expand Down Expand Up @@ -990,7 +1000,9 @@ function convertTag(
end: clone(loc.loc.end),
},
}
addToken(tokens, "Identifier", tokenLoc, code)
if (tokenLoc.range[0] < tokenLoc.range[1]) {
addToken(tokens, "Identifier", tokenLoc, code)
}
} else {
// ??
addToken(tokens, "Identifier", clone(loc), code)
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/parser/yaml-test-suite/4QFQ-output.json
Expand Up @@ -219,15 +219,15 @@
},
{
"type": "BlockFolded",
"value": "# detected\n",
"value": "\n \n # detected\n",
"range": [
25,
19,
36
],
"loc": {
"start": {
"line": 6,
"column": 2
"line": 4,
"column": 1
},
"end": {
"line": 7,
Expand Down
18 changes: 0 additions & 18 deletions tests/fixtures/parser/yaml-test-suite/52DL-output.json
Expand Up @@ -115,24 +115,6 @@
}
}
},
{
"type": "Identifier",
"value": "",
"range": [
5,
5
],
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 1
}
}
},
{
"type": "Identifier",
"value": "a",
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/parser/yaml-test-suite/7T8X-output.json
Expand Up @@ -83,15 +83,15 @@
},
{
"type": "BlockFolded",
"value": "folded\n line\n\n next\n line\n * bullet\n\n * list\n * lines\n\n last\n line\n",
"value": "\n folded\n line\n\n next\n line\n * bullet\n\n * list\n * lines\n\n last\n line\n",
"range": [
4,
2,
77
],
"loc": {
"start": {
"line": 3,
"column": 1
"line": 2,
"column": 0
},
"end": {
"line": 15,
Expand Down
18 changes: 0 additions & 18 deletions tests/fixtures/parser/yaml-test-suite/8MK2-output.json
Expand Up @@ -97,24 +97,6 @@
}
}
},
{
"type": "Identifier",
"value": "",
"range": [
1,
1
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 1
}
}
},
{
"type": "Identifier",
"value": "a",
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/parser/yaml-test-suite/DWX9-output.json
Expand Up @@ -83,15 +83,15 @@
},
{
"type": "BlockLiteral",
"value": "literal\n \n \n text\n",
"value": "\n \n literal\n \n \n text\n",
"range": [
9,
3,
31
],
"loc": {
"start": {
"line": 4,
"column": 2
"line": 2,
"column": 1
},
"end": {
"line": 8,
Expand Down
42 changes: 3 additions & 39 deletions tests/fixtures/parser/yaml-test-suite/K858-output.json
Expand Up @@ -271,24 +271,6 @@
}
}
},
{
"type": "BlockFolded",
"value": "",
"range": [
10,
10
],
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
}
},
{
"type": "Identifier",
"value": "clip",
Expand Down Expand Up @@ -343,24 +325,6 @@
}
}
},
{
"type": "BlockFolded",
"value": "",
"range": [
19,
19
],
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 0
}
}
},
{
"type": "Identifier",
"value": "keep",
Expand Down Expand Up @@ -417,14 +381,14 @@
},
{
"type": "BlockLiteral",
"value": "",
"value": "\n",
"range": [
30,
29,
30
],
"loc": {
"start": {
"line": 7,
"line": 6,
"column": 0
},
"end": {
Expand Down
16 changes: 8 additions & 8 deletions tests/fixtures/parser/yaml-test-suite/R4YG-output.json
Expand Up @@ -219,15 +219,15 @@
},
{
"type": "BlockFolded",
"value": "# detected\n",
"value": "\n \n # detected\n",
"range": [
25,
19,
36
],
"loc": {
"start": {
"line": 6,
"column": 2
"line": 4,
"column": 1
},
"end": {
"line": 7,
Expand Down Expand Up @@ -327,15 +327,15 @@
},
{
"type": "BlockFolded",
"value": "detected\n",
"value": "\n detected\n",
"range": [
60,
58,
69
],
"loc": {
"start": {
"line": 11,
"column": 1
"line": 10,
"column": 2
},
"end": {
"line": 12,
Expand Down
18 changes: 0 additions & 18 deletions tests/fixtures/parser/yaml-test-suite/S4JQ-output.json
Expand Up @@ -267,24 +267,6 @@
}
}
},
{
"type": "Identifier",
"value": "",
"range": [
51,
51
],
"loc": {
"start": {
"line": 4,
"column": 3
},
"end": {
"line": 4,
"column": 3
}
}
},
{
"type": "Numeric",
"value": "12",
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/parser/yaml-test-suite/T26H-output.json
Expand Up @@ -101,15 +101,15 @@
},
{
"type": "BlockLiteral",
"value": "literal\n \n \n text\n",
"value": "\n \n literal\n \n \n text\n",
"range": [
13,
7,
35
],
"loc": {
"start": {
"line": 4,
"column": 2
"line": 2,
"column": 1
},
"end": {
"line": 8,
Expand Down

0 comments on commit 3319035

Please sign in to comment.