Skip to content

Commit

Permalink
Update transform_files.mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 committed Oct 31, 2022
1 parent 483379a commit 6208499
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions scripts/transform_files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,19 @@ function parseClassBase(raw, find) {

const statics = [];
walk.simple(ast, {
ExpressionStatement({ expression: { left, right } }) {
ExpressionStatement(node) {
const {
expression: { left, right },
} = node;
if (
left?.type === 'MemberExpression' &&
printNode(left.object).slice(0, -1) === variableName
) {
statics.push({
type: right.type === 'FunctionExpression' ? 'method' : 'property',
key: printNode(left.property),
value: printNode(right),
node: right,
value: right,
node,
comment: findNodeComment(left),
});
}
Expand Down Expand Up @@ -316,21 +319,26 @@ function transformClass(type, raw, options = {}) {
);
});

staticProperties.forEach(({ key, node, comment }) => {
staticProperties.forEach(({ key, value, comment }) => {
classBody.push(
(comment ? printNode(comment) : '') + '\n' + key + '=' + printNode(node)
(comment ? printNode(comment) : '') +
'\n' +
'static ' +
key +
'=' +
printNode(value)
);
});

staticMethods.forEach(({ key, node, comment }) => {
staticMethods.forEach(({ key, value, comment }) => {
classBody.push(
(comment ? printNode(comment) : '') +
'\n' +
'static ' +
(node.async ? 'async ' : '') +
(value.async ? 'async ' : '') +
key +
`(${printNode(node.params).slice(0, -1)}) {\n` +
printNode(node.body.body) +
`(${printNode(value.params).slice(0, -1)}) {\n` +
printNode(value.body.body) +
'\n}'
);
});
Expand Down Expand Up @@ -367,8 +375,9 @@ function transformClass(type, raw, options = {}) {
const defaultsKey = `${_.lowerFirst(finalName)}DefaultValues`;
classDirective +=
'\n\n' +
`export const ${defaultsKey} = {\n${_.map(defaultValues, (value, key) =>
[key, value].join(':')
`export const ${defaultsKey}: TClassProperties<${finalName}> = {\n${_.map(
defaultValues,
(value, key) => [key, value].join(':')
).join(',\n')}\n};` +
'\n\n' +
`Object.assign(${finalName}.prototype, ${defaultsKey})`;
Expand Down Expand Up @@ -399,6 +408,19 @@ function transformClass(type, raw, options = {}) {
.replace(/\s*\)\s*;?/, '')}`;
}

[...staticMethods, ...staticProperties].forEach(({ node, comment }) => {
if (comment) {
rawFile = rawFile.replace(
new RegExp(
_.escapeRegExp(printNode(comment, false)).replace(' ', '\\s'),
'gm'
),
''
);
}
rawFile = rawFile.replace(printNode(node, false), '');
});

rawFile = rawFile
.replace(new RegExp(namespace.replace(/\./g, '\\.'), 'g'), name)
.replace(/fabric\.Object/g, 'FabricObject');
Expand Down

0 comments on commit 6208499

Please sign in to comment.