Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: test old TypeScript version #5368

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions .github/actions/prepare-install/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ inputs:
registry-url:
description: "Define registry-url"
required: false
typescript-version:
description: "A modified TypeScript version to setup"
required: false

# outputs: - no outputs

Expand All @@ -17,6 +20,13 @@ runs:
shell: bash
run: echo ${{ github.ref }}

- if: ${{ inputs.typescript-version != null }}
run: |
cat package.json | awk '{sub(/typescript\":\ \"[0-9.]+/,"typescript\": \"${{ inputs.typescript-version }}")}1' > package.tmp.json
cp package.tmp.json package.json
shell: bash
name: Set TypeScript resolution to ${{ inputs.typescript-version }}

- name: Use Node.js ${{ inputs.node-version }}
uses: actions/setup-node@v3
with:
Expand All @@ -34,13 +44,21 @@ runs:
path: |
${{ steps.yarn-cache-dir-path.outputs.dir }}
~/.cache/Cypress
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}-${{ hashFiles('package.json') }}
restore-keys: |
${{ runner.os }}-yarn-

# if the cache was hit - this will run in <1s
- name: Install dependencies
# if the cache was hit - these will run in <1s

- if: ${{ inputs.typescript-version == null }}
name: Install dependencies (frozen)
shell: bash
run: |
yarn --ignore-engines --frozen-lockfile --ignore-scripts
yarn check-clean-workspace-after-install

- if: ${{ inputs.typescript-version != null }}
name: Install dependencies
shell: bash
run: |
yarn --ignore-engines --ignore-scripts
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ jobs:
matrix:
# just run on the oldest and latest supported versions and assume the intermediate versions are good
node-version: [12, 18]
typescript-version: [4.0.0, null]
package: [
"ast-spec",
"eslint-plugin",
Expand All @@ -136,7 +137,7 @@ jobs:
]
env:
# Added the - at the end to function as a separator to improve readability in the PR comment from the Nx cloud app
NX_CLOUD_ENV_NAME: 'Node ${{ matrix.node-version }} -'
NX_CLOUD_ENV_NAME: 'Node ${{ matrix.node-version }} TS ${{ matrix.typescript-version }}-'
COLLECT_COVERAGE: false
steps:
- name: Checkout
Expand All @@ -147,6 +148,7 @@ jobs:
uses: ./.github/actions/prepare-install
with:
node-version: ${{ env.PRIMARY_NODE_VERSION }}
typescript-version: ${{ matrix.typescript-version }}
- name: Build
uses: ./.github/actions/prepare-build

Expand Down
4 changes: 3 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@
"@typescript-eslint/types": "5.30.7",
"@typescript-eslint/typescript-estree": "5.30.7",
"eslint-scope": "^5.1.1",
"eslint-utils": "^3.0.0"
"eslint-utils": "^3.0.0",
"semver": "^7.3.7"
},
"peerDependencies": {
"eslint": "^6.0.0 || ^7.0.0 || ^8.0.0"
},
"devDependencies": {
"@types/semver": "^7.3.10",
"typescript": "*"
},
"funding": {
Expand Down
57 changes: 35 additions & 22 deletions packages/utils/src/eslint-utils/RuleTester.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as path from 'path';
import * as semver from 'semver';
import * as ts from 'typescript';
import * as TSESLint from '../ts-eslint';
import { ValidTestCase } from '../ts-eslint';

const parser = '@typescript-eslint/parser';

Expand Down Expand Up @@ -73,31 +76,32 @@ class RuleTester extends TSESLint.RuleTester {

const tests = { ...testsReadonly };

// standardize the valid tests as objects
tests.valid = tests.valid.map(test => {
if (typeof test === 'string') {
return {
code: test,
};
}
return test;
});

tests.valid = tests.valid.map(test => {
if (typeof test !== 'string') {
if (test.parser === parser) {
throw new Error(errorMessage);
}
if (!test.filename) {
tests.valid = tests.valid
// standardize the valid tests as objects
.map(test => {
if (typeof test === 'string') {
return {
...test,
filename: this.getFilename(test.parserOptions),
code: test,
};
}
}
return test;
});
tests.invalid = tests.invalid.map(test => {
return test;
})
.filter(isValidTsVersion)
.map(test => {
if (typeof test !== 'string') {
if (test.parser === parser) {
throw new Error(errorMessage);
}
if (!test.filename) {
return {
...test,
filename: this.getFilename(test.parserOptions),
};
}
}
return test;
});
tests.invalid = tests.invalid.filter(isValidTsVersion).map(test => {
if (test.parser === parser) {
throw new Error(errorMessage);
}
Expand All @@ -114,6 +118,15 @@ class RuleTester extends TSESLint.RuleTester {
}
}

function isValidTsVersion(testCase: ValidTestCase): boolean {
return (
!testCase.tsVersion ||
semver.satisfies(ts.version, `>= ${testCase.tsVersion}`, {
includePrerelease: true,
})
);
}

/**
* Simple no-op tag to mark code samples as "should not format with prettier"
* for the internal/plugin-test-formatting lint rule
Expand Down
12 changes: 9 additions & 3 deletions packages/utils/src/ts-eslint/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
SharedConfigurationSettings,
} from './Rule';

interface ValidTestCase<TOptions extends Readonly<unknown[]>> {
interface ValidTestCase<
TOptions extends Readonly<unknown[]> = Readonly<unknown[]>,
> {
/**
* Name for the test case.
* @since 8.1.0
Expand Down Expand Up @@ -46,6 +48,10 @@ interface ValidTestCase<TOptions extends Readonly<unknown[]>> {
* Settings for the test case.
*/
readonly settings?: Readonly<SharedConfigurationSettings>;
/**
* Semver of TypeScript version(s) to run against (by default, all versions).
*/
readonly tsVersion?: string;
/**
* Run this case exclusively for debugging in supported test frameworks.
* @since 7.29.0
Expand Down Expand Up @@ -73,8 +79,8 @@ interface SuggestionOutput<TMessageIds extends string> {
}

interface InvalidTestCase<
TMessageIds extends string,
TOptions extends Readonly<unknown[]>,
TMessageIds extends string = string,
TOptions extends Readonly<unknown[]> = Readonly<unknown[]>,
> extends ValidTestCase<TOptions> {
/**
* Expected errors.
Expand Down