Skip to content

Commit cbc1929

Browse files
committed
Support flow/typescript parsers with --cursor-offset
See #1637 (comment) For example: prettier tests/flow/any/any.js --cursor-offset 40 --parser flow # prints 44
1 parent f2e0c18 commit cbc1929

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

index.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
const traverse = require('babel-traverse').default;
4+
const estraverse = require('estraverse');
45
const util = require("./src/util");
56
const cursor = require("./src/doc-builders").cursor;
67
const comments = require("./src/comments");
@@ -51,23 +52,40 @@ function ensureAllCommentsPrinted(astComments) {
5152
});
5253
}
5354

54-
function findCursorNode(ast, cursorOffset) {
55+
function findCursorNode(ast, opts) {
56+
const cursorOffset = opts.cursorOffset;
5557
let cursorNode;
56-
traverse(ast, {
57-
enter: function(path) {
58-
const node = path.node;
59-
const start = util.locStart(node);
60-
const end = util.locEnd(node);
61-
if (start <= cursorOffset && cursorOffset <= end) {
62-
cursorNode = node;
63-
} else {
64-
return path.skip();
58+
if (opts.parser === 'babylon') {
59+
traverse(ast, {
60+
enter: function(path) {
61+
const node = path.node;
62+
if (nodeContainsOffset(node, cursorOffset)) {
63+
cursorNode = node;
64+
} else {
65+
return path.skip();
66+
}
6567
}
66-
}
67-
});
68+
});
69+
} else {
70+
estraverse.traverse(ast, {
71+
enter: function(node) {
72+
if (nodeContainsOffset(node, cursorOffset)) {
73+
cursorNode = node;
74+
} else {
75+
return this.skip();
76+
}
77+
}
78+
});
79+
}
6880
return cursorNode;
6981
}
7082

83+
function nodeContainsOffset(node, offset) {
84+
const start = util.locStart(node);
85+
const end = util.locEnd(node);
86+
return start <= offset && offset <= end;
87+
}
88+
7189
function formatWithCursor(text, opts) {
7290
let prefix = "";
7391
if (text.startsWith("#!")) {
@@ -84,7 +102,7 @@ function formatWithCursor(text, opts) {
84102
const astComments = attachComments(text, ast, opts);
85103
let cursorOffset;
86104
if (opts.cursorOffset >= 0) {
87-
const cursorNode = findCursorNode(ast, opts.cursorOffset);
105+
const cursorNode = findCursorNode(ast, opts);
88106
if (cursorNode) {
89107
cursorOffset = opts.cursorOffset - util.locStart(cursorNode);
90108
opts.cursorNode = cursorNode;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"babel-traverse": "^6.24.1",
1616
"babylon": "7.0.0-beta.8",
1717
"chalk": "1.1.3",
18+
"estraverse": "^4.2.0",
1819
"esutils": "2.0.2",
1920
"flow-parser": "0.45.0",
2021
"get-stdin": "5.0.1",

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,10 @@ estraverse@^1.9.1:
663663
version "1.9.3"
664664
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-1.9.3.tgz#af67f2dc922582415950926091a4005d29c9bb44"
665665

666+
estraverse@^4.2.0:
667+
version "4.2.0"
668+
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.2.0.tgz#0dee3fed31fcd469618ce7342099fc1afa0bdb13"
669+
666670
estree-walker@^0.2.1:
667671
version "0.2.1"
668672
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.2.1.tgz#bdafe8095383d8414d5dc2ecf4c9173b6db9412e"

0 commit comments

Comments
 (0)