-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See #8 Also: * Set up tests with Vitest * Add prettier
- Loading branch information
Showing
10 changed files
with
128 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Empty extension takes advantage of Node's default require behavior to check for | ||
// eslint-local-rules.js as well as an eslint-local-rules folder with an index.js | ||
var DEFAULT_EXTENSIONS = ['', '.cjs']; | ||
|
||
module.exports = { | ||
DEFAULT_EXTENSIONS, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require('./does-not-exist'); | ||
|
||
module.exports = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {}; |
1 change: 1 addition & 0 deletions
1
fixtures/projectWithResolutionIndex/eslint-local-rules/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,23 @@ | ||
/* eslint-env node */ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
var { requireUp } = require('./requireUp'); | ||
var { DEFAULT_EXTENSIONS } = require('./constants'); | ||
|
||
// Empty extension takes advantage of Node's default require behavior to check for | ||
// eslint-local-rules.js as well as an eslint-local-rules folder with an index.js | ||
var exts = ['', '.cjs']; | ||
var rules = requireUp('eslint-local-rules', __dirname); | ||
var rules = requireUp('eslint-local-rules', DEFAULT_EXTENSIONS, __dirname); | ||
|
||
if (!rules) { | ||
throw new Error( | ||
'eslint-plugin-local-rules: ' + | ||
'Cannot find "eslint-local-rules{' + ['.js'].concat(exts.filter(Boolean)) + "} " + | ||
'or eslint-local-rules/index.js (checked all ancestors of "' + __dirname + '").' | ||
'Cannot find "eslint-local-rules{' + | ||
['.js'].concat(exts.filter(Boolean)) + | ||
'} ' + | ||
'or eslint-local-rules/index.js (checked all ancestors of "' + | ||
__dirname + | ||
'").' | ||
); | ||
} | ||
|
||
module.exports = { | ||
rules: rules, | ||
}; | ||
|
||
// Attempt to require a file, recursively checking parent directories until found | ||
// Similar to native `require` behavior, but doesn't check in `node_modules` folders | ||
// Based on https://github.com/js-cli/node-findup-sync | ||
function requireUp(filename, cwd) { | ||
var filepath = path.resolve(cwd, filename); | ||
|
||
for (var i = 0; i < exts.length; i++) { | ||
try { | ||
return require(filepath + exts[i]); | ||
} catch(error) { | ||
// Ignore OS errors (will recurse to parent directory) | ||
if (error.code !== 'MODULE_NOT_FOUND') { | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
var dir = path.dirname(cwd); | ||
if (dir === cwd) { | ||
return undefined; | ||
} | ||
|
||
return requireUp(filename, dir); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var path = require('path'); | ||
|
||
/** | ||
* Attempt to require a file, recursively checking parent directories until found. | ||
* Similar to native `require` behavior, but doesn't check in `node_modules` folders. | ||
* Based on https://github.com/js-cli/node-findup-sync. | ||
* | ||
* @param filename {string} The name of the target file | ||
* @param exts {string[]} The file extensions to look for (e.g. '.cjs') | ||
* @param cwd {string} The directory to search in | ||
*/ | ||
function requireUp(filename, exts, cwd) { | ||
for (var i = 0; i < exts.length; i++) { | ||
var filepath = path.resolve(cwd, filename) + exts[i]; | ||
try { | ||
return require(filepath); | ||
} catch (error) { | ||
var filepathNotFound = | ||
error.code === 'MODULE_NOT_FOUND' && | ||
error.message.includes(`Cannot find module '${filepath}'`); | ||
|
||
// Rethrow unless error is just saying `filepath` not found (in that case, | ||
// let next loop check parent directory instead). | ||
if (!filepathNotFound) { | ||
throw error; | ||
} | ||
} | ||
} | ||
var dir = path.dirname(cwd); | ||
if (dir === cwd) { | ||
return undefined; | ||
} | ||
return requireUp(filename, exts, dir); | ||
} | ||
|
||
module.exports = { | ||
requireUp, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import path from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
import { requireUp } from './requireUp'; | ||
import { DEFAULT_EXTENSIONS } from './constants'; | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
// Path to the 'fixtures' directory | ||
const fixturesDir = path.resolve(__dirname, './fixtures'); | ||
|
||
describe('requireUp', () => { | ||
it('should find the file at eslint-local-rules.js', () => { | ||
const file = requireUp( | ||
'eslint-local-rules', | ||
DEFAULT_EXTENSIONS, | ||
path.join(fixturesDir, './projectWithResolution/a') | ||
); | ||
expect(file).toBeDefined(); | ||
}); | ||
|
||
it('should find the file at eslint-local-rules.cjs', () => { | ||
const file = requireUp( | ||
'eslint-local-rules', | ||
DEFAULT_EXTENSIONS, | ||
path.join(fixturesDir, './projectWithResolutionCjs/a') | ||
); | ||
expect(file).toBeDefined(); | ||
}); | ||
|
||
it('should find the file at eslint-local-rules/index.js', () => { | ||
const file = requireUp( | ||
'eslint-local-rules', | ||
DEFAULT_EXTENSIONS, | ||
path.join(fixturesDir, './projectWithResolutionIndex/a') | ||
); | ||
expect(file).toBeDefined(); | ||
}); | ||
|
||
it('should fail to find a file that does not exist', () => { | ||
const file = requireUp( | ||
'some-file-that-will-not-resolve', | ||
DEFAULT_EXTENSIONS, | ||
path.join(fixturesDir, './projectWithNoResolution/a') | ||
); | ||
expect(file).not.toBeDefined(); | ||
}); | ||
|
||
it('should throw MODULE_NOT_FOUND errors for modules other than the target', () => { | ||
expect(() => { | ||
requireUp( | ||
'eslint-local-rules', | ||
DEFAULT_EXTENSIONS, | ||
path.join(fixturesDir, './projectWithBadImport/a') | ||
); | ||
}).toThrowError(`Cannot find module './does-not-exist'`); | ||
}); | ||
}); |