-
Notifications
You must be signed in to change notification settings - Fork 734
prop value shape deprecation lint rule #1094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1bc67d4
set new lint rule for prop value
lidord-wix cb47fb6
fix schema
lidord-wix 145cd44
add cases to prop-value-shape lint rule
lidord-wix 1e6c8af
Merge branch 'master' into Infra/prop-value-shape-deprecation
lidord-wix 4debe82
refactor propShapeValueDeprecation
lidord-wix 7535c73
Merge branch 'master' into Infra/prop-value-shape-deprecation
M-i-k-e-l 3a14227
Adding two (failing) tests
M-i-k-e-l 75a1176
Fix test and add two more big tests
M-i-k-e-l 0d3c9cf
More tests
M-i-k-e-l 3e54975
Merge branch 'master' into infra/prop-value-shape-deprecation
lidord-wix 6190640
Merge branch 'Infra/prop-value-shape-deprecation' of https://github.c…
lidord-wix 1faf757
Merge branch 'master' into Infra/prop-value-shape-deprecation
lidord-wix cc58e6a
add getComponentName to utils
lidord-wix e44dcaa
Renaming
lidord-wix 9ce8953
update message when there's no fix
lidord-wix 437e031
fix deprecation message
lidord-wix 7eb21f7
Merge branch 'master' into infra/prop-value-shape-deprecation
lidord-wix 4cce810
update version
lidord-wix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,139 @@ | ||
const _ = require('lodash'); | ||
const utils = require('../utils'); | ||
|
||
const MAP_SCHEMA = { | ||
type: 'object', | ||
properties: { | ||
components: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
}, | ||
propNames: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
}, | ||
shape: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
required: ['prop', 'message', 'fix'], | ||
properties: { | ||
prop: { | ||
type: 'string' | ||
}, | ||
message: { | ||
type: 'string' | ||
}, | ||
fix: { | ||
type: 'object' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "Detect deprecation of prop's value shape", | ||
category: 'Best Practices', | ||
recommended: true | ||
}, | ||
messages: { | ||
uiLib: "This prop's value shape is deprecated." | ||
}, | ||
fixable: 'code', | ||
schema: [MAP_SCHEMA] | ||
}, | ||
create(context) { | ||
function reportPropValueShapeDeprecation(propKey, prop, deprecation, node) { | ||
const componentName = utils.getComponentName(node); | ||
const newProp = _.get(deprecation, 'fix.propName'); | ||
const fixMessage = _.get(deprecation, 'message') ? ' ' + _.get(deprecation, 'message') : ''; | ||
const message = `The shape of '${prop}' prop of '${componentName}' doesn't contain '${deprecation.prop}' anymore.${fixMessage}`; | ||
context.report({ | ||
node, | ||
message, | ||
fix(fixer) { | ||
if (newProp && propKey) { | ||
return fixer.replaceText(propKey, newProp); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function testJSXAttributes(node) { | ||
try { | ||
const {deprecations} = _.get(context, 'options[0]'); | ||
const componentName = utils.getComponentName(node); | ||
_.forEach(deprecations, deprecation => { | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (_.includes(deprecation.components, componentName)) { | ||
_.forEach(node.attributes, attribute => { | ||
const attributeName = _.get(attribute, 'name.name'); | ||
if (attribute.type === 'JSXSpreadAttribute') { | ||
const spreadSource = utils.findValueNodeOfIdentifier(attribute.argument.name, context.getScope()); | ||
const spreadSourceName = _.get(spreadSource, 'properties[0].key.name'); | ||
checkAttributeProperties( | ||
spreadSource.properties[0].value.properties, | ||
spreadSourceName, | ||
deprecation, | ||
node, | ||
context | ||
); | ||
} else if (_.includes(deprecation.propNames, attributeName)) { | ||
checkAttribute(attribute, deprecation, node); | ||
} | ||
}); | ||
} | ||
}); | ||
} catch (err) { | ||
console.log('Found error in: ', context.getFilename()); | ||
} | ||
} | ||
|
||
function checkAttribute(attribute, deprecation, node) { | ||
const attributeName = _.get(attribute, 'name.name'); | ||
const attributeType = _.get(attribute, 'value.expression.type'); | ||
if (attributeType === 'Identifier') { | ||
const passedProp = utils.findValueNodeOfIdentifier(attribute.value.expression.name, context.getScope()); | ||
if (passedProp && passedProp.properties) { | ||
checkAttributeProperties(passedProp.properties, attributeName, deprecation, node, context); | ||
} | ||
} | ||
const attributeProps = _.get(attribute, 'value.expression.properties'); | ||
for (let index = 0; index < attributeProps.length; index++) { | ||
const spreadElementType = _.get(attribute, `value.expression.properties[${index}].type`); | ||
if (attributeType === 'ObjectExpression' && spreadElementType === 'ExperimentalSpreadProperty') { | ||
const spreadSource = utils.findValueNodeOfIdentifier( | ||
attribute.value.expression.properties[index].argument.name, | ||
context.getScope() | ||
); | ||
if (spreadSource && spreadSource.properties) { | ||
checkAttributeProperties(spreadSource.properties, attributeName, deprecation, node); | ||
} | ||
} | ||
} | ||
const attributeProperties = _.get(attribute, 'value.expression.properties'); | ||
checkAttributeProperties(attributeProperties, attributeName, deprecation, node); | ||
} | ||
|
||
function checkAttributeProperties(attributeProperties, attributeName, deprecation, node) { | ||
for (let i = 0; i <= attributeProperties.length; i++) { | ||
const propertyName = _.get(attributeProperties[i], 'key.name'); | ||
const origin = propertyName && _.find(deprecation.shape, ['prop', propertyName]); | ||
if (origin && origin.prop && propertyName === origin.prop) { | ||
reportPropValueShapeDeprecation(attributeProperties[i].key, attributeName, origin, node); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
JSXOpeningElement: testJSXAttributes | ||
}; | ||
} | ||
}; |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.