Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow a few exceptions to the standard eslint rules #3

Merged
merged 3 commits into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ var blockCloser = /^```$/mg

var standardMarkdown = module.exports = {}

var disabledRules = ['no-undef', 'no-unused-vars', 'no-lone-blocks', 'no-labels']

standardMarkdown.lintText = function (text, done) {
var blocks = extractCodeBlocks(text)
async.map(blocks, function (block, callback) {
return standard.lintText(block, callback)
var ignoredBlock = '/* eslint-disable ' + disabledRules.join(', ') + ' */\n' + block
return standard.lintText(ignoredBlock, callback)
}, function (err, results) {
if (err) return done(err)
results = results.map(function (r) {
return r.results.map(function (res) {
return res.messages
return res.messages.map(function (message) {
// We added an extra line to the top of the "file" so we need to remove one here
message.line -= 1
return message
})
})
})
results = flatten(flatten(results))
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ All files with `.md` or `.markdown` extension are linted, and the following dire
- `node_modules`
- `vendor`

This module disables certain rules that were considered not appropriate for linting JS blocks in markdown. See [#2](https://github.com/zeke/standard-markdown/issues/2) for more information.

Currently we disable the following rules

* [`no-undef`](http://eslint.org/docs/rules/no-undef)
* [`no-unused-vars`](http://eslint.org/docs/rules/no-unused-vars)
* [`no-lone-blocks`](http://eslint.org/docs/rules/no-lone-blocks)
* [`no-labels`](http://eslint.org/docs/2.0.0/rules/no-labels)

## Tests

```sh
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/clean.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ There are no linting errors in this file.
let wibble = 2
console.log(wibble)
```

It should allow use of undefined variables

```javascript
win.close()
```

It should allow creation of unused variables

```js
// `BrowserWindow` is declared but not used
const {BrowserWindow} = require('electron')
```
12 changes: 5 additions & 7 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ test('standardMarkdown', function (t) {
// console.error(JSON.stringify(results, null, 2))

t.comment('dirty fixture')
t.equal(results.length, 6, 'returns six linting errors')
t.equal(results.length, 5, 'returns six linting errors')

t.equal(results[0].message, "'foo' is defined but never used", 'finds errors')
t.equal(results[0].message, 'Extra semicolon.', 'finds errors in first block')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is no longer an "error" has we have disabled the no-unused-vars rule

t.equal(results[0].line, 6, 'identifies correct line number in first block')

t.equal(results[1].message, 'Extra semicolon.', 'finds errors in first block')
t.equal(results[1].line, 6, 'identifies correct line number in first block')

t.equal(results[2].message, 'Extra semicolon.', 'finds errors in second block')
t.equal(results[2].line, 20, 'identifies correct line number in first block')
t.equal(results[1].message, 'Extra semicolon.', 'finds errors in second block')
t.equal(results[1].line, 20, 'identifies correct line number in first block')

t.comment('every error')
t.ok(results.every(function (result) {
Expand Down