Skip to content

Resolve customMatcher issues #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/helpers/__tests__/createMatchers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createMatchers } from '../createMatchers';

describe('utils / createMatchers', () => {
it('should match `customMatcher` regexp', () => {
const options: IOptions = { customMatcher: '\\.css$' };
const { isCSS, isRelativeCSS } = createMatchers(options);

expect(isCSS('./myfile.css')).toBe(true);
expect(isCSS('./myfile.m.css')).toBe(true);
expect(isCSS('./myfile.scss')).toBe(false);
expect(isRelativeCSS('../folder/myfile.css')).toBe(true);
expect(isRelativeCSS('../folder/myfile.m.css')).toBe(true);
expect(isRelativeCSS('../folders/myfile.scss')).toBe(false);
});

it('should handle bad `customMatcher` regexp', () => {
const options: IOptions = { customMatcher: '$([a' };
const { isCSS, isRelativeCSS } = createMatchers(options);

expect(isCSS('./myfile.module.css')).toBe(true);
expect(isRelativeCSS('../folders/myfile.module.scss')).toBe(true);
});
});
7 changes: 6 additions & 1 deletion src/helpers/__tests__/cssExtensions.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { isCSS, isRelativeCSS } from '../cssExtensions';
import { createIsCSS, createIsRelativeCSS } from '../cssExtensions';

describe('utils / cssExtensions', () => {
describe('isCSS', () => {
const isCSS = createIsCSS();

it('should match CSS module extensions', () => {
expect(isCSS('./myfile.module.scss')).toBe(true);
expect(isCSS('./myfile.module.sass')).toBe(true);
Expand All @@ -17,6 +19,9 @@ describe('utils / cssExtensions', () => {
});

describe('isRelativeCSS', () => {
const isCSS = createIsCSS();
const isRelativeCSS = createIsRelativeCSS(isCSS);

it('should match relative CSS modules', () => {
expect(isRelativeCSS('./myfile.module.css')).toBe(true);
expect(isRelativeCSS('../folder/myfile.module.css')).toBe(true);
Expand Down
20 changes: 20 additions & 0 deletions src/helpers/createMatchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createIsCSS, createIsRelativeCSS } from './cssExtensions';

export const createMatchers = (options: IOptions = {}) => {
// Allow custom matchers to be used, and handle bad matcher patterns.
let isCSS = createIsCSS();
try {
const { customMatcher } = options;
if (customMatcher) {
const customMatcherRegExp = new RegExp(customMatcher);
isCSS = createIsCSS(customMatcherRegExp);
}
} catch (e) {
// TODO: Provide error/warning to user.
}

// Create the relative CSS checker.
const isRelativeCSS = createIsRelativeCSS(isCSS);

return { isCSS, isRelativeCSS };
};
11 changes: 7 additions & 4 deletions src/helpers/cssExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const isRelative = (fileName: string) => /^\.\.?($|[\\/])/.test(fileName);
export type isCSSFn = (fileName: string) => boolean;
const DEFAULT_REGEXP = /\.module\.(sa|sc|c)ss$/;

export const isCSS = (fileName: string) =>
/\.module\.(sa|sc|c)ss$/.test(fileName);
const isRelative = (fileName: string) => /^\.\.?($|[\\/])/.test(fileName);

export const isRelativeCSS = (fileName: string) =>
export const createIsCSS = (
customMatcher: RegExp = DEFAULT_REGEXP,
): isCSSFn => (fileName: string) => customMatcher.test(fileName);
export const createIsRelativeCSS = (isCSS: isCSSFn) => (fileName: string) =>
isCSS(fileName) && isRelative(fileName);
19 changes: 7 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import * as path from 'path';
import * as ts_module from 'typescript/lib/tsserverlibrary';
import { isCSS as _isCSS, isRelativeCSS } from './helpers/cssExtensions';
import { createMatchers } from './helpers/createMatchers';
import { isCSSFn } from './helpers/cssExtensions';
import { getDtsSnapshot } from './helpers/cssSnapshots';

function init({ typescript: ts }: { typescript: typeof ts_module }) {
let isCSS = _isCSS;
let _isCSS: isCSSFn;
function create(info: ts.server.PluginCreateInfo) {
// User options for plugin.
const options: IOptions = info.config.options || {};

// Allow custom matchers to be used, handling bad matcher patterns;
try {
const { customMatcher } = options;
if (customMatcher) {
isCSS = (fileName) => new RegExp(customMatcher).test(fileName);
}
} catch (e) {
// TODO: Provide error/warning to user.
}
// Create matchers using options object.
const { isCSS, isRelativeCSS } = createMatchers(options);
_isCSS = isCSS;

// Creates new virtual source files for the CSS modules.
const _createLanguageServiceSourceFile = ts.createLanguageServiceSourceFile;
Expand Down Expand Up @@ -97,7 +92,7 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) {
}

function getExternalFiles(project: ts_module.server.ConfiguredProject) {
return project.getFileNames().filter(isCSS);
return project.getFileNames().filter(_isCSS);
}

return { create, getExternalFiles };
Expand Down