@ordergroove/smi-precompile
Advanced tools
Comparing version 1.6.1 to 1.6.2-alpha-PR-542-24.329
@@ -353,1 +353,38 @@ /* eslint-disable no-template-curly-in-string */ | ||
}); | ||
describe('setting optimization', () => { | ||
test('should replace false literals with expression alternae', () => { | ||
const newLocal = ` {% if ('some_setting_always_false' | setting) %} always true {% endif %} | ||
`; | ||
[0, undefined, '', false, -0, null, NaN, ``, ''].forEach(some_setting_always_false => { | ||
const result = precompile(newLocal, {}, [], { | ||
some_setting_always_false | ||
}); | ||
expect(result).not.toContain('always true'); | ||
}); | ||
}); | ||
test('should replace truthy literals with expression subsequent', () => { | ||
const newLocal = ` {% if ('some_setting_always_true' | setting) %} always true {% else %} always false{% endif %} | ||
`; | ||
[true, 1, {}, [], '0'].forEach(some_setting_always_true => { | ||
const result = precompile(newLocal, {}, [], { | ||
some_setting_always_true | ||
}); | ||
expect(result).not.toContain('always false'); | ||
}); | ||
}); | ||
test('should not touch other expressions', () => { | ||
const newLocal = ` {% if (myval==='some_setting_always_true' | setting) %} always true {% else %} always false{% endif %} | ||
`; | ||
[0, undefined, '', false, -0, null, NaN, ``, '', true, 1, {}, [], '0'].forEach(some_setting_always_true => { | ||
const result = precompile(newLocal, {}, [], { | ||
some_setting_always_true | ||
}); | ||
expect(result).toContain('always false'); | ||
expect(result).toContain('always true'); | ||
}); | ||
}); | ||
}); |
const { default: traverse } = require('@babel/traverse'); | ||
const t = require('@babel/types'); | ||
const isFalsyLiteral = test => | ||
(t.isBooleanLiteral(test) && !test.value) || | ||
(t.isNumericLiteral(test) && !test.value) || | ||
(t.isStringLiteral(test) && !test.value) || | ||
t.isNullLiteral(test) || | ||
(t.isIdentifier(test) && test.name === 'undefined'); | ||
const isTruthyLiteral = test => | ||
t.isObjectExpression(test) || | ||
t.isArrayExpression(test) || | ||
(t.isStringLiteral(test) && test.value) || | ||
(t.isBooleanLiteral(test) && test.value) || | ||
(t.isNumericLiteral(test) && test.value); | ||
module.exports = function replaceLiteralValuesInTemplate(program) { | ||
traverse(program, { | ||
ConditionalExpression(path) { | ||
let test = path.node.test; | ||
if (t.isSequenceExpression(test) && test.expressions.length === 1) { | ||
test = test.expressions[0]; | ||
} | ||
if (isTruthyLiteral(test)) { | ||
path.replaceWith(path.node.consequent); | ||
} else if (isFalsyLiteral(test)) { | ||
path.replaceWith(path.node.alternate); | ||
} | ||
}, | ||
TemplateLiteral(path) { | ||
@@ -7,0 +33,0 @@ if (!path.node.expressions.some(it => t.isStringLiteral(it))) return; |
{ | ||
"name": "@ordergroove/smi-precompile", | ||
"version": "1.6.1", | ||
"version": "1.6.2-alpha-PR-542-24.329+9aab0a3e", | ||
"description": "Prcompilers for smi-core", | ||
@@ -15,10 +15,17 @@ "author": "Brian Lewis <brian.lewis@ordergroove.com>", | ||
"scripts": { | ||
"start": "../../node_modules/.bin/webpack-dev-server --mode development", | ||
"build": "rm -rf dist && ../../node_modules/.bin/webpack --mode development", | ||
"build:prod": "rm -rf dist && ../../node_modules/.bin/webpack --mode production --display errors-only", | ||
"start": "npm run build -- --serve", | ||
"build": "rm -rf dist && node build", | ||
"build:prod": "npm run build -- --prod", | ||
"test": "../../node_modules/.bin/jest --ci --silent", | ||
"test:watch": "../../node_modules/.bin/jest --watch", | ||
"prepublishOnly": "npm run -s lint && npm run -s test && npm run -s build:prod", | ||
"bundlesize": "../../node_modules/.bin/bundlesize", | ||
"prepublishOnly": "npm run -s build:prod", | ||
"lint": "../../node_modules/.bin/eslint --ignore-path ../../.gitignore ./src" | ||
}, | ||
"bundlesize": [ | ||
{ | ||
"path": "./dist/smi-precompile.js", | ||
"maxSize": "380 kB" | ||
} | ||
], | ||
"bugs": { | ||
@@ -34,3 +41,3 @@ "url": "https://github.com/ordergroove/plush-toys/issues" | ||
"@babel/types": "^7.13.0", | ||
"@ordergroove/i18n-data": "^0.3.0", | ||
"@ordergroove/i18n-data": "^0.3.1-alpha-PR-542-24.370+9aab0a3e", | ||
"glob": "^7.1.6", | ||
@@ -41,3 +48,6 @@ "less": "^4.1.1", | ||
}, | ||
"gitHead": "36347117970d3062dc66f31e4af5a8e1084a16fd" | ||
"devDependencies": { | ||
"path-browserify": "^1.0.1" | ||
}, | ||
"gitHead": "9aab0a3e8444d3a178d5ade8e92b7843d84991ab" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5549
4833691
1
2
1
+ Added@ordergroove/i18n-data@0.3.1-alpha-PR-542-24.370(transitive)
- Removed@ordergroove/i18n-data@0.3.0(transitive)
Updated@ordergroove/i18n-data@^0.3.1-alpha-PR-542-24.370+9aab0a3e