Skip to content

Commit d394cc8

Browse files
committed
Add type linting
1 parent 15381be commit d394cc8

File tree

8 files changed

+64
-10
lines changed

8 files changed

+64
-10
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @ts-check
2+
/// <reference types="node" />
3+
14
'use strict';
25

36
const slugify = require('./lib/slug').slugify;

lib/placeholders.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @ts-check
2+
/// <reference types="node" />
3+
14
'use strict';
25

36
const strftime = require('strftime');

lib/slug.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @ts-check
2+
/// <reference types="node" />
3+
14
'use strict';
25

36
const XRegExp = require('xregexp');
@@ -40,12 +43,12 @@ const SLUGIFY_TRAILING_LEADING_HYPHEN = /^-|-$/g;
4043
*
4144
* @name slugify
4245
* @param {string} str - the string to create a slug of
43-
* @param {string} [mode=default] - how string is slugified. Can be set to "default", "pretty" or "raw"
44-
* @param {boolean} [cased=false] – whether to keep the character casing or not
46+
* @param {string} [mode] - how string is slugified. Can be set to "default", "pretty" or "raw"
47+
* @param {boolean} [cased] - whether to keep the character casing or not
4548
* @return {string} the slugified string
4649
* @see {@link https://github.com/jekyll/jekyll/blob/9278eb8fcec85b17573c6658d7d67ef6ea6ffb92/lib/jekyll/utils.rb#L177|Mimicked Jekyll Code}
4750
*/
48-
const slugify = function (str, mode, cased) {
51+
const slugify = function (str, mode = 'default', cased = false) {
4952
if (!str) { return str; }
5053

5154
mode = mode || 'default';

lib/url.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @ts-check
2+
/// <reference types="node" />
3+
14
'use strict';
25

36
const urlPlaceholders = require('./placeholders').urlPlaceholders;
@@ -64,18 +67,16 @@ const generateUrl = function (template, jekyllResource) {
6467
* @param {string} options.template - The String used as template for URL generation,
6568
* or example "/:path/:basename:output_ext", where
6669
* a placeholder is prefixed with a colon.
67-
* @param {string} options.:placeholders - A hash containing the placeholders which will be
70+
* @param {Object<string,any>} options.placeholders - A hash containing the placeholders which will be
6871
* replaced when used inside the template. E.g.
6972
* { year: (new Date()).getFullYear() } would replace
7073
* the placeholder ":year" with the current year.
71-
* @param {string} options.permalink - If supplied, no URL will be generated from the
74+
* @param {string} [options.permalink] - If supplied, no URL will be generated from the
7275
* template. Instead, the given permalink will be
7376
* used as URL.
7477
* @see {@link https://github.com/jekyll/jekyll/blob/cc82d442223bdaee36a2aceada64008a0106d82b/lib/jekyll/url.rb|Mimicked Jekyll Code}
7578
*/
7679
function JekyllUrl (options) {
77-
options = options || {};
78-
7980
this.template = options.template;
8081
this.placeholders = options.placeholders;
8182
this.permalink = options.permalink;
@@ -92,7 +93,7 @@ function JekyllUrl (options) {
9293
* @throws {Error} if the relative URL contains a colon
9394
*/
9495
JekyllUrl.prototype.toString = function () {
95-
const sanitizedUrl = this.sanitize_url(this.generated_permalink() || this.generated_url());
96+
const sanitizedUrl = this.sanitize_url(this.generated_permalink() || this.generated_url() || '');
9697

9798
if (sanitizedUrl.includes(':')) {
9899
throw new Error('The URL' + sanitizedUrl + 'is invalid because it contains a colon.');
@@ -131,7 +132,7 @@ JekyllUrl.prototype.generated_url = function () {
131132
*/
132133
JekyllUrl.prototype.generate_url = function (template) {
133134
return Object.keys(this.placeholders).reduce(
134-
(result, token) => result.split(':' + token).join(this.constructor.escape_path(this.placeholders[token] || '')),
135+
(result, token) => result.split(':' + token).join(JekyllUrl.escape_path(this.placeholders[token] || '')),
135136
template
136137
);
137138
};

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"skip:check:docs": "documentation readme index.js -s \"API Usage\" --no-markdown-toc --diff-only",
1616
"check:installed-check": "installed-check -i eslint",
1717
"check:lint": "eslint .",
18+
"check:types": "tsc",
1819
"test:mocha": "NODE_ENV=test nyc --reporter=lcov --reporter text mocha test/**/*.spec.js",
1920
"test": "run-p check:* && run-p test:*",
2021
"build-docs": "documentation readme index.js -s \"API Usage\" --no-markdown-toc"
@@ -28,6 +29,9 @@
2829
"node": ">=10.0.0"
2930
},
3031
"devDependencies": {
32+
"@types/chai": "^4.2.0",
33+
"@types/mocha": "^5.2.7",
34+
"@types/node": "^12.7.1",
3135
"chai": "^4.2.0",
3236
"coveralls": "^3.0.5",
3337
"dependency-check": "^4.1.0",
@@ -43,7 +47,8 @@
4347
"installed-check": "^3.0.0",
4448
"mocha": "^6.2.0",
4549
"npm-run-all": "^4.1.5",
46-
"nyc": "^14.1.1"
50+
"nyc": "^14.1.1",
51+
"typescript": "^3.5.3"
4752
},
4853
"dependencies": {
4954
"strftime": "^0.10.0",

test/slug.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// @ts-check
2+
/// <reference types="node" />
3+
/// <reference types="mocha" />
4+
/// <reference types="chai" />
5+
16
'use strict';
27

38
const chai = require('chai');
@@ -11,6 +16,7 @@ describe('Slug', function () {
1116
// Tests taken from https://github.com/jekyll/jekyll/blob/70aa8a4e37cdbb935d8aacda3d6c6b598c2c91bb/test/test_utils.rb#L129
1217

1318
it('should return undefined if passed undefined', () => {
19+
// @ts-ignore
1420
should.not.exist(slugify());
1521
});
1622

test/url.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// @ts-check
2+
/// <reference types="node" />
3+
/// <reference types="mocha" />
4+
/// <reference types="chai" />
5+
16
'use strict';
27

38
const chai = require('chai');
@@ -11,6 +16,7 @@ describe('Url', function () {
1116
// Tests taken from https://github.com/jekyll/jekyll/blob/a29498eaaebbccd415cc3b811d050137f456cd9a/test/test_url.rb
1217

1318
it('should throw an exception if neither permalink or template is specified', () => {
19+
// @ts-ignore
1420
should.Throw(() => new JekyllUrl({ placeholders: {} }));
1521
});
1622

tsconfig.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{
3+
"files": [
4+
"index.js"
5+
],
6+
"include": [
7+
"lib/**/*.js",
8+
"test/**/*.js"
9+
],
10+
"compilerOptions": {
11+
/* Basic Options */
12+
"target": "ES2016",
13+
"module": "commonjs",
14+
"allowJs": true,
15+
"resolveJsonModule": true,
16+
"noEmit": true,
17+
"maxNodeModuleJsDepth": 3,
18+
19+
/* Strict Type-Checking Options */
20+
"strict": true,
21+
"noImplicitAny": false,
22+
"strictNullChecks": false,
23+
24+
/* Additional Checks */
25+
"noUnusedLocals": true
26+
}
27+
}

0 commit comments

Comments
 (0)