Skip to content

Commit

Permalink
Arinthros/130 fix quoted module names (#131)
Browse files Browse the repository at this point in the history
* Check method name for encapslating quotes and remove them prior to createStringLiteral

* Add semicolons per code format

* Refactor for simpler string checking logic, normalize tab spaces

* Add test for quoted module name

* Update module_quoted tests per master changes

* Replace tab with spaces

* Fix parens and spacing format

* Fix some spacing.

Co-authored-by: Chad Engler <[email protected]>
  • Loading branch information
arinthros and englercj authored May 5, 2020
1 parent d759376 commit 315b7fa
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/create_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,14 @@ export function createModule(doclet: INamespaceDoclet, nested: boolean, children
if (children)
body = ts.createModuleBlock(children as ts.Statement[]);

const name = ts.createStringLiteral(doclet.name);
let nameStr = doclet.name;
if ((nameStr[0] === '"' && nameStr[nameStr.length - 1] === '"')
|| (nameStr[0] === '\'' && nameStr[nameStr.length - 1] === '\''))
{
nameStr = nameStr.substr(1, nameStr.length - 2);
}

const name = ts.createStringLiteral(nameStr);

return handleComment(doclet, ts.createModuleDeclaration(
undefined, // decorators
Expand Down
29 changes: 29 additions & 0 deletions test/expected/module_quoted.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
declare module "1.0/array-to-object-keys" {
/**
* @param value - The original array entry
* @param index - The index of the array entry (starts at 0)
*/
type valueGenerator = (value: string, index: number) => any;
/**
* Converts an array to an object with static keys and customizable values
* @example
* arrayToObjectKeys(["a", "b"])
* // {a: null, b: null}
* @example
* arrayToObjectKeys(["a", "b"], "value")
* // {a: "value", b: "value"}
* @example
* arrayToObjectKeys(["a", "b"], (key, index) => `value for ${key} #${index + 1}`)
* // {a: "value for a #1", b: "value for b #2"}
* @param array - Keys for the generated object
* @param [valueGenerator = null] - Optional function that sets the object values based on key and index
* @returns A generated object based on the array input
*/
function arrayToObjectKeys(array: string[], valueGenerator?: valueGenerator | any): {
[key: string]: any;
};
/**
* Export arrayToObjectKeys as default.
*/
export default arrayToObjectKeys;
}
32 changes: 32 additions & 0 deletions test/fixtures/module_quoted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/** @module "1.0/array-to-object-keys" */

/**
* @typedef valueGenerator
* @type {function}
* @param {string} value The original array entry
* @param {number} index The index of the array entry (starts at 0)
* @returns {*}
*/

/**
* Converts an array to an object with static keys and customizable values
* @example
* arrayToObjectKeys(["a", "b"])
* // {a: null, b: null}
* @example
* arrayToObjectKeys(["a", "b"], "value")
* // {a: "value", b: "value"}
* @example
* arrayToObjectKeys(["a", "b"], (key, index) => `value for ${key} #${index + 1}`)
* // {a: "value for a #1", b: "value for b #2"}
* @param {string[]} array Keys for the generated object
* @param {valueGenerator|*} [valueGenerator=null] Optional function that sets the object values based on key and index
* @returns {Object<string, *>} A generated object based on the array input
*/
const arrayToObjectKeys = (array, valueGenerator = null) => {
}

/**
* Export arrayToObjectKeys as default.
*/
export default arrayToObjectKeys
1 change: 1 addition & 0 deletions test/specs/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { tsdJsdocTestCase } from '../lib';

suite('Module Checks', () => {
tsdJsdocTestCase('All', 'module_all');
tsdJsdocTestCase('Quoted', 'module_quoted');
});

0 comments on commit 315b7fa

Please sign in to comment.