Socket
Socket
Sign inDemoInstall

babel-plugin-transform-replace-expressions

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-replace-expressions - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

39

index.js

@@ -5,16 +5,45 @@ const { parseExpression } = require("@babel/parser");

const replace = options.replace || {};
const allowConflictingReplacements = !!options.allowConflictingReplacements;
function asArray(obj) {
if (Array.isArray(obj)) {
return obj;
}
return Object.keys(obj).map(key => [key, obj[key]]);
}
const types = new Map();
const values = new Set();
Object.keys(replace).forEach(key => {
asArray(replace).forEach(([key, value]) => {
const kNode = parseExpression(key);
const vNode = parseExpression(replace[key]);
const vNode = parseExpression(value);
const candidates = types.get(kNode.type) || [];
candidates.push({ key: kNode, value: vNode });
candidates.push({ key: kNode, value: vNode, originalKey: key });
types.set(kNode.type, candidates);
values.add(vNode);
for (let i = 0; i < candidates.length - 1; i++) {
if (!t.isNodesEquivalent(candidates[i].key, kNode)) {
continue;
}
if (allowConflictingReplacements) {
candidates[i] = candidates.pop();
break;
}
throw new Error(
`Expressions ${JSON.stringify(
candidates[i].originalKey
)} and ${JSON.stringify(key)} conflict`
);
}
});
const values = new Set();
types.forEach(candidates => {
candidates.forEach(candidate => {
values.add(candidate.value);
});
});
return {

@@ -21,0 +50,0 @@ name: "transform-replace-expressions",

2

package.json
{
"name": "babel-plugin-transform-replace-expressions",
"version": "0.1.1",
"version": "0.2.0",
"description": "Replace JavaScript expressions with other expressions",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,2 +0,2 @@

# babel-plugin-transform-replace-expressions [![CircleCI](https://circleci.com/gh/jviide/babel-plugin-transform-replace-expressions.svg?style=shield)](https://circleci.com/gh/jviide/babel-plugin-transform-replace-expressions) ![npm](https://img.shields.io/npm/v/babel-plugin-transform-replace-expressions.svg)
# babel-plugin-transform-replace-expressions [![CircleCI](https://circleci.com/gh/jviide/babel-plugin-transform-replace-expressions.svg?style=shield)](https://circleci.com/gh/jviide/babel-plugin-transform-replace-expressions) [![npm](https://img.shields.io/npm/v/babel-plugin-transform-replace-expressions.svg)](https://www.npmjs.com/package/babel-plugin-transform-replace-expressions)

@@ -47,2 +47,25 @@ Replace JavaScript expressions with other expressions.

## Conflict resolution
A conflict happens when two replacements have the same Babel [abstract syntax tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) representation. For example expressions `typeof A` and `typeof (A)` are formatted differently but have the same AST representation as far as the plugin is concerned. In those situations the default is to raise an error, and can be overwritten by setting the option `allowConflictingReplacements` to `true`.
You can also always give the replacements as an array of key-value pairs. When `allowConflictingReplacements` is set to `true` the _last_ conflicting replacement gets selected.
```js
{
"plugins": [
[
"babel-plugin-transform-replace-expressions",
{
"replace": [
["typeof A", "B"],
["typeof (A)", "C"]
],
"allowConflictingReplacements": true
}
]
]
}
```
## Notes

@@ -49,0 +72,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc