Skip to content

Commit 6615939

Browse files
authored
Merge pull request #79 from headwirecom/feature/strict-mode-examplePackage
Turn on strict mode, validate packages on CI
2 parents a54c5d9 + d8b8902 commit 6615939

File tree

10 files changed

+53
-33
lines changed

10 files changed

+53
-33
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ install:
1313
- npm run init
1414

1515
script:
16+
- npm run validate
1617
- npm run build
1718
- npm run bundle
1819
- npm run test-cov

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"clean": "lerna run clean",
1212
"test": "lerna run test",
1313
"test-cov": "lerna run test-cov",
14+
"validate": "lerna run validate",
1415
"lint": "tslint 'packages/**/*.{ts,tsx}' -c ./tslint.json"
1516
},
1617
"devDependencies": {

packages/example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"redux": "^4.0.4"
2121
},
2222
"scripts": {
23+
"validate": "../../node_modules/.bin/tsc --noEmit",
2324
"build": "echo 'Nothing to do'",
2425
"test": "echo 'Nothing to do'"
2526
}

packages/example/src/App.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ import { TextArea } from './TextArea';
4343

4444
function App(props: AppProps) {
4545
const setExampleByName = useCallback(
46-
(exampleName: string) => {
47-
props.changeExample(
48-
props.examples.find((example) => example.name === exampleName)
46+
(exampleName: string | number) => {
47+
const example = props.examples.find(
48+
(example) => example.name === exampleName
4949
);
50+
if (example) {
51+
props.changeExample(example);
52+
}
5053
},
5154
[props.changeExample, props.examples]
5255
);
@@ -149,7 +152,7 @@ export default initializedConnect(App);
149152

150153
function ExamplesPicker(
151154
props: Omit<AppProps, 'onChange'> & {
152-
onChange: (exampleName: string) => void;
155+
onChange: (exampleName: string | number) => void;
153156
}
154157
) {
155158
const options = [

packages/example/src/geographical-location.schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ export default {
4444
maximum: 180,
4545
},
4646
},
47-
} as any;
47+
};

packages/example/src/index.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ import {
5959
const getExampleSchemas = () => {
6060
if (window.samples) {
6161
registerExamples(window.samples);
62-
} else {
63-
registerExamples([
64-
{
65-
name: 'spectrum-test',
66-
label: 'test',
67-
data: { name: 'a sample name' },
68-
schema: undefined,
69-
uischema: undefined,
70-
},
71-
]);
7262
}
7363

7464
const examples = getExamples();
@@ -90,9 +80,10 @@ const setupStore = (
9080
renderers: renderers,
9181
},
9282
examples: {
83+
selectedExample: exampleData[exampleData.length - 1],
9384
data: exampleData,
9485
},
95-
} as any);
86+
});
9687

9788
// Resolve example configuration
9889
// Add schema to validation

packages/example/src/reduxUtil.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ export interface ExampleStateProps {
5656

5757
export interface ExampleDispatchProps {
5858
changeExample(example: ReactExampleDescription): void;
59-
getComponent(example: ReactExampleDescription): React.Component;
59+
getComponent(example: ReactExampleDescription): React.Component | null;
6060
onChange?(
6161
example: ReactExampleDescription
62-
): (state: Pick<JsonFormsCore, 'data' | 'errors'>) => void;
62+
): ((state: Pick<JsonFormsCore, 'data' | 'errors'>) => void) | null;
6363
}
6464

6565
export interface AppProps extends ExampleStateProps {
@@ -70,17 +70,17 @@ export interface AppProps extends ExampleStateProps {
7070

7171
const mapStateToProps = (state: any) => {
7272
const examples = state.examples.data;
73-
const selectedExample =
74-
state.examples.selectedExample || examples[examples.length - 1];
7573
const extensionState = state.examples.extensionState;
7674
return {
7775
dataAsString: JSON.stringify(getData(state), null, 2),
7876
examples,
79-
selectedExample,
77+
selectedExample: state.examples.selectedExample,
8078
extensionState,
8179
};
8280
};
83-
const mapDispatchToProps = (dispatch: Dispatch<AnyAction>) => ({
81+
const mapDispatchToProps = (
82+
dispatch: Dispatch<AnyAction>
83+
): ExampleDispatchProps => ({
8484
changeExample: (example: ReactExampleDescription) => {
8585
dispatch(changeExample(example));
8686
dispatch(Actions.init(example.data, example.schema, example.uischema));
@@ -91,7 +91,7 @@ const mapDispatchToProps = (dispatch: Dispatch<AnyAction>) => ({
9191
? example.customReactExtension(dispatch)
9292
: null,
9393
onChange: (example: ReactExampleDescription) =>
94-
example.onChange ? example.onChange(dispatch) : undefined,
94+
example.onChange ? example.onChange(dispatch) : null,
9595
});
9696
const mergeProps = (
9797
stateProps: ExampleStateProps,
@@ -105,8 +105,7 @@ const mergeProps = (
105105
dispatchProps.getComponent(stateProps.selectedExample),
106106
onChange:
107107
dispatchProps.onChange &&
108-
dispatchProps.onChange(stateProps.selectedExample) &&
109-
dispatchProps.onChange(stateProps.selectedExample)(
108+
dispatchProps.onChange(stateProps.selectedExample)?.(
110109
stateProps.extensionState
111110
),
112111
});
@@ -119,7 +118,13 @@ interface ExamplesState {
119118

120119
const initState: ExamplesState = {
121120
data: [],
122-
selectedExample: undefined,
121+
selectedExample: {
122+
name: 'init',
123+
label: 'Init',
124+
data: undefined,
125+
schema: {},
126+
uischema: { type: 'HorizontalLayout' },
127+
},
123128
};
124129

125130
export const exampleReducer = (

packages/example/src/util.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,32 @@ export interface OwnPropsOfI18nExample {
8080
) => (state: Pick<JsonFormsCore, 'data' | 'errors'>) => void;
8181
}
8282

83+
const supportedLocales = ['de-DE', 'en-US'] as const;
84+
type SupportedLocale = typeof supportedLocales[number];
85+
86+
function isSupportedLocale(locale: string): locale is SupportedLocale {
87+
return (supportedLocales as readonly string[]).includes(locale);
88+
}
89+
8390
class I18nExampleRenderer extends React.Component<
8491
I18nExampleProps,
8592
{
86-
localizedSchemas: Map<string, JsonSchema>;
87-
localizedUISchemas: Map<string, UISchemaElement>;
93+
localizedSchemas: Map<SupportedLocale, JsonSchema>;
94+
localizedUISchemas: Map<SupportedLocale, UISchemaElement>;
8895
}
8996
> {
9097
constructor(props: I18nExampleProps) {
9198
super(props);
9299
const { schema, uischema } = props;
93-
const localizedSchemas = new Map<string, JsonSchema>();
100+
const localizedSchemas = new Map<SupportedLocale, JsonSchema>();
94101
_.set(schema, '$id', 'http://test.json');
95102
const deSchema = _.cloneDeep(schema);
96103
_.set(deSchema, 'properties.name.description', 'Name der Person');
97104
_.set(deSchema, 'properties.name.birthDate', 'Geburtstag der Person');
98105
localizedSchemas.set('de-DE', deSchema);
99106
localizedSchemas.set('en-US', schema);
100107

101-
const localizedUISchemas = new Map<string, UISchemaElement>();
108+
const localizedUISchemas = new Map<SupportedLocale, UISchemaElement>();
102109
const deUISchema = _.cloneDeep(uischema);
103110
_.set(deUISchema, 'elements.0.elements.1.label', 'Geburtstag');
104111
_.set(deUISchema, 'elements.2.elements.0.label', 'Nationalität');
@@ -115,11 +122,14 @@ class I18nExampleRenderer extends React.Component<
115122
}
116123

117124
changeLocale = (locale: string) => {
125+
if (!isSupportedLocale(locale)) {
126+
throw new Error(`The locale ${locale} is not supported`);
127+
}
118128
const { dispatch, onChange, data, errors } = this.props;
119129
const { localizedSchemas, localizedUISchemas } = this.state;
120130
dispatch(setLocale(locale));
121-
dispatch(setSchema(localizedSchemas.get(locale)));
122-
dispatch(setUISchema(localizedUISchemas.get(locale)));
131+
dispatch(setSchema(localizedSchemas.get(locale)!));
132+
dispatch(setUISchema(localizedUISchemas.get(locale)!));
123133
dispatch(updateExampleExtensionState({ locale }));
124134
onChange(dispatch)({ locale })({ data, errors });
125135
};
@@ -139,7 +149,7 @@ const withContextToI18nProps = (
139149
): React.ComponentType<OwnPropsOfI18nExample> => ({
140150
ctx,
141151
props,
142-
}: JsonFormsStateContext & I18nExampleProps) => {
152+
}: JsonFormsStateContext & Omit<I18nExampleProps, 'data' | 'errors'>) => {
143153
const { data, errors } = ctx.core;
144154
return <Component {...props} data={data} errors={errors} />;
145155
};

packages/example/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.base",
3+
"compilerOptions": {
4+
"strict": true
5+
},
6+
"include": ["src/**/*"]
7+
}

packages/spectrum/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"report": "../../node_modules/.bin/nyc report --reporter=html",
2020
"test": "jest --no-cache",
2121
"test-cov": "jest --no-cache --coverage",
22+
"validate": "../../node_modules/.bin/tsc --noEmit",
2223
"doc": "../../node_modules/.bin/typedoc --name 'JSON Forms React Spectrum Renderers' --mode file --excludeExternals --theme ../../typedoc-jsonforms --out docs src"
2324
},
2425
"keywords": [

0 commit comments

Comments
 (0)