Skip to content

Commit

Permalink
feat(JS): Added checking for empty string semaphore in imports
Browse files Browse the repository at this point in the history
  • Loading branch information
beneboy committed Sep 2, 2019
1 parent 648ac8e commit d2e2d48
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion py/tests/test_document_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_compile_article():

def test_import_appending():
"""Found imports in a piece of code should be added to the list of imports the code chunk already specifies."""
c = CodeChunk('import moda\nimport modb', imports=['modc', 'modd'], language='python')
c = CodeChunk('import moda\nimport modb\nimport modc', imports=['modc', 'modd'], language='python')

dc = DocumentCompiler()
dc.compile(c)
Expand Down
23 changes: 22 additions & 1 deletion ts/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ export function execute(
})
}

/**
* Add `imports` to the `CodeChunk.imports` array if they aren't already there. If the existing imports array contains
* the empty string semaphore then no more imports should be added.
*/
function setCodeChunkImports(code: CodeChunk, imports: string[]): void {
if (code.imports === undefined) {
code.imports = imports
return
}

if (code.imports.includes('')) return

// Can't do any kind of de-dupe magic as code.imports might contain types other than strings.
imports.forEach(im => {
// Typescript seems to forget that this has already been checked to not be undefined
if (code.imports === undefined) return

if (!code.imports.includes(im)) code.imports.push(im)
})
}

/**
* Traverse a `Node` hierarchy and push any `Parameter` that is found onto the `parameters` array,
* and any executable code block onto the `code` array.
Expand All @@ -146,7 +167,7 @@ export function parseItem(
if (isA('CodeChunk', item)) {
const parseResult = parseCodeChunk(item)

item.imports = parseResult.imports
setCodeChunkImports(item, parseResult.imports)
item.declares = parseResult.declares
item.assigns = parseResult.assigns
item.alters = parseResult.alters
Expand Down
30 changes: 30 additions & 0 deletions ts/parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,36 @@ describe('Parsing information from an Article', () => {
'IntegerSchema'
)
})

test('it adds imports it finds to existing CodeChunk imports', () => {
const c = codeChunk(
"import { export1 } from 'module-name3'\nimport { export2 } from 'module-name4'",
{
language: 'javascript',
imports: ['module-name1', 'module-name2', 'module-name3']
}
)
parseItem(c, [], [])

expect(c.imports).toHaveLength(4)
expect(c.imports).toContain('module-name1')
expect(c.imports).toContain('module-name2')
expect(c.imports).toContain('module-name3')
expect(c.imports).toContain('module-name4')
})

test("it doesn't add imports it finds to existing CodeChunk imports if the empty string semaphore is present", () => {
const c = codeChunk(
"import { export1 } from 'module-name3'\nimport { export2 } from 'module-name4'",
{ language: 'javascript', imports: ['module-name1', 'module-name2', ''] }
)
parseItem(c, [], [])

expect(c.imports).toHaveLength(3)
expect(c.imports).toContain('module-name1')
expect(c.imports).toContain('module-name2')
expect(c.imports).toContain('')
})
})

describe('CLI parameter parsing', () => {
Expand Down

0 comments on commit d2e2d48

Please sign in to comment.