-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add --range-start
and --range-end
options to format only parts of the input
#1609
Changes from 9 commits
62bf068
5c97525
564bf13
c09e19e
3af3f01
8c99117
44bfd9b
91372be
c1a61eb
1e60895
2ca9f18
bec69d0
070b941
e1c993e
77a8fb0
ef3b992
ee94a37
7823f75
990a18c
db0ec7a
149e18c
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 |
---|---|---|
|
@@ -49,18 +49,22 @@ function shouldPrintComma(options, level) { | |
} | ||
} | ||
|
||
function hasPrettierIgnoreComment(node) { | ||
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. You don't need to extract it anymore right? 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. Right, fixed in 1e60895 |
||
return ( | ||
node && | ||
node.comments && | ||
node.comments.length > 0 && | ||
node.comments.some(comment => comment.value.trim() === "prettier-ignore") | ||
); | ||
} | ||
|
||
function genericPrint(path, options, printPath, args) { | ||
assert.ok(path instanceof FastPath); | ||
|
||
var node = path.getValue(); | ||
|
||
// Escape hatch | ||
if ( | ||
node && | ||
node.comments && | ||
node.comments.length > 0 && | ||
node.comments.some(comment => comment.value.trim() === "prettier-ignore") | ||
) { | ||
if (hasPrettierIgnoreComment(node)) { | ||
return options.originalText.slice(util.locStart(node), util.locEnd(node)); | ||
} | ||
|
||
|
@@ -4244,7 +4248,9 @@ function removeLines(doc) { | |
}); | ||
} | ||
|
||
function printAstToDoc(ast, options) { | ||
function printAstToDoc(ast, options, addIndents) { | ||
addIndents = addIndents || 0; | ||
|
||
function printGenerically(path, args) { | ||
return comments.printComments( | ||
path, | ||
|
@@ -4256,7 +4262,19 @@ function printAstToDoc(ast, options) { | |
|
||
const doc = printGenerically(FastPath.from(ast)); | ||
docUtils.propagateBreaks(doc); | ||
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 should likely be done after you add hardline as hardline intro a break. This isn't going to do anything wrong here but it would be better to do the "right" thing as if we're adding some more code that relies on this, it's going to break in unexpected ways. 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. Oh, I hadn't considered that (I didn't really think about what |
||
if (addIndents > 0) { | ||
// Add a hardline to make the indents take effect | ||
// It should be removed in index.js format() | ||
return addIndentsToDoc(concat([hardline, doc]), addIndents); | ||
} | ||
return doc; | ||
} | ||
|
||
function addIndentsToDoc(doc, numIndents) { | ||
if (numIndents <= 0) { | ||
return doc; | ||
} | ||
return addIndentsToDoc(indent(doc), numIndents - 1); | ||
} | ||
|
||
module.exports = { printAstToDoc }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`range.js 1`] = ` | ||
function ugly ( {a=1, b = 2 } ) { | ||
function ugly ( {a=1, b = 2 } ) { | ||
function ugly ( {a=1, b = 2 } ) { | ||
\`multiline template string | ||
with too much indentation\` | ||
// The [165, 246) range selects the above two lines, including the second newline | ||
} | ||
} | ||
} | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
function ugly ( {a=1, b = 2 } ) { | ||
function ugly ( {a=1, b = 2 } ) { | ||
function ugly ( {a=1, b = 2 } ) { | ||
\`multiline template string | ||
with too much indentation\`; | ||
// The [165, 246) range selects the above two lines, including the second newline | ||
} | ||
} | ||
} | ||
|
||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
run_spec(__dirname, { rangeStart: 165, rangeEnd: 246 }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function ugly ( {a=1, b = 2 } ) { | ||
function ugly ( {a=1, b = 2 } ) { | ||
function ugly ( {a=1, b = 2 } ) { | ||
`multiline template string | ||
with too much indentation` | ||
// The [165, 246) range selects the above two lines, including the second newline | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you look at template literal code, I have some piece of code that is much more precise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh, that is quite a lot better! Fixed in e1c993e and 77a8fb0