Skip to content

Commit

Permalink
Fix #66 Change formatter to not create unnecessary edits
Browse files Browse the repository at this point in the history
The formatter should not create edits on lines that are already indented
correctly. For example, if we expect a tab on a line and there is
already a tab then no edits should be created even if the creation of
such an edit is essentially a no-op.

Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Dec 25, 2019
1 parent 11ef5d2 commit e3edce2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
### Fixed
- do not append newline when printing out the output of `dockerfile-utils format` to the console ([#63](https://github.com/rcjsuen/dockerfile-utils/issues/63))
- allow paths to be quoted in WORKDIRs ([#67](https://github.com/rcjsuen/dockerfile-utils/issues/67))
- do not calculate edits for lines that are already formatted correctly ([#66](https://github.com/rcjsuen/dockerfile-utils/issues/66))

## [0.0.13] - 2019-05-22
### Added
Expand Down
14 changes: 11 additions & 3 deletions src/dockerFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,17 @@ export class DockerFormatter {
// process the next line
continue lineCheck;
default:
// non-whitespace encountered
if (i !== startOffset || indentedLines[line]) {
let edit = this.createFormattingEdit(document, startOffset, i, indentedLines[line], indentation);
// found a line that should be indented
if (indentedLines[line]) {
const originalIndentation = document.getText().substring(startOffset, i);
// change the indentation if it's not what we expect
if (originalIndentation !== indentation) {
const edit = this.createFormattingEdit(document, startOffset, i, indentedLines[line], indentation);
edits.push(edit);
}
} else if (i !== startOffset) {
// non-whitespace character encountered, realign
const edit = this.createFormattingEdit(document, startOffset, i, indentedLines[line], indentation);
edits.push(edit);
}
// process the next line
Expand Down
6 changes: 6 additions & 0 deletions test/dockerFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ describe("Dockerfile formatter", function() {
});

describe("newline escaped tabbing", function() {
it("correctly formatted", function() {
let document = createDocument("EXPOSE 8081\\\n\t8082");
let edits = formatDocument(document);
assert.equal(edits.length, 0);
});

it("single newline", function() {
let document = createDocument("EXPOSE 8081\\\n8082");
let edits = formatDocument(document);
Expand Down

0 comments on commit e3edce2

Please sign in to comment.