Skip to content

Commit

Permalink
fix(): emit private types in local component.d.ts (#2447)
Browse files Browse the repository at this point in the history
fixes #2440
  • Loading branch information
manucorporat authored May 16, 2020
1 parent 11e1ccb commit 9d444ff
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/compiler/docs/generate-doc-data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as d from '../../declarations';
import { AUTO_GENERATE_COMMENT } from './constants';
import { basename, dirname, join, relative } from 'path';
import { flatOne, isDocsPublic, normalizePath, sortBy } from '@utils';
import { flatOne, normalizePath, sortBy } from '@utils';
import { getBuildTimestamp } from '../build/build-ctx';
import { JsonDocsValue } from '../../declarations';
import { typescriptVersion, version } from '../../version';
Expand All @@ -28,7 +28,7 @@ const getDocsComponents = async (config: d.Config, compilerCtx: d.CompilerCtx, b
const readme = await getUserReadmeContent(compilerCtx, readmePath);
const usage = await generateUsages(compilerCtx, usagesDir);
return moduleFile.cmps
.filter(cmp => isDocsPublic(cmp.docs) && !cmp.isCollectionDependency)
.filter(cmp => !cmp.internal && !cmp.isCollectionDependency)
.map(cmp => ({
dirPath,
filePath: relative(config.rootDir, filePath),
Expand Down Expand Up @@ -101,7 +101,7 @@ const getDocsProperties = (cmpMeta: d.ComponentCompilerMeta): d.JsonDocsProp[] =

const getRealProperties = (properties: d.ComponentCompilerProperty[]): d.JsonDocsProp[] => {
return properties
.filter(member => isDocsPublic(member.docs))
.filter(member => !member.internal)
.map(member => ({
name: member.name,
type: member.complexType.resolved,
Expand Down Expand Up @@ -183,7 +183,7 @@ const parseTypeIntoValues = (type: string) => {

const getDocsMethods = (methods: d.ComponentCompilerMethod[]): d.JsonDocsMethod[] => {
return sortBy(methods, member => member.name)
.filter(member => isDocsPublic(member.docs))
.filter(member => !member.internal)
.map(member => ({
name: member.name,
returns: {
Expand All @@ -200,7 +200,7 @@ const getDocsMethods = (methods: d.ComponentCompilerMethod[]): d.JsonDocsMethod[

const getDocsEvents = (events: d.ComponentCompilerEvent[]): d.JsonDocsEvent[] => {
return sortBy(events, eventMeta => eventMeta.name.toLowerCase())
.filter(eventMeta => isDocsPublic(eventMeta.docs))
.filter(eventMeta => !eventMeta.internal)
.map(eventMeta => ({
event: eventMeta.name,
detail: eventMeta.complexType.resolved,
Expand Down
9 changes: 5 additions & 4 deletions src/compiler/types/generate-app-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ export const generateAppTypes = async (config: d.Config, compilerCtx: d.Compiler
// only gather components that are still root ts files we've found and have component metadata
// the compilerCtx cache may still have files that may have been deleted/renamed
const timespan = buildCtx.createTimeSpan(`generated app types started`, true);
const internal = destination === 'src';

// Generate d.ts files for component types
let componentTypesFileContent = await generateComponentTypesFile(config, buildCtx, destination);
let componentTypesFileContent = await generateComponentTypesFile(config, buildCtx, internal);

// immediately write the components.d.ts file to disk and put it into fs memory
let componentsDtsFilePath = getComponentsDtsSrcFilePath(config);

if (destination !== 'src') {
if (!internal) {
componentsDtsFilePath = resolve(destination, GENERATED_DTS);
componentTypesFileContent = updateStencilTypesImports(destination, componentsDtsFilePath, componentTypesFileContent);
}
Expand All @@ -41,15 +42,15 @@ export const generateAppTypes = async (config: d.Config, compilerCtx: d.Compiler
* @param config the project build configuration
* @param options compiler options from tsconfig
*/
const generateComponentTypesFile = async (config: d.Config, buildCtx: d.BuildCtx, _destination: string) => {
const generateComponentTypesFile = async (config: d.Config, buildCtx: d.BuildCtx, internal: boolean) => {
let typeImportData: d.TypesImportData = {};
const allTypes = new Map<string, number>();
const needsJSXElementHack = buildCtx.components.some(cmp => cmp.isLegacy);
const components = buildCtx.components.filter(m => !m.isCollectionDependency);

const modules: d.TypesModule[] = components.map(cmp => {
typeImportData = updateReferenceTypeImports(typeImportData, allTypes, cmp, cmp.sourceFilePath);
return generateComponentTypes(cmp);
return generateComponentTypes(cmp, internal);
});

const jsxAugmentation = `
Expand Down
16 changes: 11 additions & 5 deletions src/compiler/types/generate-component-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { generatePropTypes } from './generate-prop-types';
* @param cmp the metadata for the component that a type definition string is generated for
* @param importPath the path of the component file
*/
export const generateComponentTypes = (cmp: d.ComponentCompilerMeta): d.TypesModule => {
export const generateComponentTypes = (cmp: d.ComponentCompilerMeta, internal: boolean): d.TypesModule => {
const tagName = cmp.tagName.toLowerCase();
const tagNameAsPascal = dashToPascalCase(tagName);
const htmlElementName = `HTML${tagNameAsPascal}Element`;
Expand All @@ -19,9 +19,9 @@ export const generateComponentTypes = (cmp: d.ComponentCompilerMeta): d.TypesMod
const methodAttributes = generateMethodTypes(cmp.methods);
const eventAttributes = generateEventTypes(cmp.events);

const stencilComponentAttributes = attributesToMultiLineString([...propAttributes, ...methodAttributes], false);
const stencilComponentAttributes = attributesToMultiLineString([...propAttributes, ...methodAttributes], false, internal);
const isDep = cmp.isCollectionDependency;
const stencilComponentJSXAttributes = attributesToMultiLineString([...propAttributes, ...eventAttributes], true);
const stencilComponentJSXAttributes = attributesToMultiLineString([...propAttributes, ...eventAttributes], true, internal);
return {
isDep,
tagName,
Expand All @@ -38,9 +38,15 @@ var ${htmlElementName}: {
};
};

const attributesToMultiLineString = (attributes: d.TypeInfo, jsxAttributes: boolean, paddingString = '') => {
const attributesToMultiLineString = (attributes: d.TypeInfo, jsxAttributes: boolean, internal: boolean) => {
const paddingString = '';
const attributesStr = sortBy(attributes, a => a.name)
.filter(type => type.public || !jsxAttributes)
.filter(type => {
if (jsxAttributes && !internal && type.internal) {
return false;
}
return true;
})
.reduce((fullList, type) => {
if (type.jsdoc) {
fullList.push(`/**`);
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/types/generate-event-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as d from '../../declarations';
import { getTextDocs, isDocsPublic, toTitleCase } from '@utils';
import { getTextDocs, toTitleCase } from '@utils';

export const generateEventTypes = (cmpEvents: d.ComponentCompilerEvent[]): d.TypeInfo => {
return cmpEvents.map(cmpEvent => {
Expand All @@ -10,7 +10,7 @@ export const generateEventTypes = (cmpEvents: d.ComponentCompilerEvent[]): d.Typ
type,
optional: false,
required: false,
public: isDocsPublic(cmpEvent.docs),
internal: cmpEvent.internal,
jsdoc: getTextDocs(cmpEvent.docs),
};
});
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/types/generate-method-types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as d from '../../declarations';
import { getTextDocs, isDocsPublic } from '@utils';
import { getTextDocs } from '@utils';

export const generateMethodTypes = (cmpMethods: d.ComponentCompilerMethod[]): d.TypeInfo => {
return cmpMethods.map(cmpMethod => ({
name: cmpMethod.name,
type: cmpMethod.complexType.signature,
optional: false,
required: false,
public: isDocsPublic(cmpMethod.docs),
internal: cmpMethod.internal,
jsdoc: getTextDocs(cmpMethod.docs),
}));
};
6 changes: 3 additions & 3 deletions src/compiler/types/generate-prop-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as d from '../../declarations';
import { getTextDocs, isDocsPublic } from '@utils';
import { getTextDocs } from '@utils';

export const generatePropTypes = (cmpMeta: d.ComponentCompilerMeta): d.TypeInfo => {
return [
Expand All @@ -8,7 +8,7 @@ export const generatePropTypes = (cmpMeta: d.ComponentCompilerMeta): d.TypeInfo
type: cmpProp.complexType.original,
optional: cmpProp.optional,
required: cmpProp.required,
public: isDocsPublic(cmpProp.docs),
internal: cmpProp.internal,
jsdoc: getTextDocs(cmpProp.docs),
})),
...cmpMeta.virtualProperties.map(cmpProp => ({
Expand All @@ -17,7 +17,7 @@ export const generatePropTypes = (cmpMeta: d.ComponentCompilerMeta): d.TypeInfo
optional: true,
required: false,
jsdoc: cmpProp.docs,
public: true,
internal: false,
})),
];
};
2 changes: 1 addition & 1 deletion src/declarations/stencil-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,7 @@ export type TypeInfo = {
type: string;
optional: boolean;
required: boolean;
public: boolean;
internal: boolean;
jsdoc?: string;
}[];

Expand Down
4 changes: 0 additions & 4 deletions src/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ export const generatePreamble = (config: d.Config, opts: { prefix?: string; suff
return '';
};

export const isDocsPublic = (jsDocs: d.JsDoc | d.CompilerJsDoc | undefined) => {
return !(jsDocs && jsDocs.tags.some(s => s.name === 'internal'));
};

const lineBreakRegex = /\r?\n|\r/g;
export function getTextDocs(docs: d.CompilerJsDoc | undefined | null) {
if (docs == null) {
Expand Down

0 comments on commit 9d444ff

Please sign in to comment.