Skip to content

Commit 9cbf8f8

Browse files
committed
refactor loader regex patterns for different programming languages
1 parent 6fde3ad commit 9cbf8f8

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

lib/reducers/run-tests/parse-loaders.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
"use strict";
22
var path = require('path');
33
var fs = require('fs');
4-
var supportedFileTypes = ['js', 'jsx', 'ts', 'py'];
5-
var js = /^\/\/\s?load\(['"`](.+)['"`](\,\s?true)?\)/m;
6-
var loaderMatch = {
7-
js: js,
8-
ts: js,
9-
jsx: js,
10-
py: /^#\s?load\(['"`](.+)['"`](,\s?true)?\)/m
4+
var comments = {
5+
py: '#'
116
};
12-
function parseLoaders(data, fileType) {
13-
if (supportedFileTypes.indexOf(fileType) < 0) {
14-
console.log("File type \"" + fileType + "\" not supported.");
15-
return '';
7+
function loaderRegex(fileType) {
8+
var comment = '\/{2,3}';
9+
if (comments[fileType]) {
10+
comment = comments[fileType];
1611
}
12+
return new RegExp("^" + comment + " ?load\\(['\"](.+)['\"](,s?true)?\\)", 'm');
13+
}
14+
function parseLoaders(data, fileType) {
1715
var i = -1;
1816
var lines = data.split('\n');
1917
var filesLoaded = [];
18+
var loaderMatch = loaderRegex(fileType);
2019
while (i < lines.length - 1) {
2120
i += 1;
22-
var loader = lines[i].match(loaderMatch[fileType]);
21+
var loader = lines[i].match(loaderMatch);
2322
if (loader) {
2423
var fileToLoad = loader[1];
2524
if (filesLoaded.indexOf(fileToLoad) > -1) {

src/reducers/run-tests/parse-loaders.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import * as path from 'path';
22
import * as fs from 'fs';
33

4-
const supportedFileTypes = ['js', 'jsx', 'ts', 'py'];
5-
const js = /^\/\/\s?load\(['"`](.+)['"`](\,\s?true)?\)/m; // // load('file'),
6-
const loaderMatch = {
7-
js,
8-
ts: js,
9-
jsx: js,
10-
py: /^#\s?load\(['"`](.+)['"`](,\s?true)?\)/m // # load('file')
4+
// other languages may handle comments differently
5+
const comments = {
6+
py: '#'
117
};
128

13-
export default function parseLoaders(data: string, fileType: string) {
14-
15-
if (supportedFileTypes.indexOf(fileType) < 0) {
16-
console.log(`File type "${fileType}" not supported.`);
17-
return '';
9+
function loaderRegex(fileType: string) {
10+
let comment = '\/{2,3}';
11+
if (comments[fileType]) {
12+
comment = comments[fileType];
1813
}
14+
return new RegExp(`^${comment} ?load\\(['"](.+)['"](\,\s?true)?\\)`, 'm');
15+
}
16+
17+
export default function parseLoaders(data: string, fileType: string) {
1918

2019
// loop over lines and add editor files
2120
let i = -1;
2221
let lines = data.split('\n');
2322

2423
let filesLoaded = [];
24+
let loaderMatch = loaderRegex(fileType);
2525

2626
while (i < lines.length - 1) {
2727
i += 1;
28-
let loader: string[] = lines[i].match(loaderMatch[fileType]);
28+
let loader: string[] = lines[i].match(loaderMatch);
2929

3030
if (loader) {
3131
// loader found

src/reducers/task-tests/task-tests.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as path from 'path';
22
import * as fs from 'fs';
33
import * as Type from '../../actions/actionTypes';
4-
import concatTests from './concat-tests';
54

65
export default function taskTestsReducer(taskTests = '', action: CR.Action): string {
76
switch (action.type) {

0 commit comments

Comments
 (0)