Skip to content

Commit c454d94

Browse files
authored
Merge pull request #1515 from zimmi88/4.x-typings-lint
Port over linting and test for typings
2 parents 9cfb5dd + 3fb6687 commit c454d94

File tree

6 files changed

+204
-3
lines changed

6 files changed

+204
-3
lines changed

Gruntfile.js

+10
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ module.exports = function(grunt) {
187187
}
188188
},
189189

190+
bgShell: {
191+
checkTypes: {
192+
cmd: 'npm run checkTypes',
193+
bg: false,
194+
fail: true
195+
}
196+
},
197+
190198
watch: {
191199
scripts: {
192200
options: {
@@ -202,6 +210,7 @@ module.exports = function(grunt) {
202210
// Build a new version of the library
203211
this.registerTask('build', 'Builds a distributable version of the current project', [
204212
'eslint',
213+
'bgShell:checkTypes',
205214
'parser',
206215
'node',
207216
'globals']);
@@ -222,6 +231,7 @@ module.exports = function(grunt) {
222231
grunt.loadNpmTasks('grunt-contrib-uglify');
223232
grunt.loadNpmTasks('grunt-contrib-watch');
224233
grunt.loadNpmTasks('grunt-babel');
234+
grunt.loadNpmTasks('grunt-bg-shell');
225235
grunt.loadNpmTasks('grunt-eslint');
226236
grunt.loadNpmTasks('grunt-saucelabs');
227237
grunt.loadNpmTasks('grunt-webpack');

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
"babel-loader": "^5.0.0",
3434
"babel-runtime": "^5.1.10",
3535
"benchmark": "~1.0",
36+
"dtslint": "^0.5.5",
3637
"dustjs-linkedin": "^2.0.2",
3738
"eco": "~1.1.0-rc-3",
3839
"grunt": "^1.0.3",
3940
"grunt-babel": "^5.0.0",
41+
"grunt-bg-shell": "^2.3.3",
4042
"grunt-cli": "^1",
4143
"grunt-contrib-clean": "^1",
4244
"grunt-contrib-concat": "^1",
@@ -59,11 +61,12 @@
5961
"webpack-dev-server": "^1.12.1"
6062
},
6163
"main": "lib/index.js",
62-
"types": "lib/handlebars.d.ts",
64+
"types": "types/index.d.ts",
6365
"bin": {
6466
"handlebars": "bin/handlebars"
6567
},
6668
"scripts": {
69+
"checkTypes": "dtslint types",
6770
"test": "grunt"
6871
},
6972
"jspm": {
@@ -83,6 +86,7 @@
8386
"lib",
8487
"print-script",
8588
"release-notes.md",
86-
"runtime.js"
89+
"runtime.js",
90+
"types/*.d.ts"
8791
]
8892
}

lib/handlebars.d.ts types/index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
* - Sergei Dorogin <https://github.com/evil-shrike>
99
* - webbiesdk <https://github.com/webbiesdk>
1010
* For full history prior to their migration to handlebars.js, please see:
11-
* https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/handlebars
11+
* https://github.com/DefinitelyTyped/DefinitelyTyped/commits/1ce60bdc07f10e0b076778c6c953271c072bc894/types/handlebars/index.d.ts
1212
*/
13+
// TypeScript Version: 2.3
1314

1415
declare namespace Handlebars {
1516
export interface TemplateDelegate<T = any> {

types/test.ts

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* These test cases were imported from https://github.com/DefinitelyTyped/DefinitelyTyped
2+
* and includes previous contributions from the DefinitelyTyped community.
3+
* For full history prior to their migration to handlebars.js, please see:
4+
* https://github.com/DefinitelyTyped/DefinitelyTyped/commits/1ce60bdc07f10e0b076778c6c953271c072bc894/types/handlebars/handlebars-tests.ts
5+
*/
6+
7+
import * as Handlebars from 'handlebars';
8+
9+
const context = {
10+
author: { firstName: 'Alan', lastName: 'Johnson' },
11+
body: 'I Love Handlebars',
12+
comments: [{
13+
author: { firstName: 'Yehuda', lastName: 'Katz' },
14+
body: 'Me too!'
15+
}]
16+
};
17+
Handlebars.registerHelper('fullName', (person: typeof context.author) => {
18+
return person.firstName + ' ' + person.lastName;
19+
});
20+
21+
Handlebars.registerHelper('agree_button', function(this: any) {
22+
return new Handlebars.SafeString(
23+
'<button>I agree. I ' + this.emotion + ' ' + this.name + '</button>'
24+
);
25+
});
26+
27+
const source1 = '<p>Hello, my name is {{name}}. I am from {{hometown}}. I have ' +
28+
'{{kids.length}} kids:</p>' +
29+
'<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>';
30+
const template1 = Handlebars.compile(source1);
31+
template1({ name: "Alan", hometown: "Somewhere, TX", kids: [{name: "Jimmy", age: 12}, {name: "Sally", age: 4}]});
32+
33+
Handlebars.registerHelper('link_to', (context: typeof post) => {
34+
return '<a href="' + context.url + '">' + context.body + '</a>';
35+
});
36+
const post = { url: "/hello-world", body: "Hello World!" };
37+
const context2 = { posts: [post] };
38+
const source2 = '<ul>{{#posts}}<li>{{{link_to this}}}</li>{{/posts}}</ul>';
39+
const template2: HandlebarsTemplateDelegate<{ posts: { url: string, body: string }[] }> = Handlebars.compile(source2);
40+
template2(context2);
41+
42+
Handlebars.registerHelper('link_to', (title: string, context: typeof post) => {
43+
return '<a href="/posts' + context.url + '">' + title + '!</a>';
44+
});
45+
const context3 = { posts: [{url: '/hello-world', body: 'Hello World!'}] };
46+
const source3 = '<ul>{{#posts}}<li>{{{link_to "Post" this}}}</li>{{/posts}}</ul>';
47+
const template3 = Handlebars.compile<typeof context3>(source3);
48+
template3(context3);
49+
50+
const source4 = '<ul>{{#people}}<li>{{#link}}{{name}}{{/link}}</li>{{/people}}</ul>';
51+
Handlebars.registerHelper('link', function(this: any, context: any) {
52+
return '<a href="/people/' + this.id + '">' + context.fn(this) + '</a>';
53+
});
54+
const template4 = Handlebars.compile<{ people: { name: string, id: number }[] }>(source4);
55+
const data2 = { 'people': [
56+
{ 'name': 'Alan', 'id': 1 },
57+
{ 'name': 'Yehuda', 'id': 2 }
58+
]};
59+
template4(data2);
60+
61+
const source5 = '<ul>{{#people}}<li>{{> link}}</li>{{/people}}</ul>';
62+
Handlebars.registerPartial('link', '<a href="/people/{{id}}">{{name}}</a>');
63+
const template5 = Handlebars.compile(source5);
64+
const data3 = { 'people': [
65+
{ 'name': 'Alan', 'id': 1 },
66+
{ 'name': 'Yehuda', 'id': 2 }
67+
]};
68+
template5(data3);
69+
70+
const source6 = '{{#list nav}}<a href="{{url}}">{{title}}</a>{{/list}}';
71+
const template6 = Handlebars.compile(source6);
72+
Handlebars.registerHelper('list', (context, options: Handlebars.HelperOptions) => {
73+
let ret = "<ul>";
74+
for(let i=0, j=context.length; i<j; i++) {
75+
ret = ret + "<li>" + options.fn(context[i]) + "</li>";
76+
}
77+
return ret + "</ul>";
78+
});
79+
template6([{url:"", title:""}])
80+
81+
82+
const escapedExpression = Handlebars.Utils.escapeExpression('<script>alert(\'xss\');</script>');
83+
84+
Handlebars.helpers !== undefined;
85+
86+
const parsedTmpl = Handlebars.parse('<p>Hello, my name is {{name}}.</p>', {
87+
srcName: "/foo/bar/baz.hbs",
88+
ignoreStandalone: true
89+
});
90+
91+
const parsedTmplWithoutOptions = Handlebars.parse('<p>Hello, my name is {{name}}.</p>');

types/tsconfig.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": ["es6"],
5+
"noImplicitAny": true,
6+
"noImplicitThis": true,
7+
"strictNullChecks": true,
8+
"strictFunctionTypes": true,
9+
"noEmit": true,
10+
11+
"baseUrl": ".",
12+
"paths": {
13+
"handlebars": ["."]
14+
}
15+
}
16+
}

types/tslint.json

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"adjacent-overload-signatures": false,
5+
"array-type": false,
6+
"arrow-return-shorthand": false,
7+
"ban-types": false,
8+
"callable-types": false,
9+
"comment-format": false,
10+
"dt-header": false,
11+
"eofline": false,
12+
"export-just-namespace": false,
13+
"import-spacing": false,
14+
"interface-name": false,
15+
"interface-over-type-literal": false,
16+
"jsdoc-format": false,
17+
"max-line-length": false,
18+
"member-access": false,
19+
"new-parens": false,
20+
"no-any-union": false,
21+
"no-boolean-literal-compare": false,
22+
"no-conditional-assignment": false,
23+
"no-consecutive-blank-lines": false,
24+
"no-construct": false,
25+
"no-declare-current-package": false,
26+
"no-duplicate-imports": false,
27+
"no-duplicate-variable": false,
28+
"no-empty-interface": false,
29+
"no-for-in-array": false,
30+
"no-inferrable-types": false,
31+
"no-internal-module": false,
32+
"no-irregular-whitespace": false,
33+
"no-mergeable-namespace": false,
34+
"no-misused-new": false,
35+
"no-namespace": false,
36+
"no-object-literal-type-assertion": false,
37+
"no-padding": false,
38+
"no-redundant-jsdoc": false,
39+
"no-redundant-jsdoc-2": false,
40+
"no-redundant-undefined": false,
41+
"no-reference-import": false,
42+
"no-relative-import-in-test": false,
43+
"no-self-import": false,
44+
"no-single-declare-module": false,
45+
"no-string-throw": false,
46+
"no-unnecessary-callback-wrapper": false,
47+
"no-unnecessary-class": false,
48+
"no-unnecessary-generics": false,
49+
"no-unnecessary-qualifier": false,
50+
"no-unnecessary-type-assertion": false,
51+
"no-useless-files": false,
52+
"no-var-keyword": false,
53+
"no-var-requires": false,
54+
"no-void-expression": false,
55+
"no-trailing-whitespace": false,
56+
"object-literal-key-quotes": false,
57+
"object-literal-shorthand": false,
58+
"one-line": false,
59+
"one-variable-per-declaration": false,
60+
"only-arrow-functions": false,
61+
"prefer-conditional-expression": false,
62+
"prefer-const": false,
63+
"prefer-declare-function": false,
64+
"prefer-for-of": false,
65+
"prefer-method-signature": false,
66+
"prefer-template": false,
67+
"radix": false,
68+
"semicolon": false,
69+
"space-before-function-paren": false,
70+
"space-within-parens": false,
71+
"strict-export-declare-modifiers": false,
72+
"trim-file": false,
73+
"triple-equals": false,
74+
"typedef-whitespace": false,
75+
"unified-signatures": false,
76+
"void-return": false,
77+
"whitespace": false
78+
}
79+
}

0 commit comments

Comments
 (0)