fix(types): fix eslint erros and add support for custom index prop on acorn.SourceLocation objects

This commit is contained in:
Sean Larkin 2018-05-03 23:14:57 -07:00
parent 7728260095
commit ae73a0a6cd
18 changed files with 54 additions and 4 deletions

5
declarations.d.ts vendored
View File

@ -8,6 +8,11 @@ declare namespace NodeJS {
}
}
declare interface HarmonyDependencySourceLocation extends acorn.SourceLocation {
index: number;
offset?: (n: number) => acorn.Position;
}
// There are no typings for chrome-trace-event
declare module "chrome-trace-event" {
interface Event {

View File

@ -17,8 +17,11 @@ class Dependency {
this.module = null;
this.weak = false;
this.optional = false;
/** @type {SourceLocation=} */
/** @type {(SourceLocation|string|HarmonyDependencySourceLocation)=} */
this.loc = undefined;
/** @type {string} */
this.userRequest = undefined;
this.request = undefined;
}
getResourceIdentifier() {
@ -45,7 +48,8 @@ class Dependency {
}
/**
* @param {Hash} hash
* @param {Hash} hash Hash object created for module
* @returns {void}
*/
updateHash(hash) {
hash.update((this.module && this.module.id) + "");
@ -58,6 +62,7 @@ class Dependency {
/**
* @param {Dependency} a first Dependency to compare locations
* @param {Dependency} b second Dependency to compare locations
* @returns {1|0|-1|undefined} returns the sort index for compared items
*/
Dependency.compare = (a, b) => {
if (a.loc && b.loc) {

View File

@ -18,7 +18,10 @@ class JavascriptGenerator {
return new RawSource("throw new Error('No source available');");
}
const source = new ReplaceSource(originalSource);
// TODO: Update @types/webpack-sources
// for ReplaceSource to have optional
// 2nd contructor argument
const source = new ReplaceSource(originalSource, null);
this.sourceBlock(
module,

View File

@ -101,6 +101,9 @@ class Module extends DependenciesBlock {
/** @type {boolean} */
this.useSourceMap = false;
/** @type {Error=} */
this.error = undefined;
// info from build
this._source = null;
}

View File

@ -212,6 +212,9 @@ class NormalModule extends Module {
}
if (Buffer.isBuffer(source)) {
// @ts-ignore
// TODO: Update @types/webpack-sources to allow RawSource
// to accept a string | Buffer type
return new RawSource(source);
}

View File

@ -11,6 +11,8 @@ class ConstDependency extends NullDependency {
this.expression = expression;
this.range = range;
this.requireWebpackRequire = requireWebpackRequire;
/** @type {HarmonyDependencySourceLocation} */
this.loc = undefined;
}
updateHash(hash) {

View File

@ -9,6 +9,8 @@ class HarmonyCompatibilityDependency extends NullDependency {
constructor(originModule) {
super();
this.originModule = originModule;
/** @type {HarmonyDependencySourceLocation=} */
this.loc = undefined;
}
get type() {

View File

@ -19,6 +19,11 @@ module.exports = class HarmonyDetectionParserPlugin {
if (isHarmony) {
const module = parser.state.module;
const compatDep = new HarmonyCompatibilityDependency(module);
// @ts-ignore
// TODO: These are "hand crafted" HarmonyDependencySourceLocation objects
// which technically extend acorn.SourceLocation. We should ideally have a
// way for Acorn to either construct these objects given the start+end
// or have a more accurate type definition for this
compatDep.loc = {
start: {
line: -1,
@ -32,6 +37,11 @@ module.exports = class HarmonyDetectionParserPlugin {
};
module.addDependency(compatDep);
const initDep = new HarmonyInitDependency(module);
// @ts-ignore
// TODO: These are "hand crafted" HarmonyDependencySourceLocation objects
// which technically extend acorn.SourceLocation. We should ideally have a
// way for Acorn to either construct these objects given the start+end
// or have a more accurate type definition for this
initDep.loc = {
start: {
line: -1,

View File

@ -24,6 +24,7 @@ module.exports = class HarmonyExportDependencyParserPlugin {
statement.declaration && statement.declaration.range,
statement.range
);
/** @type {HarmonyDependencySourceLocation} */
dep.loc = Object.create(statement.loc);
dep.loc.index = -1;
parser.state.current.addDependency(dep);

View File

@ -11,6 +11,8 @@ class HarmonyExportExpressionDependency extends NullDependency {
this.originModule = originModule;
this.range = range;
this.rangeStatement = rangeStatement;
/** @type {HarmonyDependencySourceLocation=} */
this.loc = undefined;
}
get type() {

View File

@ -10,6 +10,8 @@ class HarmonyExportHeaderDependency extends NullDependency {
super();
this.range = range;
this.rangeStatement = rangeStatement;
/** @type {HarmonyDependencySourceLocation=} */
this.loc = undefined;
}
get type() {

View File

@ -41,6 +41,8 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
this.activeExports = activeExports;
this.otherStarExports = otherStarExports;
this.strictExportPresence = strictExportPresence;
/** @type {HarmonyDependencySourceLocation=} */
this.loc = undefined;
}
get type() {

View File

@ -11,6 +11,8 @@ class HarmonyExportSpecifierDependency extends NullDependency {
this.originModule = originModule;
this.id = id;
this.name = name;
/** @type {HarmonyDependencySourceLocation=} */
this.loc = undefined;
}
get type() {

View File

@ -8,6 +8,8 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
class HarmonyImportSideEffectDependency extends HarmonyImportDependency {
constructor(request, originModule, sourceOrder, parserScope) {
super(request, originModule, sourceOrder, parserScope);
/** @type {HarmonyDependencySourceLocation=} */
this.loc = undefined;
}
getReference() {

View File

@ -10,6 +10,8 @@ class HarmonyInitDependency extends NullDependency {
constructor(originModule) {
super();
this.originModule = originModule;
/** @type {HarmonyDependencySourceLocation=} */
this.loc = undefined;
}
get type() {

View File

@ -11,6 +11,8 @@ class ModuleHotAcceptDependency extends ModuleDependency {
super(request);
this.range = range;
this.weak = true;
/** @type {HarmonyDependencySourceLocation=} */
this.loc = undefined;
}
get type() {

View File

@ -11,6 +11,8 @@ class ModuleHotDeclineDependency extends ModuleDependency {
super(request);
this.range = range;
this.weak = true;
/** @type {HarmonyDependencySourceLocation} */
this.loc = undefined;
}
get type() {

View File

@ -621,7 +621,7 @@ class ConcatenatedModule extends Module {
});
const globalScope = scopeManager.acquire(ast);
const moduleScope = globalScope.childScopes[0];
const resultSource = new ReplaceSource(source);
const resultSource = new ReplaceSource(source, null);
info.ast = ast;
info.internalSource = source;
info.source = resultSource;