Skip to content

Commit 21310e0

Browse files
committed
fix: reuse eslint mapping for rslint templates
1 parent 6143d5b commit 21310e0

2 files changed

Lines changed: 88 additions & 3 deletions

File tree

src/index.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,19 @@ export type RslintTemplateName =
339339
| 'react-js'
340340
| 'react-ts';
341341

342+
const rslintTemplateNames = new Set<string>([
343+
'vanilla-js',
344+
'vanilla-ts',
345+
'react-js',
346+
'react-ts',
347+
]);
348+
349+
function isRslintTemplateName(
350+
templateName: ESLintTemplateName | null,
351+
): templateName is RslintTemplateName {
352+
return templateName !== null && rslintTemplateNames.has(templateName);
353+
}
354+
342355
const readJSON = async (path: string) =>
343356
JSON.parse(await fs.promises.readFile(path, 'utf-8'));
344357

@@ -787,9 +800,18 @@ export async function create({
787800
}
788801

789802
if (tool === 'rslint') {
790-
const rslintTemplateName = mapRslintTemplate
791-
? mapRslintTemplate(templateName, { distFolder })
792-
: 'vanilla-ts';
803+
let rslintTemplateName: RslintTemplateName | null;
804+
805+
if (mapRslintTemplate) {
806+
rslintTemplateName = mapRslintTemplate(templateName, { distFolder });
807+
} else {
808+
const eslintTemplateName = mapESLintTemplate
809+
? mapESLintTemplate(templateName, { distFolder })
810+
: null;
811+
rslintTemplateName = isRslintTemplateName(eslintTemplateName)
812+
? eslintTemplateName
813+
: 'vanilla-ts';
814+
}
793815

794816
if (!rslintTemplateName) {
795817
continue;

test/cli.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,69 @@ test('should scaffold mapped rslint tool template', async () => {
110110
expect(fs.existsSync(path.join(projectDir, 'react-ts'))).toBe(false);
111111
});
112112

113+
test('should reuse eslint template mapping for rslint when rslint mapping is not provided', async () => {
114+
const projectDir = path.join(testDir, 'rslint-tool-with-eslint-mapping');
115+
116+
await create({
117+
name: 'test',
118+
root: fixturesDir,
119+
templates: ['vanilla'],
120+
getTemplateName: async () => 'vanilla',
121+
mapESLintTemplate: () => 'react-js',
122+
argv: [
123+
'node',
124+
'test',
125+
'--dir',
126+
projectDir,
127+
'--template',
128+
'vanilla',
129+
'--tools',
130+
'rslint',
131+
],
132+
});
133+
134+
const config = fs.readFileSync(
135+
path.join(projectDir, 'rslint.config.ts'),
136+
'utf-8',
137+
);
138+
139+
expect(config).toContain('reactPlugin.configs.recommended');
140+
expect(config).not.toContain('ts.configs.recommended');
141+
});
142+
143+
test('should fallback to vanilla-ts when eslint mapping cannot be reused for rslint', async () => {
144+
const projectDir = path.join(
145+
testDir,
146+
'rslint-tool-with-unsupported-eslint-mapping',
147+
);
148+
149+
await create({
150+
name: 'test',
151+
root: fixturesDir,
152+
templates: ['vanilla'],
153+
getTemplateName: async () => 'vanilla',
154+
mapESLintTemplate: () => 'vue-ts',
155+
argv: [
156+
'node',
157+
'test',
158+
'--dir',
159+
projectDir,
160+
'--template',
161+
'vanilla',
162+
'--tools',
163+
'rslint',
164+
],
165+
});
166+
167+
const config = fs.readFileSync(
168+
path.join(projectDir, 'rslint.config.ts'),
169+
'utf-8',
170+
);
171+
172+
expect(config).toContain('ts.configs.recommended');
173+
expect(config).not.toContain('reactPlugin.configs.recommended');
174+
});
175+
113176
test('should skip tools selection', async () => {
114177
const projectDir = path.join(testDir, 'comma-separated-tools');
115178

0 commit comments

Comments
 (0)