Skip to content

Commit d41ec35

Browse files
committed
Do not cache file contents in memory, version bump
1 parent 16b80c0 commit d41ec35

File tree

6 files changed

+49
-15
lines changed

6 files changed

+49
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Master
44

5+
## 0.4.7 - Decrease memory consumption
6+
7+
- Do not cache file contents in memory for faster ast parsing; this will significantly decrease memory requirements for larger codebases
8+
59
## 0.4.6 - Faster dependency resolution
610

711
- Bump dependencies

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stricter",
3-
"version": "0.4.6",
3+
"version": "0.4.7",
44
"description": "A project-wide js-linting tool",
55
"files": [
66
"LICENSE",

src/file-processor/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const readFileData = (
3838
logger: Logger,
3939
): FileData => {
4040
const source = readFile(filePath);
41-
const ast = parsedExtensionsRe.test(filePath) ? () => parse(source, filePath) : undefined;
41+
const isParsedExtension = parsedExtensionsRe.test(filePath);
42+
const getAst = isParsedExtension ? () => parse(filePath) : undefined;
4243
let dependencies: string[] | undefined;
4344

4445
const hash = getHash(source);
@@ -47,10 +48,10 @@ const readFileData = (
4748
if (cachedValue && cachedValue.hash === hash) {
4849
dependencies = cachedValue.dependencies;
4950
} else {
50-
if (ast) {
51+
if (isParsedExtension) {
5152
let parsedAst: any;
5253
try {
53-
parsedAst = ast();
54+
parsedAst = parse(filePath, source);
5455
} catch (e) {
5556
logger.error(`Unable to parse ${filePath}`);
5657
throw e;
@@ -62,7 +63,7 @@ const readFileData = (
6263

6364
const result = {
6465
source,
65-
ast,
66+
ast: getAst,
6667
dependencies,
6768
};
6869

src/file-processor/parse-imports.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ describe('resolveImport', () => {
55
it('should count require', () => {
66
const result = parseImports(
77
parse(
8+
'filePath',
89
`
910
const test1 = require('test-file1');
1011
`,
11-
'filePath',
1212
),
1313
);
1414

@@ -19,10 +19,10 @@ describe('resolveImport', () => {
1919
it('should count es6 dynamic import', () => {
2020
const result = parseImports(
2121
parse(
22+
'filePath',
2223
`
2324
const test1 = import('test-file1');
2425
`,
25-
'filePath',
2626
),
2727
);
2828

@@ -33,13 +33,13 @@ describe('resolveImport', () => {
3333
it('should count es6 imports', () => {
3434
const result = parseImports(
3535
parse(
36+
'filePath',
3637
`
3738
import { default as test1 } from 'test-file1';
3839
import { test2, test3 } from 'test-file2';
3940
import test4 from 'test-file3';
4041
import * as test5 from 'test-file4';
4142
`,
42-
'filePath',
4343
),
4444
);
4545

@@ -55,12 +55,12 @@ describe('resolveImport', () => {
5555
it('should count es6 reexports', () => {
5656
const result = parseImports(
5757
parse(
58+
'filePath',
5859
`
5960
export { default as test1 } from 'test-file1';
6061
export { test2, test3 } from 'test-file2';
6162
export * from 'test-file3';
6263
`,
63-
'filePath',
6464
),
6565
);
6666

@@ -71,11 +71,11 @@ describe('resolveImport', () => {
7171
it('should not count exports', () => {
7272
const result = parseImports(
7373
parse(
74+
'filePath',
7475
`
7576
export default () => {};
7677
export const test = () => {};
7778
`,
78-
'filePath',
7979
),
8080
);
8181

@@ -86,11 +86,11 @@ describe('resolveImport', () => {
8686
it('should ignore dynamic imports', () => {
8787
const result = parseImports(
8888
parse(
89+
'filePath',
8990
`
9091
const test1 = require(foo);
9192
const test2 = import(foo);
9293
`,
93-
'filePath',
9494
),
9595
);
9696

@@ -101,10 +101,10 @@ describe('resolveImport', () => {
101101
it('should count TS import equals declaration', () => {
102102
const result = parseImports(
103103
parse(
104+
'filePath.tsx',
104105
`
105106
import test1 = require('test-file1');
106107
`,
107-
'filePath.tsx',
108108
),
109109
);
110110

@@ -115,10 +115,10 @@ describe('resolveImport', () => {
115115
it('should count jest.requireActual', () => {
116116
const result = parseImports(
117117
parse(
118+
'filePath',
118119
`
119120
const test1 = jest.requireActual('test-file1');
120121
`,
121-
'filePath',
122122
),
123123
);
124124

src/utils/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ const defaultPlugins: parser.ParserPlugin[] = [
9797
'throwExpressions',
9898
] as parser.ParserPlugin[];
9999

100-
export const parse = (source: string, filePath: string): any => {
100+
export const parse = (filePath: string, source?: string): any => {
101+
if (!source) {
102+
source = readFile(filePath);
103+
}
104+
101105
const plugins = [...defaultPlugins];
102106
const fileType = /\.([jt])s(x?)$/.exec(filePath);
103107

src/utils/test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ jest.mock('fs');
44
jest.mock('path');
55
jest.mock('@babel/parser');
66

7+
afterEach(() => {
8+
jest.resetAllMocks();
9+
});
10+
711
describe('readFile', () => {
812
it('reads file in utf8', () => {
913
const fileName = 'test';
@@ -161,7 +165,28 @@ describe('parse', () => {
161165
parseMock.mockImplementation((i: string) => i);
162166

163167
const src = 'test';
164-
const result = parse(src, 'filePath');
168+
const result = parse('filePath', src);
169+
170+
expect(parseMock.mock.calls.length).toBe(1);
171+
expect(parseMock.mock.calls[0][0]).toBe(src);
172+
expect(result).toBe(src);
173+
});
174+
175+
it('reads file if not provided', () => {
176+
const src = 'test';
177+
const fileName = 'filePath';
178+
179+
const { parse: parseMock } = require('@babel/parser');
180+
parseMock.mockImplementation((i: string) => i);
181+
182+
const { readFileSync } = require('fs');
183+
readFileSync.mockReturnValueOnce(src);
184+
185+
const result = parse(fileName);
186+
187+
expect(readFileSync.mock.calls.length).toBe(1);
188+
expect(readFileSync.mock.calls[0][0]).toBe(fileName);
189+
expect(readFileSync.mock.calls[0][1]).toBe('utf8');
165190

166191
expect(parseMock.mock.calls.length).toBe(1);
167192
expect(parseMock.mock.calls[0][0]).toBe(src);

0 commit comments

Comments
 (0)