Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78b0e17

Browse files
committed
feat: add automatic deletion to the script
1 parent 19b1753 commit 78b0e17

File tree

4 files changed

+110
-42
lines changed

4 files changed

+110
-42
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@
7474
"ts-jest": "^24.0.0",
7575
"ts-node": "^8.3.0",
7676
"tslint": "^5.19.0",
77-
"typescript": ">=3.2.1 <3.8.0 || >3.7.0-dev.0"
77+
"typescript": ">=3.2.1 <3.8.0 || >3.7.1-rc"
7878
},
7979
"collective": {
8080
"type": "opencollective",
8181
"url": "https://opencollective.com/typescript-eslint"
8282
},
8383
"resolutions": {
84-
"typescript": "^3.7.0-dev.20191018"
84+
"typescript": "^3.7.1-rc"
8585
}
8686
}

packages/shared-fixtures/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
},
1212
"devDependencies": {
1313
"@types/mkdirp": "^0.5.2",
14-
"mkdirp": "^0.5.1"
14+
"@types/rimraf": "^2.0.3",
15+
"chalk": "^2.4.2",
16+
"mkdirp": "^0.5.1",
17+
"rimraf": "^3.0.0"
1518
}
1619
}

packages/shared-fixtures/tools/generate-tests.ts

Lines changed: 91 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,130 @@
1+
import chalk from 'chalk';
12
import fs from 'fs';
23
import mkdirp from 'mkdirp';
34
import path from 'path';
5+
import rimraf from 'rimraf';
46
import jsxKnownIssues from '../jsx-known-issues';
57

68
const jsxFilesWithKnownIssues = jsxKnownIssues.map(f => `${f}.src.js`);
9+
const forceOverwrite = process.argv.includes('--force');
10+
11+
type PackageName = 'parser' | 'typescript-estree';
12+
// set built up over time listing the fixtures that were written
13+
const writtenFixtures: Readonly<Record<PackageName, Set<string>>> = {
14+
parser: new Set(),
15+
'typescript-estree': new Set(),
16+
};
17+
18+
const TEST_NAMES = {
19+
WITH_LOCATION_INFO: 'withLocationInfo.test.ts',
20+
WITHOUT_LOCATION_INFO: 'withoutLocationInfo.test.ts',
21+
};
722

823
function main(): void {
24+
// write new fixtures this also works to build the list of fixtures that should exist,
25+
// so that we can later check which fixtures have been deleted.
926
const fixtureFolder = 'fixtures';
1027
const fixtureGroups = fs.readdirSync(fixtureFolder);
1128
fixtureGroups.forEach(groupName => {
1229
const groupFolder = path.join(fixtureFolder, groupName);
13-
handleGroup(groupFolder);
30+
handleGroup(groupFolder, fixturePath => {
31+
if (
32+
!jsxFilesWithKnownIssues.some(jsxName => fixturePath.endsWith(jsxName))
33+
) {
34+
createTest(fixturePath, 'typescript-estree');
35+
createTest(fixturePath, 'parser');
36+
}
37+
});
1438
});
1539

16-
function handleGroup(groupFolder: string): void {
17-
const fixtures = fs.readdirSync(groupFolder);
18-
fixtures.forEach(fileOrSubGroupName => {
19-
const fileOrSubGroupPath = path.join(groupFolder, fileOrSubGroupName);
20-
if (fs.statSync(fileOrSubGroupPath).isDirectory()) {
21-
handleGroup(fileOrSubGroupPath);
22-
} else if (
23-
!jsxFilesWithKnownIssues.some(jsxName =>
24-
fileOrSubGroupPath.endsWith(jsxName),
40+
// cleanup deleted fixtures
41+
const removeTestRegEx = new RegExp(
42+
`[\\/\\\\]${TEST_NAMES.WITH_LOCATION_INFO}`,
43+
);
44+
(Object.keys(writtenFixtures) as PackageName[]).forEach(packageName => {
45+
const generatedTestDir = getPackageDirectory(packageName);
46+
handleGroup(generatedTestDir, fixtureFilePath => {
47+
if (!fixtureFilePath.endsWith(TEST_NAMES.WITH_LOCATION_INFO)) {
48+
return;
49+
}
50+
51+
const fixtureDir = fixtureFilePath.replace(removeTestRegEx, '');
52+
if (
53+
!writtenFixtures[packageName].has(
54+
fixtureFilePath.replace(removeTestRegEx, ''),
2555
)
2656
) {
27-
createTest(fileOrSubGroupPath, 'typescript-estree');
28-
createTest(fileOrSubGroupPath, 'parser');
57+
rimraf.sync(fixtureDir);
58+
console.log(
59+
chalk.red('deleted test:'),
60+
path.relative(__dirname, fixtureDir).replace(/\.\.\//g, ''),
61+
);
2962
}
3063
});
31-
}
64+
});
3265
}
3366

34-
function createTest(
35-
fixturePath: string,
36-
moduleName: 'parser' | 'typescript-estree',
67+
function handleGroup(
68+
groupFolder: string,
69+
callback: (fixturePath: string) => void,
3770
): void {
71+
const fixtures = fs.readdirSync(groupFolder);
72+
fixtures.forEach(fileOrSubGroupName => {
73+
const fileOrSubGroupPath = path.join(groupFolder, fileOrSubGroupName);
74+
if (fs.statSync(fileOrSubGroupPath).isDirectory()) {
75+
handleGroup(fileOrSubGroupPath, callback);
76+
} else {
77+
callback(fileOrSubGroupPath);
78+
}
79+
});
80+
}
81+
82+
function getPackageDirectory(packageName: PackageName): string {
83+
return path.resolve(
84+
__dirname,
85+
'..',
86+
'..',
87+
packageName,
88+
'tests',
89+
'shared-fixtures',
90+
);
91+
}
92+
93+
function createTest(fixturePath: string, packageName: PackageName): void {
3894
const fixtureWithoutExt = fixturePath
3995
// fixtures all end in /.src.[jt]sx?/
4096
.substring(0, fixturePath.indexOf('.'))
4197
// mark them as generated
4298
.replace('fixtures/', 'generated/');
4399

44100
const testDir = path.resolve(
45-
__dirname,
46-
'..',
47-
'..',
48-
moduleName,
49-
'tests',
50-
'shared-fixtures',
101+
getPackageDirectory(packageName),
51102
fixtureWithoutExt,
52103
);
53104

54105
// create the folder and all parent folders
55106
mkdirp.sync(testDir);
107+
writtenFixtures[packageName].add(testDir);
56108

57109
// create test files
58-
fs.writeFileSync(
59-
path.join(testDir, 'withLocationInfo.test.ts'),
60-
testContents(fixturePath, 'With'),
61-
'utf8',
62-
);
110+
function writeTestFile(type: 'With' | 'Without'): void {
111+
const testFile = path.join(
112+
testDir,
113+
type === 'With'
114+
? TEST_NAMES.WITH_LOCATION_INFO
115+
: TEST_NAMES.WITHOUT_LOCATION_INFO,
116+
);
117+
if (forceOverwrite || !fs.existsSync(testFile)) {
118+
fs.writeFileSync(testFile, testContents(fixturePath, type), 'utf8');
119+
console.log(
120+
chalk.green('wrote test:'),
121+
path.relative(__dirname, testFile).replace(/\.\.\//g, ''),
122+
);
123+
}
124+
}
125+
writeTestFile('With');
63126
// TODO - waiting for https://github.com/typescript-eslint/typescript-eslint/pull/704
64-
// fs.writeFileSync(
65-
// path.join(testDir, 'withoutLocationInfo.test.ts'),
66-
// testContents(fixturePath, 'Without'),
67-
// 'utf8',
68-
// );
69-
70-
console.log('wrote tests:', path.relative(__dirname, testDir));
127+
// writeTestFile('Without');
71128
}
72129

73130
const testContents = (

yarn.lock

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@
13441344
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
13451345
integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==
13461346

1347-
"@types/glob@^7.1.1":
1347+
"@types/glob@*", "@types/glob@^7.1.1":
13481348
version "7.1.1"
13491349
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575"
13501350
integrity sha512-1Bh06cbWJUHMC97acuD6UMG29nMt0Aqz1vF3guLfG+kHHJhy3AyohZFFxYk2f7Q1SQIrNwvncxAE0N/9s70F2w==
@@ -1453,6 +1453,14 @@
14531453
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-1.18.3.tgz#64ff53329ce16139f17c3db9d3e0487199972cd8"
14541454
integrity sha512-48rnerQdcZ26odp+HOvDGX8IcUkYOCuMc2BodWYTe956MqkHlOGAG4oFQ83cjZ0a4GAgj7mb4GUClxYd2Hlodg==
14551455

1456+
"@types/rimraf@^2.0.3":
1457+
version "2.0.3"
1458+
resolved "https://registry.yarnpkg.com/@types/rimraf/-/rimraf-2.0.3.tgz#0199a46af106729ba14213fda7b981278d8c84f2"
1459+
integrity sha512-dZfyfL/u9l/oi984hEXdmAjX3JHry7TLWw43u1HQ8HhPv6KtfxnrZ3T/bleJ0GEvnk9t5sM7eePkgMqz3yBcGg==
1460+
dependencies:
1461+
"@types/glob" "*"
1462+
"@types/node" "*"
1463+
14561464
"@types/semver@^6.0.1":
14571465
version "6.0.2"
14581466
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-6.0.2.tgz#5e8b09f0e4af53034b1d0fb9977a277847836205"
@@ -7722,10 +7730,10 @@ typedarray@^0.0.6:
77227730
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
77237731
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
77247732

7725-
typescript@*, "typescript@>=3.2.1 <3.8.0 || >3.7.0-dev.0", typescript@^3.7.0-dev.20191018:
7726-
version "3.7.0-dev.20191021"
7727-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.0-dev.20191021.tgz#e0238e0b3eed9fc265767a1b7f5346fea8ab5edb"
7728-
integrity sha512-SSx/+QkyW7PMcaGQXzVmVkrRmmaLFsdOYXhP9sY9eYMiHrfmtZE9EL2hjtbihfnpyWfCmPup69VgbB4dTTEQgg==
7733+
typescript@*, "typescript@>=3.2.1 <3.8.0 || >3.7.1-rc":
7734+
version "3.6.4"
7735+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.4.tgz#b18752bb3792bc1a0281335f7f6ebf1bbfc5b91d"
7736+
integrity sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==
77297737

77307738
uglify-js@^3.1.4:
77317739
version "3.6.0"

0 commit comments

Comments
 (0)