A numerical model based on a boundary-element method is developed to predict the performance of flapping foils for the general cases where vorticities are shed near the leading edge as well as from the trailing edge. The shed vorticities are modelled as desingularized thin shear layers which propagate with the local flow velocity. Special treatments are developed to model the unsteady and alternating leading-edge separation (LES), which is a main element of difficulty for theoretical and numerical analyses of general flapping foils. The present method is compared with existing experiments where it is shown that the inclusion of LES significantly improves the prediction of thrust and efficiency, obtaining excellent agreement with measurements over a broad range of flapping frequencies (Strouhal number) and motion amplitudes (maximum angle of attack). It is found that the neglect of LES leads to substantial over-prediction of the thrust (and efficiency). The effects of LES on thrust generation in terms of the circulation around the foil, the steady and unsteady thrust components, and the vortex-induced pressure on the foil are elucidated. The efficiency and robustness of the method render it suitable for design optimization which generally requires large numbers of performance evaluations. To illustrate this, we present a sample problem of designing the flapping motion, with the inclusion of higher harmonic components, to maximize the efficiency under specified thrust. When optimal higher harmonic motions are included, the performance of the flapping foil is appreciably improved, mitigating the adverse effects of LES vortex on the performance.