Skip to content

Commit 5cb2349

Browse files
authored
chore: use Prettier format (#458)
* improve: use prettier * add prettier config * fix * fix test * remove node 8 and node 12 support
1 parent 39afe42 commit 5cb2349

File tree

9 files changed

+753
-247
lines changed

9 files changed

+753
-247
lines changed

.eslintrc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
2-
"extends": "eslint-config-airbnb/base",
2+
"extends": ["airbnb/base", "plugin:prettier/recommended"],
33
"rules": {
4-
"no-console": [0]
4+
"no-console": [0],
5+
"prettier/prettier": 2,
6+
"global-require": 0,
7+
"func-names": 0
58
}
69
}

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/fixtures/**/*.js

.prettierrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"printWidth": 100,
5+
"proseWrap": "never",
6+
"arrowParens": "avoid",
7+
"overrides": [
8+
{
9+
"files": ".prettierrc",
10+
"options": {
11+
"parser": "json"
12+
}
13+
}
14+
]
15+
}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- 8
43
- 10
4+
- 12
55
after_success:
66
- npm run test

package.json

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@
1616
"prepack": "npm run build",
1717
"prepublishOnly": "npm run build && np --no-cleanup --yolo --no-publish"
1818
},
19-
"pre-commit": [
20-
"lint"
21-
],
19+
"husky": {
20+
"hooks": {
21+
"pre-commit": "lint-staged"
22+
}
23+
},
24+
"lint-staged": {
25+
"*.js": [
26+
"eslint",
27+
"prettier --write"
28+
]
29+
},
2230
"keywords": [
2331
"babel-plugin",
2432
"antd"
@@ -31,11 +39,16 @@
3139
"babel-core": "^7.0.0-0",
3240
"babel-preset-umi": "^1.0.0",
3341
"coveralls": "^3.0.6",
34-
"eslint": "^6.7.2",
35-
"eslint-config-airbnb": "^6.2.0",
42+
"eslint": "^7.1.0",
43+
"eslint-config-airbnb": "^18.1.0",
44+
"eslint-config-prettier": "^6.11.0",
45+
"eslint-plugin-import": "^2.20.2",
46+
"eslint-plugin-prettier": "^3.1.3",
47+
"husky": "^4.2.5",
48+
"lint-staged": "^10.2.8",
3649
"material-ui": "^0.20.2",
3750
"np": "^6.2.0",
38-
"pre-commit": "~1.2.2",
51+
"prettier": "^2.0.5",
3952
"react-toolbox": "^1.2.5",
4053
"umi-tools": "^0.4.0"
4154
},

src/Plugin.js

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { addSideEffect, addDefault, addNamed } from '@babel/helper-module-import
33

44
function transCamel(_str, symbol) {
55
const str = _str[0].toLowerCase() + _str.substr(1);
6-
return str.replace(/([A-Z])/g, ($1) => `${symbol}${$1.toLowerCase()}`);
6+
return str.replace(/([A-Z])/g, $1 => `${symbol}${$1.toLowerCase()}`);
77
}
88

99
function winPath(path) {
@@ -13,9 +13,9 @@ function winPath(path) {
1313
function normalizeCustomName(originCustomName) {
1414
// If set to a string, treat it as a JavaScript source file path.
1515
if (typeof originCustomName === 'string') {
16+
// eslint-disable-next-line import/no-dynamic-require
1617
const customNameExports = require(originCustomName);
17-
return typeof customNameExports === 'function'
18-
? customNameExports : customNameExports.default;
18+
return typeof customNameExports === 'function' ? customNameExports : customNameExports.default;
1919
}
2020

2121
return originCustomName;
@@ -34,56 +34,53 @@ export default class Plugin {
3434
customName,
3535
transformToDefaultImport,
3636
types,
37-
index = 0
37+
index = 0,
3838
) {
3939
this.libraryName = libraryName;
40-
this.libraryDirectory = typeof libraryDirectory === 'undefined'
41-
? 'lib'
42-
: libraryDirectory;
43-
this.camel2DashComponentName = typeof camel2DashComponentName === 'undefined'
44-
? true
45-
: camel2DashComponentName;
40+
this.libraryDirectory = typeof libraryDirectory === 'undefined' ? 'lib' : libraryDirectory;
41+
this.camel2DashComponentName =
42+
typeof camel2DashComponentName === 'undefined' ? true : camel2DashComponentName;
4643
this.camel2UnderlineComponentName = camel2UnderlineComponentName;
4744
this.style = style || false;
4845
this.styleLibraryDirectory = styleLibraryDirectory;
4946
this.customStyleName = normalizeCustomName(customStyleName);
5047
this.fileName = fileName || '';
5148
this.customName = normalizeCustomName(customName);
52-
this.transformToDefaultImport = typeof transformToDefaultImport === 'undefined'
53-
? true
54-
: transformToDefaultImport;
49+
this.transformToDefaultImport =
50+
typeof transformToDefaultImport === 'undefined' ? true : transformToDefaultImport;
5551
this.types = types;
5652
this.pluginStateKey = `importPluginState${index}`;
5753
}
5854

5955
getPluginState(state) {
6056
if (!state[this.pluginStateKey]) {
61-
state[this.pluginStateKey] = {}; // eslint-disable-line
57+
state[this.pluginStateKey] = {}; // eslint-disable-line
6258
}
6359
return state[this.pluginStateKey];
6460
}
6561

6662
importMethod(methodName, file, pluginState) {
6763
if (!pluginState.selectedMethods[methodName]) {
68-
const libraryDirectory = this.libraryDirectory;
69-
const style = this.style;
70-
const transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line
64+
const { style, libraryDirectory } = this;
65+
const transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line
7166
? transCamel(methodName, '_')
7267
: this.camel2DashComponentName
73-
? transCamel(methodName, '-')
74-
: methodName;
68+
? transCamel(methodName, '-')
69+
: methodName;
7570
const path = winPath(
76-
this.customName ? this.customName(transformedMethodName, file) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
71+
this.customName
72+
? this.customName(transformedMethodName, file)
73+
: join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName), // eslint-disable-line
7774
);
78-
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
75+
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
7976
? addDefault(file.path, path, { nameHint: methodName })
8077
: addNamed(file.path, methodName, path);
8178
if (this.customStyleName) {
8279
const stylePath = winPath(this.customStyleName(transformedMethodName));
8380
addSideEffect(file.path, `${stylePath}`);
8481
} else if (this.styleLibraryDirectory) {
8582
const stylePath = winPath(
86-
join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName)
83+
join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName),
8784
);
8885
addSideEffect(file.path, `${stylePath}`);
8986
} else if (style === true) {
@@ -97,33 +94,35 @@ export default class Plugin {
9794
}
9895
}
9996
}
100-
return Object.assign({}, pluginState.selectedMethods[methodName]);
97+
return { ...pluginState.selectedMethods[methodName] };
10198
}
10299

103100
buildExpressionHandler(node, props, path, state) {
104101
const file = (path && path.hub && path.hub.file) || (state && state.file);
105-
const types = this.types;
102+
const { types } = this;
106103
const pluginState = this.getPluginState(state);
107104
props.forEach(prop => {
108105
if (!types.isIdentifier(node[prop])) return;
109106
if (
110107
pluginState.specified[node[prop].name] &&
111108
types.isImportSpecifier(path.scope.getBinding(node[prop].name).path)
112109
) {
113-
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
110+
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
114111
}
115112
});
116113
}
117114

118115
buildDeclaratorHandler(node, prop, path, state) {
119116
const file = (path && path.hub && path.hub.file) || (state && state.file);
120-
const types = this.types;
117+
const { types } = this;
121118
const pluginState = this.getPluginState(state);
122119
if (!types.isIdentifier(node[prop])) return;
123-
if (pluginState.specified[node[prop].name] &&
120+
if (
121+
pluginState.specified[node[prop].name] &&
124122
path.scope.hasBinding(node[prop].name) &&
125-
path.scope.getBinding(node[prop].name).path.type === 'ImportSpecifier') {
126-
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
123+
path.scope.getBinding(node[prop].name).path.type === 'ImportSpecifier'
124+
) {
125+
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
127126
}
128127
}
129128

@@ -146,8 +145,8 @@ export default class Plugin {
146145
if (!node) return;
147146

148147
const { value } = node.source;
149-
const libraryName = this.libraryName;
150-
const types = this.types;
148+
const { libraryName } = this;
149+
const { types } = this;
151150
const pluginState = this.getPluginState(state);
152151
if (value === libraryName) {
153152
node.specifiers.forEach(spec => {
@@ -165,7 +164,7 @@ export default class Plugin {
165164
const { node } = path;
166165
const file = (path && path.hub && path.hub.file) || (state && state.file);
167166
const { name } = node.callee;
168-
const types = this.types;
167+
const { types } = this;
169168
const pluginState = this.getPluginState(state);
170169

171170
if (types.isIdentifier(node.callee)) {
@@ -176,9 +175,11 @@ export default class Plugin {
176175

177176
node.arguments = node.arguments.map(arg => {
178177
const { name: argName } = arg;
179-
if (pluginState.specified[argName] &&
178+
if (
179+
pluginState.specified[argName] &&
180180
path.scope.hasBinding(argName) &&
181-
path.scope.getBinding(argName).path.type === 'ImportSpecifier') {
181+
path.scope.getBinding(argName).path.type === 'ImportSpecifier'
182+
) {
182183
return this.importMethod(pluginState.specified[argName], file, pluginState);
183184
}
184185
return arg;
@@ -197,14 +198,10 @@ export default class Plugin {
197198
// antd.Button -> _Button
198199
path.replaceWith(this.importMethod(node.property.name, file, pluginState));
199200
} else if (pluginState.specified[node.object.name] && path.scope.hasBinding(node.object.name)) {
200-
const scope = path.scope.getBinding(node.object.name).scope;
201+
const { scope } = path.scope.getBinding(node.object.name);
201202
// global variable in file scope
202203
if (scope.path.parent.type === 'File') {
203-
node.object = this.importMethod(
204-
pluginState.specified[node.object.name],
205-
file,
206-
pluginState
207-
);
204+
node.object = this.importMethod(pluginState.specified[node.object.name], file, pluginState);
208205
}
209206
}
210207
}

src/index.js

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ export default function ({ types }) {
55
let plugins = null;
66

77
// Only for test
8+
// eslint-disable-next-line no-underscore-dangle
89
global.__clearBabelAntdPlugin = () => {
910
plugins = null;
1011
};
1112

1213
function applyInstance(method, args, context) {
14+
// eslint-disable-next-line no-restricted-syntax
1315
for (const plugin of plugins) {
1416
if (plugin[method]) {
1517
plugin[method].apply(plugin, [...args, context]);
@@ -22,34 +24,39 @@ export default function ({ types }) {
2224
// Init plugin instances once.
2325
if (!plugins) {
2426
if (Array.isArray(opts)) {
25-
plugins = opts.map(({
26-
libraryName,
27-
libraryDirectory,
28-
style,
29-
styleLibraryDirectory,
30-
customStyleName,
31-
camel2DashComponentName,
32-
camel2UnderlineComponentName,
33-
fileName,
34-
customName,
35-
transformToDefaultImport,
36-
}, index) => {
37-
assert(libraryName, 'libraryName should be provided');
38-
return new Plugin(
39-
libraryName,
40-
libraryDirectory,
41-
style,
42-
styleLibraryDirectory,
43-
customStyleName,
44-
camel2DashComponentName,
45-
camel2UnderlineComponentName,
46-
fileName,
47-
customName,
48-
transformToDefaultImport,
49-
types,
50-
index
51-
);
52-
});
27+
plugins = opts.map(
28+
(
29+
{
30+
libraryName,
31+
libraryDirectory,
32+
style,
33+
styleLibraryDirectory,
34+
customStyleName,
35+
camel2DashComponentName,
36+
camel2UnderlineComponentName,
37+
fileName,
38+
customName,
39+
transformToDefaultImport,
40+
},
41+
index,
42+
) => {
43+
assert(libraryName, 'libraryName should be provided');
44+
return new Plugin(
45+
libraryName,
46+
libraryDirectory,
47+
style,
48+
styleLibraryDirectory,
49+
customStyleName,
50+
camel2DashComponentName,
51+
camel2UnderlineComponentName,
52+
fileName,
53+
customName,
54+
transformToDefaultImport,
55+
types,
56+
index,
57+
);
58+
},
59+
);
5360
} else {
5461
assert(opts.libraryName, 'libraryName should be provided');
5562
plugins = [
@@ -64,15 +71,15 @@ export default function ({ types }) {
6471
opts.fileName,
6572
opts.customName,
6673
opts.transformToDefaultImport,
67-
types
74+
types,
6875
),
6976
];
7077
}
7178
}
72-
applyInstance('ProgramEnter', arguments, this); // eslint-disable-line
79+
applyInstance('ProgramEnter', arguments, this); // eslint-disable-line
7380
},
7481
exit() {
75-
applyInstance('ProgramExit', arguments, this); // eslint-disable-line
82+
applyInstance('ProgramExit', arguments, this); // eslint-disable-line
7683
},
7784
};
7885

@@ -98,9 +105,11 @@ export default function ({ types }) {
98105
visitor: { Program },
99106
};
100107

108+
// eslint-disable-next-line no-restricted-syntax
101109
for (const method of methods) {
102-
ret.visitor[method] = function () { // eslint-disable-line
103-
applyInstance(method, arguments, ret.visitor); // eslint-disable-line
110+
ret.visitor[method] = function () {
111+
// eslint-disable-line
112+
applyInstance(method, arguments, ret.visitor); // eslint-disable-line
104113
};
105114
}
106115

0 commit comments

Comments
 (0)