Skip to content

Commit e9db83c

Browse files
committed
add serializer for acorn Position
1 parent 601824e commit e9db83c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/util/registerExternalSerializer.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const ObjectMiddleware = require("../serialization/ObjectMiddleware");
99

10+
const Position = /** @type {TODO} */ (require("acorn")).Position;
1011
const SourceLocation = require("acorn").SourceLocation;
1112
const CachedSource = require("webpack-sources").CachedSource;
1213
const ConcatSource = require("webpack-sources").ConcatSource;
@@ -17,6 +18,7 @@ const ReplaceSource = require("webpack-sources").ReplaceSource;
1718
const SourceMapSource = require("webpack-sources").SourceMapSource;
1819

1920
/** @typedef {import("../Dependency").RealDependencyLocation} RealDependencyLocation */
21+
/** @typedef {import("../Dependency").SourcePosition} SourcePosition */
2022

2123
const CURRENT_MODULE = "webpack/lib/util/registerExternalSerializer";
2224

@@ -234,6 +236,34 @@ ObjectMiddleware.register(
234236
}()
235237
);
236238

239+
ObjectMiddleware.register(
240+
Position,
241+
CURRENT_MODULE,
242+
"acorn/Position",
243+
new class PositionSerializer {
244+
/**
245+
* @param {Position} pos the position to be serialized
246+
* @param {ObjectMiddleware.ObjectSerializerContext} context context
247+
* @returns {void}
248+
*/
249+
serialize(pos, { write }) {
250+
write(pos.line);
251+
write(pos.column);
252+
}
253+
254+
/**
255+
* @param {ObjectMiddleware.ObjectDeserializerContext} context context
256+
* @returns {SourcePosition} position
257+
*/
258+
deserialize({ read }) {
259+
return {
260+
line: read(),
261+
column: read()
262+
};
263+
}
264+
}()
265+
);
266+
237267
ObjectMiddleware.register(
238268
SourceMapSource,
239269
CURRENT_MODULE,

tooling/format-file-header.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ const schema = [
8282
},
8383
{
8484
title: "imports",
85-
regexp: /(const (\{\s+\w+(,\s+\w+)*\s+\}|\w+) = require\("[^"]+"\)(\.\w+)*;\n)+\n/g,
85+
regexp: /(const (\{\s+\w+(,\s+\w+)*\s+\}|\w+) = (\/\*\* @type \{TODO\} \*\/\s\()?require\("[^"]+"\)\)?(\.\w+)*;\n)+\n/g,
8686
updateMessage: "sort imports alphabetically",
8787
update(content) {
8888
const items = execToArray(
8989
content,
90-
/const (?:\{\s+\w+(?:,\s+\w+)*\s+\}|\w+) = require\("([^"]+)"\)((?:\.\w+)*);\n/g
90+
/const (?:\{\s+\w+(?:,\s+\w+)*\s+\}|\w+) = (?:\/\*\* @type \{TODO\} \*\/\s\()?require\("([^"]+)"\)\)?((?:\.\w+)*);\n/g
9191
);
9292
items.sort(sortImport);
9393
return items.map(item => item.content).join("") + "\n";

0 commit comments

Comments
 (0)