-
Notifications
You must be signed in to change notification settings - Fork 21
Migrated scripts to typescript #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,25 +4,29 @@ | |
| import { kebabToCamelCase } from "./utils/kebabToKamelCase"; | ||
|
|
||
| // Sort function to keep rules and config sorted alphabetically | ||
| const nameSort = (a, b) => { | ||
| const nameSort = (a: any, b: any) => { | ||
| const aName = a.key.type === "Literal" ? a.key.value : a.key.name; | ||
| const bName = a.key.type === "Literal" ? b.key.value : b.key.name; | ||
| const bName = b.key.type === "Literal" ? b.key.value : b.key.name; | ||
| if (aName < bName) return -1; | ||
| if (aName > bName) return 1; | ||
| return 0; | ||
| }; | ||
|
|
||
| const transformer = (file, api, options) => { | ||
| interface TransformerOptions { | ||
| ruleName: string; | ||
| } | ||
|
|
||
| export const transformer = (file: any, api: any, options: any): string | null => { | ||
| const j = api.jscodeshift; | ||
| const root = j(file.source); | ||
| const { ruleName } = options; // No need for rulePath in this case | ||
| const { ruleName } = options; | ||
|
|
||
| let changesMade = 0; | ||
|
|
||
| // Step 1: Add rule to the `rules` object (without parentheses) | ||
| root.find(j.Property, { key: { name: "rules" } }) | ||
| .at(0) | ||
| .forEach(path => { | ||
| .forEach((path: any) => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any in this file too |
||
| const properties = path.value.value.properties; | ||
| properties.unshift( | ||
| j.property("init", j.literal(ruleName), j.memberExpression(j.identifier("rules"), j.identifier(kebabToCamelCase(ruleName)))) | ||
|
|
@@ -32,11 +36,11 @@ const transformer = (file, api, options) => { | |
| }); | ||
|
|
||
| // Step 2: Find and modify `configs.recommended.rules` | ||
| root.find(j.Property, { key: { name: "configs" } }).forEach(configPath => { | ||
| const recommendedConfig = configPath.value.value.properties.find(prop => prop.key.name === "recommended"); | ||
| root.find(j.Property, { key: { name: "configs" } }).forEach((configPath: any) => { | ||
| const recommendedConfig = configPath.value.value.properties.find((prop: any) => prop.key.name === "recommended"); | ||
|
|
||
| if (recommendedConfig) { | ||
| const recommendedRules = recommendedConfig.value.properties.find(prop => prop.key.name === "rules"); | ||
| const recommendedRules = recommendedConfig.value.properties.find((prop: any) => prop.key.name === "rules"); | ||
|
|
||
| if (recommendedRules) { | ||
| const rulesProps = recommendedRules.value.properties; | ||
|
|
@@ -53,4 +57,4 @@ const transformer = (file, api, options) => { | |
|
|
||
| return root.toSource({ quote: "double", trailingComma: false }); | ||
| }; | ||
| module.exports = transformer; | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you test this script with the repo ex. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,14 @@ | |
| // Licensed under the MIT License. | ||
|
|
||
| /* eslint-disable no-console */ | ||
| const { resolve } = require("path"); | ||
| const { existsSync, writeFileSync } = require("fs"); | ||
| const { exec } = require("child_process"); | ||
| const yargs = require("yargs/yargs"); // Use yargs/yargs for modules | ||
| const { hideBin } = require("yargs/helpers"); // To handle CLI arguments | ||
| const ruleBoilerplateGenerator = require("./boilerplate/rule"); | ||
| const testBoilerplateGenerator = require("./boilerplate/test"); | ||
| const docBoilerplateGenerator = require("./boilerplate/doc"); | ||
| import { resolve } from "path"; | ||
| import { existsSync, writeFileSync } from "fs"; | ||
| import { exec } from "child_process"; | ||
| import yargs from "yargs/yargs"; | ||
| import { hideBin } from "yargs/helpers"; | ||
| import {ruleBoilerplateGenerator} from "./boilerplate/rule"; | ||
| import {testBoilerplateGenerator} from "./boilerplate/test"; | ||
| import {docBoilerplateGenerator} from "./boilerplate/doc"; | ||
|
|
||
| // Define the yargs configuration | ||
| const argv = yargs(hideBin(process.argv)) | ||
|
|
@@ -27,11 +27,11 @@ const argv = yargs(hideBin(process.argv)) | |
| default: "$DESCRIPTION" // Provide default value | ||
| } | ||
| }) | ||
| .demandCommand(1, "You must provide the rule name.").argv; // Make the rule name (positional) | ||
| .demandCommand(1, "You must provide the rule name.").argv as any; // Type assertion for yargs | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per Aubrey's other comments, See the yargs docs for how to avoid this casting. Since there's no async processes occurring, you can replace |
||
|
|
||
| const ruleName = argv._[0]; | ||
| const author = argv.author || "$AUTHOR"; | ||
| const description = argv.description || "$DESCRIPTION"; | ||
| const ruleName: string = argv._[0]; | ||
| const author: string = argv.author || "$AUTHOR"; | ||
| const description: string = argv.description || "$DESCRIPTION"; | ||
|
|
||
| const rulePath = resolve(`lib/rules/${ruleName}.ts`); | ||
| const testPath = resolve(`tests/lib/rules/${ruleName}-test.ts`); | ||
|
|
@@ -106,3 +106,4 @@ exec(commandForMainIndex, (error, stdout, stderr) => { | |
| console.log(`stdout: ${stdout}`); | ||
| }); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| export const kebabToCamelCase = str => { | ||
| export const kebabToCamelCase = (str: string): string => { | ||
| return str.replace(/[-_](.)/g, (_, char) => char.toUpperCase()).replace(/^(.)/, (_, char) => char.toLowerCase()); | ||
| }; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's avoid 'any' and use the types.
import { TSESTree } from "@typescript-eslint/utils";that package should have the right types.