Skip to content

Commit 1eca852

Browse files
committed
Migrated scripts to typescript
1 parent 3977d44 commit 1eca852

File tree

7 files changed

+57
-33
lines changed

7 files changed

+57
-33
lines changed

scripts/addRuleToExportIndex.js renamed to scripts/addRuleToExportIndex.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
import fs from "fs";
55
import { kebabToCamelCase } from "./utils/kebabToKamelCase";
66

7-
function transformer(file, api, options) {
7+
// @ts-ignore: No types for jscodeshift
8+
import { FileInfo, API, Options } from "jscodeshift";
9+
10+
interface TransformerOptions {
11+
ruleName: string;
12+
exportIndexFilePath: string;
13+
}
14+
15+
export function transformer(file: FileInfo, api: API, options: TransformerOptions): string {
816
const j = api.jscodeshift;
917
const { ruleName, exportIndexFilePath } = options;
1018

@@ -45,14 +53,15 @@ function transformer(file, api, options) {
4553
// Manually sort the export statements alphabetically
4654
const sortedExports = updatedExportStatements
4755
.nodes()
48-
.map(node => {
56+
57+
.map((node: any) => {
4958
if (node.specifiers && node.specifiers[0] && node.specifiers[0].exported) {
5059
return node;
5160
}
5261
return null; // Ignore nodes without valid specifiers
5362
})
54-
.filter(node => node !== null) // Remove nulls
55-
.sort((a, b) => {
63+
.filter((node: any) => node !== null) // Remove nulls
64+
.sort((a: any, b: any) => {
5665
const aName = a.specifiers[0].exported.name;
5766
const bName = b.specifiers[0].exported.name;
5867
return aName.localeCompare(bName);
@@ -63,7 +72,7 @@ function transformer(file, api, options) {
6372

6473
// Now insert the sorted export statements back into the AST
6574
const body = exportIndexSource.get().node.program.body;
66-
sortedExports.forEach(exportNode => {
75+
sortedExports.forEach((exportNode: any) => {
6776
body.push(exportNode); // Insert each export statement at the end of the body
6877
});
6978
}
@@ -74,4 +83,4 @@ function transformer(file, api, options) {
7483
// Return the original file source (this is for the main file passed in)
7584
return file.source;
7685
}
77-
module.exports = transformer;
86+

scripts/addRuleToIndex.js renamed to scripts/addRuleToIndex.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@
44
import { kebabToCamelCase } from "./utils/kebabToKamelCase";
55

66
// Sort function to keep rules and config sorted alphabetically
7-
const nameSort = (a, b) => {
7+
const nameSort = (a: any, b: any) => {
88
const aName = a.key.type === "Literal" ? a.key.value : a.key.name;
9-
const bName = a.key.type === "Literal" ? b.key.value : b.key.name;
9+
const bName = b.key.type === "Literal" ? b.key.value : b.key.name;
1010
if (aName < bName) return -1;
1111
if (aName > bName) return 1;
1212
return 0;
1313
};
1414

15-
const transformer = (file, api, options) => {
15+
interface TransformerOptions {
16+
ruleName: string;
17+
}
18+
19+
export const transformer = (file: any, api: any, options: any): string | null => {
1620
const j = api.jscodeshift;
1721
const root = j(file.source);
18-
const { ruleName } = options; // No need for rulePath in this case
22+
const { ruleName } = options;
1923

2024
let changesMade = 0;
2125

2226
// Step 1: Add rule to the `rules` object (without parentheses)
2327
root.find(j.Property, { key: { name: "rules" } })
2428
.at(0)
25-
.forEach(path => {
29+
.forEach((path: any) => {
2630
const properties = path.value.value.properties;
2731
properties.unshift(
2832
j.property("init", j.literal(ruleName), j.memberExpression(j.identifier("rules"), j.identifier(kebabToCamelCase(ruleName))))
@@ -32,11 +36,11 @@ const transformer = (file, api, options) => {
3236
});
3337

3438
// Step 2: Find and modify `configs.recommended.rules`
35-
root.find(j.Property, { key: { name: "configs" } }).forEach(configPath => {
36-
const recommendedConfig = configPath.value.value.properties.find(prop => prop.key.name === "recommended");
39+
root.find(j.Property, { key: { name: "configs" } }).forEach((configPath: any) => {
40+
const recommendedConfig = configPath.value.value.properties.find((prop: any) => prop.key.name === "recommended");
3741

3842
if (recommendedConfig) {
39-
const recommendedRules = recommendedConfig.value.properties.find(prop => prop.key.name === "rules");
43+
const recommendedRules = recommendedConfig.value.properties.find((prop: any) => prop.key.name === "rules");
4044

4145
if (recommendedRules) {
4246
const rulesProps = recommendedRules.value.properties;
@@ -53,4 +57,4 @@ const transformer = (file, api, options) => {
5357

5458
return root.toSource({ quote: "double", trailingComma: false });
5559
};
56-
module.exports = transformer;
60+

scripts/boilerplate/doc.js renamed to scripts/boilerplate/doc.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
const docBoilerplateGenerator = (name, description) => `# ${description} (@microsoft/fluentui-jsx-a11y/${name})
4+
export const docBoilerplateGenerator = (
5+
name: string,
6+
description: string
7+
): string => `# ${description} (@microsoft/fluentui-jsx-a11y/${name})
58
69
Write a useful explanation here!
710
@@ -19,4 +22,4 @@ Write more details here!
1922
2023
## Further Reading
2124
`;
22-
module.exports = docBoilerplateGenerator;
25+

scripts/boilerplate/rule.js renamed to scripts/boilerplate/rule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
const ruleBoilerplate = (name, description) => `// Copyright (c) Microsoft Corporation.
4+
export const ruleBoilerplateGenerator = (name: string, description: string): string => `// Copyright (c) Microsoft Corporation.
55
// Licensed under the MIT License.
66
77
import { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
@@ -42,4 +42,4 @@ const rule = createRule({
4242
4343
export default rule;
4444
`;
45-
module.exports = ruleBoilerplate;
45+

scripts/boilerplate/test.js renamed to scripts/boilerplate/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
const testBoilerplate = name => `// Copyright (c) Microsoft Corporation.
4+
export const testBoilerplateGenerator = (name: string): string => `// Copyright (c) Microsoft Corporation.
55
// Licensed under the MIT License.
66
77
import { Rule } from "eslint";
@@ -21,4 +21,4 @@ ruleTester.run("${name}", rule as unknown as Rule.RuleModule, {
2121
]
2222
});
2323
`;
24-
module.exports = testBoilerplate;
24+

scripts/create-rule.js renamed to scripts/create-rule.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
// Licensed under the MIT License.
33

44
/* eslint-disable no-console */
5-
const { resolve } = require("path");
6-
const { existsSync, writeFileSync } = require("fs");
7-
const { exec } = require("child_process");
8-
const yargs = require("yargs/yargs"); // Use yargs/yargs for modules
9-
const { hideBin } = require("yargs/helpers"); // To handle CLI arguments
5+
import { resolve } from "path";
6+
import { existsSync, writeFileSync } from "fs";
7+
import { exec } from "child_process";
8+
// @ts-ignore: yargs has no default export
9+
import yargs from "yargs/yargs";
10+
// @ts-ignore: yargs/helpers has no default export
11+
import { hideBin } from "yargs/helpers";
12+
// Use require for boilerplate generators (CommonJS)
13+
// @ts-ignore
1014
const ruleBoilerplateGenerator = require("./boilerplate/rule");
15+
// @ts-ignore
1116
const testBoilerplateGenerator = require("./boilerplate/test");
17+
// @ts-ignore
1218
const docBoilerplateGenerator = require("./boilerplate/doc");
1319

1420
// Define the yargs configuration
@@ -18,20 +24,20 @@ const argv = yargs(hideBin(process.argv))
1824
alias: "a",
1925
type: "string",
2026
describe: "Author of the rule",
21-
default: "$AUTHOR" // Provide default value
27+
default: "$AUTHOR"
2228
},
2329
description: {
2430
alias: "d",
2531
type: "string",
2632
describe: "Description of the rule",
27-
default: "$DESCRIPTION" // Provide default value
33+
default: "$DESCRIPTION"
2834
}
2935
})
30-
.demandCommand(1, "You must provide the rule name.").argv; // Make the rule name (positional)
36+
.demandCommand(1, "You must provide the rule name.").argv as any; // Type assertion for yargs
3137

32-
const ruleName = argv._[0];
33-
const author = argv.author || "$AUTHOR";
34-
const description = argv.description || "$DESCRIPTION";
38+
const ruleName: string = argv._[0];
39+
const author: string = argv.author || "$AUTHOR";
40+
const description: string = argv.description || "$DESCRIPTION";
3541

3642
const rulePath = resolve(`lib/rules/${ruleName}.ts`);
3743
const testPath = resolve(`tests/lib/rules/${ruleName}-test.ts`);
@@ -106,3 +112,4 @@ exec(commandForMainIndex, (error, stdout, stderr) => {
106112
console.log(`stdout: ${stdout}`);
107113
});
108114
});
115+
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
export const kebabToCamelCase = str => {
4+
export const kebabToCamelCase = (str: string): string => {
55
return str.replace(/[-_](.)/g, (_, char) => char.toUpperCase()).replace(/^(.)/, (_, char) => char.toLowerCase());
66
};
7+

0 commit comments

Comments
 (0)