Skip to content

Commit

Permalink
Create a NonCacheRewriter and export cacheRewrittenSourceMap (#111)
Browse files Browse the repository at this point in the history
* Create a NonCacheRewriter

* Fixing lint

* Add test for NonCacheRewriter
  • Loading branch information
uurien authored Jan 10, 2025
1 parent c835612 commit 8acf326
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 34 deletions.
6 changes: 5 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export interface LiteralInfo {
value: string
locations: Array<LiteralLocation>
}
export class Rewriter {
export class NonCacheRewriter {
constructor(config?: RewriterConfig | undefined | null)
rewrite(code: string, file: string): Result
csiMethods(): Array<string>
}

export const Rewriter: NonCacheRewriter

export function cacheRewrittenSourceMap(filename: string, fileContent: string): void
41 changes: 25 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class DummyRewriter {
}

let NativeRewriter
class CacheRewriter {

class NonCacheRewriter {
constructor (config) {
if (NativeRewriter) {
this.nativeRewriter = new NativeRewriter(config)
Expand All @@ -30,18 +31,7 @@ class CacheRewriter {
}

rewrite (code, file) {
const response = this.nativeRewriter.rewrite(code, file)

try {
const { metrics, content } = response
if (metrics?.status === 'modified') {
cacheRewrittenSourceMap(file, content)
}
} catch (e) {
this.logError(e)
}

return response
return this.nativeRewriter.rewrite(code, file)
}

csiMethods () {
Expand All @@ -65,21 +55,40 @@ class CacheRewriter {
}
}

function getRewriter () {
class CacheRewriter extends NonCacheRewriter {
rewrite (code, file) {
const response = super.rewrite(code, file)

try {
const { metrics, content } = response
if (metrics?.status === 'modified') {
cacheRewrittenSourceMap(file, content)
}
} catch (e) {
this.logError(e)
}

return response
}
}

function getRewriter (withoutCache = false) {
try {
const iastRewriter = require('./wasm/wasm_iast_rewriter')

NativeRewriter = iastRewriter.Rewriter
return CacheRewriter
return withoutCache ? NonCacheRewriter : CacheRewriter
} catch (e) {
return DummyRewriter
}
}

module.exports = {
Rewriter: getRewriter(),
NonCacheRewriter: getRewriter(true),
DummyRewriter,
getPrepareStackTrace,
getOriginalPathAndLineFromSourceMap,
kSymbolPrepareStackTrace
kSymbolPrepareStackTrace,
cacheRewrittenSourceMap
}
70 changes: 53 additions & 17 deletions test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,69 @@ describe('main', () => {
cacheRewrittenSourceMap
}
})

rewriter = new main.Rewriter()
})

it('loads sourceMap when source file has been modified', () => {
status = 'modified'
describe('default Rewriter', () => {
beforeEach(() => {
rewriter = new main.Rewriter()
})

const response = rewriter.rewrite('content', 'file')
it('loads sourceMap when source file has been modified', () => {
status = 'modified'

expect(response.metrics.status).to.eq('modified')
expect(cacheRewrittenSourceMap).to.be.calledOnceWith('file', 'content')
})
const response = rewriter.rewrite('content', 'file')

it('does not load sourceMap when source file has not been modified', () => {
status = 'notmodified'
expect(response.metrics.status).to.eq('modified')
expect(cacheRewrittenSourceMap).to.be.calledOnceWith('file', 'content')
})

it('does not load sourceMap when source file has not been modified', () => {
status = 'notmodified'

const response = rewriter.rewrite('content', 'file')
const response = rewriter.rewrite('content', 'file')

expect(response.metrics.status).to.eq('notmodified')
expect(cacheRewrittenSourceMap).to.not.be.called
expect(response.metrics.status).to.eq('notmodified')
expect(cacheRewrittenSourceMap).to.not.be.called
})

it('should catch errors produced in cacheRewrittenSourceMap', () => {
status = 'modified'

cacheRewrittenSourceMap.throws(() => new Error('Error reading sourceMap file'))

expect(() => rewriter.rewrite('content', 'file')).to.not.throw()
})
})

it('should catch errors produced in cacheRewrittenSourceMap', () => {
status = 'modified'
describe('NonCacheRewriter', () => {
beforeEach(() => {
rewriter = new main.NonCacheRewriter()
})

cacheRewrittenSourceMap.throws(() => new Error('Error reading sourceMap file'))
it('does not load sourceMap when source file has been modified', () => {
status = 'modified'

expect(() => rewriter.rewrite('content', 'file')).to.not.throw()
const response = rewriter.rewrite('content', 'file')

expect(response.metrics.status).to.eq('modified')
expect(cacheRewrittenSourceMap).to.not.be.called
})

it('does not load sourceMap when source file has not been modified', () => {
status = 'notmodified'

const response = rewriter.rewrite('content', 'file')

expect(response.metrics.status).to.eq('notmodified')
expect(cacheRewrittenSourceMap).to.not.be.called
})

it('should catch errors produced in cacheRewrittenSourceMap', () => {
status = 'modified'

cacheRewrittenSourceMap.throws(() => new Error('Error reading sourceMap file'))

expect(() => rewriter.rewrite('content', 'file')).to.not.throw()
})
})
})

0 comments on commit 8acf326

Please sign in to comment.