@as-covers/transform
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -14,22 +14,6 @@ /** | ||
*/ | ||
/// <reference types="assemblyscript/dist/assemblyscript" /> | ||
import { Parser } from "visitor-as/as"; | ||
declare const _default: { | ||
new (): { | ||
afterParse(parser: Parser): void; | ||
readonly program: import("visitor-as/as").Program; | ||
readonly baseDir: string; | ||
readonly stdout: import("assemblyscript/cli/asc").OutputStream; | ||
readonly stderr: import("assemblyscript/cli/asc").OutputStream; | ||
readonly log: { | ||
(...data: any[]): void; | ||
(message?: any, ...optionalParams: any[]): void; | ||
}; | ||
writeFile(filename: string, contents: string | Uint8Array, baseDir: string): boolean; | ||
readFile(filename: string, baseDir: string): string | null; | ||
listFiles(dirname: string, baseDir: string): string[] | null; | ||
afterInitialize?(program: import("visitor-as/as").Program): void; | ||
afterCompile?(module: import("visitor-as/as").Module): void; | ||
}; | ||
}; | ||
export = _default; | ||
import { Parser } from "assemblyscript/dist/assemblyscript.js"; | ||
import { Transform } from "assemblyscript/dist/transform.js"; | ||
export default class MyTransform extends Transform { | ||
afterParse(parser: Parser): void; | ||
} |
123
lib/index.js
@@ -1,2 +0,1 @@ | ||
"use strict"; | ||
/** | ||
@@ -15,19 +14,17 @@ * The primary transform file. | ||
*/ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
// Import visitor-as | ||
const as_1 = require("visitor-as/as"); | ||
const util_1 = require("./util"); | ||
const visitor_as_1 = require("visitor-as"); | ||
const transformRange_1 = require("visitor-as/dist/transformRange"); | ||
// @ts-ignore | ||
const line_column_1 = __importDefault(require("line-column")); | ||
import { NodeKind, Token, } from "assemblyscript/dist/assemblyscript.js"; | ||
import { Transform } from "assemblyscript/dist/transform.js"; | ||
import { createPointID } from "./util.js"; | ||
import { SimpleParser, BaseVisitor } from "visitor-as"; | ||
import { RangeTransform } from "visitor-as/dist/transformRange.js"; | ||
import linecol from "line-column"; | ||
// Ignored Regex | ||
const ignoredRegex = /^[\t ]*\/\/ @as-covers: ignore.*$/gm; | ||
// -- Imports | ||
class CoverTransform extends visitor_as_1.BaseVisitor { | ||
class CoverTransform extends BaseVisitor { | ||
linecol = 0; | ||
globalStatements = []; | ||
ignoredLines = new Set(); | ||
entry = null; | ||
// Declare properties. | ||
@@ -39,4 +36,4 @@ visitBinaryExpression(expr) { | ||
switch (expr.operator) { | ||
case as_1.Token.BAR_BAR: | ||
case as_1.Token.AMPERSAND_AMPERSAND: { | ||
case Token.BAR_BAR: | ||
case Token.AMPERSAND_AMPERSAND: { | ||
// If (&& or ||) | ||
@@ -54,10 +51,10 @@ // Get right expression (right side of the || or &&) | ||
return; | ||
const replacer = new transformRange_1.RangeTransform(expr); | ||
const replacer = new RangeTransform(expr); | ||
// Create id. (Hash) | ||
const rightId = util_1.createPointID(name, rightLine, rightCol, "CoverType.Expression"); | ||
const rightId = createPointID(name, rightLine, rightCol, "CoverType.Expression"); | ||
// Create Declare Statement. | ||
const rightDeclareStatement = visitor_as_1.SimpleParser.parseStatement(`__coverDeclare("${name}", ${rightId}, ${rightLine}, ${rightCol}, CoverType.Expression)`, true); | ||
const rightDeclareStatement = SimpleParser.parseStatement(`__coverDeclare("${name}", ${rightId}, ${rightLine}, ${rightCol}, CoverType.Expression)`, true); | ||
replacer.visit(rightDeclareStatement); | ||
// Create new Expression | ||
let rightCoverExpression = visitor_as_1.SimpleParser.parseExpression(`(__cover(${rightId}), $$REPLACE_ME)`); | ||
let rightCoverExpression = SimpleParser.parseExpression(`(__cover(${rightId}), $$REPLACE_ME)`); | ||
replacer.visit(rightCoverExpression); | ||
@@ -85,8 +82,8 @@ // Replace $$REPLACE_ME with the expresion | ||
return; | ||
const funcId = util_1.createPointID(name, funcLine, funcCol, "CoverType.Function"); | ||
const replacer = new transformRange_1.RangeTransform(dec); | ||
const funcDeclareStatement = visitor_as_1.SimpleParser.parseStatement(`__coverDeclare("${name}", ${funcId}, ${funcLine}, ${funcCol}, CoverType.Function)`, true); | ||
const funcId = createPointID(name, funcLine, funcCol, "CoverType.Function"); | ||
const replacer = new RangeTransform(dec); | ||
const funcDeclareStatement = SimpleParser.parseStatement(`__coverDeclare("${name}", ${funcId}, ${funcLine}, ${funcCol}, CoverType.Function)`, true); | ||
replacer.visit(funcDeclareStatement); | ||
// The cover statement | ||
const funcCoverStatement = visitor_as_1.SimpleParser.parseStatement(`__cover(${funcId})`, true); | ||
const funcCoverStatement = SimpleParser.parseStatement(`__cover(${funcId})`, true); | ||
replacer.visit(funcCoverStatement); | ||
@@ -107,7 +104,7 @@ const bodyBlock = dec.body; | ||
return; | ||
const replacer = new transformRange_1.RangeTransform(node); | ||
const parmId = util_1.createPointID(name, parmLine, parmCol, "CoverType.Expression"); | ||
const parmDeclareStatement = visitor_as_1.SimpleParser.parseStatement(`__coverDeclare("${name}", ${parmId}, ${parmLine}, ${parmCol}, CoverType.Expression)`, true); | ||
const replacer = new RangeTransform(node); | ||
const parmId = createPointID(name, parmLine, parmCol, "CoverType.Expression"); | ||
const parmDeclareStatement = SimpleParser.parseStatement(`__coverDeclare("${name}", ${parmId}, ${parmLine}, ${parmCol}, CoverType.Expression)`, true); | ||
replacer.visit(parmDeclareStatement); | ||
const parmCoverExpression = visitor_as_1.SimpleParser.parseExpression(`(__cover(${parmId}), $$REPLACE_ME)`); | ||
const parmCoverExpression = SimpleParser.parseExpression(`(__cover(${parmId}), $$REPLACE_ME)`); | ||
replacer.visit(parmCoverExpression); | ||
@@ -137,7 +134,7 @@ node.initializer = parmCoverExpression; | ||
return; | ||
const replacer = new transformRange_1.RangeTransform(dec); | ||
const replacer = new RangeTransform(dec); | ||
// Generate id hash from information | ||
const funcId = util_1.createPointID(name, funcLine, funcCol, "CoverType.Function"); | ||
const funcId = createPointID(name, funcLine, funcCol, "CoverType.Function"); | ||
// Create declare statement | ||
const funcDeclareStatement = visitor_as_1.SimpleParser.parseStatement(`__coverDeclare("${name}", ${funcId}, ${funcLine}, ${funcCol}, CoverType.Function)`, true); | ||
const funcDeclareStatement = SimpleParser.parseStatement(`__coverDeclare("${name}", ${funcId}, ${funcLine}, ${funcCol}, CoverType.Function)`, true); | ||
replacer.visit(funcDeclareStatement); | ||
@@ -147,5 +144,5 @@ // Push declare stastement to globalStatements | ||
// If the body is an expression, convert it to a block. (We need it to be a block because of multiple lines.) | ||
if (dec.body.kind === as_1.NodeKind.EXPRESSION) { | ||
if (dec.body.kind === NodeKind.EXPRESSION) { | ||
// Parse string as BlockStatement. (We replace the function's body with this.) | ||
const bodyStatement = visitor_as_1.SimpleParser.parseStatement(` | ||
const bodyStatement = SimpleParser.parseStatement(` | ||
{ | ||
@@ -161,3 +158,3 @@ __cover(${funcId}); | ||
// Manipulate the arrowKind. Make sure it works. | ||
dec.arrowKind = 2 /* ARROW_SINGLE */; | ||
dec.arrowKind = 2 /* ArrowKind.ARROW_SINGLE */; | ||
// Set the return value as the origianal body. | ||
@@ -171,3 +168,3 @@ bodyReturn.value = body.expression; | ||
// Create a cover statement and add it to the top of the function body. | ||
const funcCoverStatement = visitor_as_1.SimpleParser.parseStatement(`__cover(${funcId})`, true); | ||
const funcCoverStatement = SimpleParser.parseStatement(`__cover(${funcId})`, true); | ||
replacer.visit(funcCoverStatement); | ||
@@ -200,11 +197,11 @@ // Grab the body and cast it as a BlockStatement | ||
// If its not a block, convert it to a Block kind. | ||
if (ifTrue.kind !== as_1.NodeKind.BLOCK && !this.ignoredLines.has(trueLine)) { | ||
const replacer = new transformRange_1.RangeTransform(ifTrue); | ||
if (ifTrue.kind !== NodeKind.BLOCK && !this.ignoredLines.has(trueLine)) { | ||
const replacer = new RangeTransform(ifTrue); | ||
// Get id from hash | ||
const ifTrueId = util_1.createPointID(name, trueLine, trueCol, "CoverType.Expression"); | ||
const ifTrueId = createPointID(name, trueLine, trueCol, "CoverType.Expression"); | ||
// Cover Declare statment | ||
const coverDeclareStatement = visitor_as_1.SimpleParser.parseStatement(`__coverDeclare("${name}", ${ifTrueId}, ${trueLine}, ${trueCol}, CoverType.Expression)`, true); | ||
const coverDeclareStatement = SimpleParser.parseStatement(`__coverDeclare("${name}", ${ifTrueId}, ${trueLine}, ${trueCol}, CoverType.Expression)`, true); | ||
replacer.visit(coverDeclareStatement); | ||
// Create new cover statement as a block. | ||
const coverStatement = visitor_as_1.SimpleParser.parseStatement(`{__cover(${ifTrueId})};`, true); | ||
const coverStatement = SimpleParser.parseStatement(`{__cover(${ifTrueId})};`, true); | ||
replacer.visit(coverStatement); | ||
@@ -227,12 +224,12 @@ // This pushes the old value right below the __cover(id) | ||
const falseCol = falseLc.col; | ||
if (ifFalse.kind !== as_1.NodeKind.BLOCK && | ||
if (ifFalse.kind !== NodeKind.BLOCK && | ||
!this.ignoredLines.has(falseLine)) { | ||
const replacer = new transformRange_1.RangeTransform(ifFalse); | ||
const replacer = new RangeTransform(ifFalse); | ||
// Create id from hash | ||
const ifFalseId = util_1.createPointID(name, falseLine, falseCol, "CoverType.Expression"); | ||
const ifFalseId = createPointID(name, falseLine, falseCol, "CoverType.Expression"); | ||
// Create coverDeclare statement | ||
const coverDeclareStatement = visitor_as_1.SimpleParser.parseStatement(`__coverDeclare("${name}", ${ifFalseId}, ${falseLine}, ${falseCol}, CoverType.Expression)`, true); | ||
const coverDeclareStatement = SimpleParser.parseStatement(`__coverDeclare("${name}", ${ifFalseId}, ${falseLine}, ${falseCol}, CoverType.Expression)`, true); | ||
replacer.visit(coverDeclareStatement); | ||
// Create new cover statement as a block. | ||
const coverStatement = visitor_as_1.SimpleParser.parseStatement(`{__cover(${ifFalseId})};`, true); | ||
const coverStatement = SimpleParser.parseStatement(`{__cover(${ifFalseId})};`, true); | ||
replacer.visit(coverStatement); | ||
@@ -270,3 +267,3 @@ // Add old body right after __cover(id) | ||
visitTernaryExpression(expr) { | ||
// Call to super | ||
// Call to super first. | ||
super.visitTernaryExpression(expr); | ||
@@ -284,10 +281,10 @@ // Cast the ifThen/Else into their own variables. (Prevents circularness) | ||
if (!this.ignoredLines.has(trueLine)) { | ||
const replacer = new transformRange_1.RangeTransform(trueExpression); | ||
const replacer = new RangeTransform(trueExpression); | ||
// Create id from hash | ||
const trueId = util_1.createPointID(name, trueLine, trueCol, "CoverType.Expression"); | ||
const trueId = createPointID(name, trueLine, trueCol, "CoverType.Expression"); | ||
// Create declare statement | ||
const trueDeclareStatement = visitor_as_1.SimpleParser.parseStatement(`__coverDeclare("${name}", ${trueId}, ${trueLine}, ${trueCol}, CoverType.Expression)`, true); | ||
const trueDeclareStatement = SimpleParser.parseStatement(`__coverDeclare("${name}", ${trueId}, ${trueLine}, ${trueCol}, CoverType.Expression)`, true); | ||
replacer.visit(trueDeclareStatement); | ||
// Create cover expression | ||
let trueCoverExpression = visitor_as_1.SimpleParser.parseExpression(`(__cover(${trueId}), $$REPLACE_ME)`); | ||
let trueCoverExpression = SimpleParser.parseExpression(`(__cover(${trueId}), $$REPLACE_ME)`); | ||
replacer.visit(trueCoverExpression); | ||
@@ -307,10 +304,10 @@ // Replace $$REPLACE_ME with the original value | ||
if (!this.ignoredLines.has(falseLine)) { | ||
const replacer = new transformRange_1.RangeTransform(falseExpression); | ||
const replacer = new RangeTransform(falseExpression); | ||
// Create id from hash | ||
const falseId = util_1.createPointID(name, falseLine, falseCol, "CoverType.Expression"); | ||
const falseId = createPointID(name, falseLine, falseCol, "CoverType.Expression"); | ||
// Create coverDeclare staterment | ||
const falseDeclareStatement = visitor_as_1.SimpleParser.parseStatement(`__coverDeclare("${name}", ${falseId}, ${falseLine}, ${falseCol}, CoverType.Expression)`, true); | ||
const falseDeclareStatement = SimpleParser.parseStatement(`__coverDeclare("${name}", ${falseId}, ${falseLine}, ${falseCol}, CoverType.Expression)`, true); | ||
replacer.visit(falseDeclareStatement); | ||
// Create cover expression and cast a ParenthesizedExpression | ||
const falseCoverExpression = visitor_as_1.SimpleParser.parseExpression(`(__cover(${falseId}), $$REPLACE_ME)`); | ||
const falseCoverExpression = SimpleParser.parseExpression(`(__cover(${falseId}), $$REPLACE_ME)`); | ||
replacer.visit(falseCoverExpression); | ||
@@ -339,10 +336,10 @@ // Replace $$REPLACE_ME with the original value | ||
return; | ||
const replacer = new transformRange_1.RangeTransform(stmt); | ||
const replacer = new RangeTransform(stmt); | ||
// Create id from hash | ||
const caseId = util_1.createPointID(name, caseLine, caseCol, "CoverType.Block"); | ||
const caseId = createPointID(name, caseLine, caseCol, "CoverType.Block"); | ||
// Create declare statement | ||
const caseDeclareStatement = visitor_as_1.SimpleParser.parseStatement(`__coverDeclare("${name}", ${caseId}, ${caseLine}, ${caseCol}, CoverType.Block)`, true); | ||
const caseDeclareStatement = SimpleParser.parseStatement(`__coverDeclare("${name}", ${caseId}, ${caseLine}, ${caseCol}, CoverType.Block)`, true); | ||
replacer.visit(caseDeclareStatement); | ||
// Create a cover statement | ||
const caseCoverStatement = visitor_as_1.SimpleParser.parseStatement(`__cover(${caseId})`); | ||
const caseCoverStatement = SimpleParser.parseStatement(`__cover(${caseId})`); | ||
replacer.visit(caseCoverStatement); | ||
@@ -369,10 +366,10 @@ // Push declare to global | ||
return; | ||
const replacer = new transformRange_1.RangeTransform(node); | ||
const replacer = new RangeTransform(node); | ||
// Create id from hash | ||
const blockCoverId = util_1.createPointID(name, blockLine, blockCol, "CoverType.Block"); | ||
const blockCoverId = createPointID(name, blockLine, blockCol, "CoverType.Block"); | ||
// Create declare statement | ||
const declareStatement = visitor_as_1.SimpleParser.parseStatement(`__coverDeclare("${name}", ${blockCoverId}, ${blockLine}, ${blockCol}, CoverType.Block)`, true); | ||
const declareStatement = SimpleParser.parseStatement(`__coverDeclare("${name}", ${blockCoverId}, ${blockLine}, ${blockCol}, CoverType.Block)`, true); | ||
replacer.visit(declareStatement); | ||
// Create cover statement | ||
const coverStatement = visitor_as_1.SimpleParser.parseStatement(`__cover(${blockCoverId})`); | ||
const coverStatement = SimpleParser.parseStatement(`__cover(${blockCoverId})`); | ||
replacer.visit(coverStatement); | ||
@@ -393,3 +390,3 @@ // Push declare to global | ||
// Create linecol function. (Base it off of the file text) | ||
this.linecol = line_column_1.default(text); | ||
this.linecol = linecol(text); | ||
// Find @as-covers: ignore comments (Regex) | ||
@@ -410,3 +407,4 @@ const foundIgnores = text.matchAll(ignoredRegex); | ||
} | ||
module.exports = class MyTransform extends as_1.Transform { | ||
// Transform class | ||
export default class MyTransform extends Transform { | ||
// Trigger the transform after parse. | ||
@@ -424,3 +422,4 @@ afterParse(parser) { | ||
} | ||
}; | ||
} | ||
; | ||
//# sourceMappingURL=index.js.map |
@@ -1,4 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createPointID = exports.djb2Hash = void 0; | ||
/** | ||
@@ -11,3 +8,3 @@ * A simple djb2hash that returns a hash of a given string. See http://www.cse.yorku.ca/~oz/hash.html | ||
*/ | ||
function djb2Hash(str) { | ||
export function djb2Hash(str) { | ||
const points = Array.from(str); | ||
@@ -20,8 +17,6 @@ let h = 5381; | ||
} | ||
exports.djb2Hash = djb2Hash; | ||
/** Create a code point id. */ | ||
function createPointID(file, line, col, type) { | ||
export function createPointID(file, line, col, type) { | ||
return djb2Hash(`${file}~${line}~${col}~${type}`); | ||
} | ||
exports.createPointID = createPointID; | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "@as-covers/transform", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "The transform that enables code coverage for assemblyscript", | ||
"main": "lib/index.js", | ||
"type": "module", | ||
"scripts": { | ||
@@ -15,9 +16,12 @@ "tsc": "tsc", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"assemblyscript": "^0.20.19" | ||
}, | ||
"devDependencies": { | ||
"@types/line-column": "^1.0.0", | ||
"typescript": "^4.3.4" | ||
"typescript": "^4.7.4" | ||
}, | ||
"dependencies": { | ||
"line-column": "^1.0.2", | ||
"visitor-as": "^0.6.0" | ||
"visitor-as": "0.10.0" | ||
}, | ||
@@ -27,3 +31,3 @@ "files": [ | ||
], | ||
"gitHead": "4194d38184f20687bd8e7124728368b69b84598f" | ||
"gitHead": "0991a3270e6539d985ebc669f607ebb1c140ff24" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Yes
40014
3
452
+ Addedassemblyscript@0.20.19(transitive)
+ Addedbinaryen@109.0.0-nightly.20220813(transitive)
+ Addedlong@5.2.3(transitive)
+ Addedts-mixer@6.0.4(transitive)
+ Addedvisitor-as@0.10.0(transitive)
- Removedassemblyscript@0.18.32(transitive)
- Removedbinaryen@100.0.0-nightly.20210413(transitive)
- Removedlong@4.0.0(transitive)
- Removedts-mixer@5.4.1(transitive)
- Removedvisitor-as@0.6.0(transitive)
Updatedvisitor-as@0.10.0