MCPcopy
hub / github.com/pugjs/pug / link

Function link

packages/pug-linker/index.js:11–69  ·  view source on GitHub ↗
(ast)

Source from the content-addressed store, hash-verified

9
10module.exports = link;
11function link(ast) {
12 assert(
13 ast.type === 'Block',
14 'The top level element should always be a block'
15 );
16 var extendsNode = null;
17 if (ast.nodes.length) {
18 var hasExtends = ast.nodes[0].type === 'Extends';
19 checkExtendPosition(ast, hasExtends);
20 if (hasExtends) {
21 extendsNode = ast.nodes.shift();
22 }
23 }
24 ast = applyIncludes(ast);
25 ast.declaredBlocks = findDeclaredBlocks(ast);
26 if (extendsNode) {
27 var mixins = [];
28 var expectedBlocks = [];
29 ast.nodes.forEach(function addNode(node) {
30 if (node.type === 'NamedBlock') {
31 expectedBlocks.push(node);
32 } else if (node.type === 'Block') {
33 node.nodes.forEach(addNode);
34 } else if (node.type === 'Mixin' && node.call === false) {
35 mixins.push(node);
36 } else {
37 error(
38 'UNEXPECTED_NODES_IN_EXTENDING_ROOT',
39 'Only named blocks and mixins can appear at the top level of an extending template',
40 node
41 );
42 }
43 });
44 var parent = link(extendsNode.file.ast);
45 extend(parent.declaredBlocks, ast);
46 var foundBlockNames = [];
47 walk(parent, function(node) {
48 if (node.type === 'NamedBlock') {
49 foundBlockNames.push(node.name);
50 }
51 });
52 expectedBlocks.forEach(function(expectedBlock) {
53 if (foundBlockNames.indexOf(expectedBlock.name) === -1) {
54 error(
55 'UNEXPECTED_BLOCK',
56 'Unexpected block ' + expectedBlock.name,
57 expectedBlock
58 );
59 }
60 });
61 Object.keys(ast.declaredBlocks).forEach(function(name) {
62 parent.declaredBlocks[name] = ast.declaredBlocks[name];
63 });
64 parent.nodes = mixins.concat(parent.nodes);
65 parent.hasExtends = true;
66 return parent;
67 }
68 return ast;

Callers 4

applyIncludesFunction · 0.85
testDirFunction · 0.85
testDirErrorFunction · 0.85
compileBodyFunction · 0.85

Calls 5

checkExtendPositionFunction · 0.85
applyIncludesFunction · 0.85
findDeclaredBlocksFunction · 0.85
errorFunction · 0.85
extendFunction · 0.85

Tested by 2

testDirFunction · 0.68
testDirErrorFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…