Skip to content

Commit

Permalink
Bugfix: Crashes if a header is hasOwnProperty
Browse files Browse the repository at this point in the history
True story: <1-liners/1-liners@c087021>.

Explain change in a comment

Add test for weird headers

Closes thlorenz#70
  • Loading branch information
Tomek Wiszniewski authored and jez committed Jun 24, 2015
1 parent c0ebd76 commit 48beff0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ function countHeaders (headers) {
var header = headers[i];
var name = header.name;

if (instances.hasOwnProperty(name)) {
if (Object.prototype.hasOwnProperty.call(instances, name)) {
// `instances.hasOwnProperty(name)` fails when there’s an instance named "hasOwnProperty".
instances[name]++;
} else {
instances[name] = 0;
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/readme-with-weird-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
README to test doctoc with edge-case headers.

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
## Table of Contents

- [hasOwnProperty](#hasownproperty)
- [something else](#something-else)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->


## hasOwnProperty
## something else
22 changes: 22 additions & 0 deletions test/transform-weird-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
/*jshint asi: true */

var test = require('tap').test
, transform = require('../lib/transform');

test('\ngiven a file with edge-case header names', function (t) {
var content = require('fs').readFileSync(__dirname + '/fixtures/readme-with-weird-headers.md', 'utf8');
var headers = transform(content);

t.deepEqual(
headers.toc.split('\n')
, [ '## Table of Contents',
'',
'- [hasOwnProperty](#hasownproperty)',
'- [something else](#something-else)',
'' ]
, 'generates a correct toc when headers are weird'
)

t.end()
})

0 comments on commit 48beff0

Please sign in to comment.