Skip to content

Commit

Permalink
update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbsgilbs committed Apr 8, 2022
1 parent 91ebaae commit 644821c
Show file tree
Hide file tree
Showing 13 changed files with 2,567 additions and 4,152 deletions.
60 changes: 30 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,41 @@
"*"
],
"dependencies": {
"@babel/core": "^7.12.3",
"@babel/types": "7.9.6",
"@babel/core": "^7.17.9",
"@babel/types": "7.17.0",
"deepmerge": "^4.2.2",
"i18next": "^19.8.3",
"i18next": "^21.6.14",
"json-stable-stringify": "^1.0.1"
},
"devDependencies": {
"@babel/cli": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"@babel/preset-react": "^7.12.5",
"@babel/preset-typescript": "^7.12.1",
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-node-resolve": "^10.0.0",
"@types/babel__core": "^7.1.12",
"@types/fs-extra": "^9.0.3",
"@types/jest": "^26.0.15",
"@alex_neo/jest-expect-message": "^1.0.5",
"@babel/cli": "^7.17.6",
"@babel/preset-env": "^7.16.11",
"@babel/preset-react": "^7.16.7",
"@babel/preset-typescript": "^7.16.7",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-node-resolve": "^13.1.3",
"@types/babel__core": "^7.1.19",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^27.4.1",
"@types/jest-expect-message": "^1.0.3",
"@types/json-stable-stringify": "^1.0.32",
"@types/node": "^14.14.7",
"@types/rimraf": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"copyfiles": "^2.4.0",
"eslint": "^7.13.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.1.4",
"fs-extra": "^9.0.1",
"jest": "^26.6.3",
"jest-expect-message": "^1.0.2",
"prettier": "^2.1.2",
"@types/json-stable-stringify": "^1.0.34",
"@types/node": "^17.0.23",
"@types/rimraf": "^3.0.2",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"copyfiles": "^2.4.1",
"eslint": "^8.12.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"fs-extra": "^10.0.1",
"jest": "^27.5.1",
"prettier": "^2.6.2",
"rimraf": "^3.0.0",
"rollup": "^2.33.1",
"tslib": "^2.0.3",
"typescript": "^4.0.5"
"rollup": "^2.70.1",
"tslib": "^2.3.1",
"typescript": "^4.6.3"
},
"scripts": {
"lint": "eslint --ext=.ts,.js,.tsx,.jsx src docs tests rollup.config.js",
Expand All @@ -86,7 +86,7 @@
],
"coverageDirectory": "./coverage/",
"setupFilesAfterEnv": [
"jest-expect-message"
"@alex_neo/jest-expect-message"
]
}
}
6 changes: 3 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import { babel } from '@rollup/plugin-babel';
import { nodeResolve } from '@rollup/plugin-node-resolve';

import pkg from './package.json';

Expand Down Expand Up @@ -28,7 +28,7 @@ export default {
...Object.keys(pkg.peerDependencies || {}),
],
plugins: [
resolve({ extensions }),
nodeResolve({ extensions }),
babel({
include: 'src/**/*',
exclude: 'node_modules/**',
Expand Down
7 changes: 6 additions & 1 deletion src/exporters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ function loadTranslationFile<F>(
try {
content = fs.readFileSync(filePath, { encoding: 'utf8' });
} catch (err) {
if (err.code === 'ENOENT') return exporter.init({ config });
if (
err !== null &&
typeof err == 'object' &&
(err as NodeJS.ErrnoException).code === 'ENOENT'
)
return exporter.init({ config });
throw err;
}

Expand Down
1 change: 1 addition & 0 deletions src/exporters/jsonv3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const jsonv3Exporter: Exporter<JsonV3File, JsonV3Value> = {
},
addKey: (params) => {
const { key, file, value } = params;

return {
...file,
content: recursiveAddKey(file.content, key.keyPath, key.cleanKey, value),
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/getFixedTFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function extractGetFixedTFunction(
let keys = Array<ExtractedKey>();
for (const reference of tBinding.referencePaths) {
if (
reference.parentPath.isCallExpression() &&
reference.parentPath?.isCallExpression() &&
reference.parentPath.get('callee') === reference
) {
keys = [
Expand Down
15 changes: 6 additions & 9 deletions src/extractors/transComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,16 @@ function parseTransComponentOptions(

const nsAttr = findJSXAttributeByName(path, 'ns');
if (nsAttr) {
let value: BabelCore.NodePath<BabelTypes.Node | null> = nsAttr.get(
'value',
);
let value: BabelCore.NodePath<BabelTypes.Node | null | undefined> =
nsAttr.get('value');
if (value.isJSXExpressionContainer()) value = value.get('expression');
res.ns = getFirstOrNull(evaluateIfConfident(value));
}

const defaultsAttr = findJSXAttributeByName(path, 'defaults');
if (defaultsAttr) {
let value: BabelCore.NodePath<BabelTypes.Node | null> = defaultsAttr.get(
'value',
);
let value: BabelCore.NodePath<BabelTypes.Node | null | undefined> =
defaultsAttr.get('value');
if (value.isJSXExpressionContainer()) value = value.get('expression');
res.defaultValue = evaluateIfConfident(value);
}
Expand Down Expand Up @@ -353,9 +351,8 @@ export default function extractTransComponent(
if (getCommentHintForPath(path, 'DISABLE', commentHints)) return [];
if (!skipCheck && !isTransComponent(path)) return [];

const keyEvaluationFromAttribute = parseTransComponentKeyFromAttributes(
path,
);
const keyEvaluationFromAttribute =
parseTransComponentKeyFromAttributes(path);
const keyEvaluationFromChildren = parseTransComponentKeyFromChildren(
path,
config,
Expand Down
7 changes: 3 additions & 4 deletions src/extractors/translationRenderProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ export default function extractTranslationRenderProp(
// Try to parse ns property
const nsAttr = findJSXAttributeByName(path, 'ns');
if (nsAttr) {
let value: BabelCore.NodePath<BabelTypes.Node | null> = nsAttr.get(
'value',
);
let value: BabelCore.NodePath<BabelTypes.Node | null | undefined> =
nsAttr.get('value');
if (value.isJSXExpressionContainer()) value = value.get('expression');
ns = getFirstOrNull(evaluateIfConfident(value));
}
Expand All @@ -80,7 +79,7 @@ export default function extractTranslationRenderProp(
let keys = Array<ExtractedKey>();
for (const reference of tBinding.referencePaths) {
if (
reference.parentPath.isCallExpression() &&
reference.parentPath?.isCallExpression() &&
reference.parentPath.get('callee') === reference
) {
keys = [
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/useTranslationHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default function extractUseTranslationHook(
let keys = Array<ExtractedKey>();
for (const reference of tBinding.referencePaths) {
if (
reference.parentPath.isCallExpression() &&
reference.parentPath?.isCallExpression() &&
reference.parentPath.get('callee') === reference
) {
keys = [
Expand Down
50 changes: 26 additions & 24 deletions src/extractors/withTranslationHOC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function isWithTranslationHOCCallExpression(
function findWithTranslationHOCCallExpressionInParents(
path: BabelCore.NodePath<BabelTypes.Node>,
): BabelCore.NodePath<BabelTypes.CallExpression> | null {
const callExpr: BabelCore.NodePath = path.findParent((parentPath) => {
const callExpr = path.findParent((parentPath) => {
if (!parentPath.isCallExpression()) return false;
const callee = parentPath.get('callee');
return isWithTranslationHOCCallExpression(callee);
Expand Down Expand Up @@ -69,18 +69,21 @@ function findWithTranslationHOCCallExpressionInCompose(
const composeFunctionNames = ['compose', 'flow', 'flowRight'];

let currentPath = path.parentPath;
let withTranslationCallExpr: BabelCore.NodePath<
BabelTypes.CallExpression
> | null = null;
let withTranslationCallExpr: BabelCore.NodePath<BabelTypes.CallExpression> | null =
null;

while (currentPath.isCallExpression()) {
while (currentPath?.isCallExpression()) {
if (withTranslationCallExpr === null) {
const args: BabelCore.NodePath[] = currentPath.get('arguments');
withTranslationCallExpr =
args.find(isWithTranslationHOCCallExpression) || null;
}

let callee = currentPath.get('callee');
let callee: BabelCore.NodePath<
| BabelTypes.V8IntrinsicIdentifier
| BabelTypes.Expression
| BabelTypes.PrivateName
> = currentPath.get('callee');
if (callee.isMemberExpression()) {
// If we have a member expression, we take the right operand
// e.g. _.compose
Expand Down Expand Up @@ -176,13 +179,11 @@ function findTFunctionIdentifierInObjectPattern(
* @param path the node to check.
* @returns true if the path is the callee of a call expression.
*/
function isCallee(
path: BabelCore.NodePath,
): path is BabelCore.NodePath & {
function isCallee(path: BabelCore.NodePath): path is BabelCore.NodePath & {
parentPath: BabelCore.NodePath<BabelTypes.CallExpression>;
} {
return (
path.parentPath.isCallExpression() &&
return !!(
path.parentPath?.isCallExpression() &&
path === path.parentPath.get('callee')
);
}
Expand All @@ -201,17 +202,16 @@ function findTFunctionCallsFromPropsAssignment(
): BabelCore.NodePath<BabelTypes.CallExpression>[] {
const tReferences = Array<BabelCore.NodePath>();

const body = propsId.parentPath.get('body');
if (Array.isArray(body)) return [];
const body = propsId.parentPath?.get('body');
if (body === undefined || Array.isArray(body)) return [];
const scope = body.scope;

if (propsId.isObjectPattern()) {
// got "function MyComponent({t, other, props})"
// or "const {t, other, props} = this.props"
// we want to find references to "t"
const tFunctionIdentifier = findTFunctionIdentifierInObjectPattern(
propsId,
);
const tFunctionIdentifier =
findTFunctionIdentifierInObjectPattern(propsId);
if (tFunctionIdentifier === null) return [];
const tBinding = scope.bindings[tFunctionIdentifier.node.name];
tReferences.push(...tBinding.referencePaths);
Expand All @@ -221,9 +221,13 @@ function findTFunctionCallsFromPropsAssignment(
// we want to find references to props.t
const references = scope.bindings[propsId.node.name].referencePaths;
for (const reference of references) {
if (reference.parentPath.isMemberExpression()) {
if (reference.parentPath?.isMemberExpression()) {
const prop = reference.parentPath.get('property');
if (!Array.isArray(prop) && prop.node.name === 't') {
if (
!Array.isArray(prop) &&
prop.isIdentifier() &&
prop.node.name === 't'
) {
tReferences.push(reference.parentPath);
}
}
Expand Down Expand Up @@ -327,9 +331,8 @@ export default function extractWithTranslationHOC(
commentHints: CommentHint[] = [],
): ExtractedKey[] {
// Detect if this component is wrapped with withTranslation() somewhere
const withTranslationCallExpression = findWithTranslationHOCCallExpression(
path,
);
const withTranslationCallExpression =
findWithTranslationHOCCallExpression(path);
if (withTranslationCallExpression === null) return [];

let tCalls: BabelCore.NodePath<BabelTypes.CallExpression>[];
Expand All @@ -353,9 +356,8 @@ export default function extractWithTranslationHOC(
ns = nsCommentHint.value;
} else {
// Otherwise, try to get namespace from arguments.
const namespaceArgument = withTranslationCallExpression.get(
'arguments',
)[0];
const namespaceArgument =
withTranslationCallExpression.get('arguments')[0];
ns = getFirstOrNull(evaluateIfConfident(namespaceArgument));
}

Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import i18next from 'i18next';
import { init as i18nextInit } from 'i18next';

import plugin from './plugin';

i18next.init();
i18nextInit({
// FIXME https://github.com/gilbsgilbs/babel-plugin-i18next-extract/issues/203
compatibilityJSON: 'v3',
});

export default plugin;
2 changes: 1 addition & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export default function (
// Program node doesn't call the visitor for Program node.
if (BabelTypes.isFile(path.container)) {
this.I18NextExtract.commentHints = parseCommentHints(
path.container.comments,
path.container.comments || [],
);
}

Expand Down
15 changes: 12 additions & 3 deletions tests/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ function readExtractedFile(
try {
extracted = fs.readJSONSync(realOutputPath, { encoding: 'utf8' });
} catch (err) {
if (err.code === 'ENOENT') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (
err !== null &&
typeof err == 'object' &&
(err as NodeJS.ErrnoException).code === 'ENOENT'
) {
expect(
true,
`Couldn't find a JSON file to read at ${realOutputPath}. This probably means the ` +
Expand Down Expand Up @@ -212,12 +217,16 @@ export function runChecks(): void {
plugins: [[plugin, testData.pluginOptions]],
});
} catch (err) {
errorMessage = err.message;
if (err instanceof Error) {
errorMessage = err.message;
} else {
errorMessage = `${err}`;
}
break;
}
expect(
transformResult && transformResult.code,
`Babel transformation failed.`,
'Babel transformation failed.',
).toBeTruthy();
}

Expand Down
Loading

0 comments on commit 644821c

Please sign in to comment.