Skip to content

Commit 7427bd6

Browse files
committed
feat: add husky prettier and lint staged
1 parent 0563bf7 commit 7427bd6

File tree

7 files changed

+41
-20
lines changed

7 files changed

+41
-20
lines changed

.eslintignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
/**/*.d.ts
1+
/**/*.d.ts
2+
node_modules/**
3+
tsconfig.json
4+
dist/**

.husky/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npm run pre-commit

assets/example.gif

3.72 MB
Loading

assets/icon.png

5.9 KB
Loading

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "React Native Component Splitter",
44
"description": "Splits long components into shorter, readable subcomponents",
55
"version": "0.0.7",
6-
"icon": "src/assets/icon.png",
6+
"icon": "assets/icon.png",
77
"repository": {
88
"type": "git",
99
"url": "https://github.com/nomi9995/react-native-component-splitter"
@@ -72,7 +72,15 @@
7272
"pretest": "yarn run test-compile && yarn run lint",
7373
"lint": "eslint src --ext ts",
7474
"test": "node ./out/test/runTest.js",
75-
"publish:extension": "yarn run vscode:prepublish && vsce publish"
75+
"publish:extension": "yarn run vscode:prepublish && vsce publish",
76+
"pre-commit": "lint-staged --verbose",
77+
"prepare": "husky install"
78+
},
79+
"lint-staged": {
80+
"*.(js|jsx|ts|tsx)": [
81+
"pretty-quick --staged",
82+
"eslint \"**/*.{js,ts,tsx}\""
83+
]
7684
},
7785
"dependencies": {
7886
"@types/jest": "^27.0.1",
@@ -108,9 +116,12 @@
108116
"eslint-plugin-react-hooks": "^4.2.0",
109117
"eslint-plugin-unused-imports": "^1.1.4",
110118
"glob": "^7.1.7",
119+
"husky": "^7.0.2",
111120
"jest": "^27.1.1",
112121
"json-fixer": "^1.6.12",
122+
"lint-staged": "^11.1.2",
113123
"mocha": "^9.1.1",
124+
"pretty-quick": "^3.1.1",
114125
"ts-loader": "^9.2.5",
115126
"typescript": "^4.4.3",
116127
"vscode-test": "^1.6.1",

src/utils/splitter.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const askForComponentName = async () => {
5353
let name = await window.showInputBox({
5454
prompt: "Choose a name for the new component",
5555
ignoreFocusOut: true,
56+
5657
placeHolder: "New component name...",
5758
});
5859

@@ -150,10 +151,8 @@ const removeUnusedImports = async () => {
150151
const { document } = editor;
151152
const code = document.getText();
152153
const codeLines = _.split(code, "\n");
153-
const {
154-
firstImportLineIndex,
155-
lastImportLineIndex,
156-
} = getFirstAndLastImportLineIndexes(codeLines);
154+
const { firstImportLineIndex, lastImportLineIndex } =
155+
getFirstAndLastImportLineIndexes(codeLines);
157156

158157
const importsString = _.chain(codeLines)
159158
.slice(firstImportLineIndex, lastImportLineIndex + 1)
@@ -186,9 +185,8 @@ const updateOriginalComponent = async ({ newComponent }) => {
186185

187186
const generateReactElement = ({ name, props, jsx }) => {
188187
const numberOfProps = _.size(props);
189-
const numberOfLeadingSpacesFromStart = parseUtils.getNumberOfLeadingSpaces(
190-
jsx
191-
);
188+
const numberOfLeadingSpacesFromStart =
189+
parseUtils.getNumberOfLeadingSpaces(jsx);
192190
const leadingSpacesFromStart = _.repeat(" ", numberOfLeadingSpacesFromStart);
193191
let propsString = "";
194192

@@ -220,11 +218,13 @@ const extractRelevantImportsAndPropsAndStylesheet = () => {
220218
`;
221219
const props = parseUtils.getUndefinedVars(selectionAndImports);
222220
const imports = parseUtils.getUsedImports(selectionAndImports);
223-
const stylesheet = regexNormalizeResult(parseUtils.getStylesheet(code, selection));
221+
const stylesheet = regexNormalizeResult(
222+
parseUtils.getStylesheet(code, selection)
223+
);
224224
return {
225225
props,
226226
imports,
227-
stylesheet
227+
stylesheet,
228228
};
229229
};
230230

@@ -259,18 +259,20 @@ const createNewComponent = async (componentName: any) => {
259259
const uri = editor.document.uri;
260260
const fileExtension = parseUtils.getUriExtension(uri.fsPath);
261261
const selection = editor.document.getText(editor.selection);
262-
const { imports, props, stylesheet } = extractRelevantImportsAndPropsAndStylesheet();
262+
const { imports, props, stylesheet } =
263+
extractRelevantImportsAndPropsAndStylesheet();
263264

264265
const newComponent = {
265266
code: parseUtils.pretify(
266267
`${buildImportsString(imports)}\n\n` +
267-
`const ${componentName} = (${buildPropsString(props)}) => (\n` +
268-
`${shouldWrapCodeWithEmptyTag(selection)
269-
? `<View>\n${selection}\n</View>`
270-
: selection
271-
}\n` +
272-
`);\n\n` +
273-
`export default ${componentName};\n\n${stylesheet}\n`
268+
`const ${componentName} = (${buildPropsString(props)}) => (\n` +
269+
`${
270+
shouldWrapCodeWithEmptyTag(selection)
271+
? `<View>\n${selection}\n</View>`
272+
: selection
273+
}\n` +
274+
`);\n\n` +
275+
`export default ${componentName};\n\n${stylesheet}\n`
274276
),
275277
reactElement: generateReactElement({
276278
name: componentName,

0 commit comments

Comments
 (0)