Skip to content

Commit

Permalink
chore(v2): add stylelint (#2361)
Browse files Browse the repository at this point in the history
* Add stylelint

* Add custom stylelint plugin for copyright

* Add stylelint to FB template
  • Loading branch information
scottilee authored Mar 6, 2020
1 parent 4aee06e commit ae78c1e
Show file tree
Hide file tree
Showing 11 changed files with 1,462 additions and 36 deletions.
6 changes: 6 additions & 0 deletions .stylelintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": ["./packages/stylelint-copyright/index.js"],
"rules": {
"plugin/stylelint-copyright": true
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"prettier": "prettier --config .prettierrc --write \"**/*.{js,ts}\"",
"prettier:diff": "prettier --config .prettierrc --list-different \"**/*.{js,ts}\"",
"prettier-docs": "prettier --config .prettierrc --write \"**/*.md\"",
"lint": "eslint --cache \"**/*.js\"",
"lint": "eslint --cache \"**/*.js\" && stylelint \"**/*.css\"",
"lerna": "lerna",
"test": "jest",
"tsc": "lerna run tsc --no-private"
Expand Down Expand Up @@ -62,6 +62,7 @@
"prettier": "^1.19.1",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"stylelint": "^13.2.0",
"typescript": "^3.7.2"
},
"lint-staged": {
Expand Down
7 changes: 7 additions & 0 deletions packages/docusaurus-init/templates/classic/src/css/custom.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* 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.
*/

/**
* Any CSS included here will be global. The classic template
* bundles Infima by default. Infima is a CSS framework designed to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* 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.
*/

/**
* CSS files with the .module.css suffix will be treated as CSS modules
* and scoped locally.
Expand Down
6 changes: 6 additions & 0 deletions packages/docusaurus-init/templates/facebook/.stylelintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": ["../../..//stylelint-copyright/index.js"],
"rules": {
"plugin/stylelint-copyright": true
}
}
5 changes: 3 additions & 2 deletions packages/docusaurus-init/templates/facebook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
"ci": "yarn lint && yarn prettier:diff",
"lint": "eslint --cache \"**/*.js\"",
"lint": "eslint --cache \"**/*.js\" && stylelint \"**/*.css\"",
"prettier": "prettier --config .prettierrc --write \"**/*.{js,md}\"",
"prettier:diff": "prettier --config .prettierrc --list-different \"**/*.{js,md}\""
},
Expand All @@ -17,7 +17,8 @@
"@docusaurus/preset-classic": "^2.0.0-alpha.43",
"classnames": "^2.2.6",
"react": "^16.8.4",
"react-dom": "^16.8.4"
"react-dom": "^16.8.4",
"stylelint": "^13.2.0"
},
"devDependencies": {
"babel-eslint": "^10.0.3",
Expand Down
22 changes: 22 additions & 0 deletions packages/stylelint-copyright/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# stylelint-copyright

stylelint plugin to check css files for a copyright header

```css
/*
* Copyright ...
* ...
*/
```

## Usage

```js
// .stylelintrc
{
"plugins": ["./packages/stylelint-copyright/index.js"],
"rules": {
"plugin/stylelint-copyright": true
}
}
```
32 changes: 32 additions & 0 deletions packages/stylelint-copyright/_tests_/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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,
},
],
});
55 changes: 55 additions & 0 deletions packages/stylelint-copyright/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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 stylelint = require('stylelint');

const ruleName = 'plugin/stylelint-copyright';
const messages = stylelint.utils.ruleMessages(ruleName, {
rejected: 'Unexpected missing copyright in the first comment',
});

module.exports = stylelint.createPlugin(ruleName, actual => {
return (root, result) => {
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual,
});

if (!validOptions) {
return;
}

root.walkComments(comment => {
// ignore root comments with copyright text
if (
comment === comment.parent.first &&
/[Cc]opyright/.test(comment.text)
) {
return;
}

// ignore non-root comments
if (comment.type !== 'root' && comment !== comment.parent.first) {
return;
}

// ignore indented comments
if (comment.source.start.column > 1) {
return;
}

stylelint.utils.report({
message: messages.rejected,
node: comment,
result,
ruleName,
});
});
};
});

module.exports.ruleName = ruleName;
module.exports.messages = messages;
11 changes: 11 additions & 0 deletions packages/stylelint-copyright/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "stylelint-copyright",
"version": "1.0.0",
"description": "stylelint plugin to check css files for a copyright header",
"main": "index.js",
"license": "MIT",
"dependencies": {
"stylelint": "^13.2.0",
"stylelint-test-rule-tape": "^0.2.0"
}
}
Loading

0 comments on commit ae78c1e

Please sign in to comment.