Skip to content

Commit b7dfa19

Browse files
feat: support typescript (#213)
* feat: support typescript * chore: move `@types/webpack`, `@types/micromatch` to devDependencies * chore: move `@types/stylelint` to devDependencies
1 parent 4da99a2 commit b7dfa19

17 files changed

+4971
-1761
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/** @typedef {import('webpack').Compiler} Compiler */
2+
/** @typedef {import('./getOptions').Options} Options */
3+
/** @typedef {import('./linter').Lint} Lint */
4+
/** @typedef {import('./linter').LinterCallback} LinterCallback */
5+
/** @typedef {Partial<{timestamp:number} | number>} FileSystemInfoEntry */
6+
export default class LintDirtyModulesPlugin {
7+
/**
8+
* @param {Lint} lint
9+
* @param {Compiler} compiler
10+
* @param {Options} options
11+
*/
12+
constructor(lint: Lint, compiler: Compiler, options: Options);
13+
lint: import('./linter').Lint;
14+
compiler: import('webpack').Compiler;
15+
options: import('./getOptions').Options;
16+
startTime: number;
17+
prevTimestamps: Map<any, any>;
18+
isFirstRun: boolean;
19+
/**
20+
* @param {Compiler} compilation
21+
* @param {LinterCallback} callback
22+
* @returns {void}
23+
*/
24+
apply(compilation: Compiler, callback: LinterCallback): void;
25+
/**
26+
* @param {Map<string, number>} fileTimestamps
27+
* @param {string | ReadonlyArray<string>} glob
28+
* @returns {Array<string>}
29+
*/
30+
getChangedFiles(
31+
fileTimestamps: Map<string, number>,
32+
glob: string | ReadonlyArray<string>
33+
): Array<string>;
34+
}
35+
export type Compiler = import('webpack').Compiler;
36+
export type Options = {
37+
context?: string | undefined;
38+
emitError?: boolean | undefined;
39+
emitWarning?: boolean | undefined;
40+
failOnError?: boolean | undefined;
41+
failOnWarning?: boolean | undefined;
42+
files: string | string[];
43+
formatter: TimerHandler;
44+
lintDirtyModulesOnly?: boolean | undefined;
45+
quiet?: boolean | undefined;
46+
stylelintPath: string;
47+
};
48+
export type Lint = (
49+
options: import('./getOptions').Options
50+
) => Promise<import('stylelint').LinterResult>;
51+
export type LinterCallback = (
52+
error?: import('./StylelintError').default | null | undefined
53+
) => void;
54+
export type FileSystemInfoEntry =
55+
| number
56+
| Partial<{
57+
timestamp: number;
58+
}>;

declarations/StylelintError.d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/** @typedef {import('stylelint').LintResult} LintResult */
2+
/** @typedef {import('./getOptions').Options} Options */
3+
export default class StylelintError extends Error {
4+
/**
5+
* @param {Options} options
6+
* @param {Array<LintResult>} messages
7+
* @returns {StylelintError}
8+
*/
9+
static format(
10+
{ formatter }: Options,
11+
messages: Array<LintResult>
12+
): StylelintError;
13+
/**
14+
* @param {Partial<string>} messages
15+
*/
16+
constructor(messages: Partial<string>);
17+
}
18+
export type LintResult = import('stylelint').LintResult;
19+
export type Options = {
20+
context?: string | undefined;
21+
emitError?: boolean | undefined;
22+
emitWarning?: boolean | undefined;
23+
failOnError?: boolean | undefined;
24+
failOnWarning?: boolean | undefined;
25+
files: string | string[];
26+
formatter: TimerHandler;
27+
lintDirtyModulesOnly?: boolean | undefined;
28+
quiet?: boolean | undefined;
29+
stylelintPath: string;
30+
};

declarations/cjs.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const _exports: typeof import('.').default;
2+
export = _exports;

declarations/getOptions.d.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/** @typedef {import("stylelint")} stylelint */
2+
/**
3+
* @typedef {Object} Options
4+
* @property {string=} context
5+
* @property {boolean=} emitError
6+
* @property {boolean=} emitWarning
7+
* @property {boolean=} failOnError
8+
* @property {boolean=} failOnWarning
9+
* @property {Array<string> | string} files
10+
* @property {Function | string} formatter
11+
* @property {boolean=} lintDirtyModulesOnly
12+
* @property {boolean=} quiet
13+
* @property {string} stylelintPath
14+
*/
15+
/**
16+
* @param {Partial<Options>} pluginOptions
17+
* @returns {Options}
18+
*/
19+
export default function getOptions(pluginOptions: Partial<Options>): Options;
20+
export type stylelint = typeof import('stylelint');
21+
export type Options = {
22+
context?: string | undefined;
23+
emitError?: boolean | undefined;
24+
emitWarning?: boolean | undefined;
25+
failOnError?: boolean | undefined;
26+
failOnWarning?: boolean | undefined;
27+
files: Array<string> | string;
28+
formatter: Function | string;
29+
lintDirtyModulesOnly?: boolean | undefined;
30+
quiet?: boolean | undefined;
31+
stylelintPath: string;
32+
};

declarations/index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default StylelintWebpackPlugin;
2+
export type Compiler = import('webpack').Compiler;
3+
/** @typedef {import('webpack').Compiler} Compiler */
4+
declare class StylelintWebpackPlugin {
5+
constructor(options?: {});
6+
options: import('./getOptions').Options;
7+
/**
8+
* @param {Compiler} compiler
9+
* @returns {void}
10+
*/
11+
apply(compiler: Compiler): void;
12+
/**
13+
*
14+
* @param {Compiler} compiler
15+
* @returns {string}
16+
*/
17+
getContext(compiler: Compiler): string;
18+
}

declarations/linter.d.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/** @typedef {import('stylelint').LinterResult} LinterResult */
2+
/** @typedef {import('stylelint').LintResult} LintResult */
3+
/** @typedef {import('webpack').Compiler} Compiler */
4+
/** @typedef {import('./getOptions').Options} Options */
5+
/**
6+
* @callback Lint
7+
* @param {Options} options
8+
* @returns {Promise<LinterResult>}
9+
*/
10+
/**
11+
* @callback LinterCallback
12+
* @param {StylelintError | null=} error
13+
* @returns {void}
14+
*/
15+
/**
16+
* @param {Lint} lint
17+
* @param {Options} options
18+
* @param {Compiler} compiler
19+
* @param {LinterCallback} callback
20+
* @returns {void}
21+
*/
22+
export default function linter(
23+
lint: Lint,
24+
options: Options,
25+
compiler: Compiler,
26+
callback: LinterCallback
27+
): void;
28+
export type LinterResult = import('stylelint').LinterResult;
29+
export type LintResult = import('stylelint').LintResult;
30+
export type Compiler = import('webpack').Compiler;
31+
export type Options = {
32+
context?: string | undefined;
33+
emitError?: boolean | undefined;
34+
emitWarning?: boolean | undefined;
35+
failOnError?: boolean | undefined;
36+
failOnWarning?: boolean | undefined;
37+
files: string | string[];
38+
formatter: TimerHandler;
39+
lintDirtyModulesOnly?: boolean | undefined;
40+
quiet?: boolean | undefined;
41+
stylelintPath: string;
42+
};
43+
export type Lint = (options: Options) => Promise<LinterResult>;
44+
export type LinterCallback = (
45+
error?: (StylelintError | null) | undefined
46+
) => void;
47+
import StylelintError from './StylelintError';

declarations/utils.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {Array<string> | string} files
3+
* @param {string} context
4+
* @returns {Array<string>}
5+
*/
6+
export function parseFiles(
7+
files: Array<string> | string,
8+
context: string
9+
): Array<string>;
10+
/**
11+
* @param {string} str
12+
* @returns {string}
13+
*/
14+
export function replaceBackslashes(str: string): string;

lint-staged.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
22
'*.js': ['prettier --write', 'eslint --fix'],
3-
'*.{json,md,yml,css}': ['prettier --write'],
3+
'*.{json,md,yml,css,ts}': ['prettier --write'],
44
};

0 commit comments

Comments
 (0)