143 lines
6.5 KiB
JavaScript
143 lines
6.5 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.updateDefaultProps = void 0;
|
|
const studio_shared_1 = require("@remotion/studio-shared");
|
|
const recast = __importStar(require("recast"));
|
|
const parse_ast_1 = require("./parse-ast");
|
|
const updateDefaultProps = async ({ input, compositionId, newDefaultProps, enumPaths, }) => {
|
|
const ast = (0, parse_ast_1.parseAst)(input);
|
|
recast.types.visit(ast, {
|
|
visitJSXElement(path) {
|
|
var _a, _b;
|
|
const { openingElement } = path.node;
|
|
// 1: ensure its the element we're looking for
|
|
const openingName = openingElement.name;
|
|
if (openingName.type !== 'JSXIdentifier' &&
|
|
openingName.type !== 'JSXNamespacedName') {
|
|
this.traverse(path); // Continue traversing the AST
|
|
return;
|
|
}
|
|
if (openingName.name !== 'Composition' && openingName.name !== 'Still') {
|
|
this.traverse(path); // Continue traversing the AST
|
|
return;
|
|
}
|
|
if (!((_a = openingElement.attributes) === null || _a === void 0 ? void 0 : _a.some((attr) => {
|
|
if (attr.type === 'JSXSpreadAttribute') {
|
|
return;
|
|
}
|
|
if (!attr.value) {
|
|
return;
|
|
}
|
|
if (attr.value.type === 'JSXElement') {
|
|
return;
|
|
}
|
|
if (attr.value.type === 'JSXExpressionContainer') {
|
|
return;
|
|
}
|
|
if (attr.value.type === 'JSXFragment') {
|
|
return;
|
|
}
|
|
return attr.name.name === 'id' && attr.value.value === compositionId;
|
|
}))) {
|
|
this.traverse(path); // Continue traversing the AST
|
|
return;
|
|
}
|
|
// 2: Find the defaultProps attribute and handle related errors
|
|
const defaultPropsAttr = openingElement.attributes.find((attr) => {
|
|
if (attr.type === 'JSXSpreadAttribute') {
|
|
this.traverse(path); // Continue traversing the AST
|
|
return;
|
|
}
|
|
return attr.name.name === 'defaultProps';
|
|
});
|
|
if (!defaultPropsAttr) {
|
|
throw new Error(`No \`defaultProps\` prop found in the <Composition/> tag with the ID "${compositionId}".`);
|
|
}
|
|
if (defaultPropsAttr.type === 'JSXSpreadAttribute') {
|
|
this.traverse(path); // Continue traversing the AST
|
|
return;
|
|
}
|
|
// 3: ensure only hardcoded values are provided
|
|
if (!defaultPropsAttr.value ||
|
|
defaultPropsAttr.value.type === 'JSXElement' ||
|
|
defaultPropsAttr.value.type === 'JSXText' ||
|
|
defaultPropsAttr.value.type === 'StringLiteral' ||
|
|
defaultPropsAttr.value.type === 'NumericLiteral' ||
|
|
defaultPropsAttr.value.type === 'BigIntLiteral' ||
|
|
defaultPropsAttr.value.type === 'DecimalLiteral' ||
|
|
defaultPropsAttr.value.type === 'NullLiteral' ||
|
|
defaultPropsAttr.value.type === 'BooleanLiteral' ||
|
|
defaultPropsAttr.value.type === 'RegExpLiteral' ||
|
|
defaultPropsAttr.value.type === 'JSXFragment' ||
|
|
defaultPropsAttr.value.type === 'Literal') {
|
|
throw new Error(`\`defaultProps\` prop must be a hardcoded value in the <Composition/> tag, but it is a ${(_b = defaultPropsAttr.value) === null || _b === void 0 ? void 0 : _b.type}".`);
|
|
}
|
|
const defaultPropsValue = defaultPropsAttr.value.expression;
|
|
if (defaultPropsValue.type !== 'ObjectExpression' &&
|
|
defaultPropsValue.type !== 'TSAsExpression') {
|
|
throw new Error(`\`defaultProps\` prop must be a hardcoded value in the <Composition/> tag with the ID "${compositionId}".`);
|
|
}
|
|
defaultPropsAttr.value.expression = recast.types.builders.identifier((0, studio_shared_1.stringifyDefaultProps)({ props: newDefaultProps, enumPaths }));
|
|
this.traverse(path); // Continue traversing the AST
|
|
},
|
|
});
|
|
let prettier = null;
|
|
try {
|
|
prettier = await Promise.resolve().then(() => __importStar(require('prettier')));
|
|
}
|
|
catch (_a) {
|
|
throw new Error('Prettier cannot be found in the current project.');
|
|
}
|
|
const { format, resolveConfig, resolveConfigFile } = prettier;
|
|
const configFilePath = await resolveConfigFile();
|
|
if (!configFilePath) {
|
|
throw new Error('The Prettier config file was not found');
|
|
}
|
|
const prettierConfig = await resolveConfig(configFilePath);
|
|
if (!prettierConfig) {
|
|
throw new Error('The Prettier config file was not found. For this feature, the "prettier" package must be installed and a .prettierrc file must exist.');
|
|
}
|
|
const finalfile = (0, parse_ast_1.serializeAst)(ast);
|
|
const prettified = await format(finalfile, {
|
|
...prettierConfig,
|
|
filepath: 'test.tsx',
|
|
plugins: [],
|
|
endOfLine: 'auto',
|
|
});
|
|
return prettified;
|
|
};
|
|
exports.updateDefaultProps = updateDefaultProps;
|