Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
fix: consider export name for local id
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed May 24, 2018
1 parent d3077f7 commit dc4e904
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/analyzer/typescript-react-analyzer/typescript-react-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ interface AnalyzeContext {
program: ts.Program;
}

type PatternAnalyzer = (candidate: PatternCandidate, predicate: PatternAnalyzerPredicate) => Types.PatternAnalysis[];
type PatternAnalyzerPredicate = (ex: TypeScript.TypescriptExport, ctx: AnalyzeContext) => Types.PatternAnalysis | undefined;

export async function analyze(path: string, previousLibrary: Types.SerializedPatternLibrary): Promise<Types.LibraryAnalysis> {
type PatternAnalyzer = (
candidate: PatternCandidate,
predicate: PatternAnalyzerPredicate
) => Types.PatternAnalysis[];
type PatternAnalyzerPredicate = (
ex: TypeScript.TypescriptExport,
ctx: AnalyzeContext
) => Types.PatternAnalysis | undefined;

export async function analyze(
path: string,
previousLibrary: Types.SerializedPatternLibrary
): Promise<Types.LibraryAnalysis> {
const pkg = await readPkg(path);
const library = PatternLibrary.from(previousLibrary);

Expand All @@ -52,7 +61,7 @@ export async function analyze(path: string, previousLibrary: Types.SerializedPat
return item;
});

const compilerPatterns = patterns.map(({path: patternPath, pattern}) => ({
const compilerPatterns = patterns.map(({ path: patternPath, pattern }) => ({
id: pattern.id,
path: patternPath
}));
Expand All @@ -70,7 +79,10 @@ export async function analyze(path: string, previousLibrary: Types.SerializedPat
}

function getPatternAnalyzer(program: ts.Program): PatternAnalyzer {
return (candidate: PatternCandidate, predicate: PatternAnalyzerPredicate): Types.PatternAnalysis[] => {
return (
candidate: PatternCandidate,
predicate: PatternAnalyzerPredicate
): Types.PatternAnalysis[] => {
const sourceFile = program.getSourceFile(candidate.sourcePath);

if (!sourceFile) {
Expand All @@ -83,11 +95,11 @@ function getPatternAnalyzer(program: ts.Program): PatternAnalyzer {
};
}

function analyzePatternExport(ex: TypeScript.TypescriptExport, ctx: AnalyzeContext): Types.PatternAnalysis | undefined {
const reactType = ReactUtils.findReactComponentType(
ctx.program,
ex.type
);
function analyzePatternExport(
ex: TypeScript.TypescriptExport,
ctx: AnalyzeContext
): Types.PatternAnalysis | undefined {
const reactType = ReactUtils.findReactComponentType(ctx.program, ex.type);

if (!reactType) {
return;
Expand All @@ -102,12 +114,13 @@ function analyzePatternExport(ex: TypeScript.TypescriptExport, ctx: AnalyzeConte
const [propTypes] = reactTypeArguments;
const properties = PropertyAnalyzer.analyze(propTypes.type, ctx.program);
const slots = SlotAnalyzer.analyzeSlots(propTypes.type, ctx.program);
const exportName = ex.name || 'default';

return {
path: ctx.candidate.artifactPath,
pattern: {
contextId: ctx.candidate.id,
exportName: ex.name || 'default',
contextId: `${ctx.candidate.id}:${exportName}`,
exportName,
id: uuid.v4(),
name: ctx.candidate.displayName,
propertyIds: properties.map(p => p.id),
Expand All @@ -123,7 +136,7 @@ async function findPatternCandidates(path: string): Promise<PatternCandidate[]>
const { config, filePath } = patternplateConfig;
const cwd = filePath ? Path.dirname(filePath) : path;

const {patterns} = await loadPatternplateMeta({ entry: config.entry, cwd });
const { patterns } = await loadPatternplateMeta({ entry: config.entry, cwd });

return patterns.map(pattern => {
const sourceDirname = Path.dirname(pattern.source);
Expand Down

0 comments on commit dc4e904

Please sign in to comment.