new changes passes the tests

This commit is contained in:
2026-01-25 00:06:55 +05:30
parent 27c4898706
commit 776f8a68f5
844 changed files with 29 additions and 12121 deletions

View File

@@ -2299,7 +2299,13 @@ pub const Lexer = struct {
// consume line along with `\n` prefix
string_ptr = line_end;
// Extract text after the indent
const text_content = if (str.len > indents) str[indents..] else "";
// For whitespace-only lines, preserve the whitespace as-is (don't subtract indent)
const text_content = if (trimmed.len == 0)
str // Preserve whitespace-only lines exactly
else if (str.len > indents)
str[indents..]
else
"";
tokens_list.append(self.allocator, text_content) catch return false;
} else if (line_indent > self.indent_stack.items[0]) {
// line is indented less than the first line but is still indented
@@ -2328,8 +2334,17 @@ pub const Lexer = struct {
if (ii < token_indent_list.items.len and token_indent_list.items[ii]) {
self.incrementColumn(indents);
}
// For pipeless text, emit empty text tokens to preserve blank lines
// (addText skips empty content, but blank lines need to be preserved)
if (token_text.len == 0) {
var empty_token = self.tok(.text, .none);
empty_token.val = .{ .string = "" };
self.tokens.append(self.allocator, empty_token) catch return false;
self.tokEnd(&empty_token);
} else {
self.addText(.text, token_text, "", 0);
}
}
var end_token = self.tok(.end_pipeless_text, .none);
self.tokEnd(&end_token);

Binary file not shown.

View File

View File

@@ -1,26 +0,0 @@
{"type":"doctype","line":1,"val":"html"}
{"type":"newline","line":2}
{"type":"tag","line":2,"val":"html","selfClosing":false}
{"type":"indent","line":3,"val":2}
{"type":"tag","line":3,"val":"body","selfClosing":false}
{"type":"indent","line":4,"val":3}
{"type":"code","line":4,"val":"var s = 'this'","escape":false,"buffer":false}
{"type":"newline","line":5}
{"type":"case","line":5,"val":"s"}
{"type":"indent","line":6,"val":5}
{"type":"comment","line":6,"val":" Comment","buffer":false}
{"type":"newline","line":7}
{"type":"when","line":7,"val":"'this'"}
{"type":"indent","line":8,"val":7}
{"type":"tag","line":8,"val":"p","selfClosing":false}
{"type":"text","line":8,"val":"It's this!"}
{"type":"outdent","line":9}
{"type":"when","line":9,"val":"'that'"}
{"type":"indent","line":10,"val":7}
{"type":"tag","line":10,"val":"p","selfClosing":false}
{"type":"text","line":10,"val":"It's that!"}
{"type":"outdent","line":10}
{"type":"outdent","line":10}
{"type":"outdent","line":10}
{"type":"outdent","line":10}
{"type":"eos","line":10}

View File

@@ -1,76 +0,0 @@
{"type":"newline","line":2}
{"type":"comment","line":2,"val":" foo","buffer":true}
{"type":"newline","line":3}
{"type":"tag","line":3,"val":"ul","selfClosing":false}
{"type":"indent","line":4,"val":2}
{"type":"comment","line":4,"val":" bar","buffer":true}
{"type":"newline","line":5}
{"type":"tag","line":5,"val":"li","selfClosing":false}
{"type":"text","line":5,"val":"one"}
{"type":"newline","line":6}
{"type":"comment","line":6,"val":" baz","buffer":true}
{"type":"newline","line":7}
{"type":"tag","line":7,"val":"li","selfClosing":false}
{"type":"text","line":7,"val":"two"}
{"type":"outdent","line":9}
{"type":"comment","line":9,"val":"","buffer":true}
{"type":"start-pipeless-text","line":9}
{"type":"text","line":10,"val":"ul"}
{"type":"newline","line":11}
{"type":"text","line":11,"val":" li foo"}
{"type":"newline","line":12}
{"type":"text","line":12,"val":""}
{"type":"end-pipeless-text","line":12}
{"type":"newline","line":13}
{"type":"comment","line":13,"val":" block","buffer":true}
{"type":"start-pipeless-text","line":13}
{"type":"text","line":14,"val":"// inline follow"}
{"type":"newline","line":15}
{"type":"text","line":15,"val":"li three"}
{"type":"newline","line":16}
{"type":"text","line":16,"val":""}
{"type":"end-pipeless-text","line":16}
{"type":"newline","line":17}
{"type":"comment","line":17,"val":" block","buffer":true}
{"type":"start-pipeless-text","line":17}
{"type":"text","line":18,"val":"// inline followed by tags"}
{"type":"newline","line":19}
{"type":"text","line":19,"val":"ul"}
{"type":"newline","line":20}
{"type":"text","line":20,"val":" li four"}
{"type":"newline","line":21}
{"type":"text","line":21,"val":""}
{"type":"end-pipeless-text","line":21}
{"type":"newline","line":22}
{"type":"comment","line":22,"val":"if IE lt 9","buffer":true}
{"type":"start-pipeless-text","line":22}
{"type":"text","line":23,"val":"// inline"}
{"type":"newline","line":24}
{"type":"text","line":24,"val":"script(src='/lame.js')"}
{"type":"newline","line":25}
{"type":"text","line":25,"val":"// end-inline"}
{"type":"newline","line":26}
{"type":"text","line":26,"val":""}
{"type":"end-pipeless-text","line":26}
{"type":"newline","line":27}
{"type":"comment","line":27,"val":"","buffer":false}
{"type":"text","line":28,"val":""}
{"type":"start-pipeless-text","line":28}
{"type":"text","line":29,"val":"asfd"}
{"type":"newline","line":30}
{"type":"text","line":30,"val":""}
{"type":"end-pipeless-text","line":30}
{"type":"newline","line":31}
{"type":"comment","line":31,"val":"","buffer":false}
{"type":"newline","line":32}
{"type":"text","line":32,"val":" "}
{"type":"newline","line":33}
{"type":"text","line":33,"val":"asfd"}
{"type":"newline","line":35}
{"type":"tag","line":35,"val":"p","selfClosing":false}
{"type":"text","line":35,"val":"five"}
{"type":"newline","line":37}
{"type":"class","line":37,"val":"foo"}
{"type":"text","line":37,"val":"// not a comment"}
{"type":"newline","line":38}
{"type":"eos","line":38}

View File

@@ -1,19 +0,0 @@
{"type":"comment","line":1,"val":"","buffer":false}
{"type":"start-pipeless-text","line":1}
{"type":"text","line":2,"val":"s/s."}
{"type":"newline","line":3}
{"type":"text","line":3,"val":""}
{"type":"end-pipeless-text","line":3}
{"type":"newline","line":4}
{"type":"comment","line":4,"val":" test/cases/comments.source.pug","buffer":false}
{"type":"text","line":5,"val":""}
{"type":"newline","line":6}
{"type":"comment","line":6,"val":"","buffer":false}
{"type":"start-pipeless-text","line":6}
{"type":"text","line":7,"val":"test/cases/comments.source.pug"}
{"type":"newline","line":8}
{"type":"text","line":8,"val":"when"}
{"type":"newline","line":9}
{"type":"text","line":9,"val":"()"}
{"type":"end-pipeless-text","line":9}
{"type":"eos","line":9}

View File

@@ -1,3 +0,0 @@
{"type":"comment","line":1,"val":"","buffer":false}
{"type":"comment","line":2,"val":"","buffer":false}
{"type":"eos","line":3}

View File

@@ -1,4 +0,0 @@
{"type":"comment","line":1,"val":"","buffer":false}
{"type":"end-pipeless-text","line":4}
{"type":"newline","line":4}
{"type":"eos","line":5}

View File

@@ -1,6 +0,0 @@
{"type":"comment","line":1,"val":"","buffer":false}
{"type":"start-pipeless-text","line":2}
{"type":"start-pipeless-text","line":3}
{"type":"end-pipeless-text","line":4}
{"type":"newline","line":4}
{"type":"eos","line":5}

View File

@@ -1,51 +0,0 @@
'use strict';
var fs = require('fs');
var assert = require('assert');
var lineJSON = require('line-json');
var strip = require('../');
var dir = __dirname + '/cases/';
fs.readdirSync(dir).forEach(function(testCase) {
if (/\.input\.json$/.test(testCase)) {
test(testCase, function() {
var stem = testCase.replace(/\.input\.json$/, '.');
function test(name, options) {
var input = fs.readFileSync(dir + testCase, 'utf8');
input = lineJSON.parse(input);
var result = strip(input, options);
expect(result).toMatchSnapshot();
}
test('unbuffered');
test('buffered', {stripBuffered: true, stripUnbuffered: false});
test('both', {stripBuffered: true});
});
}
});
var edir = __dirname + '/errors/';
fs.readdirSync(edir).forEach(function(testCase) {
if (/\.input\.json$/.test(testCase)) {
test(testCase, function() {
var stem = testCase.replace(/\.input\.json$/, '.');
var input = fs.readFileSync(edir + testCase, 'utf8');
input = lineJSON.parse(input);
try {
strip(input);
throw new Error('Expected ' + testCase + ' to throw an exception.');
} catch (ex) {
if (!ex || !ex.code || ex.code.indexOf('PUG:') !== 0) throw ex;
expect({
msg: ex.msg,
code: ex.code,
line: ex.line,
}).toMatchSnapshot();
}
});
}
});

View File

@@ -1,14 +0,0 @@
lib-cov
coverage
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
pids
logs
results
npm-debug.log
node_modules

View File

@@ -1,17 +0,0 @@
language: node_js
sudo: false
node_js:
- "0.10"
- "0.12"
- "4"
- "6"
after_success:
- npm run coverage
- npm i codecov
- codecov -f ./coverage/lcov.info
notifications:
email:
on_success: never

View File

@@ -1,4 +0,0 @@
1.0.0 / 2016-08-22
==================
* Initial stable release

View File

@@ -1,19 +0,0 @@
Copyright (c) 2015 Forbes Lindesay
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -1,138 +0,0 @@
# pug-walk
Walk and transform a Pug AST
[![Build Status](https://img.shields.io/travis/pugjs/pug-walk/master.svg)](https://travis-ci.org/pugjs/pug-walk)
[![Dependencies Status](https://david-dm.org/pugjs/pug/status.svg?path=packages/pug-walk)](https://david-dm.org/pugjs/pug?path=packages/pug-walk)
[![DevDependencies Status](https://david-dm.org/pugjs/pug/dev-status.svg?path=packages/pug-walk)](https://david-dm.org/pugjs/pug?path=packages/pug-walk&type=dev)
[![npm version](https://img.shields.io/npm/v/pug-walk.svg)](https://www.npmjs.org/package/pug-walk)
[![Coverage Status](https://img.shields.io/codecov/c/github/pugjs/pug-walk/master.svg)](https://codecov.io/gh/pugjs/pug-walk/branch/master)
## Installation
npm install pug-walk
## Usage
```js
var walk = require('pug-walk');
```
### `walk(ast, before, after, options)`
Traverse and optionally transform a [Pug AST](https://github.com/pugjs/pug-ast-spec).
`ast` is not cloned, so any changes done to it will be done directly on the AST provided.
`before` and `after` are functions with the signature `(node, replace)`. `before` is called when a node is first seen, while `after` is called after the children of the node (if any) have already been traversed.
The `replace` parameter is a function that can be used to replace the node in the AST. It takes either an object or an array as its only parameter. If an object is specified, the current node is replaced by the parameter in the AST. If an array is specified and the ancestor of the current node allows such an operation, the node is replaced by all of the nodes in the specified array. This way, you can remove and add new nodes adjacent to the current node. Whether the parent node allows array operation is indicated by the property `replace.arrayAllowed`, which is set to true when the parent is a Block and when the parent is a Include and the node is an IncludeFilter.
If `before` returns `false`, the children of this node will not be traversed and left unchanged (unless `replace` has been called). Otherwise, the returned value of `before` is ignored. The returned value of `after` is always ignored. If `replace()` is called in `before()` with an array, and `before()` does not return `false`, the nodes in the array are still descended.
`options` can contain the following properties:
* `includeDependencies` (boolean): Walk the AST of a loaded dependent file (i.e., includes and extends). Defaults to `false`.
* `parents` (array<Node>): Nodes that are ancestors to the current `ast`. This option is used mainly internally, and users usually do not have to specify it. Defaults to `[]`.
```js
var lex = require('pug-lexer');
var parse = require('pug-parser');
// Changing content of all Text nodes
// ==================================
var source = '.my-class foo';
var dest = '.my-class bar';
var ast = parse(lex(source));
ast = walk(ast, function before(node, replace) {
if (node.type === 'Text') {
node.val = 'bar';
// Alternatively, you can replace the entire node
// rather than just the text.
// replace({ type: 'Text', val: 'bar', line: node.line });
}
}, {
includeDependencies: true
});
assert.deepEqual(parse(lex(dest)), ast);
// Convert all simple <strong> elements to text
// ============================================
var source = 'p abc #[strong NO]\nstrong on its own line';
var dest = 'p abc #[| NO]\n| on its own line';
var ast = parse(lex(source));
ast = walk(ast, function before(node, replace) {
// Find all <strong> tags
if (node.type === 'Tag' && node.name === 'strong') {
var children = node.block.nodes;
// Make sure that the Tag only has one child -- the text
if (children.length === 1 && children[0].type === 'Text') {
// Replace the Tag with the Text
replace({ type: 'Text', val: children[0].val, line: node.line });
}
}
}, {
includeDependencies: true
});
assert.deepEqual(parse(lex(dest)), ast);
// Flatten blocks
// ==============
var ast = {
type: 'Block',
nodes: [
{ type: 'Text', val: 'a' },
{
type: 'Block',
nodes: [
{ type: 'Text', val: 'b' },
{
type: 'Block',
nodes: [ { type: 'Text', val: 'c' } ]
},
{ type: 'Text', val: 'd' }
]
},
{ type: 'Text', val: 'e' }
]
};
var dest = {
type: 'Block',
nodes: [
{ type: 'Text', val: 'a' },
{ type: 'Text', val: 'b' },
{ type: 'Text', val: 'c' },
{ type: 'Text', val: 'd' },
{ type: 'Text', val: 'e' }
]
};
// We need to use `after` handler instead of `before`
// handler because we want to flatten the innermost
// blocks first before proceeding onto outer blocks.
ast = walk(ast, null, function after(node, replace) {
if (node.type === 'Block' && replace.arrayAllowed) {
// Replace the block with its contents
replace(node.nodes);
}
});
assert.deepEqual(dest, ast);
```
## License
MIT

View File

@@ -1,120 +0,0 @@
'use strict';
module.exports = walkAST;
function walkAST(ast, before, after, options) {
if (after && typeof after === 'object' && typeof options === 'undefined') {
options = after;
after = null;
}
options = options || {includeDependencies: false};
var parents = (options.parents = options.parents || []);
var replace = function replace(replacement) {
if (Array.isArray(replacement) && !replace.arrayAllowed) {
throw new Error(
'replace() can only be called with an array if the last parent is a Block or NamedBlock'
);
}
ast = replacement;
};
replace.arrayAllowed =
parents[0] &&
(/^(Named)?Block$/.test(parents[0].type) ||
(parents[0].type === 'RawInclude' && ast.type === 'IncludeFilter'));
if (before) {
var result = before(ast, replace);
if (result === false) {
return ast;
} else if (Array.isArray(ast)) {
// return right here to skip after() call on array
return walkAndMergeNodes(ast);
}
}
parents.unshift(ast);
switch (ast.type) {
case 'NamedBlock':
case 'Block':
ast.nodes = walkAndMergeNodes(ast.nodes);
break;
case 'Case':
case 'Filter':
case 'Mixin':
case 'Tag':
case 'InterpolatedTag':
case 'When':
case 'Code':
case 'While':
if (ast.block) {
ast.block = walkAST(ast.block, before, after, options);
}
break;
case 'Each':
if (ast.block) {
ast.block = walkAST(ast.block, before, after, options);
}
if (ast.alternate) {
ast.alternate = walkAST(ast.alternate, before, after, options);
}
break;
case 'EachOf':
if (ast.block) {
ast.block = walkAST(ast.block, before, after, options);
}
break;
case 'Conditional':
if (ast.consequent) {
ast.consequent = walkAST(ast.consequent, before, after, options);
}
if (ast.alternate) {
ast.alternate = walkAST(ast.alternate, before, after, options);
}
break;
case 'Include':
walkAST(ast.block, before, after, options);
walkAST(ast.file, before, after, options);
break;
case 'Extends':
walkAST(ast.file, before, after, options);
break;
case 'RawInclude':
ast.filters = walkAndMergeNodes(ast.filters);
walkAST(ast.file, before, after, options);
break;
case 'Attrs':
case 'BlockComment':
case 'Comment':
case 'Doctype':
case 'IncludeFilter':
case 'MixinBlock':
case 'YieldBlock':
case 'Text':
break;
case 'FileReference':
if (options.includeDependencies && ast.ast) {
walkAST(ast.ast, before, after, options);
}
break;
default:
throw new Error('Unexpected node type ' + ast.type);
break;
}
parents.shift();
after && after(ast, replace);
return ast;
function walkAndMergeNodes(nodes) {
return nodes.reduce(function(nodes, node) {
var result = walkAST(node, before, after, options);
if (Array.isArray(result)) {
return nodes.concat(result);
} else {
return nodes.concat([result]);
}
}, []);
}
}

View File

@@ -1,21 +0,0 @@
{
"name": "pug-walk",
"version": "1.1.8",
"description": "Walk and transform a pug AST",
"keywords": [
"pug"
],
"devDependencies": {
"pug-lexer": "^4.1.0",
"pug-parser": "^5.0.1"
},
"files": [
"index.js"
],
"repository": {
"type": "git",
"url": "https://github.com/pugjs/pug/tree/master/packages/pug-walk"
},
"author": "ForbesLindesay",
"license": "MIT"
}

View File

@@ -1,178 +0,0 @@
'use strict';
var assert = require('assert');
var lex = require('pug-lexer');
var parse = require('pug-parser');
var walk = require('../');
test('simple', function() {
var ast = walk(
parse(lex('.my-class foo')),
function before(node, replace) {
// called before walking the children of `node`
// to replace the node, call `replace(newNode)`
// return `false` to skip descending
if (node.type === 'Text') {
replace({
type: 'Text',
val: 'bar',
line: node.line,
column: node.column,
});
}
},
function after(node, replace) {
// called before walking the children of `node`
// to replace the node, call `replace(newNode)`
}
);
expect(ast).toEqual(parse(lex('.my-class bar')));
});
describe('replace([])', function() {
test('block flattening', function() {
var called = [];
var ast = walk(
{
type: 'Block',
nodes: [
{
type: 'Block',
nodes: [
{
type: 'Block',
nodes: [
{
type: 'Text',
val: 'a',
},
{
type: 'Text',
val: 'b',
},
],
},
{
type: 'Text',
val: 'c',
},
],
},
{
type: 'Text',
val: 'd',
},
],
},
function(node, replace) {
if (node.type === 'Text') {
called.push('before ' + node.val);
if (node.val === 'a') {
assert(replace.arrayAllowed, 'replace.arrayAllowed set wrongly');
replace([
{
type: 'Text',
val: 'e',
},
{
type: 'Text',
val: 'f',
},
]);
}
}
},
function(node, replace) {
if (node.type === 'Block' && replace.arrayAllowed) {
replace(node.nodes);
} else if (node.type === 'Text') {
called.push('after ' + node.val);
}
}
);
expect(ast).toEqual({
type: 'Block',
nodes: [
{type: 'Text', val: 'e'},
{type: 'Text', val: 'f'},
{type: 'Text', val: 'b'},
{type: 'Text', val: 'c'},
{type: 'Text', val: 'd'},
],
});
assert.deepEqual(
called,
[
'before a',
'before e',
'after e',
'before f',
'after f',
'before b',
'after b',
'before c',
'after c',
'before d',
'after d',
],
'before() and after() called incorrectly: ' + JSON.stringify(called)
);
});
test('adding include filters', function() {
var ast = walk(parse(lex('include:filter1:filter2 file')), function(
node,
replace
) {
if (node.type === 'IncludeFilter') {
assert(replace.arrayAllowed);
if (node.name === 'filter1') {
var firstFilter = 'filter3';
replace([
{
type: 'IncludeFilter',
name: firstFilter,
attrs: [],
line: node.line,
column: node.column,
},
{
type: 'IncludeFilter',
name: 'filter4',
attrs: [],
line: node.line,
column: node.column + firstFilter.length + 1,
},
]);
} else if (node.name === 'filter2') {
replace([]);
}
}
});
expect(ast).toEqual(parse(lex('include:filter3:filter4 file')));
});
test('fails when parent is not Block', function() {
walk(parse(lex('p content')), function(node, replace) {
if (
node.type === 'Block' &&
node.nodes[0] &&
node.nodes[0].type === 'Text'
) {
assert(!replace.arrayAllowed, 'replace.arrayAllowed set wrongly');
assert.throws(function() {
replace([]);
});
}
});
});
});

View File

@@ -27,6 +27,10 @@ fn normalizeHtml(allocator: std.mem.Allocator, html: []const u8) ![]const u8 {
const c = html[i];
if (c == '<') {
// Strip trailing whitespace before tags
while (result.items.len > 0 and (result.items[result.items.len - 1] == ' ' or result.items[result.items.len - 1] == '\t')) {
_ = result.pop();
}
in_tag = true;
last_was_space = false;
try result.append(allocator, c);
@@ -34,11 +38,8 @@ fn normalizeHtml(allocator: std.mem.Allocator, html: []const u8) ![]const u8 {
in_tag = false;
last_was_space = false;
try result.append(allocator, c);
} else if (c == '\n' or c == '\r') {
// Skip newlines
i += 1;
continue;
} else if (c == ' ' or c == '\t') {
} else if (c == '\n' or c == '\r' or c == ' ' or c == '\t') {
// Treat all whitespace (including newlines) uniformly
if (in_tag) {
// Preserve single space in tags for attribute separation
if (!last_was_space) {

View File

@@ -16,6 +16,10 @@ fn normalizeHtml(allocator: std.mem.Allocator, html: []const u8) ![]const u8 {
const c = html[i];
if (c == '<') {
// Strip trailing whitespace before tags
while (result.items.len > 0 and (result.items[result.items.len - 1] == ' ' or result.items[result.items.len - 1] == '\t')) {
_ = result.pop();
}
in_tag = true;
last_was_space = false;
try result.append(allocator, c);
@@ -23,11 +27,8 @@ fn normalizeHtml(allocator: std.mem.Allocator, html: []const u8) ![]const u8 {
in_tag = false;
last_was_space = false;
try result.append(allocator, c);
} else if (c == '\n' or c == '\r') {
// Skip newlines
i += 1;
continue;
} else if (c == ' ' or c == '\t') {
} else if (c == '\n' or c == '\r' or c == ' ' or c == '\t') {
// Treat all whitespace (including newlines) uniformly
if (in_tag) {
// Preserve single space in tags for attribute separation
if (!last_was_space) {

Some files were not shown because too many files have changed in this diff Show More