From 172d77cf10ca0ae7c1f28071c6212c69f9b6c7c7 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Tue, 17 May 2022 21:57:39 +0800 Subject: [PATCH] refactor(stylelint-copyright): migrate to TS --- packages/stylelint-copyright/index.js | 57 ------------------ packages/stylelint-copyright/package.json | 9 ++- .../__tests__/index.test.ts} | 37 ++++++++---- packages/stylelint-copyright/src/index.ts | 58 +++++++++++++++++++ packages/stylelint-copyright/tsconfig.json | 9 +++ 5 files changed, 100 insertions(+), 70 deletions(-) delete mode 100644 packages/stylelint-copyright/index.js rename packages/stylelint-copyright/{__tests__/index.test.js => src/__tests__/index.test.ts} (81%) create mode 100644 packages/stylelint-copyright/src/index.ts create mode 100644 packages/stylelint-copyright/tsconfig.json diff --git a/packages/stylelint-copyright/index.js b/packages/stylelint-copyright/index.js deleted file mode 100644 index fdf2ab794ba9..000000000000 --- a/packages/stylelint-copyright/index.js +++ /dev/null @@ -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; diff --git a/packages/stylelint-copyright/package.json b/packages/stylelint-copyright/package.json index c1e0c0a11af9..1afb9807cdd6 100644 --- a/packages/stylelint-copyright/package.json +++ b/packages/stylelint-copyright/package.json @@ -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" } } diff --git a/packages/stylelint-copyright/__tests__/index.test.js b/packages/stylelint-copyright/src/__tests__/index.test.ts similarity index 81% rename from packages/stylelint-copyright/__tests__/index.test.js rename to packages/stylelint-copyright/src/__tests__/index.test.ts index d2373e3d9988..f48b0499c67e 100644 --- a/packages/stylelint-copyright/__tests__/index.test.js +++ b/packages/stylelint-copyright/src/__tests__/index.test.ts @@ -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 { + 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, }, diff --git a/packages/stylelint-copyright/src/index.ts b/packages/stylelint-copyright/src/index.ts new file mode 100644 index 000000000000..a620d184d0a0 --- /dev/null +++ b/packages/stylelint-copyright/src/index.ts @@ -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; diff --git a/packages/stylelint-copyright/tsconfig.json b/packages/stylelint-copyright/tsconfig.json new file mode 100644 index 000000000000..f5902ba1089b --- /dev/null +++ b/packages/stylelint-copyright/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "incremental": true, + "tsBuildInfoFile": "./lib/.tsbuildinfo", + "rootDir": "src", + "outDir": "lib" + } +}