Skip to content

Commit 9129fe0

Browse files
authored
feat(lint): inject lint exports into config factory (#352)
1 parent 2024c93 commit 9129fe0

4 files changed

Lines changed: 24 additions & 11 deletions

File tree

packages/rstack/src/config.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import type { StagedConfig } from './staged.ts';
1111
export type RslintConfigDefinition = RslintConfig | (() => Promise<RslintConfig>);
1212
export type RspressConfigDefinition = UserConfig | UserConfigAsyncFn;
1313

14+
type RslintConfigFactory = (
15+
lint: typeof import('@rslint/core'),
16+
) => RslintConfig | Promise<RslintConfig>;
17+
1418
export type Configs = {
1519
app?: RsbuildConfigDefinition;
1620
lib?: RslibConfigDefinition;
@@ -125,10 +129,11 @@ type Define = {
125129
* Defines the Rslint config for linting.
126130
*
127131
* This config is used by the `rs lint` command.
132+
* A config factory receives the exports from `rstack/lint`.
128133
*
129134
* @see {@link https://rstack.rs/config | Rstack configuration guide}
130135
*/
131-
lint: (config: RslintConfig | (() => Promise<RslintConfig>)) => void;
136+
lint: (config: RslintConfig | RslintConfigFactory) => void;
132137
/**
133138
* Defines the Prettier config for formatting.
134139
*
@@ -165,7 +170,11 @@ export const define: Define = {
165170
lib: (config) => setConfig('lib', config),
166171
doc: (config) => setConfig('doc', config),
167172
test: (config) => setConfig('test', config),
168-
lint: (config) => setConfig('lint', config),
173+
lint: (config) =>
174+
setConfig(
175+
'lint',
176+
typeof config === 'function' ? async () => config(await import('@rslint/core')) : config,
177+
),
169178
fmt: (config) => setConfig('fmt', config),
170179
staged: (config) => setConfig('staged', config),
171180
};

packages/rstack/src/rslintConfig.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { loadRstackConfig } from './config.ts';
22
import type { RslintConfig } from '@rslint/core';
33

44
const { configs } = await loadRstackConfig();
5-
const lintExports = configs.lint ?? [];
5+
const lintDefinition = configs.lint ?? [];
66

77
let lintConfig: RslintConfig;
88

99
// TODO: support function in Rslint core
10-
if (typeof lintExports === 'function') {
11-
lintConfig = await lintExports();
10+
if (typeof lintDefinition === 'function') {
11+
lintConfig = await lintDefinition();
1212
} else {
13-
lintConfig = lintExports;
13+
lintConfig = lintDefinition;
1414
}
1515

1616
export default lintConfig;

packages/rstack/tests/types/resolution-bundler/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import {
1111
type LoadRstackConfigOptions,
1212
} from 'rstack/config';
1313
import { defineConfig as defineLibConfig } from 'rstack/lib';
14-
import { js, ts } from 'rstack/lint';
14+
import { defineConfig as defineLintConfig } from 'rstack/lint';
1515
import { expect as importedExpect, test as importedTest } from 'rstack/test';
1616

1717
const appConfig = defineAppConfig({});
1818
const libConfig = defineLibConfig({});
19+
const lintConfig = defineLintConfig([]);
1920
const loadOptions: LoadRstackConfigOptions = { configFilePath: 'rstack.config.ts' };
2021
const loadedConfig: Promise<LoadedRstackConfig> = loadRstackConfig(loadOptions);
2122
const configs: Configs = {};
@@ -26,9 +27,10 @@ void configs;
2627
void createRsbuild({ config: appConfig });
2728
define.app(appConfig);
2829
define.lib(libConfig);
30+
define.lint(lintConfig);
31+
define.lint(({ js, ts }) => [js.configs.recommended, ts.configs.recommendedTypeChecked]);
2932
define.doc({});
3033
define.test({});
31-
define.lint([js.configs.recommended, ts.configs.recommended]);
3234
define.staged({});
3335

3436
importedTest('exposes the Rstest APIs', () => {

packages/rstack/tests/types/resolution-nodenext/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This folder checks Rstack's exports and APIs with NodeNext resolution.
1+
// This folder checks Rstack's exports and APIs with bundler resolution.
22
import 'rstack/test/globals';
33
import 'rstack/test/importMeta';
44
import 'rstack/types';
@@ -11,11 +11,12 @@ import {
1111
type LoadRstackConfigOptions,
1212
} from 'rstack/config';
1313
import { defineConfig as defineLibConfig } from 'rstack/lib';
14-
import { js, ts } from 'rstack/lint';
14+
import { defineConfig as defineLintConfig } from 'rstack/lint';
1515
import { expect as importedExpect, test as importedTest } from 'rstack/test';
1616

1717
const appConfig = defineAppConfig({});
1818
const libConfig = defineLibConfig({});
19+
const lintConfig = defineLintConfig([]);
1920
const loadOptions: LoadRstackConfigOptions = { configFilePath: 'rstack.config.ts' };
2021
const loadedConfig: Promise<LoadedRstackConfig> = loadRstackConfig(loadOptions);
2122
const configs: Configs = {};
@@ -26,9 +27,10 @@ void configs;
2627
void createRsbuild({ config: appConfig });
2728
define.app(appConfig);
2829
define.lib(libConfig);
30+
define.lint(lintConfig);
31+
define.lint(({ js, ts }) => [js.configs.recommended, ts.configs.recommendedTypeChecked]);
2932
define.doc({});
3033
define.test({});
31-
define.lint([js.configs.recommended, ts.configs.recommended]);
3234
define.staged({});
3335

3436
importedTest('exposes the Rstest APIs', () => {

0 commit comments

Comments
 (0)