Skip to content

Commit

Permalink
Restrict compatibility to AVA 6
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed May 5, 2024
1 parent 41d8c6b commit 1ae15b2
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 319 deletions.
183 changes: 76 additions & 107 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: pkg.version});
if (protocol === null) {
return;
}
Expand All @@ -100,141 +100,110 @@ export default function typescriptProvider({negotiateProtocol}) {
]);
const testFileExtension = new RegExp(`\\.(${extensions.map(ext => escapeStringRegexp(ext)).join('|')})$`);

const watchMode = protocol.identifier === 'ava-3.2'
? {
ignoreChange(filePath) {
if (!testFileExtension.test(filePath)) {
return false;
}

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

resolveTestFile(testfile) { // Used under AVA 3.2 protocol by legacy watcher implementation.
if (!testFileExtension.test(testfile)) {
return testfile;
}

const rewrite = rewritePaths.find(([from]) => testfile.startsWith(from));
if (rewrite === undefined) {
return testfile;
}

const [from, to] = rewrite;
let newExtension = '.js';
if (testfile.endsWith('.cts')) {
newExtension = '.cjs';
} else if (testfile.endsWith('.mts')) {
newExtension = '.mjs';
}

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 watchMode = {
changeInterpretations,
interpretChange(filePath) {
if (config.compile === false) {
for (const [from] of rewritePaths) {
if (testFileExtension.test(filePath) && filePath.startsWith(from)) {
return changeInterpretations.waitForOutOfBandCompilation;
}
}
}

if (config.compile === 'tsc') {
for (const [, to] of rewritePaths) {
if (filePath.startsWith(to)) {
return changeInterpretations.ignoreCompiled;
}
if (config.compile === 'tsc') {
for (const [, to] of rewritePaths) {
if (filePath.startsWith(to)) {
return changeInterpretations.ignoreCompiled;
}
}
}

return changeInterpretations.unspecified;
},

resolvePossibleOutOfBandCompilationSources(filePath) {
if (config.compile !== false) {
return null;
}
return changeInterpretations.unspecified;
},

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

for (const [from, to] of rewritePaths) {
if (!filePath.startsWith(to)) {
continue;
}
// Only recognize .cjs, .mjs and .js files.
if (!/\.(c|m)?js$/.test(filePath)) {
return null;
}

const rewritten = `${from}${filePath.slice(to.length)}`;
const possibleExtensions = [];
for (const [from, to] of rewritePaths) {
if (!filePath.startsWith(to)) {
continue;
}

if (filePath.endsWith('.cjs')) {
if (extensions.includes('cjs')) {
possibleExtensions.push({replace: /\.cjs$/, extension: 'cjs'});
}
const rewritten = `${from}${filePath.slice(to.length)}`;
const possibleExtensions = [];

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

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

if (filePath.endsWith('.mjs')) {
if (extensions.includes('mjs')) {
possibleExtensions.push({replace: /\.mjs$/, extension: 'mjs'});
}
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 (possibleExtensions.length === 0) {
return null;
}
if (extensions.includes('mts')) {
possibleExtensions.push({replace: /\.mjs$/, extension: 'mts'});
}

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

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
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
2 changes: 1 addition & 1 deletion test/fixtures/install-and-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));

const provider = makeProvider({
negotiateProtocol() {
return {identifier: 'ava-3.2', ava: {version: '3.15.0'}, projectDir: __dirname};
return {identifier: 'ava-6', ava: {version: '6.0.0'}, projectDir: __dirname};
},
});

Expand Down
2 changes: 1 addition & 1 deletion test/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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 withProvider = createProviderMacro('ava-6', '6.0.0', path.join(__dirname, 'fixtures'));

const setup = async provider => ({
state: await provider.main({
Expand Down
91 changes: 0 additions & 91 deletions test/protocol-ava-3.2.js

This file was deleted.

0 comments on commit 1ae15b2

Please sign in to comment.