Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

test: add alignment test for espree & change babel sourceType to unambiguous #52

Closed
wants to merge 3 commits into from
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@
"license": "BSD-2-Clause",
"devDependencies": {
"@babel/code-frame": "7.0.0",
"@babel/parser": "7.1.6",
"@babel/parser": "7.2.2",
"@commitlint/cli": "^7.1.2",
"@commitlint/config-conventional": "^7.1.2",
"@commitlint/travis-cli": "^7.1.2",
"@types/babel-code-frame": "^6.20.1",
"@types/estree": "^0.0.39",
"@types/jest": "^23.3.9",
"@types/lodash.isplainobject": "^4.0.4",
"@types/lodash.unescape": "^4.0.4",
"@types/node": "^10.12.2",
"@types/semver": "^5.5.0",
"@types/shelljs": "^0.8.0",
"cz-conventional-changelog": "2.1.0",
"espree": "^5.0.0",
"glob": "7.1.2",
"husky": "0.14.3",
"jest": "23.1.0",
Expand Down
99 changes: 99 additions & 0 deletions tests/ast-alignment/fixtures-tester.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import glob from 'glob';
import fs from 'fs';
import path from 'path';

export interface CreateFixturePatternConfig {
ignoreBabel?: string[];
ignoreEspree?: string[];
fileType?: string;
ignoreSourceType?: string[];
}

export interface Fixture {
filename: string;
ignoreSourceType: boolean;
}

export interface FixturePatternConfig {
pattern: string;
ignoreSourceType: boolean;
}

const fixturesDirPath = path.join(__dirname, '../fixtures');

export class FixturesTester {
protected babelFixtures: FixturePatternConfig[] = [];
protected espreeFixtures: FixturePatternConfig[] = [];

constructor() {}

public registerTest(
fixturesSubPath: string,
config: CreateFixturePatternConfig = {}
) {
if (!fs.existsSync(path.join(fixturesDirPath, fixturesSubPath))) {
throw new Error(
`Registered path '${path.join(
__dirname,
fixturesSubPath
)}' was not found`
);
}

const ignoreBabel = config.ignoreBabel || [];
const ignoreEspree = config.ignoreEspree || [];
const fileType = config.fileType || 'js';
const ignoreSourceType = config.ignoreSourceType || [];

if (fileType === 'js' || fileType === 'jsx') {
this.espreeFixtures.push({
pattern: `${fixturesSubPath}/!(${ignoreEspree.join(
'|'
)}).src.${fileType}`,
ignoreSourceType: false
});
}

/**
* https://github.com/babel/babel/issues/9213
*/
if (ignoreSourceType.length) {
ignoreBabel.push(...ignoreSourceType);
for (const fixture of ignoreSourceType) {
this.babelFixtures.push({
// It needs to be the full path from within fixtures/ for the pattern
pattern: `${fixturesSubPath}/${fixture}.src.${config.fileType}`,
ignoreSourceType: true
});
}
}

this.babelFixtures.push({
pattern: `${fixturesSubPath}/!(${ignoreBabel.join('|')}).src.${fileType}`,
ignoreSourceType: false
});
}

protected processFixtures(fixtures: FixturePatternConfig[]): Fixture[] {
return fixtures
.map(fixtures => {
return glob
.sync(`${fixturesDirPath}/${fixtures.pattern}`, {})
.map(filename => {
return {
filename,
ignoreSourceType: fixtures.ignoreSourceType
};
});
})
.reduce((acc, x) => acc.concat(x), []);
}

public getForBabel(): Fixture[] {
return this.processFixtures(this.babelFixtures);
}

public getForEspree(): Fixture[] {
return this.processFixtures(this.espreeFixtures);
}
}
Loading