Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions js/__tests__/Picker.types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

const path = require('path');
const ts = require('typescript');

const TYPINGS_ROOT = path.resolve(__dirname, '../../typings');

function getTypingsDiagnostics() {
const compilerOptions = {
target: ts.ScriptTarget.ES6,
module: ts.ModuleKind.CommonJS,
jsx: ts.JsxEmit.React,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
strict: true,
skipLibCheck: false,
noEmit: true,
esModuleInterop: true,
};
const program = ts.createProgram(
[
path.join(TYPINGS_ROOT, 'index.d.ts'),
path.join(TYPINGS_ROOT, 'Picker.d.ts'),
path.join(TYPINGS_ROOT, 'PickerIOS.d.ts'),
],
compilerOptions,
);

return ts.getPreEmitDiagnostics(program).filter((diagnostic) => {
if (!diagnostic.file) {
return false;
}
return diagnostic.file.fileName.replace(/\\/g, '/').includes('/typings/');
});
}

function formatDiagnostics(diagnostics) {
return diagnostics
.map((diagnostic) => {
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(
diagnostic.start,
);
const message = ts.flattenDiagnosticMessageText(
diagnostic.messageText,
'\n',
);
const fileName = path.relative(process.cwd(), diagnostic.file.fileName);
return `${fileName}:${line + 1}:${character + 1} TS${
diagnostic.code
}: ${message}`;
})
.join('\n');
}

function checkPickerTypings() {
const formatted = formatDiagnostics(getTypingsDiagnostics());
if (formatted !== '') {
throw new Error(`Picker typings failed:\n${formatted}`);
}
}

if (typeof describe === 'function') {
describe('Picker typings', () => {
it('does not reference class type parameters from static members', () => {
checkPickerTypings();
});
});
} else {
checkPickerTypings();
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test": "yarn validate:eslint && yarn validate:flow && yarn validate:typescript && yarn test:jest",
"validate:eslint": "eslint 'js/**/*.js' 'example/**/*.js'",
"validate:flow": "flow check",
"validate:typescript": "tsc --project ./",
"validate:typescript": "tsc --project ./ && node js/__tests__/Picker.types.test.js",
"test:jest": "jest js/",
"test:detox:android:test:debug": "detox test -c android.emu.debug",
"test:detox:android:test:release": "detox test -c android.emu.release",
Expand Down
4 changes: 3 additions & 1 deletion typings/Picker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export interface PickerProps<T = ItemValue> extends ViewProps {
onBlur?: (e: NativeSyntheticEvent<TargetedEvent>) => void;
}

declare class PickerItem<T = ItemValue> extends React.Component<PickerItemProps<T>, {}> {}

declare class Picker<T> extends React.Component<PickerProps<T>, {}> {
/**
* On Android, display the options in a dialog (this is the default).
Expand All @@ -132,7 +134,7 @@ declare class Picker<T> extends React.Component<PickerProps<T>, {}> {
*/
static readonly MODE_DROPDOWN: 'dropdown';

static Item: React.ComponentType<PickerItemProps<T>>;
static Item: typeof PickerItem;

/**
* @platform android
Expand Down