|
@@ -1,5 +1,5 @@
|
|
|
-const path = require('node:path');
|
|
|
|
|
-const fs = require('node:fs');
|
|
|
|
|
|
|
+const path = require('node:path')
|
|
|
|
|
+const fs = require('node:fs')
|
|
|
|
|
|
|
|
module.exports = {
|
|
module.exports = {
|
|
|
meta: {
|
|
meta: {
|
|
@@ -14,26 +14,39 @@ module.exports = {
|
|
|
hasSuggestions: true,
|
|
hasSuggestions: true,
|
|
|
schema: [],
|
|
schema: [],
|
|
|
messages: {
|
|
messages: {
|
|
|
- unresolvablePath: 'The path "{{pathValue}}" in vi.doMock() cannot be resolved relative to the current file.',
|
|
|
|
|
- notAStringLiteral: 'The first argument of vi.doMock() must be (or resolve to) a string literal representing a path.',
|
|
|
|
|
|
|
+ unresolvablePath:
|
|
|
|
|
+ 'The path "{{pathValue}}" in vi.doMock() cannot be resolved relative to the current file.',
|
|
|
|
|
+ notAStringLiteral:
|
|
|
|
|
+ 'The first argument of vi.doMock() must be (or resolve to) a string literal representing a path.',
|
|
|
noArguments: 'vi.doMock() called with no arguments.',
|
|
noArguments: 'vi.doMock() called with no arguments.',
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
|
create(context) {
|
|
create(context) {
|
|
|
- const currentFilePath = context.getFilename();
|
|
|
|
|
|
|
+ const currentFilePath = context.getFilename()
|
|
|
// ESLint can sometimes pass <text> or <input> for snippets not in a file
|
|
// ESLint can sometimes pass <text> or <input> for snippets not in a file
|
|
|
if (currentFilePath === '<text>' || currentFilePath === '<input>') {
|
|
if (currentFilePath === '<text>' || currentFilePath === '<input>') {
|
|
|
return {}
|
|
return {}
|
|
|
}
|
|
}
|
|
|
- const currentDirectory = path.dirname(currentFilePath);
|
|
|
|
|
|
|
+ const currentDirectory = path.dirname(currentFilePath)
|
|
|
|
|
|
|
|
function canResolve(modulePath) {
|
|
function canResolve(modulePath) {
|
|
|
try {
|
|
try {
|
|
|
- require.resolve(path.resolve(currentDirectory, modulePath));
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ require.resolve(path.resolve(currentDirectory, modulePath))
|
|
|
|
|
+ return true
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
- const absolutePath = path.resolve(currentDirectory, modulePath);
|
|
|
|
|
- const extensions = ['', '.js', '.mjs', '.ts', '.jsx', '.tsx', '.json', '.node', '/index.js', '/index.ts']; // Add common extensions
|
|
|
|
|
|
|
+ const absolutePath = path.resolve(currentDirectory, modulePath)
|
|
|
|
|
+ const extensions = [
|
|
|
|
|
+ '',
|
|
|
|
|
+ '.js',
|
|
|
|
|
+ '.mjs',
|
|
|
|
|
+ '.ts',
|
|
|
|
|
+ '.jsx',
|
|
|
|
|
+ '.tsx',
|
|
|
|
|
+ '.json',
|
|
|
|
|
+ '.node',
|
|
|
|
|
+ '/index.js',
|
|
|
|
|
+ '/index.ts',
|
|
|
|
|
+ ] // Add common extensions
|
|
|
for (const ext of extensions) {
|
|
for (const ext of extensions) {
|
|
|
if (fs.existsSync(absolutePath + ext)) {
|
|
if (fs.existsSync(absolutePath + ext)) {
|
|
|
return true
|
|
return true
|
|
@@ -63,9 +76,14 @@ module.exports = {
|
|
|
const firstArg = node.arguments[0]
|
|
const firstArg = node.arguments[0]
|
|
|
let pathValue = firstArg.value
|
|
let pathValue = firstArg.value
|
|
|
|
|
|
|
|
- if (firstArg.type !== 'Literal' || typeof firstArg.value !== 'string') {
|
|
|
|
|
|
|
+ if (
|
|
|
|
|
+ firstArg.type !== 'Literal' ||
|
|
|
|
|
+ typeof firstArg.value !== 'string'
|
|
|
|
|
+ ) {
|
|
|
if (firstArg.type === 'Identifier') {
|
|
if (firstArg.type === 'Identifier') {
|
|
|
- const variable = context.getScope().variables.find(v => v.name === firstArg.name);
|
|
|
|
|
|
|
+ const variable = context
|
|
|
|
|
+ .getScope()
|
|
|
|
|
+ .variables.find(v => v.name === firstArg.name)
|
|
|
if (
|
|
if (
|
|
|
variable &&
|
|
variable &&
|
|
|
variable.defs.length > 0 &&
|
|
variable.defs.length > 0 &&
|
|
@@ -80,15 +98,13 @@ module.exports = {
|
|
|
// If the first argument was a variable that didn't resolve then we can't auto-fix it
|
|
// If the first argument was a variable that didn't resolve then we can't auto-fix it
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- context.report({
|
|
|
|
|
- node: firstArg,
|
|
|
|
|
- messageId: 'notAStringLiteral',
|
|
|
|
|
- })
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
|
|
+ context.report({
|
|
|
|
|
+ node: firstArg,
|
|
|
|
|
+ messageId: 'notAStringLiteral',
|
|
|
|
|
+ })
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
if (!pathValue.startsWith('.')) {
|
|
if (!pathValue.startsWith('.')) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -97,12 +113,13 @@ module.exports = {
|
|
|
const mjsPath = pathValue.replace('.js', '.mjs')
|
|
const mjsPath = pathValue.replace('.js', '.mjs')
|
|
|
const additionalReportOptions = {}
|
|
const additionalReportOptions = {}
|
|
|
if (canResolve(mjsPath)) {
|
|
if (canResolve(mjsPath)) {
|
|
|
- additionalReportOptions.fix = (fixer) => fixer.replaceText(firstArg, `'${mjsPath}'`)
|
|
|
|
|
|
|
+ additionalReportOptions.fix = fixer =>
|
|
|
|
|
+ fixer.replaceText(firstArg, `'${mjsPath}'`)
|
|
|
additionalReportOptions.suggest = [
|
|
additionalReportOptions.suggest = [
|
|
|
{
|
|
{
|
|
|
desc: `Replace with "${pathValue.replace('.js', '.mjs')}"`,
|
|
desc: `Replace with "${pathValue.replace('.js', '.mjs')}"`,
|
|
|
- fix: (fixer) => fixer.replaceText(firstArg, `'${mjsPath}'`),
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ fix: fixer => fixer.replaceText(firstArg, `'${mjsPath}'`),
|
|
|
|
|
+ },
|
|
|
]
|
|
]
|
|
|
}
|
|
}
|
|
|
context.report({
|
|
context.report({
|
|
@@ -111,11 +128,11 @@ module.exports = {
|
|
|
data: {
|
|
data: {
|
|
|
pathValue,
|
|
pathValue,
|
|
|
},
|
|
},
|
|
|
- ...additionalReportOptions
|
|
|
|
|
|
|
+ ...additionalReportOptions,
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
-};
|
|
|
|
|
|
|
+}
|