本文整理匯總了TypeScript中source-map.SourceMapConsumer.eachMapping方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript SourceMapConsumer.eachMapping方法的具體用法?TypeScript SourceMapConsumer.eachMapping怎麽用?TypeScript SourceMapConsumer.eachMapping使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類source-map.SourceMapConsumer
的用法示例。
在下文中一共展示了SourceMapConsumer.eachMapping方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: testMapping
async function testMapping(
sourcemap: RawSourceMap, html: string, name: string) {
const consumer = new SourceMapConsumer(sourcemap!);
let foundMapping = false;
const mappings: MappingItem[] = [];
consumer.eachMapping((mapping) => mappings.push(mapping));
for (let j = 0; j < mappings.length; j++) {
if (mappings[j].name === name) {
foundMapping = true;
const generatedLine = getLine(html, mappings[j].generatedLine);
assert(generatedLine, 'generated line not found');
assert.equal(
mappings[j].generatedColumn,
generatedLine!.indexOf(name),
'generated column');
const originalContents =
await urlLoader.load(mappings[j].source as ResolvedUrl);
const originalLine =
getLine(originalContents, mappings[j].originalLine);
assert(originalLine, 'original line not found');
assert.equal(
mappings[j].originalColumn,
originalLine!.indexOf(name),
'original column');
}
}
}
示例2: offsetSourceMap
function offsetSourceMap(
sourcemap: RawSourceMap, lineOffset: number, firstLineCharOffset: number) {
const consumer = new SourceMapConsumer(sourcemap);
const generator = new SourceMapGenerator();
consumer.eachMapping((mapping) => {
if (typeof mapping.originalLine !== 'number' ||
typeof mapping.originalColumn !== 'number') {
return;
}
const newMapping: Mapping = {
source: mapping.source,
generated: {
line: mapping.generatedLine + lineOffset,
column: mapping.generatedColumn +
(mapping.generatedLine === 1 ? firstLineCharOffset : 0)
},
original: {line: mapping.originalLine, column: mapping.originalColumn}
};
if (mapping.name) {
newMapping.name = mapping.name;
}
generator.addMapping(newMapping);
});
return generator.toJSON();
}
示例3: LookupPath
/**
* Uses the source map to determine which mappings are associated with a certain JSON path.
* @param path The JSON path to lookup.
*/
public LookupPath(path: JsonPath): sourceMap.MappingItem[] {
const result: sourceMap.MappingItem[] = [];
this.consumer.eachMapping(mi => {
const itemPath = this.ExtractJsonPath(mi);
if (itemPath && itemPath.length === path.length && itemPath.every((part, index) => itemPath[index].toString() === path[index].toString())) {
result.push(mi);
}
});
return result;
}
示例4: LookupBackwards
/**
* Uses the source map to determine which locations in the source files where influenced by given location in the generated file.
* @param line Line in the generated file having influenced the generated file.
* @param column Column in the generated file having influenced the generated file.
*/
public LookupBackwards(line: number, column: number): { file: string, line: number, column: number, path: JsonPath | null }[] {
const sameLineResults: sourceMap.MappingItem[] = [];
this.consumer.eachMapping(mi => {
if (mi.generatedLine === line && mi.generatedColumn <= column) {
sameLineResults.push(mi);
}
});
const maxColumn = sameLineResults.map(mi => mi.generatedColumn).reduce((a, b) => Math.max(a, b), 0);
return sameLineResults
.filter(mi => mi.generatedColumn === maxColumn)
.map(mi => { return { file: mi.source, line: mi.originalLine, column: mi.originalColumn, path: this.ExtractJsonPath(mi) }; });
}
示例5: mappingItemsOf
function mappingItemsOf(text: string): MappingItem[] {
// find the source map:
const sourceMapMatch = /sourceMappingURL\=data\:application\/json;base64,(.*)$/.exec(text);
const sourceMapBase64 = sourceMapMatch ![1];
const sourceMapBuffer = Buffer.from(sourceMapBase64, 'base64');
const sourceMapText = sourceMapBuffer.toString('utf8');
const sourceMap: RawSourceMap = JSON.parse(sourceMapText);
const consumer = new SourceMapConsumer(sourceMap);
const mappings: MappingItem[] = [];
consumer.eachMapping(mapping => { mappings.push(mapping); });
return mappings;
}
示例6: merge
export function merge(
oldMap: RawSourceMap,
newMap: RawSourceMap
): RawSourceMap {
if (!oldMap) return newMap
if (!newMap) return oldMap
const oldMapConsumer: SourceMapConsumer = new SourceMapConsumer(oldMap) as any
const newMapConsumer: SourceMapConsumer = new SourceMapConsumer(newMap) as any
const mergedMapGenerator = new SourceMapGenerator()
// iterate on new map and overwrite original position of new map with one of old map
newMapConsumer.eachMapping((mapping: MappingItem) => {
// pass when `originalLine` is null.
// It occurs in case that the node does not have origin in original code.
if (mapping.originalLine == null) return
const origPosInOldMap = oldMapConsumer.originalPositionFor({
line: mapping.originalLine,
column: mapping.originalColumn
})
if (origPosInOldMap.source == null) return
mergedMapGenerator.addMapping({
original: {
line: origPosInOldMap.line,
column: origPosInOldMap.column
},
generated: {
line: mapping.generatedLine,
column: mapping.generatedColumn
},
source: origPosInOldMap.source,
name: origPosInOldMap.name
} as any)
})
const maps = [oldMap, newMap]
const consumers = [oldMapConsumer, newMapConsumer]
consumers.forEach((consumer, index) => {
maps[index].sources.forEach(sourceFile => {
const sourceContent = consumer.sourceContentFor(sourceFile, true)
if (sourceContent !== null) {
mergedMapGenerator.setSourceContent(sourceFile, sourceContent)
}
})
})
return JSON.parse(mergedMapGenerator.toString())
}
示例7: getMappedSegments
export function getMappedSegments(
env: NgtscTestEnvironment, generatedFileName: string): SegmentMapping[] {
const generated = new TestSourceFile(generatedFileName, env.getContents(generatedFileName));
const sourceMapFileName = generated.getSourceMapFileName(generated.contents);
const sources = new Map<string, TestSourceFile>();
const mappings: MappingItem[] = [];
const mapContents = env.getContents(sourceMapFileName);
const sourceMapConsumer = new SourceMapConsumer(JSON.parse(mapContents));
sourceMapConsumer.eachMapping(item => {
if (!sources.has(item.source)) {
sources.set(item.source, new TestSourceFile(item.source, env.getContents(item.source)));
}
mappings.push(item);
});
const segments: SegmentMapping[] = [];
let currentMapping = mappings.shift();
while (currentMapping) {
const nextMapping = mappings.shift();
if (nextMapping) {
const source = sources.get(currentMapping.source) !;
const segment = {
generated: generated.getSegment('generated', currentMapping, nextMapping),
source: source.getSegment('original', currentMapping, nextMapping),
sourceUrl: source.url
};
if (segment.generated !== segment.source) {
segments.push(segment);
}
}
currentMapping = nextMapping;
}
return segments;
}
示例8: it
it("should compose with inputSourceMap", function() {
function addUseStrict(ast: any) {
return recast.visit(ast, {
visitFunction: function(path) {
path.get("body", "body").unshift(
b.expressionStatement(b.literal("use strict"))
);
this.traverse(path);
}
});
}
function stripConsole(ast: any) {
return recast.visit(ast, {
visitCallExpression: function(path) {
var node = path.value;
if (n.MemberExpression.check(node.callee) &&
n.Identifier.check(node.callee.object) &&
node.callee.object.name === "console") {
n.ExpressionStatement.assert(path.parent.node);
path.parent.replace();
return false;
}
return;
}
});
}
var code = [
"function add(a, b) {",
" var sum = a + b;",
" console.log(a, b);",
" return sum;",
"}"
].join(eol);
var ast = parse(code, {
sourceFileName: "original.js"
});
var useStrictResult = new Printer({
sourceMapName: "useStrict.map.json"
}).print(addUseStrict(ast));
var useStrictAst = parse(useStrictResult.code, {
sourceFileName: "useStrict.js"
});
var oneStepResult = new Printer({
sourceMapName: "oneStep.map.json"
}).print(stripConsole(ast));
var twoStepResult = new Printer({
sourceMapName: "twoStep.map.json",
inputSourceMap: useStrictResult.map
}).print(stripConsole(useStrictAst));
assert.strictEqual(
oneStepResult.code,
twoStepResult.code
);
var smc1 = new sourceMap.SourceMapConsumer(oneStepResult.map);
var smc2 = new sourceMap.SourceMapConsumer(twoStepResult.map);
smc1.eachMapping(function(mapping) {
var pos = {
line: mapping.generatedLine,
column: mapping.generatedColumn
};
var orig1 = smc1.originalPositionFor(pos);
var orig2 = smc2.originalPositionFor(pos);
// The composition of the source maps generated separately from
// the two transforms should be equivalent to the source map
// generated from the composition of the two transforms.
assert.deepEqual(orig1, orig2);
// Make sure the two-step source map refers back to the original
// source instead of the intermediate source.
assert.strictEqual(orig2.source, "original.js");
});
});