Skip to content

Commit

Permalink
Do not cache file contents in memory, version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Li0liQ committed Mar 29, 2021
1 parent 16b80c0 commit d41ec35
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Master

## 0.4.7 - Decrease memory consumption

- Do not cache file contents in memory for faster ast parsing; this will significantly decrease memory requirements for larger codebases

## 0.4.6 - Faster dependency resolution

- Bump dependencies
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stricter",
"version": "0.4.6",
"version": "0.4.7",
"description": "A project-wide js-linting tool",
"files": [
"LICENSE",
Expand Down
9 changes: 5 additions & 4 deletions src/file-processor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const readFileData = (
logger: Logger,
): FileData => {
const source = readFile(filePath);
const ast = parsedExtensionsRe.test(filePath) ? () => parse(source, filePath) : undefined;
const isParsedExtension = parsedExtensionsRe.test(filePath);
const getAst = isParsedExtension ? () => parse(filePath) : undefined;
let dependencies: string[] | undefined;

const hash = getHash(source);
Expand All @@ -47,10 +48,10 @@ const readFileData = (
if (cachedValue && cachedValue.hash === hash) {
dependencies = cachedValue.dependencies;
} else {
if (ast) {
if (isParsedExtension) {
let parsedAst: any;
try {
parsedAst = ast();
parsedAst = parse(filePath, source);
} catch (e) {
logger.error(`Unable to parse ${filePath}`);
throw e;
Expand All @@ -62,7 +63,7 @@ const readFileData = (

const result = {
source,
ast,
ast: getAst,
dependencies,
};

Expand Down
16 changes: 8 additions & 8 deletions src/file-processor/parse-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ describe('resolveImport', () => {
it('should count require', () => {
const result = parseImports(
parse(
'filePath',
`
const test1 = require('test-file1');
`,
'filePath',
),
);

Expand All @@ -19,10 +19,10 @@ describe('resolveImport', () => {
it('should count es6 dynamic import', () => {
const result = parseImports(
parse(
'filePath',
`
const test1 = import('test-file1');
`,
'filePath',
),
);

Expand All @@ -33,13 +33,13 @@ describe('resolveImport', () => {
it('should count es6 imports', () => {
const result = parseImports(
parse(
'filePath',
`
import { default as test1 } from 'test-file1';
import { test2, test3 } from 'test-file2';
import test4 from 'test-file3';
import * as test5 from 'test-file4';
`,
'filePath',
),
);

Expand All @@ -55,12 +55,12 @@ describe('resolveImport', () => {
it('should count es6 reexports', () => {
const result = parseImports(
parse(
'filePath',
`
export { default as test1 } from 'test-file1';
export { test2, test3 } from 'test-file2';
export * from 'test-file3';
`,
'filePath',
),
);

Expand All @@ -71,11 +71,11 @@ describe('resolveImport', () => {
it('should not count exports', () => {
const result = parseImports(
parse(
'filePath',
`
export default () => {};
export const test = () => {};
`,
'filePath',
),
);

Expand All @@ -86,11 +86,11 @@ describe('resolveImport', () => {
it('should ignore dynamic imports', () => {
const result = parseImports(
parse(
'filePath',
`
const test1 = require(foo);
const test2 = import(foo);
`,
'filePath',
),
);

Expand All @@ -101,10 +101,10 @@ describe('resolveImport', () => {
it('should count TS import equals declaration', () => {
const result = parseImports(
parse(
'filePath.tsx',
`
import test1 = require('test-file1');
`,
'filePath.tsx',
),
);

Expand All @@ -115,10 +115,10 @@ describe('resolveImport', () => {
it('should count jest.requireActual', () => {
const result = parseImports(
parse(
'filePath',
`
const test1 = jest.requireActual('test-file1');
`,
'filePath',
),
);

Expand Down
6 changes: 5 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ const defaultPlugins: parser.ParserPlugin[] = [
'throwExpressions',
] as parser.ParserPlugin[];

export const parse = (source: string, filePath: string): any => {
export const parse = (filePath: string, source?: string): any => {
if (!source) {
source = readFile(filePath);
}

const plugins = [...defaultPlugins];
const fileType = /\.([jt])s(x?)$/.exec(filePath);

Expand Down
27 changes: 26 additions & 1 deletion src/utils/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ jest.mock('fs');
jest.mock('path');
jest.mock('@babel/parser');

afterEach(() => {
jest.resetAllMocks();
});

describe('readFile', () => {
it('reads file in utf8', () => {
const fileName = 'test';
Expand Down Expand Up @@ -161,7 +165,28 @@ describe('parse', () => {
parseMock.mockImplementation((i: string) => i);

const src = 'test';
const result = parse(src, 'filePath');
const result = parse('filePath', src);

expect(parseMock.mock.calls.length).toBe(1);
expect(parseMock.mock.calls[0][0]).toBe(src);
expect(result).toBe(src);
});

it('reads file if not provided', () => {
const src = 'test';
const fileName = 'filePath';

const { parse: parseMock } = require('@babel/parser');
parseMock.mockImplementation((i: string) => i);

const { readFileSync } = require('fs');
readFileSync.mockReturnValueOnce(src);

const result = parse(fileName);

expect(readFileSync.mock.calls.length).toBe(1);
expect(readFileSync.mock.calls[0][0]).toBe(fileName);
expect(readFileSync.mock.calls[0][1]).toBe('utf8');

expect(parseMock.mock.calls.length).toBe(1);
expect(parseMock.mock.calls[0][0]).toBe(src);
Expand Down

0 comments on commit d41ec35

Please sign in to comment.