Skip to content

Commit 92d5088

Browse files
chore: update README.md (#32)
1 parent 2535ba2 commit 92d5088

6 files changed

Lines changed: 44 additions & 6 deletions

File tree

src/cli.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ program
1818
.addOption(
1919
new Option('-t, --transform <transform>', 'Transform name').choices(transformNames).makeOptionMandatory()
2020
)
21+
// TODO: make it `true` by default after switching to bulk format.
22+
.option('-f, --format [format]', 'Format Typescript source files with ESLint', false)
2123
.option('-w, --write [write]', 'Persist codemod changes to the filesystem', false)
2224
.argument('<fileGlob>', 'File glob or globs to change files based on')
2325
.action(async (fileGlob: string, options: CodemodCliOptions) => {
24-
const { write: shouldWriteFiles, transform } = options
26+
const { write: shouldWriteFiles, format: shouldFormat, transform } = options
2527
const projectGlob = path.isAbsolute(fileGlob) ? fileGlob : path.join(process.cwd(), fileGlob)
2628

2729
signale.start(`Starting codemod "${transform}" with project glob "${projectGlob}"`)
2830

2931
const project = new Project()
3032
project.addSourceFilesAtPaths(projectGlob)
3133

32-
const results = await transforms[transform]({ project, shouldWriteFiles })
34+
const results = await transforms[transform]({ project, shouldWriteFiles, shouldFormat })
3335

3436
if (results) {
3537
if (shouldWriteFiles) {

src/transforms/globalCssToCssModule/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,36 @@ Convert globally scoped stylesheet tied to the React component into a CSS Module
1212

1313
## Usage
1414

15+
See available commands and options:
16+
17+
```sh
18+
yarn transform --help
19+
```
20+
21+
Apply codemod:
22+
1523
```sh
1624
yarn transform -t globalCssToCssModule --write true "/client/web/src/search/**/*.tsx"
1725
```
26+
27+
To automatically fix formatting in the updated React components use the `--format true` flag.
28+
29+
```sh
30+
yarn transform -t globalCssToCssModule --write true --format true "/client/web/src/search/**/*.tsx"
31+
```
32+
33+
Currently, it's quite slow because a new ESLint instance is initialized for each file. Consider running `eslint --fix` after the codemod if you're changing a lot of files in one command.
34+
35+
## Post codemod steps in the Sourcegraph repo
36+
37+
1. Remove global styles import from the codebase: `@import 'DeletedGlobalStylesFile';`.
38+
2. Check "Unused CSS classes" reported in the CLI output. These are classes that exist in the CSS module but were not found in the adjacent React component.
39+
1. Remove redundant CSS classes from the CSS module.
40+
2. Fix classes used outside of the component by using CSS module there or convert them to props to eliminate the implicit dependency.
41+
3. Double-check the usage of the deleted global CSS classes in integration tests or other stylesheets.
42+
1. If global CSS class is used in the integration test — add `data-testid="some-name-here"` to the relevant JSX element and use `[data-testid="some-name-here"]` selector in the integration test.
43+
4. Fix usage of the global SCSS variables in the CSS module:`color: $success; -> color: var(--success);`
44+
1. If CSS variable doesn't exist — add it next to the SCSS variable: `$spacer: 1px; -> $spacer: 1px; :root { --spacer: #{$spacer}; }`
45+
5. Double-check that the `:global(...)` selector usage in the created CSS module is correct.
46+
6. Run `yarn test -u` to update Jest snapshots.
47+
7. Run `eslint --fix` to update the Typescript source files formatting if `--format` flag was not used in the codemod command.

src/transforms/globalCssToCssModule/__tests__/globalCssToCssModule.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('globalCssToCssModule', () => {
1010
it('transforms correctly', async () => {
1111
const project = new Project()
1212
project.addSourceFilesAtPaths(TARGET_FILE)
13-
const codemodResults = await globalCssToCssModule({ project })
13+
const codemodResults = await globalCssToCssModule({ project, shouldFormat: true })
1414

1515
expect(codemodResults).toBeTruthy()
1616

src/transforms/globalCssToCssModule/globalCssToCssModule.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { transformComponentFile } from './ts/transformComponentFile'
2525
*
2626
*/
2727
export async function globalCssToCssModule(options: CodemodOptions): CodemodResult {
28-
const { project, shouldWriteFiles } = options
28+
const { project, shouldWriteFiles, shouldFormat } = options
2929
/**
3030
* Find `.tsx` files with co-located `.scss` file.
3131
* For example `RepoHeader.tsx` should have matching `RepoHeader.scss` in the same folder.
@@ -75,7 +75,9 @@ export async function globalCssToCssModule(options: CodemodOptions): CodemodResu
7575
moduleSpecifier: `./${path.parse(cssModuleFileName).base}`,
7676
})
7777

78-
formatWithPrettierEslint(tsSourceFile)
78+
if (shouldFormat) {
79+
formatWithPrettierEslint(tsSourceFile)
80+
}
7981
const formattedCssModuleSource = await formatWithStylelint(cssModuleSource, cssFilePath)
8082

8183
/**

src/transforms/globalCssToCssModule/ts/getClassNameNodeReplacement.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ export function getClassNameNodeReplacement(
9292
if (
9393
parentKind === SyntaxKind.ConditionalExpression ||
9494
parentKind === SyntaxKind.CallExpression ||
95-
parentKind === SyntaxKind.BinaryExpression
95+
parentKind === SyntaxKind.BinaryExpression ||
96+
parentKind === SyntaxKind.VariableDeclaration
9697
) {
9798
// Replace one class string inside of `ConditionalExpression` with the `exportName`.
9899
// className={classNames('d-flex', isActive ? 'kek' : 'pek')} -> className={classNames('d-flex', isActive ? styles.kek : 'pek')}
99100
return { isParentTransformed: false, replacement: replacementWithoutBraces }
100101
}
102+
101103
if (parentKind === SyntaxKind.JsxAttribute) {
102104
const replacement = ts.factory.createJsxExpression(undefined, replacementWithoutBraces)
103105

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ export interface CodemodOptions {
1818
project: Project
1919
/** If `true` persist changes made by the codemod to the filesystem. */
2020
shouldWriteFiles?: boolean
21+
/** If `true` format Typescript source files with `prettier-eslint`. */
22+
shouldFormat?: boolean
2123
}

0 commit comments

Comments
 (0)