Skip to content

Commit e91c14c

Browse files
committed
Don't run codemod on files that don't match the available types
1 parent f4d4efb commit e91c14c

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"editor.formatOnSave": true,
33
"editor.defaultFormatter": "esbenp.prettier-vscode",
4+
"editor.codeActionsOnSave": {
5+
"source.fixAll": true,
6+
"source.organizeImports": true
7+
},
48
"typescript.preferences.importModuleSpecifierEnding": "index",
59
"typescript.suggest.autoImports": true
610
}

transforms/ember-object/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ const transformer: Transform = function (
3333

3434
const userOptions = getConfig();
3535

36-
if (userOptions.type && !isFileOfType(filePath, userOptions.type)) {
37-
logger.info({
38-
filePath,
39-
info: `SKIPPED: Type mismatch, expected type '${userOptions.type}' did not match type of file`,
40-
});
36+
if (!isFileOfType(filePath, userOptions.type)) {
37+
if (userOptions.type) {
38+
logger.info({
39+
filePath,
40+
info: `SKIPPED: Type mismatch, expected type '${userOptions.type}' did not match type of file`,
41+
});
42+
}
4143
return; // status: 'skipped'
4244
}
4345

transforms/helpers/options.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
StringBooleanSchema,
88
StringFalseSchema,
99
} from './schema-helper';
10+
import { twoOrMoreMap } from './util/types';
1011

1112
const DEFAULT_DECORATOR_CONFIG = {
1213
inObjectLiterals: [],
@@ -49,20 +50,21 @@ const QuoteSchema = z.union([z.literal('single'), z.literal('double')], {
4950
},
5051
});
5152

53+
export const TYPES = [
54+
'services',
55+
'routes',
56+
'components',
57+
'controllers',
58+
] as const;
59+
5260
const TypeSchema = z.union(
53-
[
54-
z.literal('services'),
55-
z.literal('routes'),
56-
z.literal('components'),
57-
z.literal('controllers'),
58-
],
61+
twoOrMoreMap(TYPES, (type) => z.literal(type)),
5962
{
6063
errorMap: (issue, ctx) => {
6164
if (issue.code === z.ZodIssueCode.invalid_union) {
65+
const expected = TYPES.map((type) => `'${type}'`).join(' or ');
6266
return {
63-
message: `Expected 'services', 'routes', 'components', or 'controllers', received ${inspect(
64-
ctx.data
65-
)}`,
67+
message: `Expected ${expected}, received ${inspect(ctx.data)}`,
6668
};
6769
}
6870
return { message: ctx.defaultError };

transforms/helpers/util/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,14 @@ export function defined<T>(
4040
assert(value !== undefined, message);
4141
return value;
4242
}
43+
44+
/**
45+
* Wraps Array.map in a type-check that the initial array has at least two
46+
* items, then returns an array asserting that it has the same number of items.
47+
*/
48+
export function twoOrMoreMap<T, U>(
49+
array: readonly [T, T, ...T[]],
50+
callbackfn: (value: T, index: number, array: readonly T[]) => U
51+
): [U, U, ...U[]] {
52+
return array.map(callbackfn) as unknown as [U, U, ...U[]];
53+
}
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import { minimatch } from 'minimatch';
2-
import type { Options } from './options';
2+
import { TYPES, type Options } from './options';
3+
import { defined } from './util/types';
34

4-
const TYPE_PATTERNS = {
5-
service: '**/services/**/*.js',
6-
services: '**/services/**/*.js',
7-
controller: '**/controllers/**/*.js',
8-
controllers: '**/controllers/**/*.js',
9-
component: '**/components/**/*.js',
10-
components: '**/components/**/*.js',
11-
route: '**/routes/**/*.js',
12-
routes: '**/routes/**/*.js',
13-
} as const;
5+
const TYPE_PATTERNS = new Map(
6+
TYPES.map((type) => [type, `**/${type}/**/*.js`])
7+
);
8+
9+
const ANY_TYPE_PATTERN = `**/(${TYPES.join('|')})/**/*.js`;
1410

1511
const TEST_FILE_PATTERN = '**/*-test.js' as const;
1612

@@ -24,9 +20,6 @@ export function isTestFile(file: string): boolean {
2420
* The glob patterns are specified by `TYPE_PATTERNS`
2521
*/
2622
export function isFileOfType(file: string, type: Options['type']): boolean {
27-
return (
28-
// False positive
29-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
30-
!!type && !!TYPE_PATTERNS[type] && minimatch(file, TYPE_PATTERNS[type])
31-
);
23+
const pattern = type ? defined(TYPE_PATTERNS.get(type)) : ANY_TYPE_PATTERN;
24+
return minimatch(file, pattern);
3225
}

0 commit comments

Comments
 (0)