Skip to content

Commit

Permalink
match on asserts
Browse files Browse the repository at this point in the history
change Map to Record
improve test case
  • Loading branch information
sokra committed Jul 16, 2021
1 parent b166f46 commit 7f22e47
Show file tree
Hide file tree
Showing 20 changed files with 237 additions and 127 deletions.
7 changes: 7 additions & 0 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ declare module "browserslist" {
export = browserslist;
}

// TODO remove that when @types/estree is updated
interface ImportAttributeNode {
type: "ImportAttribute";
key: import("estree").Identifier | import("estree").Literal;
value: import("estree").Literal;
}

type TODO = any;

type RecursiveArrayOrRecord<T> =
Expand Down
8 changes: 6 additions & 2 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,13 @@ class NormalModule extends Module {
*/
identifier() {
if (this.layer === null) {
return this.request;
if (this.type === "javascript/auto") {
return this.request;
} else {
return `${this.type}|${this.request}`;
}
} else {
return `${this.request}|${this.layer}`;
return `${this.type}|${this.request}|${this.layer}`;
}
}

Expand Down
18 changes: 6 additions & 12 deletions lib/NormalModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ModuleGraph = require("./ModuleGraph");
const NormalModule = require("./NormalModule");
const BasicEffectRulePlugin = require("./rules/BasicEffectRulePlugin");
const BasicMatcherRulePlugin = require("./rules/BasicMatcherRulePlugin");
const DescriptionDataMatcherRulePlugin = require("./rules/DescriptionDataMatcherRulePlugin");
const ObjectMatcherRulePlugin = require("./rules/ObjectMatcherRulePlugin");
const RuleSetCompiler = require("./rules/RuleSetCompiler");
const UseEffectRulePlugin = require("./rules/UseEffectRulePlugin");
const LazySet = require("./util/LazySet");
Expand All @@ -44,7 +44,7 @@ const { parseResource } = require("./util/identifier");
* @property {ModuleFactoryCreateData["resolveOptions"]} resolveOptions
* @property {string} context
* @property {string} request
* @property {Map} assertions
* @property {Record<string, any> | undefined} assertions
* @property {ModuleDependency[]} dependencies
* @property {Object} createData
* @property {LazySet<string>} fileDependencies
Expand Down Expand Up @@ -183,14 +183,14 @@ const ruleSetCompiler = new RuleSetCompiler([
new BasicMatcherRulePlugin("issuer"),
new BasicMatcherRulePlugin("compiler"),
new BasicMatcherRulePlugin("issuerLayer"),
new DescriptionDataMatcherRulePlugin(),
new ObjectMatcherRulePlugin("assert", "assertions"),
new ObjectMatcherRulePlugin("descriptionData"),
new BasicEffectRulePlugin("type"),
new BasicEffectRulePlugin("sideEffects"),
new BasicEffectRulePlugin("parser"),
new BasicEffectRulePlugin("resolve"),
new BasicEffectRulePlugin("generator"),
new BasicEffectRulePlugin("layer"),
new BasicEffectRulePlugin("assert"),
new UseEffectRulePlugin()
]);

Expand Down Expand Up @@ -341,7 +341,7 @@ class NormalModuleFactory extends ModuleFactory {
context,
dependencies,
request,
assertions = new Map(),
assertions,
resolveOptions,
fileDependencies,
missingDependencies,
Expand Down Expand Up @@ -450,6 +450,7 @@ class NormalModuleFactory extends ModuleFactory {
resourceQuery: resourceDataForRules.query,
resourceFragment: resourceDataForRules.fragment,
scheme,
assertions,
mimetype: matchResourceData ? "" : resourceData.data.mimetype || "",
dependency: dependencyType,
descriptionData: matchResourceData
Expand Down Expand Up @@ -517,13 +518,6 @@ class NormalModuleFactory extends ModuleFactory {
} else {
type = "javascript/auto";
}
if (assertions.has("type") && type !== assertions.get("type")) {
throw new Error(
`type mismatch; requested type ${assertions.get(
"type"
)} but got ${type}`
);
}
}
const resolveOptions = settings.resolve;
const layer = settings.layer;
Expand Down
36 changes: 20 additions & 16 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,11 @@ const applyModuleDefaults = (
},
{
test: /\.json$/i,
type: "json",
assert: { type: "json" }
type: "json"
},
{
mimetype: "application/json",
type: "json",
assert: { type: "json" }
type: "json"
},
{
test: /\.mjs$/i,
Expand Down Expand Up @@ -484,18 +482,6 @@ const applyModuleDefaults = (
or: ["text/javascript", "application/javascript"]
},
...esm
},
{
dependency: "url",
oneOf: [
{
scheme: /^data$/,
type: "asset/inline"
},
{
type: "asset/resource"
}
]
}
];
if (asyncWebAssembly) {
Expand Down Expand Up @@ -543,6 +529,24 @@ const applyModuleDefaults = (
...wasm
});
}
rules.push(
{
dependency: "url",
oneOf: [
{
scheme: /^data$/,
type: "asset/inline"
},
{
type: "asset/resource"
}
]
},
{
assert: { type: "json" },
type: "json"
}
);
return rules;
});
};
Expand Down
9 changes: 6 additions & 3 deletions lib/dependencies/HarmonyExportDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency")
const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
const {
harmonySpecifierTag
harmonySpecifierTag,
getAssertions
} = require("./HarmonyImportDependencyParserPlugin");
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");

Expand Down Expand Up @@ -48,7 +49,8 @@ module.exports = class HarmonyExportDependencyParserPlugin {
parser.state.module.addPresentationalDependency(clearDep);
const sideEffectDep = new HarmonyImportSideEffectDependency(
source,
parser.state.lastHarmonyImportOrder
parser.state.lastHarmonyImportOrder,
getAssertions(statement)
);
sideEffectDep.loc = Object.create(statement.loc);
sideEffectDep.loc.index = -1;
Expand Down Expand Up @@ -127,7 +129,8 @@ module.exports = class HarmonyExportDependencyParserPlugin {
harmonyNamedExports,
null,
this.strictExportPresence,
null
null,
settings.assertions
);
} else {
dep = new HarmonyExportSpecifierDependency(id, name);
Expand Down
6 changes: 4 additions & 2 deletions lib/dependencies/HarmonyExportImportedSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
* @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency> | Iterable<HarmonyExportImportedSpecifierDependency>} otherStarExports other star exports in the module before this import
* @param {boolean} strictExportPresence when true, missing exports in the imported module lead to errors instead of warnings
* @param {HarmonyStarExportsList} allStarExports all star exports in the module
* @param {Record<string, any>=} assertions import assertions
*/
constructor(
request,
Expand All @@ -168,9 +169,10 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
activeExports,
otherStarExports,
strictExportPresence,
allStarExports
allStarExports,
assertions
) {
super(request, sourceOrder);
super(request, sourceOrder, assertions);

this.ids = ids;
this.name = name;
Expand Down
7 changes: 5 additions & 2 deletions lib/dependencies/HarmonyImportDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class HarmonyImportDependency extends ModuleDependency {
*
* @param {string} request request string
* @param {number} sourceOrder source order
* @param {Map} assertions import assertions
* @param {Record<string, any>=} assertions import assertions
*/
constructor(request, sourceOrder, assertions = new Map()) {
constructor(request, sourceOrder, assertions) {
super(request);
this.sourceOrder = sourceOrder;
this.assertions = assertions;
}

get category() {
Expand Down Expand Up @@ -202,12 +203,14 @@ class HarmonyImportDependency extends ModuleDependency {
serialize(context) {
const { write } = context;
write(this.sourceOrder);
write(this.assertions);
super.serialize(context);
}

deserialize(context) {
const { read } = context;
this.sourceOrder = read();
this.assertions = read();
super.deserialize(context);
}
}
Expand Down
49 changes: 36 additions & 13 deletions lib/dependencies/HarmonyImportDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const HarmonyExports = require("./HarmonyExports");
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");

/** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclaration */
/** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclaration */
/** @typedef {import("estree").Identifier} Identifier */
/** @typedef {import("estree").ImportDeclaration} ImportDeclaration */
/** @typedef {import("estree").ImportExpression} ImportExpression */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("../optimize/InnerGraph").InnerGraph} InnerGraph */
/** @typedef {import("../optimize/InnerGraph").TopLevelSymbol} TopLevelSymbol */
Expand All @@ -29,16 +33,30 @@ const harmonySpecifierTag = Symbol("harmony import");
* @property {number} sourceOrder
* @property {string} name
* @property {boolean} await
* @property {Record<string, any> | undefined} assertions
*/

function getAssertionMap(node) {
if (node.assertions === undefined) {
return new Map();
/**
* @param {ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration | ImportExpression} node node with assertions
* @returns {Record<string, any> | undefined} assertions
*/
function getAssertions(node) {
// TODO remove cast when @type/estree has been updated to import assertions
const assertions = /** @type {{ assertions?: ImportAttributeNode[] }} */ (
node
).assertions;
if (assertions === undefined) {
return undefined;
}
const result = {};
for (const assertion of assertions) {
const key =
assertion.key.type === "Identifier"
? assertion.key.name
: assertion.key.value;
result[key] = assertion.value.value;
}
return node.assertions.reduce((map, assert) => {
map.set(assert.key.name, assert.value.value);
return map;
}, new Map());
return result;
}

module.exports = class HarmonyImportDependencyParserPlugin {
Expand Down Expand Up @@ -75,11 +93,11 @@ module.exports = class HarmonyImportDependencyParserPlugin {
clearDep.loc = statement.loc;
parser.state.module.addPresentationalDependency(clearDep);
parser.unsetAsiPosition(statement.range[1]);
const asserts = getAssertionMap(statement);
const assertions = getAssertions(statement);
const sideEffectDep = new HarmonyImportSideEffectDependency(
source,
parser.state.lastHarmonyImportOrder,
asserts
assertions
);
sideEffectDep.loc = statement.loc;
parser.state.module.addDependency(sideEffectDep);
Expand All @@ -94,7 +112,8 @@ module.exports = class HarmonyImportDependencyParserPlugin {
name,
source,
ids,
sourceOrder: parser.state.lastHarmonyImportOrder
sourceOrder: parser.state.lastHarmonyImportOrder,
assertions: getAssertions(statement)
});
return true;
}
Expand All @@ -109,7 +128,8 @@ module.exports = class HarmonyImportDependencyParserPlugin {
settings.ids,
settings.name,
expr.range,
this.strictExportPresence
this.strictExportPresence,
settings.assertions
);
dep.shorthand = parser.scope.inShorthand;
dep.directImport = true;
Expand All @@ -130,7 +150,8 @@ module.exports = class HarmonyImportDependencyParserPlugin {
ids,
settings.name,
expr.range,
this.strictExportPresence
this.strictExportPresence,
settings.assertions
);
dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
dep.loc = expr.loc;
Expand All @@ -150,7 +171,8 @@ module.exports = class HarmonyImportDependencyParserPlugin {
ids,
settings.name,
callee.range,
this.strictExportPresence
this.strictExportPresence,
settings.assertions
);
dep.directImport = members.length === 0;
dep.call = true;
Expand Down Expand Up @@ -218,3 +240,4 @@ module.exports = class HarmonyImportDependencyParserPlugin {
};

module.exports.harmonySpecifierTag = harmonySpecifierTag;
module.exports.getAssertions = getAssertions;
12 changes: 10 additions & 2 deletions lib/dependencies/HarmonyImportSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
const idsSymbol = Symbol("HarmonyImportSpecifierDependency.ids");

class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
constructor(request, sourceOrder, ids, name, range, strictExportPresence) {
super(request, sourceOrder);
constructor(
request,
sourceOrder,
ids,
name,
range,
strictExportPresence,
assertions
) {
super(request, sourceOrder, assertions);
this.ids = ids;
this.name = name;
this.range = range;
Expand Down
10 changes: 8 additions & 2 deletions lib/dependencies/ModuleDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ class ModuleDependency extends Dependency {
this.request = request;
this.userRequest = request;
this.range = undefined;
this.assertions = new Map();
// assertions must be serialized by subclasses that use it
/** @type {Record<string, any> | undefined} */
this.assertions = undefined;
}

/**
* @returns {string | null} an identifier to merge equal requests
*/
getResourceIdentifier() {
return `module${this.request}`;
let str = `module${this.request}`;
if (this.assertions !== undefined) {
str += JSON.stringify(this.assertions);
}
return str;
}

/**
Expand Down
Loading

0 comments on commit 7f22e47

Please sign in to comment.