|
| 1 | +import glob from 'glob'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +export interface CreateFixturePatternConfig { |
| 5 | + ignoreBabel?: string[]; |
| 6 | + ignoreEspree?: string[]; |
| 7 | + fileType?: string; |
| 8 | + ignoreSourceType?: string[]; |
| 9 | +} |
| 10 | + |
| 11 | +export interface Fixture { |
| 12 | + filename: string; |
| 13 | + ignoreSourceType: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +export interface FixturePatternConfig { |
| 17 | + pattern: string; |
| 18 | + ignoreSourceType: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +export class FixturesTester { |
| 22 | + protected babelFixtures: FixturePatternConfig[] = []; |
| 23 | + protected espreeFixtures: FixturePatternConfig[] = []; |
| 24 | + |
| 25 | + constructor() {} |
| 26 | + |
| 27 | + public registerTest( |
| 28 | + fixturesSubPath: string, |
| 29 | + config: CreateFixturePatternConfig = {} |
| 30 | + ) { |
| 31 | + const ignoreBabel = config.ignoreBabel || []; |
| 32 | + const ignoreEspree = config.ignoreEspree || []; |
| 33 | + const fileType = config.fileType || 'js'; |
| 34 | + const ignoreSourceType = config.ignoreSourceType || []; |
| 35 | + |
| 36 | + if (fileType === 'js' || fileType === 'jsx') { |
| 37 | + this.espreeFixtures.push({ |
| 38 | + pattern: `${fixturesSubPath}/!(${ignoreEspree.join( |
| 39 | + '|' |
| 40 | + )}).src.${fileType}`, |
| 41 | + ignoreSourceType: false |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * https://github.com/babel/babel/issues/9213 |
| 47 | + */ |
| 48 | + if (ignoreSourceType.length) { |
| 49 | + ignoreBabel.push(...ignoreSourceType); |
| 50 | + for (const fixture of ignoreSourceType) { |
| 51 | + this.babelFixtures.push({ |
| 52 | + // It needs to be the full path from within fixtures/ for the pattern |
| 53 | + pattern: `${fixturesSubPath}/${fixture}.src.${config.fileType}`, |
| 54 | + ignoreSourceType: true |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + this.babelFixtures.push({ |
| 60 | + pattern: `${fixturesSubPath}/!(${ignoreBabel.join('|')}).src.${fileType}`, |
| 61 | + ignoreSourceType: false |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + protected processFixtures(fixtures: FixturePatternConfig[]): Fixture[] { |
| 66 | + const fixturesDirPath = path.join(__dirname, '../fixtures'); |
| 67 | + |
| 68 | + return fixtures |
| 69 | + .map(fixtures => { |
| 70 | + return glob |
| 71 | + .sync(`${fixturesDirPath}/${fixtures.pattern}`, {}) |
| 72 | + .map(filename => { |
| 73 | + return { |
| 74 | + filename, |
| 75 | + ignoreSourceType: fixtures.ignoreSourceType |
| 76 | + }; |
| 77 | + }); |
| 78 | + }) |
| 79 | + .reduce((acc, x) => acc.concat(x), []); |
| 80 | + } |
| 81 | + |
| 82 | + public getForBabel(): Fixture[] { |
| 83 | + return this.processFixtures(this.babelFixtures); |
| 84 | + } |
| 85 | + |
| 86 | + public getForEspree(): Fixture[] { |
| 87 | + return this.processFixtures(this.espreeFixtures); |
| 88 | + } |
| 89 | +} |
0 commit comments