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

Skip to content

Commit 4019a17

Browse files
Added version filtering to ast-spec and scope-manager fixtures
1 parent c27c67d commit 4019a17

File tree

10 files changed

+183
-86
lines changed

10 files changed

+183
-86
lines changed

packages/ast-spec/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@
4848
"@babel/parser": "*",
4949
"@microsoft/api-extractor": "^7.23.2",
5050
"@types/babel__core": "*",
51+
"eslint": "*",
5152
"glob": "*",
5253
"jest-diff": "*",
5354
"jest-snapshot": "*",
5455
"jest-specific-snapshot": "*",
5556
"make-dir": "*",
5657
"pretty-format": "*",
58+
"semver": "*",
5759
"typescript": "*"
5860
}
5961
}

packages/ast-spec/tests/fixtures.test.ts

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { ESLint } from 'eslint';
12
import fs from 'fs';
23
import glob from 'glob';
34
import makeDir from 'make-dir';
45
import path from 'path';
6+
import * as semver from 'semver';
7+
import * as ts from 'typescript';
58

69
import { parseBabel } from './util/parsers/babel';
710
import type {
@@ -44,24 +47,53 @@ const ERROR_FIXTURES: readonly string[] = glob.sync(
4447
`${SRC_DIR}/**/fixtures/_error_/*/fixture.{ts,tsx}`,
4548
);
4649

47-
const FIXTURES: readonly Fixture[] = [...VALID_FIXTURES, ...ERROR_FIXTURES].map(
48-
absolute => {
50+
function loadConfig(configPath: string): ASTFixtureConfig {
51+
try {
52+
return require(configPath).default;
53+
} catch {
54+
return {};
55+
}
56+
}
57+
58+
function versionsSemverExcluded(
59+
versions: ASTFixtureVersionsConfig | undefined,
60+
): boolean {
61+
if (!versions) {
62+
return false;
63+
}
64+
65+
if (versions.eslint && !semver.satisfies(ESLint.version, versions.eslint)) {
66+
return true;
67+
}
68+
69+
if (
70+
versions.typescript &&
71+
!semver.satisfies(ts.version, versions.typescript)
72+
) {
73+
return true;
74+
}
75+
76+
return false;
77+
}
78+
79+
const FIXTURES: readonly Fixture[] = [...VALID_FIXTURES, ...ERROR_FIXTURES]
80+
.map(absolute => {
4981
const relativeToSrc = path.relative(SRC_DIR, absolute);
5082
const { dir, ext } = path.parse(relativeToSrc);
5183
const segments = dir.split(path.sep).filter(s => s !== 'fixtures');
5284
const name = segments.pop()!;
5385
const fixtureDir = path.join(SRC_DIR, dir);
5486
const configPath = path.join(fixtureDir, 'config' /* .ts */);
5587
const snapshotPath = path.join(fixtureDir, 'snapshots');
88+
const config = loadConfig(configPath);
89+
90+
if (versionsSemverExcluded(config.versions)) {
91+
return undefined;
92+
}
93+
5694
return {
5795
absolute,
58-
config: ((): ASTFixtureConfig => {
59-
try {
60-
return require(configPath).default;
61-
} catch {
62-
return {};
63-
}
64-
})(),
96+
config,
6597
ext,
6698
isError: absolute.includes('/_error_/'),
6799
isJSX: ext.endsWith('x'),
@@ -99,8 +131,8 @@ const FIXTURES: readonly Fixture[] = [...VALID_FIXTURES, ...ERROR_FIXTURES].map(
99131
},
100132
snapshotPath,
101133
};
102-
},
103-
);
134+
})
135+
.filter((f): f is NonNullable<typeof f> => !!f);
104136

105137
function hasErrorCode(e: unknown): e is { code: unknown } {
106138
return typeof e === 'object' && e != null && 'code' in e;

packages/ast-spec/typings/global.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,17 @@ interface ASTFixtureConfig {
1111
* The value should be a description of why there isn't support - for example a github issue URL.
1212
*/
1313
readonly expectBabelToNotSupport?: string;
14+
15+
/**
16+
* Dependency version semver ranges to filter to in regression testing.
17+
*/
18+
readonly versions?: ASTFixtureVersionsConfig;
19+
}
20+
21+
/**
22+
* Dependency version semver ranges to filter to in regression testing.
23+
*/
24+
interface ASTFixtureVersionsConfig {
25+
readonly eslint?: string;
26+
readonly typescript?: string;
1427
}

packages/scope-manager/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
"devDependencies": {
4545
"@types/glob": "*",
4646
"@typescript-eslint/typescript-estree": "5.52.0",
47+
"eslint": "*",
4748
"glob": "*",
4849
"jest-specific-snapshot": "*",
4950
"make-dir": "*",
5051
"prettier": "*",
5152
"pretty-format": "*",
5253
"rimraf": "*",
54+
"semver": "*",
5355
"typescript": "*"
5456
},
5557
"funding": {

packages/scope-manager/tests/eslint-scope/references.test.ts

Lines changed: 81 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import * as semver from 'semver';
2+
import * as ts from 'typescript';
3+
14
import { getRealVariables, parseAndAnalyze } from '../util';
25

36
describe('References:', () => {
@@ -541,10 +544,11 @@ describe('References:', () => {
541544
);
542545
});
543546

544-
describe('When emitDecoratorMetadata is true', () => {
545-
it('check type referenced by decorator metadata', () => {
546-
const { scopeManager } = parseAndAnalyze(
547-
`
547+
if (semver.satisfies(ts.version, '>=4')) {
548+
describe('When emitDecoratorMetadata is true', () => {
549+
it('check type referenced by decorator metadata', () => {
550+
const { scopeManager } = parseAndAnalyze(
551+
`
548552
@deco
549553
class A {
550554
property: Type1;
@@ -585,75 +589,78 @@ describe('References:', () => {
585589
foo(): TypeC;
586590
}
587591
`,
588-
{
589-
emitDecoratorMetadata: true,
590-
},
591-
);
592-
593-
const classAScope = scopeManager.globalScope!.childScopes[0];
594-
const propertyTypeRef = classAScope.references[2];
595-
expect(propertyTypeRef.identifier.name).toBe('a');
596-
expect(propertyTypeRef.isTypeReference).toBe(true);
597-
expect(propertyTypeRef.isValueReference).toBe(true);
598-
599-
const setterParamTypeRef = classAScope.childScopes[0].references[0];
600-
expect(setterParamTypeRef.identifier.name).toBe('SetterType');
601-
expect(setterParamTypeRef.isTypeReference).toBe(true);
602-
expect(setterParamTypeRef.isValueReference).toBe(false);
603-
604-
const constructorParamTypeRef = classAScope.childScopes[1].references[0];
605-
expect(constructorParamTypeRef.identifier.name).toBe('b');
606-
expect(constructorParamTypeRef.isTypeReference).toBe(true);
607-
expect(constructorParamTypeRef.isValueReference).toBe(true);
608-
609-
const methodParamTypeRef = classAScope.childScopes[2].references[0];
610-
expect(methodParamTypeRef.identifier.name).toBe('Type2');
611-
expect(methodParamTypeRef.isTypeReference).toBe(true);
612-
expect(methodParamTypeRef.isValueReference).toBe(true);
613-
const methodParamTypeRef0 = classAScope.childScopes[2].references[2];
614-
expect(methodParamTypeRef0.identifier.name).toBe('Type0');
615-
expect(methodParamTypeRef0.isTypeReference).toBe(true);
616-
expect(methodParamTypeRef0.isValueReference).toBe(true);
617-
618-
const methodParamTypeRef1 = classAScope.childScopes[3].references[0];
619-
expect(methodParamTypeRef1.identifier.name).toBe('Type3');
620-
expect(methodParamTypeRef1.isTypeReference).toBe(true);
621-
expect(methodParamTypeRef1.isValueReference).toBe(true);
622-
623-
const methodReturnTypeRef = classAScope.childScopes[4].references[0];
624-
expect(methodReturnTypeRef.identifier.name).toBe('Type4');
625-
expect(methodReturnTypeRef.isTypeReference).toBe(true);
626-
expect(methodReturnTypeRef.isValueReference).toBe(true);
627-
628-
const setterParamTypeRef1 = classAScope.childScopes[5].references[0];
629-
expect(setterParamTypeRef1.identifier.name).toBe('Type5');
630-
expect(setterParamTypeRef1.isTypeReference).toBe(true);
631-
expect(setterParamTypeRef1.isValueReference).toBe(true);
632-
633-
const setterParamTypeRef2 = classAScope.childScopes[6].references[0];
634-
expect(setterParamTypeRef2.identifier.name).toBe('Type6');
635-
expect(setterParamTypeRef2.isTypeReference).toBe(true);
636-
expect(setterParamTypeRef2.isValueReference).toBe(true);
637-
638-
const classBScope = scopeManager.globalScope!.childScopes[1];
639-
640-
const constructorParamTypeRef1 = classBScope.childScopes[0].references[0];
641-
expect(constructorParamTypeRef1.identifier.name).toBe('c');
642-
expect(constructorParamTypeRef1.isTypeReference).toBe(true);
643-
expect(constructorParamTypeRef1.isValueReference).toBe(true);
644-
645-
const setterParamTypeRef3 = classBScope.childScopes[1].references[0];
646-
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
647-
expect(setterParamTypeRef3.identifier.name).toBe('Type');
648-
expect(setterParamTypeRef3.isTypeReference).toBe(true);
649-
expect(setterParamTypeRef3.isValueReference).toBe(false);
650-
651-
const classCScope = scopeManager.globalScope!.childScopes[2];
652-
653-
const methodReturnTypeRef1 = classCScope.childScopes[0].references[0];
654-
expect(methodReturnTypeRef1.identifier.name).toBe('TypeC');
655-
expect(methodReturnTypeRef1.isTypeReference).toBe(true);
656-
expect(methodReturnTypeRef1.isValueReference).toBe(false);
592+
{
593+
emitDecoratorMetadata: true,
594+
},
595+
);
596+
597+
const classAScope = scopeManager.globalScope!.childScopes[0];
598+
const propertyTypeRef = classAScope.references[2];
599+
expect(propertyTypeRef.identifier.name).toBe('a');
600+
expect(propertyTypeRef.isTypeReference).toBe(true);
601+
expect(propertyTypeRef.isValueReference).toBe(true);
602+
603+
const setterParamTypeRef = classAScope.childScopes[0].references[0];
604+
expect(setterParamTypeRef.identifier.name).toBe('SetterType');
605+
expect(setterParamTypeRef.isTypeReference).toBe(true);
606+
expect(setterParamTypeRef.isValueReference).toBe(false);
607+
608+
const constructorParamTypeRef =
609+
classAScope.childScopes[1].references[0];
610+
expect(constructorParamTypeRef.identifier.name).toBe('b');
611+
expect(constructorParamTypeRef.isTypeReference).toBe(true);
612+
expect(constructorParamTypeRef.isValueReference).toBe(true);
613+
614+
const methodParamTypeRef = classAScope.childScopes[2].references[0];
615+
expect(methodParamTypeRef.identifier.name).toBe('Type2');
616+
expect(methodParamTypeRef.isTypeReference).toBe(true);
617+
expect(methodParamTypeRef.isValueReference).toBe(true);
618+
const methodParamTypeRef0 = classAScope.childScopes[2].references[2];
619+
expect(methodParamTypeRef0.identifier.name).toBe('Type0');
620+
expect(methodParamTypeRef0.isTypeReference).toBe(true);
621+
expect(methodParamTypeRef0.isValueReference).toBe(true);
622+
623+
const methodParamTypeRef1 = classAScope.childScopes[3].references[0];
624+
expect(methodParamTypeRef1.identifier.name).toBe('Type3');
625+
expect(methodParamTypeRef1.isTypeReference).toBe(true);
626+
expect(methodParamTypeRef1.isValueReference).toBe(true);
627+
628+
const methodReturnTypeRef = classAScope.childScopes[4].references[0];
629+
expect(methodReturnTypeRef.identifier.name).toBe('Type4');
630+
expect(methodReturnTypeRef.isTypeReference).toBe(true);
631+
expect(methodReturnTypeRef.isValueReference).toBe(true);
632+
633+
const setterParamTypeRef1 = classAScope.childScopes[5].references[0];
634+
expect(setterParamTypeRef1.identifier.name).toBe('Type5');
635+
expect(setterParamTypeRef1.isTypeReference).toBe(true);
636+
expect(setterParamTypeRef1.isValueReference).toBe(true);
637+
638+
const setterParamTypeRef2 = classAScope.childScopes[6].references[0];
639+
expect(setterParamTypeRef2.identifier.name).toBe('Type6');
640+
expect(setterParamTypeRef2.isTypeReference).toBe(true);
641+
expect(setterParamTypeRef2.isValueReference).toBe(true);
642+
643+
const classBScope = scopeManager.globalScope!.childScopes[1];
644+
645+
const constructorParamTypeRef1 =
646+
classBScope.childScopes[0].references[0];
647+
expect(constructorParamTypeRef1.identifier.name).toBe('c');
648+
expect(constructorParamTypeRef1.isTypeReference).toBe(true);
649+
expect(constructorParamTypeRef1.isValueReference).toBe(true);
650+
651+
const setterParamTypeRef3 = classBScope.childScopes[1].references[0];
652+
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
653+
expect(setterParamTypeRef3.identifier.name).toBe('Type');
654+
expect(setterParamTypeRef3.isTypeReference).toBe(true);
655+
expect(setterParamTypeRef3.isValueReference).toBe(false);
656+
657+
const classCScope = scopeManager.globalScope!.childScopes[2];
658+
659+
const methodReturnTypeRef1 = classCScope.childScopes[0].references[0];
660+
expect(methodReturnTypeRef1.identifier.name).toBe('TypeC');
661+
expect(methodReturnTypeRef1.isTypeReference).toBe(true);
662+
expect(methodReturnTypeRef1.isValueReference).toBe(false);
663+
});
657664
});
658-
});
665+
}
659666
});

packages/scope-manager/tests/fixtures.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { ESLint } from 'eslint';
12
import fs from 'fs';
23
import glob from 'glob';
34
import makeDir from 'make-dir';
45
import path from 'path';
6+
import * as semver from 'semver';
7+
import * as ts from 'typescript';
58

69
import type { AnalyzeOptions } from './util';
710
import { parseAndAnalyze } from './util';
@@ -25,8 +28,10 @@ const fixtures = glob
2528
const { name, dir, ext } = path.parse(relative);
2629
const segments = dir.split(path.sep);
2730
const snapshotPath = path.join(FIXTURES_DIR, dir);
31+
const configPath = path.join(snapshotPath, `${name}.config.json`);
2832
return {
2933
absolute,
34+
configPath: fs.existsSync(configPath) ? configPath : undefined,
3035
name,
3136
ext,
3237
segments,
@@ -51,6 +56,27 @@ const ALLOWED_OPTIONS: Map<string, ALLOWED_VALUE> = new Map<
5156
['emitDecoratorMetadata', ['boolean']],
5257
]);
5358

59+
interface FixtureConfig {
60+
eslint?: string;
61+
typescript?: string;
62+
}
63+
64+
function shouldSkipFixture(configPath: string): boolean {
65+
const config = JSON.parse(
66+
fs.readFileSync(configPath, 'utf8').toString(),
67+
) as FixtureConfig;
68+
69+
if (config.eslint && !semver.satisfies(ESLint.version, config.eslint)) {
70+
return true;
71+
}
72+
73+
if (config.typescript && !semver.satisfies(ts.version, config.typescript)) {
74+
return true;
75+
}
76+
77+
return false;
78+
}
79+
5480
function nestDescribe(
5581
fixture: typeof fixtures[number],
5682
segments = fixture.segments,
@@ -164,7 +190,10 @@ function nestDescribe(
164190
}
165191
};
166192

167-
if ([...fixture.segments, fixture.name].join(path.sep) === ONLY) {
193+
if (fixture.configPath && shouldSkipFixture(fixture.configPath)) {
194+
// eslint-disable-next-line jest/no-disabled-tests
195+
it.skip(fixture.name, test);
196+
} else if ([...fixture.segments, fixture.name].join(path.sep) === ONLY) {
168197
// eslint-disable-next-line jest/no-focused-tests
169198
it.only(fixture.name, test);
170199
} else {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript": ">=4.9"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript": ">=4.9"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript": ">=4.9"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript": ">=4.9"
3+
}

0 commit comments

Comments
 (0)