Skip to content

Commit c11f990

Browse files
committed
Add customMatchers helper
1 parent 09dfccd commit c11f990

File tree

5 files changed

+63
-51
lines changed

5 files changed

+63
-51
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createMatchers } from '../createMatchers';
2+
3+
describe('utils / createMatchers', () => {
4+
it('should match `customMatcher` regexp', () => {
5+
const options: IOptions = { customMatcher: '\\.css$' };
6+
const { isCSS, isRelativeCSS } = createMatchers(options);
7+
8+
expect(isCSS('./myfile.css')).toBe(true);
9+
expect(isCSS('./myfile.m.css')).toBe(true);
10+
expect(isCSS('./myfile.scss')).toBe(false);
11+
expect(isRelativeCSS('../folder/myfile.css')).toBe(true);
12+
expect(isRelativeCSS('../folder/myfile.m.css')).toBe(true);
13+
expect(isRelativeCSS('../folders/myfile.scss')).toBe(false);
14+
});
15+
16+
it('should handle bad `customMatcher` regexp', () => {
17+
const options: IOptions = { customMatcher: '$([a' };
18+
const { isCSS, isRelativeCSS } = createMatchers(options);
19+
20+
expect(isCSS('./myfile.module.css')).toBe(true);
21+
expect(isRelativeCSS('../folders/myfile.module.scss')).toBe(true);
22+
});
23+
});
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import {
2-
isCSS,
3-
isRelativeCSS,
4-
setExtensionsPattern,
5-
DEFAULT_EXTENSIONS_PATTERN,
6-
} from '../cssExtensions';
1+
import { createIsCSS, createIsRelativeCSS } from '../cssExtensions';
72

83
describe('utils / cssExtensions', () => {
94
describe('isCSS', () => {
5+
const isCSS = createIsCSS();
6+
107
it('should match CSS module extensions', () => {
118
expect(isCSS('./myfile.module.scss')).toBe(true);
129
expect(isCSS('./myfile.module.sass')).toBe(true);
@@ -22,6 +19,9 @@ describe('utils / cssExtensions', () => {
2219
});
2320

2421
describe('isRelativeCSS', () => {
22+
const isCSS = createIsCSS();
23+
const isRelativeCSS = createIsRelativeCSS(isCSS);
24+
2525
it('should match relative CSS modules', () => {
2626
expect(isRelativeCSS('./myfile.module.css')).toBe(true);
2727
expect(isRelativeCSS('../folder/myfile.module.css')).toBe(true);
@@ -31,18 +31,4 @@ describe('utils / cssExtensions', () => {
3131
expect(isRelativeCSS('myfile.module.css')).toBe(false);
3232
});
3333
});
34-
35-
describe('setExtensionsPattern', () => {
36-
afterEach(() => setExtensionsPattern(DEFAULT_EXTENSIONS_PATTERN));
37-
38-
it('should overwrite default extensions pattern', () => {
39-
const pattern = new RegExp('\\.css$');
40-
setExtensionsPattern(pattern);
41-
42-
expect(isCSS('./myfile.css')).toBe(true);
43-
expect(isCSS('./myfile.scss')).toBe(false);
44-
expect(isRelativeCSS('../folder/myfile.css')).toBe(true);
45-
expect(isRelativeCSS('../folders/myfile.scss')).toBe(false);
46-
});
47-
});
4834
});

src/helpers/createMatchers.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createIsCSS, createIsRelativeCSS } from './cssExtensions';
2+
3+
export const createMatchers = (options: IOptions = {}) => {
4+
// Allow custom matchers to be used, and handle bad matcher patterns.
5+
let isCSS = createIsCSS();
6+
try {
7+
const { customMatcher } = options;
8+
if (customMatcher) {
9+
const customMatcherRegExp = new RegExp(customMatcher);
10+
isCSS = createIsCSS(customMatcherRegExp);
11+
}
12+
} catch (e) {
13+
// TODO: Provide error/warning to user.
14+
}
15+
16+
// Create the relative CSS checker.
17+
const isRelativeCSS = createIsRelativeCSS(isCSS);
18+
19+
return { isCSS, isRelativeCSS };
20+
};

src/helpers/cssExtensions.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
const isRelative = (fileName: string) => /^\.\.?($|[\\/])/.test(fileName);
2-
3-
export const DEFAULT_EXTENSIONS_PATTERN = /\.module\.(sa|sc|c)ss$/;
4-
5-
let extensionsPattern: RegExp = DEFAULT_EXTENSIONS_PATTERN;
1+
export type isCSSFn = (fileName: string) => boolean;
2+
const DEFAULT_REGEXP = /\.module\.(sa|sc|c)ss$/;
63

7-
export const isCSS = (fileName: string) => extensionsPattern.test(fileName);
4+
const isRelative = (fileName: string) => /^\.\.?($|[\\/])/.test(fileName);
85

9-
export const isRelativeCSS = (fileName: string) =>
6+
export const createIsCSS = (
7+
customMatcher: RegExp = DEFAULT_REGEXP,
8+
): isCSSFn => (fileName: string) => customMatcher.test(fileName);
9+
export const createIsRelativeCSS = (isCSS: isCSSFn) => (fileName: string) =>
1010
isCSS(fileName) && isRelative(fileName);
11-
12-
export const setExtensionsPattern = (newPattern?: RegExp) => {
13-
if (newPattern !== undefined) {
14-
extensionsPattern = newPattern;
15-
}
16-
};

src/index.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
import * as path from 'path';
22
import * as ts_module from 'typescript/lib/tsserverlibrary';
3-
import {
4-
isCSS,
5-
isRelativeCSS,
6-
setExtensionsPattern,
7-
} from './helpers/cssExtensions';
3+
import { createMatchers } from './helpers/createMatchers';
4+
import { isCSSFn } from './helpers/cssExtensions';
85
import { getDtsSnapshot } from './helpers/cssSnapshots';
96

107
function init({ typescript: ts }: { typescript: typeof ts_module }) {
8+
let _isCSS: isCSSFn;
119
function create(info: ts.server.PluginCreateInfo) {
1210
// User options for plugin.
1311
const options: IOptions = info.config.options || {};
1412

15-
// Allow custom matchers to be used, handling bad matcher patterns;
16-
let extensionsPattern: RegExp | undefined;
17-
try {
18-
const { customMatcher } = options;
19-
if (customMatcher) {
20-
extensionsPattern = new RegExp(customMatcher);
21-
}
22-
} catch (e) {
23-
// TODO: Provide error/warning to user.
24-
}
25-
26-
setExtensionsPattern(extensionsPattern);
13+
// Create matchers using options object.
14+
const { isCSS, isRelativeCSS } = createMatchers(options);
15+
_isCSS = isCSS;
2716

2817
// Creates new virtual source files for the CSS modules.
2918
const _createLanguageServiceSourceFile = ts.createLanguageServiceSourceFile;
@@ -103,7 +92,7 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) {
10392
}
10493

10594
function getExternalFiles(project: ts_module.server.ConfiguredProject) {
106-
return project.getFileNames().filter(isCSS);
95+
return project.getFileNames().filter(_isCSS);
10796
}
10897

10998
return { create, getExternalFiles };

0 commit comments

Comments
 (0)