Skip to content

Commit

Permalink
refactor(stylelint-copyright): migrate to TS (#7441)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena authored May 17, 2022
1 parent 71b5901 commit a0b0477
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 70 deletions.
57 changes: 0 additions & 57 deletions packages/stylelint-copyright/index.js

This file was deleted.

9 changes: 7 additions & 2 deletions packages/stylelint-copyright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -97,6 +105,8 @@ function testStylelintRule(config, tests) {
}

return {
message: () =>
'Expected "reject" test case to not have a "message" property',
pass: true,
};
},
Expand All @@ -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'}],
},
Expand Down Expand Up @@ -143,7 +153,8 @@ testStylelintRule(
* Copyright
*/
.foo {}`,
message: messages.rejected,
message:
'Missing copyright in the header comment (docusaurus/copyright-header)',
line: 1,
column: 1,
},
Expand All @@ -154,7 +165,8 @@ testStylelintRule(
* Copyright
*/
.foo {}`,
message: messages.rejected,
message:
'Missing copyright in the header comment (docusaurus/copyright-header)',
line: 1,
column: 1,
},
Expand All @@ -173,7 +185,8 @@ testStylelintRule(
*/
.foo {}`,
message: messages.rejected,
message:
'Missing copyright in the header comment (docusaurus/copyright-header)',
line: 1,
column: 1,
},
Expand All @@ -192,7 +205,8 @@ testStylelintRule(
*/
.foo {}`,
message: messages.rejected,
message:
'Missing copyright in the header comment (docusaurus/copyright-header)',
line: 1,
column: 1,
},
Expand All @@ -217,7 +231,8 @@ testStylelintRule(
* Copyright
*/
.foo {}`,
message: messages.rejected,
message:
'Missing copyright in the header comment (docusaurus/copyright-header)',
line: 1,
column: 1,
},
Expand Down
58 changes: 58 additions & 0 deletions packages/stylelint-copyright/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions packages/stylelint-copyright/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"rootDir": "src",
"outDir": "lib"
}
}

0 comments on commit a0b0477

Please sign in to comment.