forked from jsx-eslint/eslint-plugin-react
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New] Change allowed propWrapperFunctions setting values
This change is adapting the `propWrapperFunctions` setting to allow for using objects along side the already present strings. This will help facilitate adding extra attributes to these objects like for exact prop functions. Precursor to jsx-eslint#1547.
- Loading branch information
Showing
9 changed files
with
105 additions
and
17 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
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
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,24 @@ | ||
/** | ||
* @fileoverview Utility functions for propWrapperFunctions setting | ||
*/ | ||
'use strict'; | ||
|
||
function getPropWrapperFunctions(context) { | ||
return new Set(context.settings.propWrapperFunctions || []); | ||
} | ||
|
||
function isPropWrapperFunction(context, name) { | ||
const propWrapperFunctions = getPropWrapperFunctions(context); | ||
const splitName = name.split('.'); | ||
return Array.from(propWrapperFunctions).some(func => { | ||
if (splitName.length === 2 && func.object === splitName[0] && func.property === splitName[1]) { | ||
return true; | ||
} | ||
return name === func || func.property === name; | ||
}); | ||
} | ||
|
||
module.exports = { | ||
getPropWrapperFunctions: getPropWrapperFunctions, | ||
isPropWrapperFunction: isPropWrapperFunction | ||
}; |
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,62 @@ | ||
/* eslint-env mocha */ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const propWrapperUtil = require('../../lib/util/propWrapper'); | ||
|
||
describe('PropWrapperFunctions', () => { | ||
describe('getPropWrapperFunctions', () => { | ||
it('returns set of functions if setting exists', () => { | ||
const propWrapperFunctions = ['Object.freeze', { | ||
property: 'forbidExtraProps' | ||
}]; | ||
const context = { | ||
settings: { | ||
propWrapperFunctions: propWrapperFunctions | ||
} | ||
}; | ||
assert.deepStrictEqual(propWrapperUtil.getPropWrapperFunctions(context), new Set(propWrapperFunctions)); | ||
}); | ||
|
||
it('returns empty array if no setting', () => { | ||
const context = { | ||
settings: {} | ||
}; | ||
assert.deepStrictEqual(propWrapperUtil.getPropWrapperFunctions(context), new Set([])); | ||
}); | ||
}); | ||
|
||
describe('isPropWrapperFunction', () => { | ||
it('with string', () => { | ||
const context = { | ||
settings: { | ||
propWrapperFunctions: ['Object.freeze'] | ||
} | ||
}; | ||
assert.equal(propWrapperUtil.isPropWrapperFunction(context, 'Object.freeze'), true); | ||
}); | ||
|
||
it('with Object with object and property keys', () => { | ||
const context = { | ||
settings: { | ||
propWrapperFunctions: [{ | ||
property: 'freeze', | ||
object: 'Object' | ||
}] | ||
} | ||
}; | ||
assert.equal(propWrapperUtil.isPropWrapperFunction(context, 'Object.freeze'), true); | ||
}); | ||
|
||
it('with Object with only property key', () => { | ||
const context = { | ||
settings: { | ||
propWrapperFunctions: [{ | ||
property: 'forbidExtraProps' | ||
}] | ||
} | ||
}; | ||
assert.equal(propWrapperUtil.isPropWrapperFunction(context, 'forbidExtraProps'), true); | ||
}); | ||
}); | ||
}); |