-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ruleset): allow require calls in Node.JS (#1011)
* feat(ruleset): allow require calls in Node.JS * test: cover require * docs: add a note on require * docs: remove comment Co-Authored-By: Phil Sturgeon <[email protected]> * doc: note on require Co-Authored-By: Phil Sturgeon <[email protected]> * test: minor tweak Co-authored-by: Phil Sturgeon <[email protected]>
- Loading branch information
Showing
15 changed files
with
210 additions
and
21 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
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,11 @@ | ||
const { hasIn } = require('lodash'); | ||
|
||
module.exports = (targetVal, opts) => { | ||
if (!(hasIn(targetVal, opts.path))) { | ||
return [ | ||
{ | ||
message: `Object does not have ${opts.prop} property`, | ||
}, | ||
]; | ||
} | ||
}; |
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 @@ | ||
module.exports = () => 'hello!'; |
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,41 @@ | ||
import * as path from '@stoplight/path'; | ||
import { evaluateExport } from '../evaluators'; | ||
|
||
describe('Code evaluators', () => { | ||
describe('Export evaluator', () => { | ||
it('supports require', () => { | ||
expect(evaluateExport(`module.exports = require('./foo')`, path.join(__dirname, '__fixtures__/a.js'))()).toEqual( | ||
'hello!', | ||
); | ||
|
||
expect( | ||
evaluateExport( | ||
`module.exports = () => require('path').join('/', 'hello!')`, | ||
path.join(__dirname, '__fixtures__/a.js'), | ||
)(), | ||
).toEqual(require('path').join('/', 'hello!')); | ||
|
||
expect( | ||
evaluateExport( | ||
`module.exports = () => require('@stoplight/path').join('/', 'hello!')`, | ||
path.join(__dirname, '__fixtures__/a.js'), | ||
)(), | ||
).toEqual(path.join('/', 'hello!')); | ||
}); | ||
|
||
it('supports require.resolve', () => { | ||
expect( | ||
path.normalize( | ||
evaluateExport( | ||
`module.exports = () => require.resolve('./foo', { paths: ['${path.join(__dirname, '__fixtures__')}'] } )`, | ||
null, | ||
)(), | ||
), | ||
).toEqual(path.join(__dirname, '__fixtures__/foo.js')); | ||
}); | ||
|
||
it.each(['cache', 'extensions'])('exposes %s', member => { | ||
expect(evaluateExport(`module.exports = () => require['${member}']`, null)()).toStrictEqual(require[member]); | ||
}); | ||
}); | ||
}); |
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,13 @@ | ||
import { evaluateExport } from '../evaluators'; | ||
|
||
describe('Code evaluators', () => { | ||
describe('Export evaluator', () => { | ||
it('does not support require', () => { | ||
expect(evaluateExport.bind(null, `require('./d')`, null)).toThrowError(ReferenceError); | ||
expect(evaluateExport.bind(null, `require.resolve('./d')`, null)).toThrowError(ReferenceError); | ||
expect(evaluateExport.bind(null, `require.main`, null)).toThrowError(ReferenceError); | ||
expect(evaluateExport.bind(null, `require.cache`, null)).toThrowError(ReferenceError); | ||
expect(evaluateExport.bind(null, `require.extensions`, null)).toThrowError(ReferenceError); | ||
}); | ||
}); | ||
}); |
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
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
Oops, something went wrong.