-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add ref forwarding to API docs (#15135)
Adds explicit documentation to `https://material-ui.com/api/*` about ref forwarding. Requires #15131 to not report false positives. Finishes one point in #14415.
- Loading branch information
Showing
117 changed files
with
355 additions
and
14 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,102 @@ | ||
import * as babel from '@babel/core'; | ||
import { readFile } from 'fs-extra'; | ||
import * as path from 'path'; | ||
|
||
const workspaceRoot = path.join(__dirname, '../../../../'); | ||
const babelConfigPath = path.join(workspaceRoot, 'babel.config.js'); | ||
|
||
function withExtension(filepath, extension) { | ||
return path.join( | ||
path.dirname(filepath), | ||
path.basename(filepath, path.extname(filepath)) + extension, | ||
); | ||
} | ||
|
||
/** | ||
* @param {string} filename | ||
* @param {string} configFilePath | ||
*/ | ||
async function parseWithConfig(filename, configFilePath) { | ||
const source = await readFile(filename, { encoding: 'utf8' }); | ||
const partialConfig = babel.loadPartialConfig({ | ||
configFile: configFilePath, | ||
filename, | ||
}); | ||
return babel.parseAsync(source, partialConfig.options); | ||
} | ||
|
||
function findConformanceDescriptor(program) { | ||
const { types: t } = babel; | ||
|
||
let descriptor = {}; | ||
babel.traverse(program, { | ||
CallExpression(babelPath) { | ||
const { node: callExpression } = babelPath; | ||
const { callee } = callExpression; | ||
if (t.isIdentifier(callee) && callee.name === 'describeConformance') { | ||
// describeConformance(element, () => options); | ||
descriptor = callExpression.arguments[1].body; | ||
} | ||
}, | ||
}); | ||
|
||
if (descriptor.type != null && !t.isObjectExpression(descriptor)) { | ||
throw new Error(`Expected an object expression as a descriptor but found ${descriptor.type}`); | ||
} | ||
|
||
return descriptor; | ||
} | ||
|
||
/** | ||
* | ||
* @param {import('@babel/core').Node} valueNode | ||
*/ | ||
function getRefInstance(valueNode) { | ||
if (!babel.types.isMemberExpression(valueNode)) { | ||
throw new Error('Expected a member expression in refInstanceof'); | ||
} | ||
|
||
switch (valueNode.object.name) { | ||
case 'window': | ||
return valueNode.property.name; | ||
case 'React': | ||
return `React.${valueNode.property.name}`; | ||
default: | ||
throw new Error(`Unrecognized member expression starting with '${valueNode.object.name}'`); | ||
} | ||
} | ||
|
||
/** | ||
* @typedef {Object} ParseResult | ||
* @property {string?} forwardsRefTo | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {string} componentFilename | ||
* @returns {ParseResult} | ||
*/ | ||
export default async function parseTest(componentFilename) { | ||
const testFilename = withExtension(componentFilename, '.test.js'); | ||
const babelParseResult = await parseWithConfig(testFilename, babelConfigPath); | ||
const descriptor = findConformanceDescriptor(babelParseResult.program); | ||
|
||
const result = { | ||
forwardsRefTo: undefined, | ||
}; | ||
|
||
const { properties = [] } = descriptor; | ||
properties.forEach(property => { | ||
const key = property.key.name; | ||
|
||
switch (key) { | ||
case 'refInstanceof': | ||
result.forwardsRefTo = getRefInstance(property.value); | ||
break; | ||
default: | ||
break; | ||
} | ||
}); | ||
|
||
return result; | ||
} |
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
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
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
Oops, something went wrong.