@-0/utils
Advanced tools
Comparing version 0.2.108 to 0.2.109
import { CMD_WORK } from "@-0/keys"; | ||
import { stringify_fn } from "./stringify_fn"; | ||
const STRIP_COMMENTS = /(\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s*=[^,\)]*(('(?:\\'|[^'\r\n])*')|("(?:\\"|[^"\r\n])*"))|(\s*=[^,\)]*))/gm; | ||
const ARGUMENT_NAMES = /([^\s,]+)/g; | ||
const DESTRUCT = /{|}/g; | ||
let err_str = (name = "", comp = "", CMD = null) => CMD | ||
@@ -10,4 +7,4 @@ ? ` | ||
Computed property names prevent static analysis of args passed to | ||
your ${CMD_WORK} handler in this Command: | ||
Computed property names prevent static analysis of args | ||
passed to your ${CMD_WORK} handler in this Command: | ||
@@ -20,14 +17,25 @@ ${stringify_fn(CMD, 2)} | ||
: ` | ||
@-0/utls > \`get_param_names\`: ${name}({ ${comp} ... | ||
Computed property names prevent static analysis of function | ||
parameters. Consider using static destructuring instead | ||
🔥 Warning: Complex Properties 🔥 | ||
${name}({ ${comp} ... | ||
Nested/computed properties prevent reliable static runtime | ||
analysis of function parameters against their incoming | ||
arguments. Consider using shallow & static destructuring. | ||
`; | ||
const STRIP_COMMENTS = /(\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s*=[^,\)]*(('(?:\\'|[^'\r\n])*')|("(?:\\"|[^"\r\n])*"))|(\s*=[^,\)]*))/gm; | ||
const ARGUMENT_NAMES = /([^\s,]+)/g; | ||
const DESTRUCT = /{|}/g; | ||
const VALID_VAR = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/g; | ||
export function get_param_names(func, CMD = null) { | ||
const fnStr = func.toString().replace(STRIP_COMMENTS, ""); | ||
let results = fnStr.slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")")).match(ARGUMENT_NAMES); | ||
if (results === null) | ||
results = []; | ||
if (!results.length) | ||
return []; | ||
let is_destructured = false; | ||
const computed = results.map(x => x.replace(DESTRUCT, "")).filter(x => x !== ""); | ||
let done = []; | ||
let warned = false; | ||
let fnStr = func.toString().replace(STRIP_COMMENTS, ""); | ||
let result = fnStr.slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")")).match(ARGUMENT_NAMES); | ||
if (result === null) | ||
result = []; | ||
let computed = result.map(x => x.replace(DESTRUCT, "")).filter(x => x !== ""); | ||
let done = []; | ||
computed.forEach((c, i, d) => { | ||
@@ -38,5 +46,4 @@ if (c.match(/\]/g)) { | ||
} | ||
if (c.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*$/g)) { | ||
if (c.match(VALID_VAR)) | ||
return done.push(c); | ||
} | ||
if (warned) | ||
@@ -43,0 +50,0 @@ return; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.2.108", | ||
"version": "0.2.109", | ||
"description": "utilities for the `-0` org/framework built on @thi.ng/umbrella ecosystem", | ||
@@ -34,3 +34,3 @@ "main": "./lib/index.js", | ||
"peerDependencies": { | ||
"@-0/keys": "^0.2.111", | ||
"@-0/keys": "^0.2.112", | ||
"@thi.ng/checks": "^2.8.0", | ||
@@ -40,8 +40,8 @@ "@thi.ng/rstream": "^5.1.0" | ||
"devDependencies": { | ||
"@-0/keys": "^0.2.112", | ||
"@babel/plugin-transform-modules-commonjs": "^7.12.13", | ||
"@-0/keys": "^0.2.111", | ||
"@thi.ng/checks": "^2.8.0", | ||
"@thi.ng/rstream": "^5.1.0", | ||
"@types/jest": "^26.0.20", | ||
"@types/node": "^14.14.27", | ||
"@types/node": "^14.14.28", | ||
"@typescript-eslint/eslint-plugin": "^4.15.0", | ||
@@ -54,2 +54,3 @@ "@typescript-eslint/parser": "^4.15.0", | ||
"ts-jest": "^26.5.1", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.1.5" | ||
@@ -56,0 +57,0 @@ }, |
@@ -10,2 +10,3 @@ import { get_param_names } from "../lib" | ||
const _7 = ({ [`comp `]: c, d }) => {} | ||
// prettier-ignore | ||
@@ -17,2 +18,3 @@ const _8 = ({ | ||
}) => {} | ||
// prettier-ignore | ||
@@ -24,2 +26,3 @@ const _9 = ({ | ||
}) => {} | ||
// prettier-ignore | ||
@@ -32,12 +35,15 @@ const _10 = ({ | ||
}) => {} | ||
const key = "computed" | ||
// prettier-ignore | ||
const _11 = ({ | ||
// with comment above | ||
[`comp `]: c, // inline | ||
d, | ||
[`another` + "comp"]: w = "destructured", | ||
e | ||
[key]: d_c, // inline | ||
e: { | ||
e2 | ||
}, | ||
[`another` + 1]: s_c = `destructured`, | ||
t | ||
}) => {} | ||
// | ||
get_param_names(_1) //? | ||
@@ -44,0 +50,0 @@ get_param_names(_2) //? |
@@ -68,3 +68,3 @@ import { get_param_names } from "../src" | ||
}) | ||
test("11: destructured with Computed properties and default values + linebreaks", () => { | ||
test("11: Computed properties &+ default values: total warnings = 1", () => { | ||
const warn_spy = warned() | ||
@@ -83,2 +83,23 @@ | ||
}) | ||
test("12: Nested properties gets a warning", () => { | ||
const warn_spy = warned() | ||
// prettier-ignore | ||
const _11 = ({ | ||
// with comment above | ||
c: { | ||
d // inline | ||
}, | ||
e | ||
}) => {} | ||
expect(get_param_names(_11)).toMatchObject([ "d", "e" ]) | ||
expect(warn_spy.mock.calls.length).toBe(1) | ||
}) | ||
test("13: Single (default) parameter returns empty list", () => { | ||
const warn_spy = warned() | ||
// prettier-ignore | ||
expect(get_param_names((x) => x)).toMatchObject([]) | ||
expect(get_param_names(x => x)).toMatchObject([]) | ||
expect(warn_spy.mock.calls.length).toBe(0) | ||
}) | ||
}) |
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
27861
612
15