Skip to content

Commit

Permalink
fix(rules): Check static require in expressions
Browse files Browse the repository at this point in the history
Static requires inside expressions were not checked by
use of VariableDeclaration. Update implementation to
use CallExpression

fixes: #6
  • Loading branch information
biblicalph authored and esatterwhite committed Sep 17, 2022
1 parent 81b6f41 commit 72964d0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
21 changes: 19 additions & 2 deletions lib/common/require-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const isBuiltinModule = require('is-builtin-module')
const {get} = require('dot-prop')

module.exports = {
isRequire
getRequiredModuleName
, isRequire
, isStaticRequire
, isBuiltin
, isScoped
Expand Down Expand Up @@ -39,12 +40,28 @@ function isBuiltin(node) {
return !!(value && isBuiltinModule(value))
}

/**
* Determine if a given variable declaration or call expression
* node is a local require statement
* @param {'VariableDeclaration' | 'CallExpression'} node
* @returns
*/
function isLocal(node) {
const value = get(node, 'declarations.0.init.arguments.0.value')
const value = getRequiredModuleName(node)
return !!(value && value.startsWith('.'))
}

function isTopLevel(node) {
const value = get(node, 'parent.type')
return !!(value && value === 'Program')
}

/**
* Returns the name of the module loaded in a require statement
* @param {'CallExpression' | 'VariableDeclaration'} node
* @returns
*/
function getRequiredModuleName(node) {
return get(node, 'declarations.0.init.arguments.0.value')
|| get(node, 'arguments.0.value')
}
12 changes: 6 additions & 6 deletions lib/rules/require-file-extension.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const {get} = require('dot-prop')
const {
isRequire
, isLocal
isLocal,
isStaticRequire,
getRequiredModuleName
} = require('../common/require-type.js')

const REQUIRE_EXTENSIONS = new Set(['js', 'json', 'node'])
Expand All @@ -20,14 +20,14 @@ module.exports = {
const nodes = []

return {
VariableDeclaration(node) {
if (!isRequire(node)) return
CallExpression(node) {
if (!isStaticRequire(node)) return
if (!isLocal(node)) return
nodes.push(node)
}
, 'Program:exit'() {
for (const node of nodes) {
const path = get(node, 'declarations.0.init.arguments.0.value')
const path = getRequiredModuleName(node)
const parts = path.split('.')
const hasExtension = parts.length > 1
&& REQUIRE_EXTENSIONS.has(parts[parts.length - 1])
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"homepage": "https://github.com/logdna/eslint-plugin-logdna#readme",
"scripts": {
"lint": "eslint ./",
"lint:fix": "eslint --fix ./",
"tap": "tap",
"pretest": "npm run lint",
"test": "tap",
Expand Down
10 changes: 9 additions & 1 deletion test/lib/rules/require-file-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,28 @@ test(RULE_NAME, async (t) => {
if (bar) {
const baz = require('./baz.json');
}
registerTransform(require('./transforms/parse.js'))
module.exports = {
bar: require('./bar2.js')
}
`
}
]
, invalid: [
{
code: `
const foo = require('../test/foo')
const foo = require('../test/foo');
if (bar) {
const baz = require('./baz');
}
registerTransform(require('../transforms/parse'))
module.exports = require('../foo/bar');
`
, errors: [
{message: 'Missing file extension for local module.'}
, {message: 'Missing file extension for local module.'}
, {message: 'Missing file extension for local module.'}
, {message: 'Missing file extension for local module.'}
]
}
]
Expand Down

0 comments on commit 72964d0

Please sign in to comment.