-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@power-doctest/asciidoctor): add asciidoctor parser
- Loading branch information
Showing
13 changed files
with
184 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,100 @@ | ||
import { ParsedCode, ParsedResults, ParserArgs } from "@power-doctest/types"; | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
|
||
const Asciidoctor = require("asciidoctor"); | ||
const asciidoctor = Asciidoctor(); | ||
type Attributes = { | ||
[index: string]: string; | ||
} | ||
const getState = (attributes: Attributes): "none" | "enabled" | "disabled" => { | ||
const state = attributes["doctest-state"]; | ||
if (!state) { | ||
return "none"; | ||
} | ||
if (/enable(d)?/.test(state)) { | ||
return "enabled"; | ||
} else if (/disable(d)?/.test(state)) { | ||
return "disabled"; | ||
} | ||
return "none"; | ||
}; | ||
|
||
const getMeta = (attributes: Attributes): {} | undefined => { | ||
const meta = attributes["doctest-meta"]; | ||
if (!meta) { | ||
return; | ||
} | ||
try { | ||
return JSON.parse(meta); | ||
} catch (error) { | ||
// parse error | ||
throw new Error(`Can not parsed. doctest-meta={...} should be JSON object: ${error}`); | ||
} | ||
}; | ||
|
||
const getOptions = (attributes: Attributes): {} | undefined => { | ||
const meta = attributes["doctest-options"]; | ||
if (!meta) { | ||
return; | ||
} | ||
try { | ||
return JSON.parse(meta); | ||
} catch (error) { | ||
// parse error | ||
throw new Error(`Can not parsed. doctest-options={...} should be JSON object: ${error}`); | ||
} | ||
}; | ||
|
||
// inlining include:: | ||
const inlineCode = (code: string, baseFilePath: string): string => { | ||
// include:: -> link: | ||
const pattern = /link:(.+)\[.*?]/; | ||
const dirName = path.dirname(baseFilePath); | ||
return code.replace(pattern, (all, filePath) => { | ||
const fileName = path.resolve(dirName, filePath); | ||
if (fs.existsSync(fileName)) { | ||
return fs.readFileSync(fileName, "utf-8"); | ||
} | ||
return all; | ||
}); | ||
}; | ||
|
||
export function parse(code: string) { | ||
const doc = asciidoctor.load(code); | ||
return doc; | ||
export function parse(args: ParserArgs): ParsedResults { | ||
const doc = asciidoctor.load(args.content); | ||
return doc.getBlocks() | ||
.filter((block: any) => { | ||
const attributes = block.getAttributes(); | ||
return attributes.style === "source" && (attributes.language === "js" || attributes.language === "javascript"); | ||
}) | ||
.map((block: any) => { | ||
const lineNumber: number = block.document.getReader().lineno; | ||
const attributes: {} = block.getAttributes(); | ||
const code: string = block.getSource(); | ||
const lines: string[] = block.getSourceLines(); | ||
const meta = getMeta(attributes); | ||
const doctestOptions = getOptions(attributes); | ||
const parsedCode: ParsedCode = { | ||
code: inlineCode(code, args.filePath), | ||
state: getState(attributes), | ||
location: { | ||
start: { | ||
line: lineNumber, | ||
column: 0 | ||
}, | ||
end: { | ||
line: lineNumber + lines.length, | ||
column: 0 | ||
} | ||
}, | ||
metadata: meta, | ||
doctestOptions: doctestOptions ? { | ||
filePath: args.filePath, | ||
...doctestOptions | ||
} : { | ||
filePath: args.filePath | ||
} | ||
}; | ||
return parsedCode; | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import * as assert from "assert"; | ||
// transform function | ||
import { parse } from "../src"; | ||
|
||
const fixturesDir = path.join(__dirname, "snapshots"); | ||
|
||
const trimUndefinedProperty = <T>(o: T, baseDir: string): T => { | ||
return JSON.parse(stringify(o, baseDir)); | ||
}; | ||
const stringify = (o: {}, baseDir: string): string => { | ||
return JSON.stringify(o, (key: string, value: any) => { | ||
if (key === "filePath" && typeof value === "string") { | ||
return path.relative(baseDir, value); | ||
} else { | ||
return value; | ||
} | ||
}, 4); | ||
}; | ||
describe("Snapshot testing", () => { | ||
fs.readdirSync(fixturesDir) | ||
.map(caseName => { | ||
const normalizedTestName = caseName.replace(/-/g, " "); | ||
it(`Test ${normalizedTestName}`, async function() { | ||
const fixtureDir = path.join(fixturesDir, caseName); | ||
const actualFilePath = path.join(fixtureDir, "input.adoc"); | ||
const actualContent = fs.readFileSync(actualFilePath, "utf-8"); | ||
const results = parse({ | ||
content: actualContent, | ||
filePath: actualFilePath | ||
}); | ||
const expectedFilePath = path.join(fixtureDir, "output.json"); | ||
// Usage: update snapshots | ||
// UPDATE_SNAPSHOT=1 npm test | ||
if (!fs.existsSync(expectedFilePath) || process.env.UPDATE_SNAPSHOT) { | ||
fs.writeFileSync(expectedFilePath, stringify(results, fixtureDir)); | ||
this.skip(); // skip when updating snapshots | ||
return; | ||
} | ||
// compare input and output | ||
const expectedContent = JSON.parse(fs.readFileSync(expectedFilePath, "utf-8")); | ||
assert.deepStrictEqual( | ||
trimUndefinedProperty(results, fixtureDir), | ||
expectedContent | ||
); | ||
}); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
packages/@power-doctest/asciidoctor/test/snapshots/disabled/input.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[source,javascript,doctest-state="disabled"] | ||
---- | ||
const str = "string"; | ||
console.log(str); | ||
---- |
5 changes: 5 additions & 0 deletions
5
packages/@power-doctest/asciidoctor/test/snapshots/enabled/input.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[source,javascript,doctest-state="enabled"] | ||
---- | ||
const str = "string"; | ||
console.log(str); | ||
---- |
5 changes: 5 additions & 0 deletions
5
packages/@power-doctest/asciidoctor/test/snapshots/js/input.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[source,js] | ||
---- | ||
const str = "string"; | ||
console.log(str); | ||
---- |
5 changes: 5 additions & 0 deletions
5
packages/@power-doctest/asciidoctor/test/snapshots/meta/input.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[source,javascript,doctest-meta={ "ECMAScript": 2017 }] | ||
---- | ||
const str = "string"; | ||
console.log(str); | ||
---- |
5 changes: 5 additions & 0 deletions
5
packages/@power-doctest/asciidoctor/test/snapshots/options/input.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[source,javascript,doctest-options={ "runMode": "any" }] | ||
---- | ||
const str = "string"; | ||
console.log(str); | ||
---- |
5 changes: 5 additions & 0 deletions
5
packages/@power-doctest/asciidoctor/test/snapshots/simple/input.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[source,javascript] | ||
---- | ||
const str = "string"; | ||
console.log(str); | ||
---- |
4 changes: 4 additions & 0 deletions
4
packages/@power-doctest/asciidoctor/test/snapshots/source-include/input.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[source,javascript] | ||
---- | ||
include::source.js[] | ||
---- |
1 change: 1 addition & 0 deletions
1
packages/@power-doctest/asciidoctor/test/snapshots/source-include/source.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("from source.js"); |