mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-11 15:22:29 +02:00
refactor(stylelint-copyright): migrate to TS (#7441)
This commit is contained in:
parent
71b5901bcd
commit
a0b0477dee
5 changed files with 100 additions and 70 deletions
|
@ -1,57 +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 stylelint = require('stylelint');
|
|
||||||
|
|
||||||
const ruleName = 'docusaurus/copyright-header';
|
|
||||||
const messages = stylelint.utils.ruleMessages(ruleName, {
|
|
||||||
rejected: 'Missing copyright in the header comment',
|
|
||||||
});
|
|
||||||
|
|
||||||
const plugin = stylelint.createPlugin(
|
|
||||||
ruleName,
|
|
||||||
(primaryOption, secondaryOption, context) => (root, result) => {
|
|
||||||
stylelint.utils.validateOptions(
|
|
||||||
result,
|
|
||||||
ruleName,
|
|
||||||
{
|
|
||||||
actual: primaryOption,
|
|
||||||
possible: [true, false],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
actual: secondaryOption,
|
|
||||||
possible: (v) => typeof v.header === 'string',
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
root.first &&
|
|
||||||
root.first.type === 'comment' &&
|
|
||||||
root.first.source.start.column === 1
|
|
||||||
) {
|
|
||||||
const {text} = root.first;
|
|
||||||
if (text === secondaryOption.header) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (context.fix) {
|
|
||||||
root.first?.before(`/*${secondaryOption.header}\n */`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
stylelint.utils.report({
|
|
||||||
message: messages.rejected,
|
|
||||||
node: root,
|
|
||||||
result,
|
|
||||||
ruleName,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
module.exports = plugin;
|
|
||||||
module.exports.ruleName = ruleName;
|
|
||||||
module.exports.messages = messages;
|
|
|
@ -2,14 +2,19 @@
|
||||||
"name": "stylelint-copyright",
|
"name": "stylelint-copyright",
|
||||||
"version": "2.0.0-beta.20",
|
"version": "2.0.0-beta.20",
|
||||||
"description": "Stylelint plugin to check CSS files for a copyright header.",
|
"description": "Stylelint plugin to check CSS files for a copyright header.",
|
||||||
"main": "index.js",
|
"main": "lib/index.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"watch": "tsc --watch"
|
||||||
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/facebook/docusaurus.git",
|
"url": "https://github.com/facebook/docusaurus.git",
|
||||||
"directory": "packages/stylelint-copyright"
|
"directory": "packages/stylelint-copyright"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"stylelint": "^14.8.2"
|
"stylelint": "^14.8.2",
|
||||||
|
"tslib": "^2.4.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,21 @@
|
||||||
*/
|
*/
|
||||||
/* eslint-disable jest/no-conditional-expect */
|
/* eslint-disable jest/no-conditional-expect */
|
||||||
|
|
||||||
const path = require('path');
|
import path from 'path';
|
||||||
const stylelint = require('stylelint');
|
import stylelint, {type LinterResult} from 'stylelint';
|
||||||
const rule = require('..');
|
import rule from '../index';
|
||||||
|
|
||||||
const {ruleName, messages} = rule;
|
const {ruleName} = rule;
|
||||||
|
|
||||||
function getOutputCss(output) {
|
declare global {
|
||||||
|
namespace jest {
|
||||||
|
interface Matchers<R> {
|
||||||
|
toHaveMessage: () => R;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getOutputCss(output: LinterResult) {
|
||||||
// eslint-disable-next-line no-underscore-dangle
|
// eslint-disable-next-line no-underscore-dangle
|
||||||
const result = output.results[0]._postcssResult;
|
const result = output.results[0]._postcssResult;
|
||||||
return result.root.toString(result.opts.syntax);
|
return result.root.toString(result.opts.syntax);
|
||||||
|
@ -97,6 +105,8 @@ function testStylelintRule(config, tests) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
message: () =>
|
||||||
|
'Expected "reject" test case to not have a "message" property',
|
||||||
pass: true,
|
pass: true,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
@ -106,7 +116,7 @@ function testStylelintRule(config, tests) {
|
||||||
|
|
||||||
testStylelintRule(
|
testStylelintRule(
|
||||||
{
|
{
|
||||||
plugins: [path.join(__dirname, '..')],
|
plugins: [path.join(__dirname, '../../lib/index.js')],
|
||||||
rules: {
|
rules: {
|
||||||
[ruleName]: [true, {header: '*\n * Copyright'}],
|
[ruleName]: [true, {header: '*\n * Copyright'}],
|
||||||
},
|
},
|
||||||
|
@ -143,7 +153,8 @@ testStylelintRule(
|
||||||
* Copyright
|
* Copyright
|
||||||
*/
|
*/
|
||||||
.foo {}`,
|
.foo {}`,
|
||||||
message: messages.rejected,
|
message:
|
||||||
|
'Missing copyright in the header comment (docusaurus/copyright-header)',
|
||||||
line: 1,
|
line: 1,
|
||||||
column: 1,
|
column: 1,
|
||||||
},
|
},
|
||||||
|
@ -154,7 +165,8 @@ testStylelintRule(
|
||||||
* Copyright
|
* Copyright
|
||||||
*/
|
*/
|
||||||
.foo {}`,
|
.foo {}`,
|
||||||
message: messages.rejected,
|
message:
|
||||||
|
'Missing copyright in the header comment (docusaurus/copyright-header)',
|
||||||
line: 1,
|
line: 1,
|
||||||
column: 1,
|
column: 1,
|
||||||
},
|
},
|
||||||
|
@ -173,7 +185,8 @@ testStylelintRule(
|
||||||
*/
|
*/
|
||||||
|
|
||||||
.foo {}`,
|
.foo {}`,
|
||||||
message: messages.rejected,
|
message:
|
||||||
|
'Missing copyright in the header comment (docusaurus/copyright-header)',
|
||||||
line: 1,
|
line: 1,
|
||||||
column: 1,
|
column: 1,
|
||||||
},
|
},
|
||||||
|
@ -192,7 +205,8 @@ testStylelintRule(
|
||||||
*/
|
*/
|
||||||
|
|
||||||
.foo {}`,
|
.foo {}`,
|
||||||
message: messages.rejected,
|
message:
|
||||||
|
'Missing copyright in the header comment (docusaurus/copyright-header)',
|
||||||
line: 1,
|
line: 1,
|
||||||
column: 1,
|
column: 1,
|
||||||
},
|
},
|
||||||
|
@ -217,7 +231,8 @@ testStylelintRule(
|
||||||
* Copyright
|
* Copyright
|
||||||
*/
|
*/
|
||||||
.foo {}`,
|
.foo {}`,
|
||||||
message: messages.rejected,
|
message:
|
||||||
|
'Missing copyright in the header comment (docusaurus/copyright-header)',
|
||||||
line: 1,
|
line: 1,
|
||||||
column: 1,
|
column: 1,
|
||||||
},
|
},
|
58
packages/stylelint-copyright/src/index.ts
Normal file
58
packages/stylelint-copyright/src/index.ts
Normal 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import stylelint from 'stylelint';
|
||||||
|
|
||||||
|
const ruleName = 'docusaurus/copyright-header';
|
||||||
|
const messages = stylelint.utils.ruleMessages(ruleName, {
|
||||||
|
rejected: 'Missing copyright in the header comment',
|
||||||
|
});
|
||||||
|
|
||||||
|
type SecondaryOption = {header?: string};
|
||||||
|
|
||||||
|
const plugin = stylelint.createPlugin(
|
||||||
|
ruleName,
|
||||||
|
(primaryOption: boolean, secondaryOption: SecondaryOption, context) =>
|
||||||
|
(root, result) => {
|
||||||
|
stylelint.utils.validateOptions(
|
||||||
|
result,
|
||||||
|
ruleName,
|
||||||
|
{
|
||||||
|
actual: primaryOption,
|
||||||
|
possible: [true, false],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
actual: secondaryOption,
|
||||||
|
possible: (v) => typeof (v as SecondaryOption)?.header === 'string',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
root.first &&
|
||||||
|
root.first.type === 'comment' &&
|
||||||
|
root.first.source?.start?.column === 1
|
||||||
|
) {
|
||||||
|
const {text} = root.first;
|
||||||
|
if (text === secondaryOption.header) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (context.fix) {
|
||||||
|
root.first?.before(`/*${secondaryOption.header}\n */`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
stylelint.utils.report({
|
||||||
|
message: messages.rejected,
|
||||||
|
node: root,
|
||||||
|
result,
|
||||||
|
ruleName,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export = plugin;
|
9
packages/stylelint-copyright/tsconfig.json
Normal file
9
packages/stylelint-copyright/tsconfig.json
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"extends": "../../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"incremental": true,
|
||||||
|
"tsBuildInfoFile": "./lib/.tsbuildinfo",
|
||||||
|
"rootDir": "src",
|
||||||
|
"outDir": "lib"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue