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

Restrict compatibility to AVA 6; support Node.js 22 #54

Merged
merged 6 commits into from
May 5, 2024
Merged
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
11 changes: 4 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [^14.19, ^16.15, ^18, ^20, ^21]
node-version: [^18.18, ^20.8, ^21, ^22]
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install npm@8 for Node.js 14
if: matrix.node-version == '^14.19'
run: npm install --global npm@^8
- run: npm install --no-audit
- run: npm test
- uses: codecov/codecov-action@v2
- uses: codecov/codecov-action@v4
with:
files: coverage/lcov.info
name: ${{ matrix.os }}/${{ matrix.node-version }}
211 changes: 90 additions & 121 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {pathToFileURL} from 'node:url';
import escapeStringRegexp from 'escape-string-regexp';
import {execa} from 'execa';

const pkg = JSON.parse(fs.readFileSync(new URL('package.json', import.meta.url)));
const help = `See https://github.com/avajs/typescript/blob/v${pkg.version}/README.md`;
const package_ = JSON.parse(fs.readFileSync(new URL('package.json', import.meta.url)));
const help = `See https://github.com/avajs/typescript/blob/v${package_.version}/README.md`;

function isPlainObject(x) {
return x !== null && typeof x === 'object' && Reflect.getPrototypeOf(x) === Object.prototype;
Expand Down Expand Up @@ -36,8 +36,8 @@ function validate(target, properties) {
}
}

async function compileTypeScript(projectDir) {
return execa('tsc', ['--incremental'], {preferLocal: true, cwd: projectDir});
async function compileTypeScript(projectDirectory) {
return execa('tsc', ['--incremental'], {preferLocal: true, cwd: projectDirectory});
}

const configProperties = {
Expand All @@ -62,7 +62,7 @@ const configProperties = {
isValid(extensions) {
return Array.isArray(extensions)
&& extensions.length > 0
&& extensions.every(ext => typeof ext === 'string' && ext !== '')
&& extensions.every(extension => typeof extension === 'string' && extension !== '')
&& new Set(extensions).size === extensions.length;
},
},
Expand All @@ -75,7 +75,7 @@ const changeInterpretations = Object.freeze(Object.assign(Object.create(null), {
}));

export default function typescriptProvider({negotiateProtocol}) {
const protocol = negotiateProtocol(['ava-6', 'ava-3.2'], {version: pkg.version});
const protocol = negotiateProtocol(['ava-6'], {version: package_.version});
if (protocol === null) {
return;
}
Expand All @@ -98,143 +98,112 @@ export default function typescriptProvider({negotiateProtocol}) {
path.join(protocol.projectDir, from),
path.join(protocol.projectDir, to),
]);
const testFileExtension = new RegExp(`\\.(${extensions.map(ext => escapeStringRegexp(ext)).join('|')})$`);
const testFileExtension = new RegExp(`\\.(${extensions.map(extension => escapeStringRegexp(extension)).join('|')})$`);

const watchMode = {
changeInterpretations,
interpretChange(filePath) {
if (config.compile === false) {
for (const [from] of rewritePaths) {
if (testFileExtension.test(filePath) && filePath.startsWith(from)) {
return changeInterpretations.waitForOutOfBandCompilation;
}
}
}

const watchMode = protocol.identifier === 'ava-3.2'
? {
ignoreChange(filePath) {
if (!testFileExtension.test(filePath)) {
return false;
if (config.compile === 'tsc') {
for (const [, to] of rewritePaths) {
if (filePath.startsWith(to)) {
return changeInterpretations.ignoreCompiled;
}
}
}

return rewritePaths.some(([from]) => filePath.startsWith(from));
},
return changeInterpretations.unspecified;
},

resolveTestFile(testfile) { // Used under AVA 3.2 protocol by legacy watcher implementation.
if (!testFileExtension.test(testfile)) {
return testfile;
}
resolvePossibleOutOfBandCompilationSources(filePath) {
if (config.compile !== false) {
return null;
}

const rewrite = rewritePaths.find(([from]) => testfile.startsWith(from));
if (rewrite === undefined) {
return testfile;
}
// Only recognize .cjs, .mjs and .js files.
if (!/\.(c|m)?js$/.test(filePath)) {
return null;
}

const [from, to] = rewrite;
let newExtension = '.js';
if (testfile.endsWith('.cts')) {
newExtension = '.cjs';
} else if (testfile.endsWith('.mts')) {
newExtension = '.mjs';
for (const [from, to] of rewritePaths) {
if (!filePath.startsWith(to)) {
continue;
}

return `${to}${testfile.slice(from.length)}`.replace(testFileExtension, newExtension);
},
}
: {
changeInterpretations,
interpretChange(filePath) {
if (config.compile === false) {
for (const [from] of rewritePaths) {
if (testFileExtension.test(filePath) && filePath.startsWith(from)) {
return changeInterpretations.waitForOutOfBandCompilation;
}
}
}
const rewritten = `${from}${filePath.slice(to.length)}`;
const possibleExtensions = [];

if (config.compile === 'tsc') {
for (const [, to] of rewritePaths) {
if (filePath.startsWith(to)) {
return changeInterpretations.ignoreCompiled;
}
if (filePath.endsWith('.cjs')) {
if (extensions.includes('cjs')) {
possibleExtensions.push({replace: /\.cjs$/, extension: 'cjs'});
}
}

return changeInterpretations.unspecified;
},

resolvePossibleOutOfBandCompilationSources(filePath) {
if (config.compile !== false) {
return null;
}
if (extensions.includes('cts')) {
possibleExtensions.push({replace: /\.cjs$/, extension: 'cts'});
}

// Only recognize .cjs, .mjs and .js files.
if (!/\.(c|m)?js$/.test(filePath)) {
return null;
if (possibleExtensions.length === 0) {
return null;
}
}

for (const [from, to] of rewritePaths) {
if (!filePath.startsWith(to)) {
continue;
if (filePath.endsWith('.mjs')) {
if (extensions.includes('mjs')) {
possibleExtensions.push({replace: /\.mjs$/, extension: 'mjs'});
}

const rewritten = `${from}${filePath.slice(to.length)}`;
const possibleExtensions = [];

if (filePath.endsWith('.cjs')) {
if (extensions.includes('cjs')) {
possibleExtensions.push({replace: /\.cjs$/, extension: 'cjs'});
}

if (extensions.includes('cts')) {
possibleExtensions.push({replace: /\.cjs$/, extension: 'cts'});
}

if (possibleExtensions.length === 0) {
return null;
}
if (extensions.includes('mts')) {
possibleExtensions.push({replace: /\.mjs$/, extension: 'mts'});
}

if (filePath.endsWith('.mjs')) {
if (extensions.includes('mjs')) {
possibleExtensions.push({replace: /\.mjs$/, extension: 'mjs'});
}

if (extensions.includes('mts')) {
possibleExtensions.push({replace: /\.mjs$/, extension: 'mts'});
}

if (possibleExtensions.length === 0) {
return null;
}
if (possibleExtensions.length === 0) {
return null;
}
}

if (filePath.endsWith('.js')) {
if (extensions.includes('js')) {
possibleExtensions.push({replace: /\.js$/, extension: 'js'});
}

if (extensions.includes('ts')) {
possibleExtensions.push({replace: /\.js$/, extension: 'ts'});
}
if (filePath.endsWith('.js')) {
if (extensions.includes('js')) {
possibleExtensions.push({replace: /\.js$/, extension: 'js'});
}

if (extensions.includes('tsx')) {
possibleExtensions.push({replace: /\.js$/, extension: 'tsx'});
}
if (extensions.includes('ts')) {
possibleExtensions.push({replace: /\.js$/, extension: 'ts'});
}

if (possibleExtensions.length === 0) {
return null;
}
if (extensions.includes('tsx')) {
possibleExtensions.push({replace: /\.js$/, extension: 'tsx'});
}

const possibleDeletedFiles = [];
for (const {replace, extension} of possibleExtensions) {
const possibleFilePath = rewritten.replace(replace, `.${extension}`);
if (possibleExtensions.length === 0) {
return null;
}
}

// Pick the first file path that exists.
if (fs.existsSync(possibleFilePath)) {
return [possibleFilePath];
}
const possibleDeletedFiles = [];
for (const {replace, extension} of possibleExtensions) {
const possibleFilePath = rewritten.replace(replace, `.${extension}`);

possibleDeletedFiles.push(possibleFilePath);
// Pick the first file path that exists.
if (fs.existsSync(possibleFilePath)) {
return [possibleFilePath];
}

return possibleDeletedFiles;
possibleDeletedFiles.push(possibleFilePath);
}

return null;
},
};
return possibleDeletedFiles;
}

return null;
},
};

return {
...watchMode,
Expand Down Expand Up @@ -276,21 +245,21 @@ export default function typescriptProvider({negotiateProtocol}) {

worker({extensionsToLoadAsModules, state: {extensions, rewritePaths}}) {
const importJs = extensionsToLoadAsModules.includes('js');
const testFileExtension = new RegExp(`\\.(${extensions.map(ext => escapeStringRegexp(ext)).join('|')})$`);
const testFileExtension = new RegExp(`\\.(${extensions.map(extension => escapeStringRegexp(extension)).join('|')})$`);

return {
canLoad(ref) {
return testFileExtension.test(ref) && rewritePaths.some(([from]) => ref.startsWith(from));
canLoad(reference) {
return testFileExtension.test(reference) && rewritePaths.some(([from]) => reference.startsWith(from));
},

async load(ref, {requireFn}) {
const [from, to] = rewritePaths.find(([from]) => ref.startsWith(from));
let rewritten = `${to}${ref.slice(from.length)}`;
async load(reference, {requireFn}) {
const [from, to] = rewritePaths.find(([from]) => reference.startsWith(from));
let rewritten = `${to}${reference.slice(from.length)}`;
let useImport = true;
if (ref.endsWith('.cts')) {
if (reference.endsWith('.cts')) {
rewritten = rewritten.replace(/\.cts$/, '.cjs');
useImport = false;
} else if (ref.endsWith('.mts')) {
} else if (reference.endsWith('.mts')) {
rewritten = rewritten.replace(/\.mts$/, '.mjs');
} else {
rewritten = rewritten.replace(testFileExtension, '.js');
Expand Down
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "4.1.0",
"description": "TypeScript provider for AVA",
"engines": {
"node": "^14.19 || ^16.15 || ^18 || ^20 || ^21"
"node": "^18.18 || ^20.8 || ^21 || ^22"
},
"files": [
"index.js"
Expand All @@ -24,14 +24,14 @@
},
"dependencies": {
"escape-string-regexp": "^5.0.0",
"execa": "^7.1.1"
"execa": "^8.0.1"
},
"devDependencies": {
"ava": "^5.3.1",
"c8": "^8.0.0",
"del": "^7.0.0",
"typescript": "^5.1.3",
"xo": "^0.54.2"
"ava": "^6.1.2",
"c8": "^9.1.0",
"del": "^7.1.0",
"typescript": "^5.4.5",
"xo": "^0.58.0"
},
"c8": {
"reporter": [
Expand All @@ -44,10 +44,12 @@
"files": [
"!test/broken-fixtures/**"
],
"ignoredByWatcher": [
"test/fixtures/**",
"test/broken-fixtures/**"
],
"watcher": {
"ignoreChanges": [
"test/fixtures/**",
"test/broken-fixtures/**"
]
},
"timeout": "60s"
},
"xo": {
Expand Down
10 changes: 5 additions & 5 deletions test/_with-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import {fileURLToPath} from 'node:url';
import makeProvider from '@ava/typescript';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)));
const package_ = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)));

const createProviderMacro = (identifier, avaVersion, projectDir = __dirname) => (t, run) => run(t, makeProvider({
const createProviderMacro = (identifier, avaVersion, projectDirectory = __dirname) => (t, run) => run(t, makeProvider({
negotiateProtocol(identifiers, {version}) {
t.true(identifiers.includes(identifier));
t.is(version, pkg.version);
t.is(version, package_.version);
return {
ava: {avaVersion},
identifier,
normalizeGlobPatterns: patterns => patterns,
async findFiles({patterns}) {
return patterns.map(file => path.join(projectDir, file));
return patterns.map(file => path.join(projectDirectory, file));
},
projectDir,
projectDir: projectDirectory,
};
},
}));
Expand Down
4 changes: 2 additions & 2 deletions test/compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {execaNode} from 'execa';
import createProviderMacro from './_with-provider.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const withProvider = createProviderMacro('ava-3.2', '3.2.0', path.join(__dirname, 'fixtures'));
const withAltProvider = createProviderMacro('ava-3.2', '3.2.0', path.join(__dirname, 'broken-fixtures'));
const withProvider = createProviderMacro('ava-6', '6.0.0', path.join(__dirname, 'fixtures'));
const withAltProvider = createProviderMacro('ava-6', '6.0.0', path.join(__dirname, 'broken-fixtures'));

test.before('deleting compiled files', async t => {
t.log(await deleteAsync('test/fixtures/typescript/compiled'));
Expand Down
Loading
Loading