fix(v2): improve stylelint copyright header rule (#2363)

* fix(v2): improve stylelint copyright header rule

* Whitelist stylelint-copyright from Jest
This commit is contained in:
Yangshun Tay 2020-03-07 11:49:52 +08:00 committed by GitHub
parent 03791ce694
commit 6cc0aa2e75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 113 additions and 81 deletions

13
.stylelintrc.js Normal file
View file

@ -0,0 +1,13 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
module.exports = {
plugins: ['stylelint-copyright'],
rules: {
'docusaurus/copyright-header': true,
},
};

View file

@ -1,6 +0,0 @@
{
"plugins": ["./packages/stylelint-copyright/index.js"],
"rules": {
"plugin/stylelint-copyright": true
}
}

View file

@ -63,6 +63,7 @@
"react": "^16.8.4",
"react-dom": "^16.8.4",
"stylelint": "^13.2.0",
"stylelint-copyright": "1.0.0",
"typescript": "^3.7.2"
},
"lint-staged": {

View file

@ -1,10 +1,4 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* stylelint-disable docusaurus/copyright-header */
/**
* Any CSS included here will be global. The classic template
* bundles Infima by default. Infima is a CSS framework designed to

View file

@ -1,10 +1,4 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* stylelint-disable docusaurus/copyright-header */
/**
* CSS files with the .module.css suffix will be treated as CSS modules
* and scoped locally.

View file

@ -0,0 +1,13 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
module.exports = {
plugins: ['stylelint-copyright'],
rules: {
'docusaurus/copyright-header': true,
},
};

View file

@ -1,6 +0,0 @@
{
"plugins": ["../../..//stylelint-copyright/index.js"],
"rules": {
"plugin/stylelint-copyright": true
}
}

View file

@ -17,8 +17,7 @@
"@docusaurus/preset-classic": "^2.0.0-alpha.43",
"classnames": "^2.2.6",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"stylelint": "^13.2.0"
"react-dom": "^16.8.4"
},
"devDependencies": {
"babel-eslint": "^10.0.3",
@ -30,7 +29,8 @@
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.16.0",
"eslint-plugin-react-hooks": "^2.3.0",
"prettier": "^1.19.1"
"prettier": "^1.19.1",
"stylelint": "^13.2.0"
},
"browserslist": {
"production": [

View file

@ -1,6 +1,6 @@
# stylelint-copyright
stylelint plugin to check css files for a copyright header
stylelint plugin to check CSS files for a copyright header.
```css
/*
@ -11,12 +11,12 @@ stylelint plugin to check css files for a copyright header
## Usage
```js
```json
// .stylelintrc
{
"plugins": ["./packages/stylelint-copyright/index.js"],
"plugins": ["stylelint-copyright"],
"rules": {
"plugin/stylelint-copyright": true
"docusaurus/copyright-header": true
}
}
```

View file

@ -1,32 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const testRule = require('stylelint-test-rule-tape');
const selectorCopyright = require('..');
testRule(selectorCopyright.rule, {
ruleName: selectorCopyright.ruleName,
config: [true],
accept: [
{
code: '/**\n* Copyright',
description: 'Copyright in the first comment',
},
{
code: '/**\n* copyright',
description: 'Copyright in the first comment',
},
],
reject: [
{
code: '/**\n* Hello',
message: 'Missing copyright in the first comment',
line: 2,
column: 3,
},
],
});

View file

@ -7,9 +7,9 @@
const stylelint = require('stylelint');
const ruleName = 'plugin/stylelint-copyright';
const ruleName = 'docusaurus/copyright-header';
const messages = stylelint.utils.ruleMessages(ruleName, {
rejected: 'Unexpected missing copyright in the first comment',
rejected: 'Missing copyright in the header comment',
});
module.exports = stylelint.createPlugin(ruleName, actual => {
@ -23,7 +23,7 @@ module.exports = stylelint.createPlugin(ruleName, actual => {
}
root.walkComments(comment => {
// ignore root comments with copyright text
// Ignore root comments with copyright text.
if (
comment === comment.parent.first &&
/[Cc]opyright/.test(comment.text)
@ -31,12 +31,12 @@ module.exports = stylelint.createPlugin(ruleName, actual => {
return;
}
// ignore non-root comments
// Ignore non-root comments.
if (comment.type !== 'root' && comment !== comment.parent.first) {
return;
}
// ignore indented comments
// Ignore indented comments.
if (comment.source.start.column > 1) {
return;
}

View file

@ -4,6 +4,9 @@
"description": "stylelint plugin to check css files for a copyright header",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "node tests"
},
"dependencies": {
"stylelint": "^13.2.0",
"stylelint-test-rule-tape": "^0.2.0"

View file

@ -0,0 +1,58 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const testRule = require('stylelint-test-rule-tape');
const stylelintCopyright = require('..');
testRule(stylelintCopyright.rule, {
ruleName: stylelintCopyright.ruleName,
accept: [
{
code: `
/**
* Copyright
*/
.foo {}`,
},
{
code: `
/**
* copyright
*/
.foo {}`,
},
],
reject: [
{
code: `
/**
* Copyleft
*/
.foo {}`,
message: `Missing copyright in the header comment (${stylelintCopyright.ruleName})`,
line: 2,
column: 1,
},
{
code: `
/**
* Copyleft
*/
/**
* Copyright
*/
.foo {}`,
message: `Missing copyright in the header comment (${stylelintCopyright.ruleName})`,
line: 2,
column: 1,
},
],
});