Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Enum and const values containing BrightScript hex literals (`&hFF0000FF`) are now normalized to valid JS syntax (`0xFF0000FF`) before being emitted, instead of breaking jsdoc's parser ([#14](https://github.com/markwpearce/brighterscript-jsdocs-plugin/issues/14))
- Class fields with an object literal default value (`foo = {}`) no longer crash with `this.getLookupTable is not a function` (fixed upstream in `brighterscript@1.0.0-alpha.56`) ([#17](https://github.com/markwpearce/brighterscript-jsdocs-plugin/issues/17))
- Constants with a non-literal value (eg. `1.0 / 30.0`, `-1`, or a reference to another constant) no longer emit `[object Object]` and break jsdoc's parser; the value's source text is used as the `@default` instead ([#18](https://github.com/markwpearce/brighterscript-jsdocs-plugin/issues/18))

### Added

Expand Down
3 changes: 3 additions & 0 deletions examples/source/testConst.bs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ const PI = 3.14159265359;

namespace Alpha
const ROKU = "roku";

' Longest time step a single frame can count
const MAX_STEP_SECONDS = 1.0 / 30.0
end namespace
57 changes: 57 additions & 0 deletions src/convert-brighterscript-docs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,63 @@ describe('convertBrighterscriptDocs', () => {
var MY_CONSTANT = 0xFF0000FF;
`);
});

it('uses the source text as the default for non-literal constant values', () => {
expectOutput(cbd.convertBrighterscriptDocs(`
namespace alpha
const MAX_STEP = 1.0 / 30.0
const NEG = -1
const REF = MAX_STEP
end namespace
`), `
/**
* @global
* @namespace alpha
*/
var alpha = {};

/**
* @memberof! alpha
* @readonly
* @constant
* @default 1.0 / 30.0
*/
var MAX_STEP = undefined;
alpha.MAX_STEP = MAX_STEP;
/**
* @memberof! alpha
* @readonly
* @constant
* @default -1
*/
var NEG = undefined;
alpha.NEG = NEG;
/**
* @memberof! alpha
* @readonly
* @constant
* @default MAX_STEP
*/
var REF = undefined;
alpha.REF = REF;
`);
});

it('collapses multi-line non-literal constant values onto one line', () => {
expectOutput(cbd.convertBrighterscriptDocs(`
const ITEMS = [
1,
2
]
`), `
/**
* @readonly
* @constant
* @default [ 1, 2 ]
*/
var ITEMS = undefined;
`);
});
});

describe('comment adjacency', () => {
Expand Down
34 changes: 26 additions & 8 deletions src/convert-brighterscript-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ export function resetCreatedCache() {

let parserLines: string[] = [];

/**
* Gets the original source text of a node, with any line breaks collapsed to single spaces
*/
function getSourceText(node?: bs.AstNode): string {
const range = node?.location?.range;
if (!range) {
return '';
}
const lines = parserLines.slice(range.start.line, range.end.line + 1);
if (lines.length === 0) {
return '';
}
lines[lines.length - 1] = lines[lines.length - 1].slice(0, range.end.character);
lines[0] = lines[0].slice(range.start.character);
return lines.map(line => line.trim()).filter(line => line).join(' ');
}

/**
* Gets the type name for the given type
* Defaults to "dynamic" if it can't decide
Expand Down Expand Up @@ -305,10 +322,7 @@ function processFunction(func: bs.FunctionStatement | bs.InterfaceMethodStatemen

let paramLine = ` * @param {${paramType}} `;
if (param.defaultValue?.location?.range) {
let start = param.defaultValue.location.range.start;
let end = param.defaultValue.location.range.end;
let defaultValue = parserLines[start.line].slice(start.character, end.character);
paramLine += `[${paramName}=${defaultValue}]`;
paramLine += `[${paramName}=${getSourceText(param.defaultValue)}]`;
} else {

paramLine += paramName;
Expand Down Expand Up @@ -579,13 +593,17 @@ function processConst(constStatement: bs.ConstStatement, moduleName = '', namesp
}
commentLines.push(' * @readonly');
commentLines.push(' * @constant');
commentLines.push(' * @default');
commentLines.push(' */');
output.push(...commentLines);
let valueOutput = {};
// Literals are emitted as JS so jsdoc infers the default; anything else (eg. `1.0 / 30.0`) may not
// be valid JS, so put its source text on the @default tag instead
let valueOutput = 'undefined';
if (bs.isLiteralExpression(constStatement.value)) {
commentLines.push(' * @default');
valueOutput = normalizeBrightScriptNumericLiteral(constStatement.value.tokens.value.text);
} else {
commentLines.push(` * @default ${getSourceText(constStatement.value).replace(/\*\//g, '*\\/')}`);
}
commentLines.push(' */');
output.push(...commentLines);
output.push(`var ${constStatement.name} = ${valueOutput};`);

if (namespaceName) {
Expand Down
Loading