Skip to content

Commit

Permalink
Do not escape underscores inside word
Browse files Browse the repository at this point in the history
  • Loading branch information
SemenchenkoVitaliy committed May 31, 2019
1 parent 81c27f3 commit 83e515f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to
- `eslint-disable` comments processing
- Support for functions definitions in parameters.
- Class constructor handling.
- Do not escape underscores inside word.

## [0.5.5][] - 2019-05-11

Expand Down
36 changes: 33 additions & 3 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,40 @@ const generateReturns = (comments, customTypes, usedTypes) => {
return [line, '', ...res];
};

const ESCAPE_MD_REGEXP = /[_[\]]/g;
const isAlphaNumeric = str => {
if (!str) return false;
for (const char of str) {
const code = char.charCodeAt();
if (
!(code >= 48 && code <= 57) && // 0-9
!(code >= 65 && code <= 90) && // A-Z
!(code >= 97 && code <= 122) && // a-z
!(code >= 0xdf && code <= 0xff)
) {
return false;
}
}
return true;
};

const escapeMd = str => {
let res = '';
for (let i = 0; i < str.length; i++) {
if (
str[i] === '>' ||
str[i] === '[' ||
str[i] === ']' ||
(str[i] === '_' &&
!(isAlphaNumeric(str[i - 1]) && isAlphaNumeric(str[i + 1])))
) {
res += '\\';
}
res += str[i];
}
return res;
};

const headerify = (str, level = 1) =>
`${'#'.repeat(level)} ${str.replace(ESCAPE_MD_REGEXP, '\\$&')}`;
const headerify = (str, level = 1) => `${'#'.repeat(level)} ${escapeMd(str)}`;

const generateProps = (method, comments, customTypes, usedTypes, level) => {
const buf = [];
Expand Down
8 changes: 8 additions & 0 deletions test/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ const lambdaInLambda = x => y => x + y;
// generated unless it is specified via cli or config file
const undocumentedFunction = (a, b, c) => {};

const __SOME_DEFAULT__VALUE_ = 42;

const underscoresInParamsFunctions = (num = __SOME_DEFAULT__VALUE_) => {};

const bracketsInParamsFunctions = (arr = []) => {};

// ExampleClass description
// Static properties:
// staticProp <string> static property
Expand Down Expand Up @@ -375,6 +381,8 @@ module.exports = {
callAsyncFunction,
functionWithFunction,
undocumentedFunction,
underscoresInParamsFunctions,
bracketsInParamsFunctions,

functionsInFunctionParams,
lambdaInLambda,
Expand Down
6 changes: 5 additions & 1 deletion test/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ _Returns:_ [`<number>`][number] result of `fn` execution

Function with function

## functionsInFunctionParams(lambda1 = () => {}, lambda2 = () => 123, lambda3 = (...args) => {console.log(args);}, lambda4 = a => {}, fn = function(num) {console.log(num + num);})
## underscoresInParamsFunctions(num = \_\_SOME_DEFAULT\_\_VALUE\_)

## bracketsInParamsFunctions(arr = \[\])

## functionsInFunctionParams(lambda1 = () =\> {}, lambda2 = () =\> 123, lambda3 = (...args) =\> {console.log(args);}, lambda4 = a =\> {}, fn = function(num) {console.log(num + num);})

## lambdaInLambda(x)

Expand Down

0 comments on commit 83e515f

Please sign in to comment.