refactor(stylelint-copyright): migrate to TS (#7441)

This commit is contained in:
Joshua Chen 2022-05-17 22:13:47 +08:00 committed by GitHub
parent 71b5901bcd
commit a0b0477dee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 70 deletions

View file

@ -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;

View file

@ -2,14 +2,19 @@
"name": "stylelint-copyright",
"version": "2.0.0-beta.20",
"description": "Stylelint plugin to check CSS files for a copyright header.",
"main": "index.js",
"main": "lib/index.js",
"license": "MIT",
"scripts": {
"build": "tsc",
"watch": "tsc --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/facebook/docusaurus.git",
"directory": "packages/stylelint-copyright"
},
"dependencies": {
"stylelint": "^14.8.2"
"stylelint": "^14.8.2",
"tslib": "^2.4.0"
}
}

View file

@ -6,13 +6,21 @@
*/
/* eslint-disable jest/no-conditional-expect */
const path = require('path');
const stylelint = require('stylelint');
const rule = require('..');
import path from 'path';
import stylelint, {type LinterResult} from 'stylelint';
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
const result = output.results[0]._postcssResult;
return result.root.toString(result.opts.syntax);
@ -97,6 +105,8 @@ function testStylelintRule(config, tests) {
}
return {
message: () =>
'Expected "reject" test case to not have a "message" property',
pass: true,
};
},
@ -106,7 +116,7 @@ function testStylelintRule(config, tests) {
testStylelintRule(
{
plugins: [path.join(__dirname, '..')],
plugins: [path.join(__dirname, '../../lib/index.js')],
rules: {
[ruleName]: [true, {header: '*\n * Copyright'}],
},
@ -143,7 +153,8 @@ testStylelintRule(
* Copyright
*/
.foo {}`,
message: messages.rejected,
message:
'Missing copyright in the header comment (docusaurus/copyright-header)',
line: 1,
column: 1,
},
@ -154,7 +165,8 @@ testStylelintRule(
* Copyright
*/
.foo {}`,
message: messages.rejected,
message:
'Missing copyright in the header comment (docusaurus/copyright-header)',
line: 1,
column: 1,
},
@ -173,7 +185,8 @@ testStylelintRule(
*/
.foo {}`,
message: messages.rejected,
message:
'Missing copyright in the header comment (docusaurus/copyright-header)',
line: 1,
column: 1,
},
@ -192,7 +205,8 @@ testStylelintRule(
*/
.foo {}`,
message: messages.rejected,
message:
'Missing copyright in the header comment (docusaurus/copyright-header)',
line: 1,
column: 1,
},
@ -217,7 +231,8 @@ testStylelintRule(
* Copyright
*/
.foo {}`,
message: messages.rejected,
message:
'Missing copyright in the header comment (docusaurus/copyright-header)',
line: 1,
column: 1,
},

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.
*/
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;

View file

@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"rootDir": "src",
"outDir": "lib"
}
}