Issues (117)

lib/handlebars/javascript.js (2 issues)

1 1
var format     = require('string-template');
2 1
var handlebars = require('handlebars');
3 1
var hljs       = require('highlight.js');
4 1
var kebab      = require('kebab-case');
5
6
/**
7
 * Formats a JavaScript plugin constructor name to look like this:
8
 * ```js
9
 * var elem = new Foundation.Plugin(element, options);
10
 * ```
11
 *
12
 * @param {string} name - Name of the plugin.
13
 * @returns {string} A formatted code sample.
14
 */
15 1
handlebars.registerHelper('writeJsConstructor', function(name) {
16 1
  var str = 'var elem = new YourApp.'+name+'(element, options);';
17 1
  return new handlebars.SafeString(hljs.highlight('js', str).value);
18
})
19
20
/**
21
 * Formats a JavaScript plugin method to look like this:
22
 * ```js
23
 * $('#element').foundation('method', param1, param2);
24
 * ```
25
 *
26
 * @param {object} method - Method data, taken from a JSDoc object.
27
 * @returns {string} A formatted code sample.
28
 */
29 1
handlebars.registerHelper('writeJsFunction', function(method) {
30 2
  var str = '$(\'#element\').your_app(\'' + method.name;
31 4
  if (method.params && method.params.length) {
32 1
    str += '\', ';
33 2
    str += (method.params || []).map(function(val) {
34 2
      return val.name;
35
    }).join(', ');
36 1
    str += ');';
37
  }
38
  else {
39 1
    str += '\');'
40
  }
41
42 2
  return new handlebars.SafeString(hljs.highlight('js', str).value);
43
});
44
45
/**
46
 * Convert JSDoc's module definition to a filename.
47
 * @param {string} value - Module name to convert.
48
 * @returns {string} A JavaScript filename.
49
 */
50 1
handlebars.registerHelper('formatJsModule', function(value) {
51 1
  return value.replace('module:', '') + '.js';
52
});
53
54
/**
55
 * Format a JavaScript plugin option by converting it to an HTML data attribute.
56
 * @param {string} name - Plugin option name.
57
 * @returns {string} Plugin option as an HTML data attribute.
58
 */
59 1
handlebars.registerHelper('formatJsOptionName', function(name) {
60 1
  return 'data-' + kebab(name);
61
});
62
63
/**
64
 * Formats JsDoc "type" definitions to read "x or y or z"
65
 * @param {string[]} types - Array of types.
66
 * @returns {string} A formatted string.
67
 */
68 1
handlebars.registerHelper('formatJsOptionTypes', function(types) {
69 4
  if (typeof types === 'undefined' || typeof types.names == 'undefined') return '';
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
70 1
  var types = types.names;
71 1
  var output = '';
72
73 1
  for (var i in types) {
74 3
    output += types[i] + ' or ';
75
  }
76
77 1
  return output.slice(0, -4);
78
});
79
80
/**
81
 * Format a JavaScript plugin option's default value:
82
 *   - For string values, remove the quotes on either side.
83
 *   - For all other values, return as-is.
84
 * @param {string} value - Value to process.
85
 * @returns {string} Processed value.
86
 */
87 1
handlebars.registerHelper('formatJsOptionValue', function(name) {
88 4
  if (typeof name === 'undefined') return '';
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
89
90
  var output;
91
92 3
  if (name.match(/^('|").+('|")$/)) {
93 2
    output = name.slice(1, -1);
94
  }
95
  else {
96 1
    output = name;
97
  }
98
99 3
  return new handlebars.SafeString(output);
100
});
101
102
/**
103
 * Format the name of a JavaScript event. This converts JSDoc's slightly convoluted syntax to match our namespaced event names: `event.zf.pluginname`.
104
 * @todo Make this unnecessary by using the actual event names in JSDoc annotations.
105
 * @param {string} name - Event name.
106
 * @param {string} title - Plugin name, used for the event namespace.
107
 * @returns {string} Formatted event name.
108
 */
109 1
handlebars.registerHelper('formatJsEventName', function(name, title) {
110 2
  title = title.charAt(0).toLowerCase() + title.slice(1);
111 2
  return format('{0}.zf.{1}', [name, title.replace(/(-| )/, '')]);
112
});
113