-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #456 from hildjj/library-mode
Add library mode
- Loading branch information
Showing
44 changed files
with
3,575 additions
and
4,711 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
module.exports = { | ||
"reject": [ | ||
"chai", // Moved to es6 | ||
"@types/chai", // Should match chai | ||
], | ||
}; |
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
Large diffs are not rendered by default.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// @ts-check | ||
"use strict"; | ||
|
||
/** | ||
* Intern strings or objects, so there is only one copy of each, by value. | ||
* Objects may need to be converted to another representation before storing. | ||
* Each inputs corresponds to a number, starting with 0. | ||
* | ||
* @template [T=string],[V=T] | ||
*/ | ||
class Intern { | ||
/** | ||
* @typedef {object} InternOptions | ||
* @property {(input: V) => string} [stringify=String] Represent the | ||
* converted input as a string, for value comparison. | ||
* @property {(input: T) => V} [convert=(x) => x] Convert the input to its | ||
* stored form. Required if type V is not the same as type T. Return | ||
* falsy value to have this input not be added; add() will return -1 in | ||
* this case. | ||
*/ | ||
|
||
/** | ||
* @param {InternOptions} [options] | ||
*/ | ||
constructor(options) { | ||
/** @type {Required<InternOptions>} */ | ||
this.options = { | ||
stringify: String, | ||
convert: x => /** @type {V} */ (/** @type {unknown} */ (x)), | ||
...options, | ||
}; | ||
/** @type {V[]} */ | ||
this.items = []; | ||
/** @type {Record<string, number>} */ | ||
this.offsets = {}; | ||
} | ||
|
||
/** | ||
* Intern an item, getting it's asssociated number. Returns -1 for falsy | ||
* inputs. O(1) with constants tied to the convert and stringify options. | ||
* | ||
* @param {T} input | ||
* @return {number} | ||
*/ | ||
add(input) { | ||
const c = this.options.convert(input); | ||
if (!c) { | ||
return -1; | ||
} | ||
const s = this.options.stringify(c); | ||
let num = this.offsets[s]; | ||
if (num === undefined) { | ||
num = this.items.push(c) - 1; | ||
this.offsets[s] = num; | ||
} | ||
return num; | ||
} | ||
|
||
/** | ||
* @param {number} i | ||
* @returns {V} | ||
*/ | ||
get(i) { | ||
return this.items[i]; | ||
} | ||
|
||
/** | ||
* @template U | ||
* @param {(value: V, index: number, array: V[]) => U} fn | ||
* @returns {U[]} | ||
*/ | ||
map(fn) { | ||
return this.items.map(fn); | ||
} | ||
} | ||
|
||
module.exports = Intern; |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// @ts-check | ||
"use strict"; | ||
|
||
/** | ||
* Generate trampoline stubs for each rule imported into this namespace. | ||
* | ||
* @example | ||
* import bar from "./lib.js" // Default rule imported into this namespace | ||
* import {baz} from "./lib.js" // One rule imported into this namespace by name | ||
* | ||
* @type {PEG.Pass} | ||
*/ | ||
function addImportedRules(ast) { | ||
let libraryNumber = 0; | ||
for (const imp of ast.imports) { | ||
for (const what of imp.what) { | ||
let original = undefined; | ||
switch (what.type) { | ||
case "import_binding_all": | ||
// Don't create stub. | ||
continue; | ||
case "import_binding_default": | ||
// Use the default (usually first) rule. | ||
break; | ||
case "import_binding": | ||
original = what.binding; | ||
break; | ||
case "import_binding_rename": | ||
original = what.rename; | ||
break; | ||
default: | ||
throw new TypeError("Unknown binding type"); | ||
} | ||
ast.rules.push({ | ||
type: "rule", | ||
name: what.binding, | ||
nameLocation: what.location, | ||
expression: { | ||
type: "library_ref", | ||
name: original, | ||
library: imp.from.module, | ||
libraryNumber, | ||
location: what.location, | ||
}, | ||
location: imp.from.location, | ||
}); | ||
} | ||
libraryNumber++; | ||
} | ||
} | ||
|
||
module.exports = addImportedRules; |
Oops, something went wrong.