Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Forbid License, Licence and Contribute sections #123

Merged
merged 1 commit into from
Oct 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = [
require('./contributing'),
require('./git-repo-age'),
require('./github'),
/// require('./license'),
require('./license'),
require('./list-item'),
require('./no-ci-badge'),
require('./spell-check'),
Expand Down
36 changes: 2 additions & 34 deletions rules/license.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
'use strict';
const find = require('unist-util-find');
const findAllAfter = require('unist-util-find-all-after');
const rule = require('unified-lint-rule');
const toString = require('mdast-util-to-string');
const visit = require('unist-util-visit');

module.exports = rule('remark-lint:awesome-license', (ast, file) => {
const license = find(ast, node => (
node.type === 'heading' &&
(toString(node) === 'Licence' || toString(node) === 'License')
));

if (!license) {
file.message('Missing License section', ast);
return;
if (license) {
file.message('Forbidden license section found', ast);
}

if (license.depth !== 2) {
file.message('License section must be at heading depth 2', license);
return;
}

const headingsPost = findAllAfter(ast, license, {
type: 'heading'
});

if (headingsPost.length > 0) {
file.message('License must be the last section', headingsPost[0]);
return;
}

const children = findAllAfter(ast, license, () => true);
const content = {type: 'root', children};
const value = toString(content);

if (!value) {
file.message('License must not be empty', license);
}

visit(content, 'image', node => {
if (/\.png/i.test(node.url)) {
file.message('License image must be SVG', node);
return false;
}
});
});
3 changes: 0 additions & 3 deletions rules/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ const slugger = new GitHubSlugger();
const maxListItemDepth = 1;

const sectionHeadingBlacklist = new Set([
'Contribute',
'Contributing',
'Licence',
'License',
'Footnotes'
]);

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/license/error0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

non-empty

## LICENSE
## Licence

This is an invalid license section.
Licence section is forbidden.
2 changes: 1 addition & 1 deletion test/fixtures/license/error2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## License

This license is invalid because it is not the last section.
License section is forbidden, even if first section.

## Foo

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/license/error3.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ non-empty

# License

This license is invalid because its heading should be at depth 2.
License is forbidden even at depth 1.
2 changes: 1 addition & 1 deletion test/fixtures/license/error4.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

[![CC4](https://mirrors.creativecommons.org/presskit/buttons/88x31/png/by.png)](https://creativecommons.org/licenses/by/4.0/)

This license is invalid because it points to a png instead of an svg.
License is forbidden even if ut has images.
8 changes: 1 addition & 7 deletions test/fixtures/license/success0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@

## Foo

This license section below is 100% valid.

## License

[![CC0](https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/cc-zero.svg)](https://creativecommons.org/publicdomain/zero/1.0/)

To the extent possible under law, [Sindre Sorhus](https://sindresorhus.com) has waived all copyright and related or neighboring rights to this work.
This list is 100% valid because it has no license section.
4 changes: 0 additions & 4 deletions test/fixtures/toc/0.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ non-empty

non-empty

## License

non-empty

## Footnotes

non-empty
6 changes: 1 addition & 5 deletions test/fixtures/toc/1.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ non-empty

non-empty

## Contribute

non-empty

## Licence
## Contributing

non-empty

Expand Down
6 changes: 1 addition & 5 deletions test/fixtures/toc/2.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ non-empty

non-empty

## License

non-empty

## Contribute
## Contributing

non-empty

Expand Down
6 changes: 1 addition & 5 deletions test/fixtures/toc/3.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ non-empty

non-empty

## Licence

non-empty

## Contribute
## Contributing

non-empty
6 changes: 1 addition & 5 deletions test/fixtures/toc/4.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ non-empty

non-empty

## Contribute

non-empty

## Licence
## Contributing

non-empty
8 changes: 0 additions & 8 deletions test/fixtures/toc/5.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@

## Foo

## Contribute

This section should be ignored.

## Contributing

This section should be ignored.

## License

This section should be ignored.

## Footnotes

This section should be ignored.
33 changes: 14 additions & 19 deletions test/rules/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,61 @@ import lint from '../_lint';
const config = {
plugins: [
require('remark-lint'),
/// require('remark-lint-no-empty-sections'),
require('../../rules/license')
]
};

test('license - missing', async t => {
test('licence - forbidden section', async t => {
const messages = await lint({config, filename: 'test/fixtures/license/error0.md'});
t.deepEqual(messages, [
{
line: 1,
ruleId: 'awesome-license',
message: 'Missing License section'
message: 'Forbidden license section found'
}
]);
});

test('license - empty', async t => {
test('license - forbidden empty section', async t => {
const messages = await lint({config, filename: 'test/fixtures/license/error1.md'});
t.deepEqual(messages, [
// {
// ruleId: 'no-empty-sections',
// message: 'Remove empty section: "License"'
// },
{
line: 11,
line: 1,
ruleId: 'awesome-license',
message: 'License must not be empty'
message: 'Forbidden license section found'
}
]);
});

test('license - not last section', async t => {
test('license - forbidden last section', async t => {
const messages = await lint({config, filename: 'test/fixtures/license/error2.md'});
t.deepEqual(messages, [
{
line: 11,
line: 1,
ruleId: 'awesome-license',
message: 'License must be the last section'
message: 'Forbidden license section found'
}
]);
});

test('license - incorrect heading depth', async t => {
test('license - forbidden heading depth section', async t => {
const messages = await lint({config, filename: 'test/fixtures/license/error3.md'});
t.deepEqual(messages, [
{
line: 11,
line: 1,
ruleId: 'awesome-license',
message: 'License section must be at heading depth 2'
message: 'Forbidden license section found'
}
]);
});

test('license - png image', async t => {
test('license - forbidden image section', async t => {
const messages = await lint({config, filename: 'test/fixtures/license/error4.md'});
t.deepEqual(messages, [
{
line: 3,
line: 1,
ruleId: 'awesome-license',
message: 'License image must be SVG'
message: 'Forbidden license section found'
}
]);
});
Expand Down