Skip to content

Commit

Permalink
Add insertSpaceAfterConditionalCompileSymbol, fix conditional compi…
Browse files Browse the repository at this point in the history
…le formatting (#87)

* Fix formatting for conditional compile with spaces after #

* brighterscript@0.65.25

* Fix code coverage
  • Loading branch information
TwitchBronBron authored Mar 7, 2024
1 parent ef92eec commit ac73213
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 48 deletions.
95 changes: 53 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"brighterscript-formatter": "dist/cli.js"
},
"dependencies": {
"brighterscript": "^0.65.23",
"brighterscript": "^0.65.25",
"glob-all": "^3.3.0",
"jsonc-parser": "^3.0.0",
"source-map": "^0.7.3",
Expand Down
123 changes: 123 additions & 0 deletions src/Formatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ describe('Formatter', () => {
});

describe('formatIndent', () => {
it('formats conditional compile items with spaces around the keywords', () => {
expect(formatter.format(undent`
sub Main(inputArguments as object)
#if true
print" one"
#else if true
print "two"
#else
print "three"
#end if
print "done"
end sub
`)).to.equal(undent`
sub Main(inputArguments as object)
#if true
print" one"
#else if true
print "two"
#else
print "three"
#end if
print "done"
end sub
`);
});

it('formats with optional chaining operators', () => {
formatEqualTrim(`
sub setPoster()
Expand Down Expand Up @@ -655,6 +681,103 @@ end sub`;
});
});

it('removes whitespace between conditional compile symbol and keyword', () => {
expect(formatter.format(undent`
sub Main(inputArguments as object)
#\t const SOME_CONST = true
#\t if true
print" one"
#\t else if true
print "two"
#\t else
print "three"
#\t error
#\t error message 123
#\t end if
print "done"
end sub
`, { insertSpaceAfterConditionalCompileSymbol: false })).to.equal(undent`
sub Main(inputArguments as object)
#const SOME_CONST = true
#if true
print" one"
#else if true
print "two"
#else
print "three"
#error
#error message 123
#end if
print "done"
end sub
`);

});

it('reduces to single space between conditional compile symbol and keyword', () => {
expect(formatter.format(undent`
sub Main(inputArguments as object)
#\t const SOME_CONST = true
#\t if true
print" one"
#\t else if true
print "two"
#\t else
print "three"
#\t error
#\t error message 123
#\t end if
print "done"
end sub
`, { insertSpaceAfterConditionalCompileSymbol: true })).to.equal(undent`
sub Main(inputArguments as object)
# const SOME_CONST = true
# if true
print" one"
# else if true
print "two"
# else
print "three"
# error
# error message 123
# end if
print "done"
end sub
`);
});

it('adds single space between conditional compile symbol and keyword', () => {
expect(formatter.format(undent`
sub Main(inputArguments as object)
#const SOME_CONST = true
#if true
print" one"
#else if true
print "two"
#else
print "three"
#error
#error message 123
#end if
print "done"
end sub
`, { insertSpaceAfterConditionalCompileSymbol: true })).to.equal(undent`
sub Main(inputArguments as object)
# const SOME_CONST = true
# if true
print" one"
# else if true
print "two"
# else
print "three"
# error
# error message 123
# end if
print "done"
end sub
`);
});

it('removes space between empty parens', () => {
formatEqual(`main( )`, `main()`);
formatEqual(`main()`, `main()`);
Expand Down
11 changes: 10 additions & 1 deletion src/FormattingOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ export interface FormattingOptions {
*/
typeCaseOverride?: Record<string, FormattingOptions['keywordCase']>;
/**
* If true (the default), all whitespace between items is reduced to exactly 1 space character,
* If true (the default), all whitespace between items are reduced to exactly 1 space character,
* and certain keywords and operators are padded with whitespace (i.e. `1+1` becomes `1 + 1`).
* This is a catchall property that will also disable the following rules:
* - insertSpaceBeforeFunctionParenthesis
* - insertSpaceBetweenEmptyCurlyBraces
* - insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces
* - insertSpaceAfterConditionalCompileSymbol
* - insertSpaceBetweenAssociativeArrayLiteralKeyAndColon
*/
formatInteriorWhitespace?: boolean;
/**
Expand All @@ -72,6 +74,12 @@ export interface FormattingOptions {
* @default false
*/
insertSpaceBetweenEmptyCurlyBraces?: boolean;
/**
* if true, conditional compile symbols will contain exactly 1 whitespace char (i.e. `# if true`)
* if false, ensure there is no whitespace between the `#` and the keyword (i.e. `#if true`)
* @default false
*/
insertSpaceAfterConditionalCompileSymbol?: boolean;
/**
* If true, ensure exactly 1 space after leading and before trailing curly braces
* If false, REMOVE all whitespace after leading and before trailing curly braces (excluding beginning-of-line indentation spacing)
Expand Down Expand Up @@ -115,6 +123,7 @@ export function normalizeOptions(options: FormattingOptions) {
formatInteriorWhitespace: true,
insertSpaceBeforeFunctionParenthesis: false,
insertSpaceBetweenEmptyCurlyBraces: false,
insertSpaceAfterConditionalCompileSymbol: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
insertSpaceBetweenAssociativeArrayLiteralKeyAndColon: false,
formatMultiLineObjectsAndArrays: true,
Expand Down
Loading

0 comments on commit ac73213

Please sign in to comment.