Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add no-trailing-punctuation, fix #18
- Loading branch information
Showing
5 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const lint = require('../lint'); | ||
|
||
describe('no-trailing-punctuation', () => { | ||
test('success', () => { | ||
const md = `## header 2`; | ||
expect(lint(md)).toEqual([]); | ||
}); | ||
|
||
test('fail', () => { | ||
const md = `### header 3**!**`; | ||
expect(lint(md)).toEqual([{ | ||
column: 1, | ||
level: 'error', | ||
line: 1, | ||
text: `Header content can not end with symbol: 'header 3!'`, | ||
type: 'no-trailing-punctuation' | ||
}]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { Plugin } = require('ast-plugin'); | ||
const _ = require('lodash'); | ||
const { astToText } = require('./helper/ast'); | ||
|
||
const Symbols = '.,;:!?。,;:!?…'; | ||
|
||
/** | ||
* Header 内容不能以标点符号结尾 | ||
* no-trailing-punctuation | ||
*/ | ||
module.exports = class extends Plugin { | ||
|
||
static get type() { | ||
return 'no-trailing-punctuation'; | ||
}; | ||
|
||
pre() {} | ||
|
||
visitor() { | ||
return { | ||
heading: ast => { | ||
const text = astToText(ast.node); | ||
const line = ast.node.position.start.line; | ||
const column = ast.node.position.start.column; | ||
|
||
if (_.includes(Symbols, _.last(_.trimEnd(text)))) { | ||
this.cfg.throwError({ | ||
line, | ||
column, | ||
text: `Header content can not end with symbol: '${text}'`, | ||
}); | ||
} | ||
}, | ||
} | ||
} | ||
|
||
post() {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
### header 3**!** |