Skip to content

Commit

Permalink
Let users use or discard explicit types (#744)
Browse files Browse the repository at this point in the history
* Add a useExplicitTypes configuration option

* Prevent writing the 'as type' when useExplicitType is false

* Add documentation for the useExplicitTypes config

* Rename option to removeParameterTypes

Co-authored-by: Bronley Plumb <bronley@gmail.com>
  • Loading branch information
xgouchet and TwitchBronBron authored Dec 2, 2022
1 parent 29628da commit 28d41fb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ Type: `boolean`
Prevent the staging folder from being deleted after creating the package. Defaults to `false`, meaning that the folder is deleted every time.
#### `removeParameterTypes`
Type: `boolean`
If true, removes the explicit type to function's parameters and return (i.e. the `as type` syntax); otherwise keep this information.

#### `files`

Type:
Expand Down
5 changes: 5 additions & 0 deletions bsconfig.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@
"type": "boolean",
"default": false
},
"removeParameterTypes": {
"description": "Removes the explicit type to function's parameters and return (i.e. the `as type` syntax) ",
"type": "boolean",
"default": false
},
"diagnosticFilters": {
"description": "A collection of filters used to hide diagnostics for certain files",
"type": "array",
Expand Down
6 changes: 6 additions & 0 deletions src/BsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ export interface BsConfig {
*/
emitDefinitions?: boolean;

/**
* If true, removes the explicit type to function's parameters and return (i.e. the `as type` syntax); otherwise keep this information.
* @default false
*/
removeParameterTypes?: boolean;

/**
* A list of filters used to exclude diagnostics from the output
*/
Expand Down
24 changes: 24 additions & 0 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,30 @@ describe('BrsFile', () => {
`);
});

it('discard parameter types when removeParameterTypes is true', () => {
program.options.removeParameterTypes = true;
testTranspile(`
sub one(a as integer, b = "" as string, c = invalid as dynamic)
end sub
`, `
sub one(a, b = "", c = invalid)
end sub
`);
});

it('discard return type when removeParameterTypes is true', () => {
program.options.removeParameterTypes = true;
testTranspile(`
function one() as string
return ""
end function
`, `
function one()
return ""
end function
`);
});

it('transpiles local var assignment operators', () => {
testTranspile(`
sub main()
Expand Down
4 changes: 2 additions & 2 deletions src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class FunctionExpression extends Expression implements TypedefProvider {
state.transpileToken(this.rightParen)
);
//as [Type]
if (this.asToken) {
if (this.asToken && !state.options.removeParameterTypes) {
results.push(
' ',
//as
Expand Down Expand Up @@ -317,7 +317,7 @@ export class FunctionParameterExpression extends Expression {
result.push(this.defaultValue.transpile(state));
}
//type declaration
if (this.asToken) {
if (this.asToken && !state.options.removeParameterTypes) {
result.push(' ');
result.push(state.transpileToken(this.asToken));
result.push(' ');
Expand Down
1 change: 1 addition & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ export class Util {
config.sourceRoot = config.sourceRoot ? standardizePath(config.sourceRoot) : undefined;
config.allowBrighterScriptInBrightScript = config.allowBrighterScriptInBrightScript === true ? true : false;
config.emitDefinitions = config.emitDefinitions === true ? true : false;
config.removeParameterTypes = config.removeParameterTypes === true ? true : false;
if (typeof config.logLevel === 'string') {
config.logLevel = LogLevel[(config.logLevel as string).toLowerCase()];
}
Expand Down

0 comments on commit 28d41fb

Please sign in to comment.