-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Did not change any logic, just moved/renamed functions out of parsePageFile.ts
- Loading branch information
Showing
4 changed files
with
107 additions
and
95 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,19 @@ | ||
import { PossibleModuleNames } from '../../shared/models' | ||
|
||
export default function getComponentModuleName( | ||
name: string, | ||
imports: Record<string, string[]>, | ||
isLayout: boolean | ||
): PossibleModuleNames { | ||
let moduleName = Object.keys(imports).find(importIdentifier => { | ||
const importedNames = imports[importIdentifier] | ||
return importedNames.includes(name) | ||
}) | ||
if (!moduleName) { | ||
throw new Error(`Could not find import path/module for component "${name}"`) | ||
} | ||
if (moduleName.startsWith('.')) { | ||
moduleName = isLayout ? 'localLayouts' : 'localComponents' | ||
} | ||
return moduleName as PossibleModuleNames | ||
} |
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,30 @@ | ||
import { JsxOpeningElement, JsxSelfClosingElement, ts, JsxAttribute } from 'ts-morph' | ||
import { ComponentMetadata, PropState } from '../../shared/models' | ||
import { getPropName, getJsxAttributeValue, validatePropState } from '../common' | ||
|
||
export default function parseJsxAttributes( | ||
n: JsxOpeningElement | JsxSelfClosingElement, | ||
componentMetaData: ComponentMetadata | ||
): PropState { | ||
const props = {} | ||
n.getDescendantsOfKind(ts.SyntaxKind.JsxAttribute).forEach((jsxAttribute: JsxAttribute) => { | ||
const propName = getPropName(jsxAttribute) | ||
if (!propName) { | ||
throw new Error('Could not parse jsx attribute prop name: ' + jsxAttribute.getFullText()) | ||
} | ||
const propType = componentMetaData.propShape?.[propName]?.type | ||
if (!propType) { | ||
throw new Error('Could not find prop type for: ' + jsxAttribute.getFullText()) | ||
} | ||
const propValue = getJsxAttributeValue(jsxAttribute) | ||
const propState = { | ||
type: propType, | ||
value: propValue | ||
} | ||
if (!validatePropState(propState)) { | ||
throw new Error(`Could not validate propState ${JSON.stringify(propState, null, 2)}`) | ||
} | ||
props[propName] = propState | ||
}) | ||
return props | ||
} |
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,51 @@ | ||
import { ComponentState } from 'react' | ||
import { SourceFile, JsxElement, JsxFragment, ts } from 'ts-morph' | ||
import { v1 } from 'uuid' | ||
import { getDefaultExport, getComponentName } from '../common' | ||
import getComponentModuleName from './getComponentModuleName' | ||
|
||
export default function parseLayoutState( | ||
sourceFile: SourceFile, | ||
imports: Record<string, string[]> | ||
): { layoutState: ComponentState, layoutNode: JsxElement | JsxFragment } { | ||
const defaultExport = getDefaultExport(sourceFile) | ||
const returnStatement = defaultExport.getFirstDescendantByKind(ts.SyntaxKind.ReturnStatement) | ||
if (!returnStatement) { | ||
throw new Error('No return statement found for page') | ||
} | ||
const JsxNodeWrapper = returnStatement.getFirstChildByKind(ts.SyntaxKind.ParenthesizedExpression) | ||
?? returnStatement | ||
const topLevelJsxNode = JsxNodeWrapper.getChildren() | ||
.find(n => n.getKind() === ts.SyntaxKind.JsxElement || n.getKind() === ts.SyntaxKind.JsxFragment) | ||
if (!topLevelJsxNode) { | ||
throw new Error('Unable to find top level JSX element or JsxFragment type from file.') | ||
} | ||
|
||
let layoutState: ComponentState | ||
if (topLevelJsxNode.getKind() === ts.SyntaxKind.JsxElement) { | ||
const name = getComponentName((topLevelJsxNode as JsxElement).getOpeningElement()) | ||
layoutState = { | ||
name, | ||
props: {}, | ||
uuid: v1(), | ||
moduleName: 'builtIn' | ||
} | ||
const isBuiltinJsxElement = name.charAt(0) === name.charAt(0).toLowerCase() | ||
if (!isBuiltinJsxElement && !['Fragment', 'React.Fragment'].includes(name)) { | ||
layoutState.moduleName = getComponentModuleName(name, imports, true) | ||
} | ||
} else { | ||
// This handles the React.Fragment shorthand <></> | ||
layoutState = { | ||
name: '', | ||
props: {}, | ||
uuid: v1(), | ||
moduleName: 'builtIn' | ||
} | ||
} | ||
|
||
return { | ||
layoutState, | ||
layoutNode: topLevelJsxNode as JsxElement | JsxFragment | ||
} | ||
} |
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