Skip to content

Commit

Permalink
chore: use Prettier format (umijs#458)
Browse files Browse the repository at this point in the history
* improve: use prettier

* add prettier config

* fix

* fix test

* remove node 8 and node 12 support
  • Loading branch information
shaodahong authored Jun 4, 2020
1 parent cbb43af commit 2172ad2
Show file tree
Hide file tree
Showing 9 changed files with 753 additions and 247 deletions.
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"extends": "eslint-config-airbnb/base",
"extends": ["airbnb/base", "plugin:prettier/recommended"],
"rules": {
"no-console": [0]
"no-console": [0],
"prettier/prettier": 2,
"global-require": 0,
"func-names": 0
}
}
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/fixtures/**/*.js
15 changes: 15 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 100,
"proseWrap": "never",
"arrowParens": "avoid",
"overrides": [
{
"files": ".prettierrc",
"options": {
"parser": "json"
}
}
]
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- 8
- 10
- 12
after_success:
- npm run test
25 changes: 19 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@
"prepack": "npm run build",
"prepublishOnly": "npm run build && np --no-cleanup --yolo --no-publish"
},
"pre-commit": [
"lint"
],
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": [
"eslint",
"prettier --write"
]
},
"keywords": [
"babel-plugin",
"antd"
Expand All @@ -31,11 +39,16 @@
"babel-core": "^7.0.0-0",
"babel-preset-umi": "^1.0.0",
"coveralls": "^3.0.6",
"eslint": "^6.7.2",
"eslint-config-airbnb": "^6.2.0",
"eslint": "^7.1.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-prettier": "^3.1.3",
"husky": "^4.2.5",
"lint-staged": "^10.2.8",
"material-ui": "^0.20.2",
"np": "^6.2.0",
"pre-commit": "~1.2.2",
"prettier": "^2.0.5",
"react-toolbox": "^1.2.5",
"umi-tools": "^0.4.0"
},
Expand Down
77 changes: 37 additions & 40 deletions src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { addSideEffect, addDefault, addNamed } from '@babel/helper-module-import

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

function winPath(path) {
Expand All @@ -13,9 +13,9 @@ function winPath(path) {
function normalizeCustomName(originCustomName) {
// If set to a string, treat it as a JavaScript source file path.
if (typeof originCustomName === 'string') {
// eslint-disable-next-line import/no-dynamic-require
const customNameExports = require(originCustomName);
return typeof customNameExports === 'function'
? customNameExports : customNameExports.default;
return typeof customNameExports === 'function' ? customNameExports : customNameExports.default;
}

return originCustomName;
Expand All @@ -34,56 +34,53 @@ export default class Plugin {
customName,
transformToDefaultImport,
types,
index = 0
index = 0,
) {
this.libraryName = libraryName;
this.libraryDirectory = typeof libraryDirectory === 'undefined'
? 'lib'
: libraryDirectory;
this.camel2DashComponentName = typeof camel2DashComponentName === 'undefined'
? true
: camel2DashComponentName;
this.libraryDirectory = typeof libraryDirectory === 'undefined' ? 'lib' : libraryDirectory;
this.camel2DashComponentName =
typeof camel2DashComponentName === 'undefined' ? true : camel2DashComponentName;
this.camel2UnderlineComponentName = camel2UnderlineComponentName;
this.style = style || false;
this.styleLibraryDirectory = styleLibraryDirectory;
this.customStyleName = normalizeCustomName(customStyleName);
this.fileName = fileName || '';
this.customName = normalizeCustomName(customName);
this.transformToDefaultImport = typeof transformToDefaultImport === 'undefined'
? true
: transformToDefaultImport;
this.transformToDefaultImport =
typeof transformToDefaultImport === 'undefined' ? true : transformToDefaultImport;
this.types = types;
this.pluginStateKey = `importPluginState${index}`;
}

getPluginState(state) {
if (!state[this.pluginStateKey]) {
state[this.pluginStateKey] = {}; // eslint-disable-line
state[this.pluginStateKey] = {}; // eslint-disable-line
}
return state[this.pluginStateKey];
}

importMethod(methodName, file, pluginState) {
if (!pluginState.selectedMethods[methodName]) {
const libraryDirectory = this.libraryDirectory;
const style = this.style;
const transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line
const { style, libraryDirectory } = this;
const transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line
? transCamel(methodName, '_')
: this.camel2DashComponentName
? transCamel(methodName, '-')
: methodName;
? transCamel(methodName, '-')
: methodName;
const path = winPath(
this.customName ? this.customName(transformedMethodName, file) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
this.customName
? this.customName(transformedMethodName, file)
: join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName), // eslint-disable-line
);
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
? addDefault(file.path, path, { nameHint: methodName })
: addNamed(file.path, methodName, path);
if (this.customStyleName) {
const stylePath = winPath(this.customStyleName(transformedMethodName));
addSideEffect(file.path, `${stylePath}`);
} else if (this.styleLibraryDirectory) {
const stylePath = winPath(
join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName)
join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName),
);
addSideEffect(file.path, `${stylePath}`);
} else if (style === true) {
Expand All @@ -97,33 +94,35 @@ export default class Plugin {
}
}
}
return Object.assign({}, pluginState.selectedMethods[methodName]);
return { ...pluginState.selectedMethods[methodName] };
}

buildExpressionHandler(node, props, path, state) {
const file = (path && path.hub && path.hub.file) || (state && state.file);
const types = this.types;
const { types } = this;
const pluginState = this.getPluginState(state);
props.forEach(prop => {
if (!types.isIdentifier(node[prop])) return;
if (
pluginState.specified[node[prop].name] &&
types.isImportSpecifier(path.scope.getBinding(node[prop].name).path)
) {
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
}
});
}

buildDeclaratorHandler(node, prop, path, state) {
const file = (path && path.hub && path.hub.file) || (state && state.file);
const types = this.types;
const { types } = this;
const pluginState = this.getPluginState(state);
if (!types.isIdentifier(node[prop])) return;
if (pluginState.specified[node[prop].name] &&
if (
pluginState.specified[node[prop].name] &&
path.scope.hasBinding(node[prop].name) &&
path.scope.getBinding(node[prop].name).path.type === 'ImportSpecifier') {
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
path.scope.getBinding(node[prop].name).path.type === 'ImportSpecifier'
) {
node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
}
}

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

const { value } = node.source;
const libraryName = this.libraryName;
const types = this.types;
const { libraryName } = this;
const { types } = this;
const pluginState = this.getPluginState(state);
if (value === libraryName) {
node.specifiers.forEach(spec => {
Expand All @@ -165,7 +164,7 @@ export default class Plugin {
const { node } = path;
const file = (path && path.hub && path.hub.file) || (state && state.file);
const { name } = node.callee;
const types = this.types;
const { types } = this;
const pluginState = this.getPluginState(state);

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

node.arguments = node.arguments.map(arg => {
const { name: argName } = arg;
if (pluginState.specified[argName] &&
if (
pluginState.specified[argName] &&
path.scope.hasBinding(argName) &&
path.scope.getBinding(argName).path.type === 'ImportSpecifier') {
path.scope.getBinding(argName).path.type === 'ImportSpecifier'
) {
return this.importMethod(pluginState.specified[argName], file, pluginState);
}
return arg;
Expand All @@ -197,14 +198,10 @@ export default class Plugin {
// antd.Button -> _Button
path.replaceWith(this.importMethod(node.property.name, file, pluginState));
} else if (pluginState.specified[node.object.name] && path.scope.hasBinding(node.object.name)) {
const scope = path.scope.getBinding(node.object.name).scope;
const { scope } = path.scope.getBinding(node.object.name);
// global variable in file scope
if (scope.path.parent.type === 'File') {
node.object = this.importMethod(
pluginState.specified[node.object.name],
file,
pluginState
);
node.object = this.importMethod(pluginState.specified[node.object.name], file, pluginState);
}
}
}
Expand Down
75 changes: 42 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ export default function ({ types }) {
let plugins = null;

// Only for test
// eslint-disable-next-line no-underscore-dangle
global.__clearBabelAntdPlugin = () => {
plugins = null;
};

function applyInstance(method, args, context) {
// eslint-disable-next-line no-restricted-syntax
for (const plugin of plugins) {
if (plugin[method]) {
plugin[method].apply(plugin, [...args, context]);
Expand All @@ -22,34 +24,39 @@ export default function ({ types }) {
// Init plugin instances once.
if (!plugins) {
if (Array.isArray(opts)) {
plugins = opts.map(({
libraryName,
libraryDirectory,
style,
styleLibraryDirectory,
customStyleName,
camel2DashComponentName,
camel2UnderlineComponentName,
fileName,
customName,
transformToDefaultImport,
}, index) => {
assert(libraryName, 'libraryName should be provided');
return new Plugin(
libraryName,
libraryDirectory,
style,
styleLibraryDirectory,
customStyleName,
camel2DashComponentName,
camel2UnderlineComponentName,
fileName,
customName,
transformToDefaultImport,
types,
index
);
});
plugins = opts.map(
(
{
libraryName,
libraryDirectory,
style,
styleLibraryDirectory,
customStyleName,
camel2DashComponentName,
camel2UnderlineComponentName,
fileName,
customName,
transformToDefaultImport,
},
index,
) => {
assert(libraryName, 'libraryName should be provided');
return new Plugin(
libraryName,
libraryDirectory,
style,
styleLibraryDirectory,
customStyleName,
camel2DashComponentName,
camel2UnderlineComponentName,
fileName,
customName,
transformToDefaultImport,
types,
index,
);
},
);
} else {
assert(opts.libraryName, 'libraryName should be provided');
plugins = [
Expand All @@ -64,15 +71,15 @@ export default function ({ types }) {
opts.fileName,
opts.customName,
opts.transformToDefaultImport,
types
types,
),
];
}
}
applyInstance('ProgramEnter', arguments, this); // eslint-disable-line
applyInstance('ProgramEnter', arguments, this); // eslint-disable-line
},
exit() {
applyInstance('ProgramExit', arguments, this); // eslint-disable-line
applyInstance('ProgramExit', arguments, this); // eslint-disable-line
},
};

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

// eslint-disable-next-line no-restricted-syntax
for (const method of methods) {
ret.visitor[method] = function () { // eslint-disable-line
applyInstance(method, arguments, ret.visitor); // eslint-disable-line
ret.visitor[method] = function () {
// eslint-disable-line
applyInstance(method, arguments, ret.visitor); // eslint-disable-line
};
}

Expand Down
Loading

0 comments on commit 2172ad2

Please sign in to comment.