Skip to content

Commit b743af8

Browse files
committed
Set up prettier
1 parent 9c77905 commit b743af8

File tree

23 files changed

+1077
-552
lines changed

23 files changed

+1077
-552
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ jobs:
2525
run: yarn tsc
2626
# - name: Lint
2727
# run: yarn lint --max-warnings 0
28-
# - name: Check formatting
29-
# run: yarn fmt:check
28+
- name: Check formatting
29+
run: yarn fmt:check

.prettierignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.github/workflows
2+
.pnp.cjs
3+
.pnp.loader.mjs
4+
.vim
5+
.vscode
6+
.yarn
7+
.yarnrc.yml
8+
cjs/dist
9+
dist

.vscode/extensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"recommendations": [
3-
"arcanis.vscode-zipfs"
3+
"arcanis.vscode-zipfs",
4+
"esbenp.prettier-vscode"
45
]
56
}

.yarn/sdks/prettier/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
3+
const {existsSync} = require(`fs`);
4+
const {createRequire} = require(`module`);
5+
const {resolve} = require(`path`);
6+
7+
const relPnpApiPath = "../../../.pnp.cjs";
8+
9+
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
10+
const absRequire = createRequire(absPnpApiPath);
11+
12+
if (existsSync(absPnpApiPath)) {
13+
if (!process.versions.pnp) {
14+
// Setup the environment to be able to require prettier/index.js
15+
require(absPnpApiPath).setup();
16+
}
17+
}
18+
19+
// Defer to the real prettier/index.js your application uses
20+
module.exports = absRequire(`prettier/index.js`);

.yarn/sdks/prettier/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "prettier",
3+
"version": "2.8.7-sdk",
4+
"main": "./index.js",
5+
"type": "commonjs"
6+
}

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
This codemod automatically transforms **React class components** into **React functional components using Hooks** for you!
44

5-
|Before|After|
6-
|---|---|
7-
|![before example 1](./img/example1-before.png)|![after example 1](./img/example1-after.png)|
5+
| Before | After |
6+
| ---------------------------------------------- | -------------------------------------------- |
7+
| ![before example 1](./img/example1-before.png) | ![after example 1](./img/example1-after.png) |
88

99
## Features
1010

@@ -31,6 +31,7 @@ npx codemod --plugin react-declassify 'src/**/*.tsx'
3131

3232
Before:
3333

34+
<!-- prettier-ignore -->
3435
```tsx
3536
import React from "react";
3637

@@ -73,6 +74,7 @@ export class C extends React.Component<Props, State> {
7374

7475
After:
7576

77+
<!-- prettier-ignore -->
7678
```tsx
7779
import React from "react";
7880

@@ -106,6 +108,7 @@ export const C: React.FC<Props> = props => {
106108

107109
Before:
108110

111+
<!-- prettier-ignore -->
109112
```jsx
110113
import React from "react";
111114

@@ -125,6 +128,7 @@ export class C extends React.Component {
125128

126129
After:
127130

131+
<!-- prettier-ignore -->
128132
```jsx
129133
import React from "react";
130134

@@ -201,6 +205,7 @@ Class components may receive refs; this is to be supported in the future. Once i
201205

202206
This codemod relies on [recast](https://github.com/benjamn/recast) for pretty-printing and sometimes generates code that does not match your preferred style. This is ineviable. For example it does not currently emit parentheses for the arrow function:
203207

208+
<!-- prettier-ignore -->
204209
```js
205210
const MyComponent: FC = props => {
206211
// ^^^^^ no parentheses

babel-cjs.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
/** @type {import("@babel/core").TransformOptions} */
44
const config = {
55
extends: "./babel.config.js",
6-
presets: [
7-
["@babel/env", { modules: "commonjs" }],
8-
],
6+
presets: [["@babel/env", { modules: "commonjs" }]],
97
};
108
export default config;
11-

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"build": "$npm_execpath build:esm && $npm_execpath build:cjs",
3636
"build:cjs": "babel -x .ts -d cjs/dist src --ignore '**/*.test.ts' --config-file ./babel-cjs.config.js",
3737
"build:esm": "babel -x .ts -d dist src --ignore '**/*.test.ts'",
38+
"fmt": "prettier -w .",
39+
"fmt:check": "prettier -c .",
3840
"prepack": "$npm_execpath build",
3941
"test": "NODE_OPTIONS='--experimental-vm-modules' yarn jest"
4042
},
@@ -52,6 +54,7 @@
5254
"@yarnpkg/sdks": "^3.0.0-rc.40",
5355
"babel-jest": "^29.5.0",
5456
"jest": "^29.5.0",
57+
"prettier": "^2.8.7",
5558
"ts-jest-resolver": "^2.0.1",
5659
"typescript": "^5.0.2"
5760
},

renovate.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
{
22
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
3-
"extends": [
4-
"config:base"
5-
],
3+
"extends": ["config:base"],
64
"packageRules": [
75
{
86
"matchManagers": ["npm"],

src/analysis.ts

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
11
import type { NodePath } from "@babel/core";
22
import type { Scope } from "@babel/traverse";
3-
import type { ClassDeclaration, ClassMethod, Identifier, JSXIdentifier, TSType, TSTypeParameterDeclaration } from "@babel/types";
3+
import type {
4+
ClassDeclaration,
5+
ClassMethod,
6+
Identifier,
7+
JSXIdentifier,
8+
TSType,
9+
TSTypeParameterDeclaration,
10+
} from "@babel/types";
411
import { AnalysisError } from "./analysis/error.js";
512
import { BindThisSite, analyzeClassFields } from "./analysis/class_fields.js";
613
import { analyzeState, StateObjAnalysis } from "./analysis/state.js";
714
import { getAndDelete } from "./utils.js";
815
import { analyzeProps, needAlias, PropsObjAnalysis } from "./analysis/prop.js";
916
import { LocalManager, RemovableNode } from "./analysis/local.js";
10-
import { analyzeUserDefined, postAnalyzeCallbackDependencies, UserDefinedAnalysis } from "./analysis/user_defined.js";
17+
import {
18+
analyzeUserDefined,
19+
postAnalyzeCallbackDependencies,
20+
UserDefinedAnalysis,
21+
} from "./analysis/user_defined.js";
1122
import type { PreAnalysisResult } from "./analysis/pre.js";
1223
import type { LibRef } from "./analysis/lib.js";
1324
import { EffectAnalysis, analyzeEffects } from "./analysis/effect.js";
1425

1526
export { AnalysisError } from "./analysis/error.js";
1627

1728
export type { LibRef } from "./analysis/lib.js";
18-
export type {
19-
PreAnalysisResult
20-
} from "./analysis/pre.js";
29+
export type { PreAnalysisResult } from "./analysis/pre.js";
2130
export { preanalyzeClass } from "./analysis/pre.js";
2231
export type { LocalManager } from "./analysis/local.js";
23-
export type { StateObjAnalysis, SetStateSite, SetStateFieldSite } from "./analysis/state.js";
32+
export type {
33+
StateObjAnalysis,
34+
SetStateSite,
35+
SetStateFieldSite,
36+
} from "./analysis/state.js";
2437
export { needAlias } from "./analysis/prop.js";
2538
export type { PropsObjAnalysis } from "./analysis/prop.js";
2639

@@ -53,18 +66,36 @@ export function analyzeClass(
5366
preanalysis: PreAnalysisResult
5467
): AnalysisResult {
5568
const locals = new LocalManager(path);
56-
const { instanceFields: sites, staticFields, bindThisSites } = analyzeClassFields(path);
69+
const {
70+
instanceFields: sites,
71+
staticFields,
72+
bindThisSites,
73+
} = analyzeClassFields(path);
5774

5875
const propsObjAnalysis = getAndDelete(sites, "props") ?? { sites: [] };
59-
const defaultPropsObjAnalysis = getAndDelete(staticFields, "defaultProps") ?? { sites: [] };
76+
const defaultPropsObjAnalysis = getAndDelete(
77+
staticFields,
78+
"defaultProps"
79+
) ?? { sites: [] };
6080

6181
const stateObjAnalysis = getAndDelete(sites, "state") ?? { sites: [] };
6282
const setStateAnalysis = getAndDelete(sites, "setState") ?? { sites: [] };
63-
const states = analyzeState(stateObjAnalysis, setStateAnalysis, locals, preanalysis);
83+
const states = analyzeState(
84+
stateObjAnalysis,
85+
setStateAnalysis,
86+
locals,
87+
preanalysis
88+
);
6489

65-
const componentDidMount = getAndDelete(sites, "componentDidMount") ?? { sites: [] };
66-
const componentDidUpdate = getAndDelete(sites, "componentDidUpdate") ?? { sites: [] };
67-
const componentWillUnmount = getAndDelete(sites, "componentWillUnmount") ?? { sites: [] };
90+
const componentDidMount = getAndDelete(sites, "componentDidMount") ?? {
91+
sites: [],
92+
};
93+
const componentDidUpdate = getAndDelete(sites, "componentDidUpdate") ?? {
94+
sites: [],
95+
};
96+
const componentWillUnmount = getAndDelete(sites, "componentWillUnmount") ?? {
97+
sites: [],
98+
};
6899

69100
const renderAnalysis = getAndDelete(sites, "render") ?? { sites: [] };
70101

@@ -92,13 +123,13 @@ export function analyzeClass(
92123
if (!renderPath) {
93124
throw new AnalysisError(`Missing render method`);
94125
}
95-
const props = analyzeProps(propsObjAnalysis, defaultPropsObjAnalysis, locals, preanalysis);
96-
postAnalyzeCallbackDependencies(
97-
userDefined,
98-
props,
99-
states,
100-
sites,
126+
const props = analyzeProps(
127+
propsObjAnalysis,
128+
defaultPropsObjAnalysis,
129+
locals,
130+
preanalysis
101131
);
132+
postAnalyzeCallbackDependencies(userDefined, props, states, sites);
102133

103134
for (const [name, propAnalysis] of props.props) {
104135
if (needAlias(propAnalysis)) {
@@ -113,19 +144,25 @@ export function analyzeClass(
113144
componentDidMount,
114145
componentDidUpdate,
115146
componentWillUnmount,
116-
userDefined,
147+
userDefined
117148
);
118149

119150
const render = analyzeRender(renderPath, locals);
120151

121152
for (const [name, stateAnalysis] of states.states.entries()) {
122153
const bindingPaths = stateAnalysis.sites.map((site) => site.path);
123154
stateAnalysis.localName = locals.newLocal(name, bindingPaths);
124-
stateAnalysis.localSetterName = locals.newLocal(`set${name.replace(/^[a-z]/, (s) => s.toUpperCase())}`, bindingPaths);
155+
stateAnalysis.localSetterName = locals.newLocal(
156+
`set${name.replace(/^[a-z]/, (s) => s.toUpperCase())}`,
157+
bindingPaths
158+
);
125159
}
126160

127161
for (const [name, field] of userDefined.fields) {
128-
field.localName = locals.newLocal(name, field.sites.map((site) => site.path));
162+
field.localName = locals.newLocal(
163+
name,
164+
field.sites.map((site) => site.path)
165+
);
129166
}
130167

131168
if (effects.cdmPath || effects.cduPath || effects.cwuPath) {
@@ -157,20 +194,18 @@ export type RenderAnalysis = {
157194
};
158195

159196
export type LocalRename = {
160-
scope: Scope,
197+
scope: Scope;
161198
oldName: string;
162199
newName: string;
163200
};
164201

165202
function analyzeRender(
166203
path: NodePath<ClassMethod>,
167-
locals: LocalManager,
204+
locals: LocalManager
168205
): RenderAnalysis {
169206
const renames: LocalRename[] = [];
170207
for (const [name, binding] of Object.entries(path.scope.bindings)) {
171-
if (
172-
locals.allRemovePaths.has(binding.path as NodePath<RemovableNode>)
173-
) {
208+
if (locals.allRemovePaths.has(binding.path as NodePath<RemovableNode>)) {
174209
// Already handled as an alias
175210
continue;
176211
}
@@ -184,10 +219,13 @@ function analyzeRender(
184219
return { path, renames };
185220
}
186221

187-
function analyzeOuterCapturings(classPath: NodePath<ClassDeclaration>, locals: LocalManager): Set<string> {
222+
function analyzeOuterCapturings(
223+
classPath: NodePath<ClassDeclaration>,
224+
locals: LocalManager
225+
): Set<string> {
188226
const capturings = new Set<string>();
189227
function visitIdent(path: NodePath<Identifier | JSXIdentifier>) {
190-
path.getOuterBindingIdentifiers
228+
path.getOuterBindingIdentifiers;
191229
const binding = path.scope.getBinding(path.node.name);
192230
if (!binding || binding.path.isAncestor(classPath)) {
193231
capturings.add(path.node.name);
@@ -204,7 +242,7 @@ function analyzeOuterCapturings(classPath: NodePath<ClassDeclaration>, locals: L
204242
if (path.isReferencedIdentifier()) {
205243
visitIdent(path);
206244
}
207-
}
245+
},
208246
});
209247
return capturings;
210248
}

0 commit comments

Comments
 (0)