Okay, here's a CodeQL query to find calls to the map function from the Lodash library. This query attempts to cover various ways Lodash might be imported and used.
/**
* @name Find Lodash map calls
* @description Finds all calls to the 'map' function from the Lodash library.
* @kind problem
* @problem.severity recommendation
* @id js/lodash-map-calls
* @tags lodash utility map
*/
import javascript
/**
* Holds if `m` is the Lodash module.
* This is a simplified check; in a real-world scenario, you might
* want more robust module resolution (e.g., checking node_modules paths).
*/
predicate isLodashModule(Module m) {
// CommonJS: require('lodash')
exists(Require r | r.getResolvedModule() = m and r.getArgument(0).(StringLiteral).getValue() = "lodash") or
// ES6: import ... from 'lodash'
exists(Import i | i.getResolvedModule() = m and i.getImportedPath() = "lodash")
}
/**
* Holds if `e` is an expression that refers to the Lodash library object itself.
* For example, `_` in `import _ from 'lodash'` or `lodash` in `const lodash = require('lodash')`.
*/
predicate isLodashObject(Expr e) {
// Case 1: Default import `import _ from 'lodash';`
exists(ImportDefaultSpecifier ids, Module lodashMod |
isLodashModule(lodashMod) and
ids.getImport().getResolvedModule() = lodashMod and
e = ids.getLocal()
) or
// Case 2: Namespace import `import * as _ from 'lodash';`
exists(ImportNamespaceSpecifier ins, Module lodashMod |
isLodashModule(lodashMod) and
ins.getImport().getResolvedModule() = lodashMod and
e = ins.getLocal()
) or
// Case 3: CommonJS require `const _ = require('lodash');`
exists(VariableDeclarator vd, Require r, Module lodashMod |
isLodashModule(lodashMod) and
r.getResolvedModule() = lodashMod and
vd.getInit() = r and
// Ensure the variable declarator's pattern is the expression `e`
// This handles `const _ = ...`, `const lodash = ...`
e = vd.getBindingPattern()
)
}
/**
* Holds if `e` is an expression that refers directly to the Lodash `map` function.
* For example, `map` in `import { map } from 'lodash'` or `const { map } = require('lodash')`.
*/
predicate isDirectLodashMapFunction(Expr e) {
// Case 1: Named import `import { map } from 'lodash';`
// Also handles `import { map as myMap } from 'lodash';` where `e` would be `myMap`
exists(ImportSpecifier is, Module lodashMod |
isLodashModule(lodashMod) and
is.getImport().getResolvedModule() = lodashMod and
is.getImportedName() = "map" and
e = is.getLocal()
) or
// Case 2: Destructured require `const { map } = require('lodash');`
// Also handles `const { map: myMap } = require('lodash');`
exists(VariableDeclarator vd, ObjectPattern op, Property p, Require r, Module lodashMod |
isLodashModule(lodashMod) and
r.getResolvedModule() = lodashMod and
vd.getInit() = r and
op = vd.getPattern() and // The pattern is an ObjectPattern like `{ map }`
p = op.getAProperty() and // Get a property from the pattern
p.getKeyName() = "map" and // The key is 'map'
e = p.getValuePattern() // The value pattern is the identifier (e.g., `map` or `myMap`)
)
}
from CallExpr call
where
// Scenario 1: _.map(...) or lodash.map(...)
exists(MemberExpr me |
call.getCallee() = me and
me.getPropertyName() = "map" and
isLodashObject(me.getObject()) // The object part (e.g., `_`) is the Lodash object
)
or
// Scenario 2: map(...) where map is directly imported/destructured
isDirectLodashMapFunction(call.getCallee())
select call, "Call to Lodash 'map' function."
Explanation:
import javascript: Imports the standard CodeQL library for JavaScript analysis.
isLodashModule(Module m) Predicate:
- This predicate tries to identify if a
Module object represents the "lodash" module.
- It checks both CommonJS
require('lodash') and ES6 import ... from 'lodash'.
getResolvedModule() attempts to find the actual module file.
getArgument(0).(StringLiteral).getValue() gets the string value from require calls.
getImportedPath() gets the string from import statements.
isLodashObject(Expr e) Predicate:
- This predicate checks if an expression
e refers to the main Lodash object (often named _ or lodash).
- Case 1 (Default Import):
import _ from 'lodash'; Here, _ is an ImportDefaultSpecifier, and e would be its local name.
- Case 2 (Namespace Import):
import * as _ from 'lodash'; Here, _ is an ImportNamespaceSpecifier.
- Case 3 (CommonJS Require):
const _ = require('lodash'); Here, the initializer of a VariableDeclarator is a Require call to Lodash, and e is the bindingPattern (the variable name _).
isDirectLodashMapFunction(Expr e) Predicate:
- This predicate checks if an expression
e refers directly to the map function imported/destructured from Lodash.
- Case 1 (Named Import):
import { map } from 'lodash'; or import { map as myMap } from 'lodash';. is.getImportedName() is "map", and e is is.getLocal() (which would be map or myMap).
- Case 2 (Destructured Require):
const { map } = require('lodash'); or const { map: myMap } = require('lodash');. It looks for a VariableDeclarator whose initializer is a call and whose pattern is an containing a property with key "map". is the of that property.
How to Use:
- Save this code as a
.ql file (e.g., FindLodashMap.ql).
- Add it to your CodeQL query pack or run it directly against a CodeQL database of your JavaScript project.
- If using VS Code with the CodeQL extension, you can right-click the database and select "CodeQL: Run Query".
- From the command line:
codeql query run FindLodashMap.ql -d /path/to/your/javascript_database --output=results.bqrs
This query is reasonably comprehensive. For extremely complex aliasing or dynamic requires, it might need further refinement, but it covers the most common patterns.