-
Notifications
You must be signed in to change notification settings - Fork 63
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
Fix: Indent multiline fixes (fixes #120) #124
Changes from all commits
5502a18
f308f99
0e14506
74c1473
80edd59
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 |
---|---|---|
|
@@ -554,6 +554,124 @@ describe("plugin", () => { | |
assert.strictEqual(actual, expected); | ||
}); | ||
|
||
it("multiline autofix", () => { | ||
const input = [ | ||
"This is Markdown.", | ||
"", | ||
" ```js", | ||
" console.log('Hello, \\", | ||
" world!')", | ||
" console.log('Hello, \\", | ||
" world!')", | ||
" ```" | ||
].join("\n"); | ||
const expected = [ | ||
"This is Markdown.", | ||
"", | ||
" ```js", | ||
" console.log(\"Hello, \\", | ||
" world!\")", | ||
" console.log(\"Hello, \\", | ||
" world!\")", | ||
" ```" | ||
].join("\n"); | ||
const report = cli.executeOnText(input, "test.md"); | ||
const actual = report.results[0].output; | ||
|
||
assert.strictEqual(actual, expected); | ||
}); | ||
|
||
it("underindented multiline autofix", () => { | ||
const input = [ | ||
" ```js", | ||
" console.log('Hello, world!')", | ||
" console.log('Hello, \\", | ||
" world!')", | ||
" console.log('Hello, world!')", | ||
" ```" | ||
].join("\n"); | ||
|
||
// The Markdown parser doesn't have any concept of a "negative" | ||
// indent left of the opening code fence, so autofixes move | ||
// lines that were previously underindented to the same level | ||
// as the opening code fence. | ||
const expected = [ | ||
" ```js", | ||
" console.log(\"Hello, world!\")", | ||
" console.log(\"Hello, \\", | ||
" world!\")", | ||
" console.log(\"Hello, world!\")", | ||
" ```" | ||
].join("\n"); | ||
const report = cli.executeOnText(input, "test.md"); | ||
const actual = report.results[0].output; | ||
|
||
assert.strictEqual(actual, expected); | ||
}); | ||
|
||
it("multiline autofix in blockquote", () => { | ||
const input = [ | ||
"This is Markdown.", | ||
"", | ||
"> ```js", | ||
"> console.log('Hello, \\", | ||
"> world!')", | ||
"> console.log('Hello, \\", | ||
"> world!')", | ||
"> ```" | ||
].join("\n"); | ||
const expected = [ | ||
"This is Markdown.", | ||
"", | ||
"> ```js", | ||
"> console.log(\"Hello, \\", | ||
"> world!\")", | ||
"> console.log(\"Hello, \\", | ||
"> world!\")", | ||
"> ```" | ||
].join("\n"); | ||
const report = cli.executeOnText(input, "test.md"); | ||
const actual = report.results[0].output; | ||
|
||
assert.strictEqual(actual, expected); | ||
}); | ||
|
||
it("multiline autofix in nested blockquote", () => { | ||
const input = [ | ||
"This is Markdown.", | ||
"", | ||
"> This is a nested blockquote.", | ||
">", | ||
"> > ```js", | ||
"> > console.log('Hello, \\", | ||
"> > world!')", | ||
"> > console.log('Hello, \\", | ||
"> > world!')", | ||
"> > ```" | ||
].join("\n"); | ||
|
||
// The Markdown parser doesn't have any concept of a "negative" | ||
// indent left of the opening code fence, so autofixes move | ||
// lines that were previously underindented to the same level | ||
// as the opening code fence. | ||
const expected = [ | ||
"This is Markdown.", | ||
"", | ||
"> This is a nested blockquote.", | ||
">", | ||
"> > ```js", | ||
"> > console.log(\"Hello, \\", | ||
"> > world!\")", | ||
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. Same thing here? Is this 1–2 too many spaces? 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. Yep, same cause. I added comments in 80edd59 and explained further in #124 (comment). 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 checked this case in https://spec.commonmark.org/dingus/ and the actual and expected code snippets produce the same rendered JS, with the same amount of spaces between "Hello," and "world!". |
||
"> > console.log(\"Hello, \\", | ||
"> > world!\")", | ||
"> > ```" | ||
].join("\n"); | ||
const report = cli.executeOnText(input, "test.md"); | ||
const actual = report.results[0].output; | ||
|
||
assert.strictEqual(actual, expected); | ||
}); | ||
|
||
it("by one space with comments", () => { | ||
const input = [ | ||
"This is Markdown.", | ||
|
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.
I’m not 100% sure how leading whitespace works in weird code blocks like this, but didn’t we cause one more space to appear in the JS string here?
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.
I just pushed 80edd59 that adds comments explaining why this occurs for the benefit of future readers of these tests.
When a line inside a fenced code block is indented less than the opening fence, Markdown treats it as having 0 indent, even though its indent in the source is negative. The parser doesn't provide any information to distinguish lines that begin at the same level as the opening fence from those that are underindented. Whenever one of those lines gets autofixed, it gets shifted to align with the opening fence. Lines that that have positive indentation inside the code fence retain their original indentation because those are representable.
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.
I checked this case in https://spec.commonmark.org/dingus/ and the actual and expected code snippets produce the same rendered JS, with the same amount of spaces between "Hello," and "world!".