messages-modules
Advanced tools
Comparing version 1.1.14 to 1.1.15
@@ -14,3 +14,3 @@ import { HijackTarget, KeyValueObject } from '../'; | ||
*/ | ||
export declare function getMessages(propertiesFilePath: string): KeyValueObject; | ||
export declare const getMessages: (propertiesFilePath: string) => KeyValueObject; | ||
/** | ||
@@ -23,2 +23,3 @@ * This an example Babel plugin. | ||
*/ | ||
export default function plugin(): PluginObj; | ||
declare const plugin: () => PluginObj; | ||
export default plugin; |
@@ -22,5 +22,5 @@ "use strict"; | ||
*/ | ||
function getMessages(propertiesFilePath) { | ||
var getMessages = function (propertiesFilePath) { | ||
return (0, properties_file_1.propertiesToJson)(propertiesFilePath); | ||
} | ||
}; | ||
exports.getMessages = getMessages; | ||
@@ -34,5 +34,3 @@ /** | ||
*/ | ||
function plugin() { | ||
return (0, __1.messageModulePlugin)(exports.hijackTargets, 'properties', getMessages); | ||
} | ||
var plugin = function () { return (0, __1.messageModulePlugin)(exports.hijackTargets, 'properties', exports.getMessages); }; | ||
exports.default = plugin; |
@@ -62,3 +62,3 @@ import * as BabelTypes from '@babel/types'; | ||
* */ | ||
export declare function messageModulePlugin(hijackTargets: HijackTarget[], messagesFileExtension: string, getMessages: (messagesFilePath: string) => KeyValueObject): PluginObj; | ||
export declare const messageModulePlugin: (hijackTargets: HijackTarget[], messagesFileExtension: string, getMessages: (messagesFilePath: string) => KeyValueObject) => PluginObj; | ||
/** | ||
@@ -65,0 +65,0 @@ * Get the injected localized messages in a local scope. |
103
lib/index.js
@@ -27,5 +27,3 @@ "use strict"; | ||
*/ | ||
function escapeRegExp(regexp) { | ||
return regexp.replace(/[$()*+.?[\\\]^{|}]/g, '\\$&'); // $& means the whole matched string | ||
} | ||
var escapeRegExp = function (regexp) { return regexp.replace(/[$()*+.?[\\\]^{|}]/g, '\\$&'); }; | ||
var InjectedMessages = /** @class */ (function () { | ||
@@ -56,3 +54,3 @@ /** | ||
*/ | ||
function getInjectedMessages(sourceFilePath, messagesFileExtension, getMessages) { | ||
var getInjectedMessages = function (sourceFilePath, messagesFileExtension, getMessages) { | ||
var parsedSourceFile = (0, node_path_1.parse)(sourceFilePath); | ||
@@ -78,3 +76,3 @@ var sourceFileDirectoryPath = parsedSourceFile.dir; | ||
return JSON.stringify(injectedMessages); | ||
} | ||
}; | ||
/** | ||
@@ -88,5 +86,3 @@ * Verify if an import or export statement matches the target module. | ||
*/ | ||
function isMatchingModule(nodePath, hijackTarget) { | ||
return !!nodePath.node.source && nodePath.node.source.value === hijackTarget.module; | ||
} | ||
var isMatchingModule = function (nodePath, hijackTarget) { return !!nodePath.node.source && nodePath.node.source.value === hijackTarget.module; }; | ||
/** | ||
@@ -100,3 +96,3 @@ * Verify if an import or export statement matches the target function. | ||
*/ | ||
function isMatchingFunction(nodePath, hijackTarget) { | ||
var isMatchingFunction = function (nodePath, hijackTarget) { | ||
return nodePath.node.specifiers.some(function (specifier) { | ||
@@ -106,3 +102,3 @@ return ((isImportSpecifier(specifier) && isMatchingImportFunction(specifier, hijackTarget)) || | ||
}); | ||
} | ||
}; | ||
/** | ||
@@ -116,5 +112,3 @@ * Verify if an import specifier matches the target function. | ||
*/ | ||
function isMatchingImportFunction(specifier, hijackTarget) { | ||
return isIdentifier(specifier.imported) && specifier.imported.name === hijackTarget.function; | ||
} | ||
var isMatchingImportFunction = function (specifier, hijackTarget) { return isIdentifier(specifier.imported) && specifier.imported.name === hijackTarget.function; }; | ||
/** | ||
@@ -128,5 +122,3 @@ * Verify if an export specifier matches the target function. | ||
*/ | ||
function isMatchingExportFunction(specifier, hijackTarget) { | ||
return isIdentifier(specifier.local) && specifier.local.name === hijackTarget.function; | ||
} | ||
var isMatchingExportFunction = function (specifier, hijackTarget) { return isIdentifier(specifier.local) && specifier.local.name === hijackTarget.function; }; | ||
/** | ||
@@ -140,7 +132,7 @@ * Verify if a named export declaration node matches the target module and function. | ||
*/ | ||
function isMatchingNamedExport(nodePath, hijackTarget) { | ||
return (nodePath.isExportNamedDeclaration() && | ||
var isMatchingNamedExport = function (nodePath, hijackTarget) { | ||
return nodePath.isExportNamedDeclaration() && | ||
isMatchingFunction(nodePath, hijackTarget) && | ||
isMatchingModule(nodePath, hijackTarget)); | ||
} | ||
isMatchingModule(nodePath, hijackTarget); | ||
}; | ||
/** | ||
@@ -154,7 +146,7 @@ * Verify if an import declaration node matches the target module and function. | ||
*/ | ||
function isMatchingNamedImport(nodePath, hijackTarget) { | ||
return (nodePath.isImportDeclaration() && | ||
var isMatchingNamedImport = function (nodePath, hijackTarget) { | ||
return nodePath.isImportDeclaration() && | ||
isMatchingFunction(nodePath, hijackTarget) && | ||
isMatchingModule(nodePath, hijackTarget)); | ||
} | ||
isMatchingModule(nodePath, hijackTarget); | ||
}; | ||
var Messages = /** @class */ (function () { | ||
@@ -219,5 +211,5 @@ /** | ||
*/ | ||
function getVariableName(nodePath, hijackTarget, suffix) { | ||
var getVariableName = function (nodePath, hijackTarget, suffix) { | ||
return nodePath.scope.generateUidIdentifier("".concat(hijackTarget.function).concat(suffix)).name; | ||
} | ||
}; | ||
/** | ||
@@ -233,3 +225,3 @@ * "Hijack" a named import (e.g., `import { useMessages } from`). | ||
*/ | ||
function hijackNamedImport(nodePath, hijackTarget, messages) { | ||
var hijackNamedImport = function (nodePath, hijackTarget, messages) { | ||
var node = nodePath.node; | ||
@@ -239,7 +231,7 @@ node.specifiers.forEach(function (specifier) { | ||
// The current function name used in the local scope. | ||
var currentName_1 = specifier.local.name; | ||
var currentName = specifier.local.name; | ||
// This is the scope-unique variable name that will replace all matching function bindings. | ||
var hijackedFunction_1 = getVariableName(nodePath, hijackTarget, 'Function'); | ||
// Rename all bindings with the the new name (this excludes the import declaration). | ||
var binding = nodePath.scope.getBinding(currentName_1); | ||
var binding = nodePath.scope.getBinding(currentName); | ||
if (!binding) { | ||
@@ -249,9 +241,12 @@ return; // If the function is unused (no binding), no need to hijack. | ||
binding.referencePaths.forEach(function (referencePath) { | ||
referencePath.scope.rename(currentName_1, hijackedFunction_1, referencePath.parent); | ||
// We used `scope.rename` before but is caused conflicts between array expressions and import statements. | ||
if (referencePath.isIdentifier()) { | ||
referencePath.node.name = hijackedFunction_1; | ||
} | ||
}); | ||
// Insert the new "hijacked" variable, with the correct binding. | ||
nodePath.insertAfter(template_1.default.ast("const ".concat(hijackedFunction_1, " = ").concat(currentName_1, ".bind(").concat(messages.getVariableName(), ");"))); | ||
nodePath.insertAfter(template_1.default.ast("const ".concat(hijackedFunction_1, " = ").concat(currentName, ".bind(").concat(messages.getVariableName(), ");"))); | ||
} | ||
}); | ||
} | ||
}; | ||
/** | ||
@@ -268,3 +263,3 @@ * "Hijack" a named export (e.g., `export { useMessages } from`). | ||
*/ | ||
function hijackNamedExport(nodePath, hijackTarget, messages) { | ||
var hijackNamedExport = function (nodePath, hijackTarget, messages) { | ||
var node = nodePath.node; | ||
@@ -307,3 +302,3 @@ __spreadArray([], node.specifiers, true).reverse().forEach(function (specifier, index, specifiersCopy) { | ||
} | ||
} | ||
}; | ||
/** | ||
@@ -315,24 +310,24 @@ * Dynamically returns a plugin based on the specified parameters. | ||
* */ | ||
function messageModulePlugin(hijackTargets, messagesFileExtension, getMessages) { | ||
return { | ||
visitor: { | ||
Program: function (programNodePath, pluginPass) { | ||
var messages = new Messages(programNodePath, pluginPass, messagesFileExtension, getMessages); | ||
programNodePath.get('body').forEach(function (bodyNodePath) { | ||
hijackTargets.forEach(function (hijackTarget) { | ||
// Try to hijack matching named import statements. | ||
if (isMatchingNamedImport(bodyNodePath, hijackTarget)) { | ||
hijackNamedImport(bodyNodePath, hijackTarget, messages); | ||
} | ||
// Try to hijack matching named export statements. | ||
if (isMatchingNamedExport(bodyNodePath, hijackTarget)) { | ||
hijackNamedExport(bodyNodePath, hijackTarget, messages); | ||
} | ||
}); | ||
var messageModulePlugin = function (hijackTargets, messagesFileExtension, getMessages) { return ({ | ||
visitor: { | ||
Program: function (programNodePath, pluginPass) { | ||
var messages = new Messages(programNodePath, pluginPass, messagesFileExtension, getMessages); | ||
void programNodePath.get('body').forEach(function (bodyNodePath) { | ||
hijackTargets.forEach(function (hijackTarget) { | ||
// Try to hijack matching named import statements. | ||
if (isMatchingNamedImport(bodyNodePath, hijackTarget)) { | ||
// console.log('----------') | ||
// console.dir(bodyNodePath) | ||
hijackNamedImport(bodyNodePath, hijackTarget, messages); | ||
} | ||
// Try to hijack matching named export statements. | ||
if (isMatchingNamedExport(bodyNodePath, hijackTarget)) { | ||
hijackNamedExport(bodyNodePath, hijackTarget, messages); | ||
} | ||
}); | ||
messages.injectIfMatchesFound(); | ||
}, | ||
}); | ||
messages.injectIfMatchesFound(); | ||
}, | ||
}; | ||
} | ||
}, | ||
}); }; | ||
exports.messageModulePlugin = messageModulePlugin; | ||
@@ -339,0 +334,0 @@ /** |
{ | ||
"name": "messages-modules", | ||
"version": "1.1.14", | ||
"version": "1.1.15", | ||
"description": "Messages (localized strings) that are scoped locally.", | ||
@@ -47,9 +47,9 @@ "author": "Avansai (https://avansai.com)", | ||
"@babel/cli": "^7.19.3", | ||
"@babel/core": "^7.19.3", | ||
"@babel/core": "^7.19.6", | ||
"@release-it/conventional-changelog": "^5.1.1", | ||
"@types/babel__core": "^7.1.19", | ||
"@types/jest": "^29.1.2", | ||
"@types/node": "^18.11.0", | ||
"@typescript-eslint/eslint-plugin": "^5.40.0", | ||
"@typescript-eslint/parser": "^5.40.0", | ||
"@types/jest": "^29.2.0", | ||
"@types/node": "^18.11.3", | ||
"@typescript-eslint/eslint-plugin": "^5.40.1", | ||
"@typescript-eslint/parser": "^5.40.1", | ||
"dotenv-cli": "^6.0.0", | ||
@@ -59,8 +59,9 @@ "eslint": "^8.25.0", | ||
"eslint-import-resolver-node": "^0.3.6", | ||
"eslint-import-resolver-typescript": "^3.5.1", | ||
"eslint-import-resolver-typescript": "^3.5.2", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-jest": "^27.1.2", | ||
"eslint-plugin-jest": "^27.1.3", | ||
"eslint-plugin-prefer-arrow-functions": "^3.1.4", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint-plugin-unicorn": "^44.0.2", | ||
"jest": "^29.2.0", | ||
"jest": "^29.2.1", | ||
"prettier": "^2.7.1", | ||
@@ -75,3 +76,3 @@ "prettier-plugin-organize-imports": "^3.1.1", | ||
"dependencies": { | ||
"properties-file": "^2.1.10" | ||
"properties-file": "^2.1.11" | ||
}, | ||
@@ -78,0 +79,0 @@ "engines": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28850
26
460
Updatedproperties-file@^2.1.11