Skip to content

Commit 39ced54

Browse files
committed
fix(scripts): unify eslint config
1 parent 931f7d3 commit 39ced54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1819
-1798
lines changed

.commitlintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"rules": {
33
"header-max-length": [2, "always", 72],
44
"scope-empty": [2, "never"],
5-
"scope-case": [2, "always", "pascal-case"],
5+
"scope-case": [2, "always", "lower-case"],
66
"subject-empty": [2, "never"],
77
"subject-full-stop": [2, "never", "."],
88
"type-empty": [2, "never"],

.eslintrc

+1-22
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,5 @@
33
"es6": true,
44
"node": true
55
},
6-
"extends": "eslint:recommended",
7-
"rules": {
8-
"indent": [
9-
"error",
10-
2,
11-
{
12-
"SwitchCase": 1
13-
}
14-
],
15-
"linebreak-style": [
16-
"error",
17-
"unix"
18-
],
19-
"quotes": [
20-
"error",
21-
"single"
22-
],
23-
"semi": [
24-
"error",
25-
"always"
26-
]
27-
}
6+
"extends": "@alifd/eslint-config-next"
287
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
"release": "node ./scripts/release/index.js",
4141
"test": "node --max_old_space_size=4096 ./scripts/test/index.js",
4242
"order-var": "node ./scripts/order-var.js",
43-
"eslint": "eslint '@(src|scripts)/**/*.@(js|jsx)' && eslint 'docs/**/@(demo|theme)/*.@(md)'",
44-
"stylelint": "stylelint 'src/**/*.@(css|scss)'",
43+
"eslint": "eslint --fix '@(src|scripts)/**/*.@(js|jsx)' && eslint --fix 'docs/**/@(demo|theme)/*.@(md)'",
44+
"stylelint": "stylelint --fix 'src/**/*.@(css|scss)'",
4545
"commitmsg": "commitlint -E GIT_PARAMS",
4646
"changelog": "node ./scripts/changelog.js",
4747
"prepub": "npm run build && npm run docs && npm run pack && npm run pack -- minimize && npm run changelog",

scripts/build/export-api-schema.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,45 @@ const fs = require('fs-extra');
33
const apiExtractor = require('@alifd/api-extractor');
44
const tsgen = require('@alifd/dts-generator');
55
const { logger } = require('../utils');
6+
67
const cwd = process.cwd();
78

89
module.exports = function (options) {
9-
const libPath = path.join(cwd, 'lib');
10-
const entriesPath = path.join(libPath, 'index.d.ts');
11-
const entries = [];
10+
const libPath = path.join(cwd, 'lib');
11+
const entriesPath = path.join(libPath, 'index.d.ts');
12+
const entries = [];
1213

13-
options.entries.map(entry => {
14-
const shortName = entry.name;
14+
options.entries.forEach(entry => {
15+
const shortName = entry.name;
1516

16-
if(['core', 'field', 'locale', 'mixin-ui-state', 'util', 'validate'].indexOf(shortName) > -1) {
17-
return false;
18-
}
17+
if (['core', 'field', 'locale', 'mixin-ui-state', 'util', 'validate'].indexOf(shortName) > -1) {
18+
return false;
19+
}
1920

20-
const srcComponentPath = path.join(cwd, 'src', shortName);
21-
const libComponentPath = path.join(cwd, 'lib', shortName);
22-
const apiInfo = apiExtractor.extract(srcComponentPath);
21+
const srcComponentPath = path.join(cwd, 'src', shortName);
22+
const libComponentPath = path.join(cwd, 'lib', shortName);
23+
const apiInfo = apiExtractor.extract(srcComponentPath);
2324

24-
const apiPath = path.join(libComponentPath, 'api-schema.json');
25-
const exportDTSPath = path.join(libComponentPath, 'index.d.ts');
25+
const apiPath = path.join(libComponentPath, 'api-schema.json');
26+
const exportDTSPath = path.join(libComponentPath, 'index.d.ts');
2627

27-
if (apiInfo) {
28-
const apiString = JSON.stringify(apiInfo, null, 2);
28+
if (apiInfo) {
29+
const apiString = JSON.stringify(apiInfo, null, 2);
2930

30-
entries.push(`export { default as ${apiInfo.name} } from './${shortName}';\n`);
31+
entries.push(`export { default as ${apiInfo.name} } from './${shortName}';\n`);
3132

32-
fs.writeFileSync(apiPath, apiString);
33-
tsgen(apiInfo).then(apiData => {
34-
fs.writeFileSync(exportDTSPath, apiData.message);
35-
});
36-
}
37-
else {
38-
logger.warn(`Can not generate ${apiPath}`);
39-
}
33+
fs.writeFileSync(apiPath, apiString);
34+
tsgen(apiInfo).then(apiData => {
35+
fs.writeFileSync(exportDTSPath, apiData.message);
36+
});
37+
} else {
38+
logger.warn(`Can not generate ${apiPath}`);
39+
}
4040

41-
});
41+
});
4242

43-
// hack Field
44-
entries.push('export const Field: any;');
43+
// hack Field
44+
entries.push('export const Field: any;');
4545

46-
fs.writeFileSync(entriesPath, entries.join(''));
46+
fs.writeFileSync(entriesPath, entries.join(''));
4747
};

scripts/build/generate-api.js

+101-100
Original file line numberDiff line numberDiff line change
@@ -3,150 +3,151 @@ const path = require('path');
33
const remarkAbstract = require('remark');
44
const extractor = require('@alifd/api-extractor');
55
const { logger } = require('../utils');
6+
67
const remark = remarkAbstract();
78
const cwd = process.cwd();
89

910
const blackPropList = ['prefix', 'className', 'style', 'locale'];
1011

1112
const defaultOrderMap = {
12-
size: 0,
13-
type: 1,
14-
shape: 2
13+
size: 0,
14+
type: 1,
15+
shape: 2
1516
};
1617

1718
module.exports = function generateApis(componentName = 'all') {
18-
if (componentName === 'all') {
19+
if (componentName === 'all') {
1920

20-
const components = fs.readdirSync(path.join(cwd, 'docs'));
21+
const components = fs.readdirSync(path.join(cwd, 'docs'));
2122

22-
components.forEach(componentName => {
23-
if (/^\./.test(componentName)) {
24-
return;
25-
}
26-
generateApi(componentName);
27-
});
28-
} else {
29-
generateApi(componentName);
30-
}
23+
components.forEach(componentName => {
24+
if (/^\./.test(componentName)) {
25+
return;
26+
}
27+
generateApi(componentName);
28+
});
29+
} else {
30+
generateApi(componentName);
31+
}
3132

3233
};
3334

3435
function generateApi(componentName) {
35-
if (['core'].indexOf(componentName) > -1) {
36-
return false;
37-
}
38-
39-
const readmePath = path.join(cwd, 'docs', componentName, 'index.md');
40-
41-
if (!fs.existsSync(readmePath)) {
42-
return logger.error(`Can not find index.md: ${readmePath}`);
43-
}
44-
const readme = fs.readFileSync(readmePath, 'utf8');
45-
const ast = remark.parse(readme);
46-
const currentComponentPath = path.join(cwd, 'src', componentName);
47-
48-
let mainApiInfo = extractor.extract(currentComponentPath);
36+
if (['core'].indexOf(componentName) > -1) {
37+
return false;
38+
}
4939

50-
if (mainApiInfo) {
51-
let apiInfos = mainApiInfo.subComponents;
40+
const readmePath = path.join(cwd, 'docs', componentName, 'index.md');
5241

53-
apiInfos.forEach(apiInfo => {
54-
apiInfo.name = `${mainApiInfo.name}.${apiInfo.name}`;
55-
});
56-
delete mainApiInfo.subComponents;
57-
apiInfos.unshift(mainApiInfo);
58-
59-
let apiAST = generateAPIAST(apiInfos, cwd);
60-
let apiExtraAST = getAPIExtraAST(ast);
61-
apiAST.children = apiAST.children.concat(apiExtraAST);
62-
updateAST(ast, apiAST);
63-
let readme = remark.stringify(ast).replace(/\*\s\*\s\*/g, '---');
64-
fs.writeFileSync(readmePath, readme);
65-
} else {
66-
logger.warn(`Can not get API information of ${currentComponentPath}`);
67-
}
42+
if (!fs.existsSync(readmePath)) {
43+
return logger.error(`Can not find index.md: ${readmePath}`);
44+
}
45+
const readme = fs.readFileSync(readmePath, 'utf8');
46+
const ast = remark.parse(readme);
47+
const currentComponentPath = path.join(cwd, 'src', componentName);
48+
49+
const mainApiInfo = extractor.extract(currentComponentPath);
50+
51+
if (mainApiInfo) {
52+
const apiInfos = mainApiInfo.subComponents;
53+
54+
apiInfos.forEach(apiInfo => {
55+
apiInfo.name = `${mainApiInfo.name}.${apiInfo.name}`;
56+
});
57+
delete mainApiInfo.subComponents;
58+
apiInfos.unshift(mainApiInfo);
59+
60+
const apiAST = generateAPIAST(apiInfos, cwd);
61+
const apiExtraAST = getAPIExtraAST(ast);
62+
apiAST.children = apiAST.children.concat(apiExtraAST);
63+
updateAST(ast, apiAST);
64+
const readme = remark.stringify(ast).replace(/\*\s\*\s\*/g, '---');
65+
fs.writeFileSync(readmePath, readme);
66+
} else {
67+
logger.warn(`Can not get API information of ${currentComponentPath}`);
68+
}
6869
}
6970

7071
function generateAPIAST(apiInfos) {
71-
const apiDocs = '## API\n' + apiInfos.reduce((ret, apiInfo) => {
72-
if (apiInfo.props) {
73-
let props = filterProps(apiInfo.props);
74-
props = orderProps(props);
75-
ret = `${ret}\n### ${apiInfo.name}\n${apiInfo.description ? '>' + apiInfo.description + '\n' : ''}${extractor.generatePropsMD(props)}\n`;
76-
}
77-
return ret;
78-
}, '');
79-
80-
const apiAST = remark.parse(apiDocs);
81-
apiAST.children = apiAST.children.filter(child => {
82-
return !(child.children && child.children[0] && child.children[0].type === 'linkReference');
83-
});
72+
const apiDocs = `## API\n${apiInfos.reduce((ret, apiInfo) => {
73+
if (apiInfo.props) {
74+
let props = filterProps(apiInfo.props);
75+
props = orderProps(props);
76+
ret = `${ret}\n### ${apiInfo.name}\n${apiInfo.description ? `>${apiInfo.description}\n` : ''}${extractor.generatePropsMD(props)}\n`;
77+
}
78+
return ret;
79+
}, '')}`;
80+
81+
const apiAST = remark.parse(apiDocs);
82+
apiAST.children = apiAST.children.filter(child => {
83+
return !(child.children && child.children[0] && child.children[0].type === 'linkReference');
84+
});
8485

85-
return apiAST;
86+
return apiAST;
8687
}
8788

8889
function filterProps(props) {
89-
return Object.keys(props).reduce((ret, name) => {
90-
if (blackPropList.indexOf(name) === -1) {
91-
ret[name] = props[name];
92-
}
93-
return ret;
94-
}, {});
90+
return Object.keys(props).reduce((ret, name) => {
91+
if (blackPropList.indexOf(name) === -1) {
92+
ret[name] = props[name];
93+
}
94+
return ret;
95+
}, {});
9596
}
9697

9798
function orderProps(props) {
98-
const orderMap = Object.assign({}, defaultOrderMap);
99-
const names = Object.keys(props);
100-
names.forEach((name, index) => {
101-
if (!(name in orderMap)) {
102-
orderMap[name] = index * 10;
103-
}
104-
});
105-
const orderedNames = names.sort((prev, next) => orderMap[prev] - orderMap[next]);
99+
const orderMap = Object.assign({}, defaultOrderMap);
100+
const names = Object.keys(props);
101+
names.forEach((name, index) => {
102+
if (!(name in orderMap)) {
103+
orderMap[name] = index * 10;
104+
}
105+
});
106+
const orderedNames = names.sort((prev, next) => orderMap[prev] - orderMap[next]);
106107

107-
return orderedNames.reduce((ret, name) => {
108-
ret[name] = props[name];
109-
return ret;
110-
}, {});
108+
return orderedNames.reduce((ret, name) => {
109+
ret[name] = props[name];
110+
return ret;
111+
}, {});
111112
}
112113

113114
function getAPIExtraAST(ast) {
114-
const generateReg = key => new RegExp(`^<!--\\s*api-extra-${key}\\s*-->$`, 'i');
115-
const startIndex = ast.children.findIndex(child =>
116-
child.type === 'html' && generateReg('start').test(child.value));
117-
if (startIndex > -1) {
118-
const endIndex = ast.children.findIndex(child =>
119-
child.type === 'html' && generateReg('end').test(child.value));
120-
if (endIndex > -1 && startIndex < endIndex) {
121-
return ast.children.slice(startIndex, endIndex + 1);
115+
const generateReg = key => new RegExp(`^<!--\\s*api-extra-${key}\\s*-->$`, 'i');
116+
const startIndex = ast.children.findIndex(child =>
117+
child.type === 'html' && generateReg('start').test(child.value));
118+
if (startIndex > -1) {
119+
const endIndex = ast.children.findIndex(child =>
120+
child.type === 'html' && generateReg('end').test(child.value));
121+
if (endIndex > -1 && startIndex < endIndex) {
122+
return ast.children.slice(startIndex, endIndex + 1);
123+
}
122124
}
123-
}
124125

125-
return [];
126+
return [];
126127
}
127128

128129
function updateAST(ast, apiAST) {
129-
const apiIndex = ast.children.findIndex(child =>
130-
child.type === 'heading' &&
130+
const apiIndex = ast.children.findIndex(child =>
131+
child.type === 'heading' &&
131132
child.depth === 2 &&
132133
child.children &&
133134
child.children[0] &&
134135
child.children[0].value === 'API');
135-
if (apiIndex > -1) {
136-
const toNextHeading2 = ast.children.slice(apiIndex + 1).findIndex(child =>
137-
child.type === 'heading' &&
136+
if (apiIndex > -1) {
137+
const toNextHeading2 = ast.children.slice(apiIndex + 1).findIndex(child =>
138+
child.type === 'heading' &&
138139
child.depth === 2
139-
) + 1;
140+
) + 1;
140141

141-
if (toNextHeading2 === 0) {
142-
ast.children = ast.children.slice(0, apiIndex).concat(apiAST.children);
142+
if (toNextHeading2 === 0) {
143+
ast.children = ast.children.slice(0, apiIndex).concat(apiAST.children);
144+
} else {
145+
ast.children = ast.children.slice(0, apiIndex).concat(apiAST.children, ast.children.slice(apiIndex + toNextHeading2));
146+
}
143147
} else {
144-
ast.children = ast.children.slice(0, apiIndex).concat(apiAST.children, ast.children.slice(apiIndex + toNextHeading2));
148+
ast.children = ast.children.concat(apiAST.children);
145149
}
146-
} else {
147-
ast.children = ast.children.concat(apiAST.children);
148-
}
149150

150-
return ast;
151+
return ast;
151152
}
152153

0 commit comments

Comments
 (0)