Skip to content

Commit

Permalink
feat(sortApiTable): support process deprecated props (#283)
Browse files Browse the repository at this point in the history
* feat(sortApiTable): support process deprecated props

* feat: support output report only
  • Loading branch information
Wxh16144 authored Dec 14, 2024
1 parent 14ee166 commit 4da4eee
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions lib/sortApiTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,31 @@ const remarkWithYaml = unified()

const stream = majo();

function get(obj, pathStr = '', defaultValue) {
return pathStr.split('.').reduce((acc, key) => acc && acc[key], obj) || defaultValue;
}

function getCellValue(node) {
return node.children[0].children[0].value;
let cloneNode = { ...node };

while (cloneNode.type !== 'text' && cloneNode.children) {
[cloneNode] = cloneNode.children;
}

return cloneNode.value || '';
}

function checkForCellDeletion(node) {
let cloneNode = { ...node };

while (Array.isArray(cloneNode.children)) {
if (cloneNode.type === 'delete') {
return true;
}
[cloneNode] = cloneNode.children;
}

return cloneNode.type === 'delete';
}

// from small to large
Expand All @@ -37,6 +60,8 @@ const whiteMethodList = ['afterChange', 'beforeChange'];
const groups = {
isDynamic: val => /^on[A-Z]/.test(val) || whiteMethodList.indexOf(val) > -1,
isSize: val => sizeBreakPoints.indexOf(val) > -1,
// https://github.com/ant-design/ant-design/pull/51342
isDeprecated: checkForCellDeletion,
};

function asciiSort(prev, next) {
Expand Down Expand Up @@ -72,6 +97,7 @@ function sort(ast, filename) {
static: new Set(),
size: new Set(),
dynamic: new Set(),
deprecated: new Set(),
};

ast.children.forEach(child => {
Expand All @@ -81,13 +107,20 @@ function sort(ast, filename) {
// one of ['xs', 'sm', 'md', 'lg', 'xl']
const sizeProps = [];

// deprecated props ~~props~~
const deprecatedProps = [];

// find table markdown type
if (child.type === 'table') {
// slice will create new array, so sort can affect the original array.
// slice(1) cut down the thead
child.children.slice(1).forEach(node => {
const value = getCellValue(node);
if (groups.isDynamic(value)) {

if (groups.isDeprecated(node)) {
deprecatedProps.push(node);
fileAPIs[componentName].deprecated.add(value);
} else if (groups.isDynamic(value)) {
dynamicProps.push(node);
fileAPIs[componentName].dynamic.add(value);
} else if (groups.isSize(value)) {
Expand All @@ -105,6 +138,8 @@ function sort(ast, filename) {
...alphabetSort(staticProps),
...sizeSort(sizeProps),
...alphabetSort(dynamicProps),
// deprecated props should be the last
...alphabetSort(deprecatedProps),
];
}
});
Expand All @@ -119,7 +154,15 @@ function sortAPI(md, filename) {
function sortMiddleware(ctx) {
Object.keys(ctx.files).forEach(filename => {
const content = ctx.fileContents(filename);
ctx.writeContents(filename, sortAPI(content, filename));

const sortedContent = sortAPI(content, filename);

if (get(ctx.meta, 'program.report', false)) {
console.log(chalk.cyan(`🔍 report ${filename}`));
} else {
// write the sorted content back to the file
ctx.writeContents(filename, sortedContent);
}
});
}

Expand All @@ -135,12 +178,21 @@ module.exports = () => {
'components/**/index.+(zh-CN|en-US).md'
)
.option('-o, --output [output]', 'Specify component api output path', '~component-api.json')
.option('-r, --report', 'Only output the report, will not modify the file', false)
.parse(process.argv);
// Get the markdown file all need to be transformed

// inject context to the majo stream
function injectContext(ctx) {
if (typeof ctx.meta !== 'object') ctx.meta = {};

Object.assign(ctx.meta, { program });
}

/* eslint-disable no-console */
stream
.source(program.file)
.use(injectContext)
.use(sortMiddleware)
.dest('.')
.then(() => {
Expand All @@ -151,6 +203,7 @@ module.exports = () => {
static: [...fileAPIs[componentName].static],
size: [...fileAPIs[componentName].size],
dynamic: [...fileAPIs[componentName].dynamic],
deprecated: [...fileAPIs[componentName].deprecated],
};
});

Expand Down

0 comments on commit 4da4eee

Please sign in to comment.