Skip to content

Commit

Permalink
remove babel-core dep and upgrade babel-template to `@babel/templ…
Browse files Browse the repository at this point in the history
…ate`

Fixes opensearch-project#1088

Signed-off-by: Josh Romero <[email protected]>
  • Loading branch information
joshuarrrr committed Oct 16, 2023
1 parent 41be1e7 commit d67fb64
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 140 deletions.
8 changes: 4 additions & 4 deletions i18ntokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -2653,12 +2653,12 @@
"start": {
"line": 143,
"column": 4,
"index": 4700
"index": 4717
},
"end": {
"line": 148,
"column": 44,
"index": 4885
"index": 4902
}
},
"filepath": "src/components/list_group/pinnable_list_group/pinnable_list_group.tsx"
Expand All @@ -2671,12 +2671,12 @@
"start": {
"line": 143,
"column": 4,
"index": 4700
"index": 4717
},
"end": {
"line": 148,
"column": 44,
"index": 4885
"index": 4902
}
},
"filepath": "src/components/list_group/pinnable_list_group/pinnable_list_group.tsx"
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
"@babel/preset-env": "^7.11.0",
"@babel/preset-react": "^7.10.4",
"@babel/preset-typescript": "^7.12.1",
"@babel/template": "^7.10.4",
"@elastic/charts": "^30.2.0",
"@elastic/eslint-config-kibana": "^0.15.0",
"@faker-js/faker": "^8.0.1",
Expand All @@ -163,15 +164,13 @@
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"autoprefixer": "^9.8.6",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^24.1.0",
"babel-loader": "^8.1.0",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-plugin-dynamic-import-node": "^2.3.3",
"babel-plugin-inline-react-svg": "^1.1.1",
"babel-plugin-pegjs-inline-precompile": "^0.1.1",
"babel-template": "^6.26.0",
"cache-loader": "^4.1.0",
"chalk": "^4.1.2",
"chokidar": "^3.5.3",
Expand Down
70 changes: 33 additions & 37 deletions scripts/babel/proptypes-from-ts-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

const fs = require('fs');
const path = require('path');
const babelTemplate = require('babel-template');
const babelTemplate = require('@babel/template').default;
const babelCore = require('@babel/core');

const importedDefinitionsCache = new Map();
Expand Down Expand Up @@ -137,10 +137,10 @@ function resolveOmitToPropTypes(node, state) {
// extract the string values of keys to remove
const keysToRemove = new Set(
toRemovePropTypes.arguments[0].elements
.map(keyToRemove =>
.map((keyToRemove) =>
types.isStringLiteral(keyToRemove) ? keyToRemove.value : null
)
.filter(x => x !== null)
.filter((x) => x !== null)
);

// filter out omitted properties
Expand Down Expand Up @@ -335,7 +335,7 @@ function resolveIdentifierToPropTypes(node, state) {
const propertyName = property.key.name;
const isOptional = !isPropTypeRequired(types, property.value);
const existsOnB =
propsOnB.find(property => property.key.name === propertyName) != null;
propsOnB.find((property) => property.key.name === propertyName) != null;
if (isOptional || !existsOnB) {
optionalProps.add(propertyName);
}
Expand All @@ -345,7 +345,7 @@ function resolveIdentifierToPropTypes(node, state) {
const propertyName = property.key.name;
const isOptional = !isPropTypeRequired(types, property.value);
const existsOnA =
propsOnA.find(property => property.key.name === propertyName) != null;
propsOnA.find((property) => property.key.name === propertyName) != null;
if (isOptional || !existsOnA) {
optionalProps.add(propertyName);
}
Expand Down Expand Up @@ -540,7 +540,7 @@ function getPropTypesForNode(node, optional, state) {

// translates intersections (Foo & Bar & Baz) to a shape with the types' members (Foo, Bar, Baz) merged together
case 'TSIntersectionType':
const usableNodes = [...node.types].filter(node => {
const usableNodes = [...node.types].filter((node) => {
const nodePropTypes = getPropTypesForNode(node, true, state);

if (
Expand Down Expand Up @@ -577,7 +577,7 @@ function getPropTypesForNode(node, optional, state) {
nodePropTypes.callee.property.name === 'oneOfType'
) {
const properties = nodePropTypes.arguments[0].elements
.map(propType => {
.map((propType) => {
// This exists on a oneOfType which must be expressed as an optional proptype
propType = makePropTypeOptional(types, propType);

Expand All @@ -592,12 +592,12 @@ function getPropTypesForNode(node, optional, state) {
}

// extract all of the properties from this group and make them optional
return propType.arguments[0].properties.map(property => {
return propType.arguments[0].properties.map((property) => {
property.value = makePropTypeOptional(types, property.value);
return property;
});
})
.filter(x => x !== null)
.filter((x) => x !== null)
.reduce((allProperties, properties) => {
return [...allProperties, ...properties];
}, []);
Expand Down Expand Up @@ -638,7 +638,9 @@ function getPropTypesForNode(node, optional, state) {
if (mergedProperties.hasOwnProperty(typeProperty.key.name)) {
const existing = mergedProperties[typeProperty.key.name];
if (!areExpressionsIdentical(existing, typeProperty.value)) {
mergedProperties[typeProperty.key.name] = types.callExpression(
mergedProperties[
typeProperty.key.name
] = types.callExpression(
types.memberExpression(
types.identifier('PropTypes'),
types.identifier('oneOfType')
Expand Down Expand Up @@ -681,7 +683,7 @@ function getPropTypesForNode(node, optional, state) {
),
[
types.objectExpression(
propertyKeys.map(propKey => {
propertyKeys.map((propKey) => {
const objectProperty = types.objectProperty(
types.identifier(propKey),
mergedProperties[propKey]
Expand Down Expand Up @@ -718,13 +720,13 @@ function getPropTypesForNode(node, optional, state) {
// This helps filter out index signatures from interfaces,
// which don't translate to prop types.
.filter(
property =>
(property) =>
property.key != null &&
!types.isTSNeverKeyword(
property.typeAnnotation.typeAnnotation
)
)
.map(property => {
.map((property) => {
let propertyPropType =
property.type === 'TSMethodSignature'
? getPropTypesForNode(
Expand Down Expand Up @@ -798,7 +800,7 @@ function getPropTypesForNode(node, optional, state) {
),
[
types.arrayExpression(
node.properties.map(property =>
node.properties.map((property) =>
types.stringLiteral(
property.key.name || property.key.name || property.key.value
)
Expand All @@ -818,7 +820,7 @@ function getPropTypesForNode(node, optional, state) {
[
types.objectExpression(
node.members
.map(property => {
.map((property) => {
// skip never keyword
if (
types.isTSNeverKeyword(property.typeAnnotation.typeAnnotation)
Expand Down Expand Up @@ -853,7 +855,7 @@ function getPropTypesForNode(node, optional, state) {
}
return objectProperty;
})
.filter(x => x != null)
.filter((x) => x != null)
),
]
);
Expand All @@ -864,7 +866,7 @@ function getPropTypesForNode(node, optional, state) {
// literal values are extracted into a `oneOf`, if all members are literals this oneOf is used
// otherwise `oneOfType` is used - if there are any literal values it contains the literals' `oneOf`
case 'TSUnionType':
const tsUnionTypes = node.types.map(node =>
const tsUnionTypes = node.types.map((node) =>
getPropTypesForNode(node, false, state)
);

Expand Down Expand Up @@ -934,7 +936,7 @@ function getPropTypesForNode(node, optional, state) {

// translate enum to PropTypes.oneOf
case 'TSEnumDeclaration':
const memberTypes = node.members.map(member =>
const memberTypes = node.members.map((member) =>
getPropTypesForNode(member, true, state)
);
propType = types.callExpression(
Expand Down Expand Up @@ -1088,7 +1090,7 @@ const typeDefinitionExtractors = {
// only process relative imports for typescript definitions (avoid node_modules)
if (isPathRelative) {
// find the variable names being imported
const importedTypeNames = node.specifiers.map(specifier => {
const importedTypeNames = node.specifiers.map((specifier) => {
switch (specifier.type) {
case 'ImportSpecifier':
return specifier.imported.name;
Expand Down Expand Up @@ -1187,14 +1189,12 @@ const typeDefinitionExtractors = {
* @param node
* @returns Array
*/
TSInterfaceDeclaration: node => {
TSInterfaceDeclaration: (node) => {
const { id } = node;

if (id.type !== 'Identifier') {
throw new Error(
`TSInterfaceDeclaration typeDefinitionExtract could not understand id type ${
id.type
}`
`TSInterfaceDeclaration typeDefinitionExtract could not understand id type ${id.type}`
);
}

Expand All @@ -1206,14 +1206,12 @@ const typeDefinitionExtractors = {
* @param node
* @returns Array
*/
TSTypeAliasDeclaration: node => {
TSTypeAliasDeclaration: (node) => {
const { id, typeAnnotation } = node;

if (id.type !== 'Identifier') {
throw new Error(
`TSTypeAliasDeclaraction typeDefinitionExtract could not understand id type ${
id.type
}`
`TSTypeAliasDeclaraction typeDefinitionExtract could not understand id type ${id.type}`
);
}
return [{ name: id.name, definition: typeAnnotation }];
Expand All @@ -1224,14 +1222,12 @@ const typeDefinitionExtractors = {
* @param node
* @returns Array
*/
TSEnumDeclaration: node => {
TSEnumDeclaration: (node) => {
const { id } = node;

if (id.type !== 'Identifier') {
throw new Error(
`TSEnumDeclaration typeDefinitionExtract could not understand id type ${
id.type
}`
`TSEnumDeclaration typeDefinitionExtract could not understand id type ${id.type}`
);
}

Expand All @@ -1243,7 +1239,7 @@ const typeDefinitionExtractors = {
* @param node
* @returns Array
*/
VariableDeclaration: node => {
VariableDeclaration: (node) => {
return node.declarations.reduce((declarations, declaration) => {
if (
declaration.init != null &&
Expand Down Expand Up @@ -1415,7 +1411,7 @@ module.exports = function propTypesFromTypeScript({ types }) {
{
ImportDeclaration: ({ node }) => {
if (node.source.value === 'react') {
node.specifiers.forEach(specifier => {
node.specifiers.forEach((specifier) => {
if (specifier.type === 'ImportSpecifier') {
importsFromReact.add(specifier.local.name);
}
Expand All @@ -1440,7 +1436,7 @@ module.exports = function propTypesFromTypeScript({ types }) {
this.file.opts.filename
),
fs: opts.fs || fs,
parse: code => babelCore.parse(code, state.file.opts),
parse: (code) => babelCore.parse(code, state.file.opts),
};

// collect named TS type definitions for later reference
Expand Down Expand Up @@ -1471,10 +1467,10 @@ module.exports = function propTypesFromTypeScript({ types }) {
// remove any exported identifiers that are TS types or interfaces
// this prevents TS-only identifiers from leaking into ES code
programPath.traverse({
ExportNamedDeclaration: path => {
ExportNamedDeclaration: (path) => {
const specifiers = path.get('specifiers');
const source = path.get('source');
specifiers.forEach(specifierPath => {
specifiers.forEach((specifierPath) => {
if (types.isExportSpecifier(specifierPath)) {
const {
node: { local },
Expand Down Expand Up @@ -1581,7 +1577,7 @@ module.exports = function propTypesFromTypeScript({ types }) {
)
return;

const resolveVariableDeclarator = variableDeclarator => {
const resolveVariableDeclarator = (variableDeclarator) => {
const { id } = variableDeclarator;
const idTypeAnnotation = id.typeAnnotation;
let fileCodeNeedsUpdating = false;
Expand Down
Loading

0 comments on commit d67fb64

Please sign in to comment.