Skip to content

Commit

Permalink
error: rename src option to source
Browse files Browse the repository at this point in the history
I don't like abbreviations. Source looks better properly spelled out.
  • Loading branch information
matheusmoreira committed Sep 10, 2023
1 parent 30c2aae commit 26f9adf
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 47 deletions.
6 changes: 3 additions & 3 deletions packages/error/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Create a pugneum error object.
- `filename`: the name of the file causing the error
- `line`: the offending line
- `column`: the offending column
- `src`: the pugneum source, if available, for pretty-printing the error context
- `source`: the pugneum source, if available, for pretty-printing the error context

The resulting error object is a simple Error object with additional properties given in the arguments.

Expand All @@ -35,13 +35,13 @@ The resulting error object is a simple Error object with additional properties g
```js
var error = require('pugneum-error');

var err = error('MY_CODE', 'My message', {line: 3, filename: 'myfile', src: 'foo\nbar\nbaz\nbash\nbing'});
var err = error('MY_CODE', 'My message', {line: 3, filename: 'myfile', source: 'foo\nbar\nbaz\nbash\nbing'});
// { code: 'PUGNEUM:MY_CODE',
// msg: 'My message',
// line: 3,
// column: undefined,
// filename: 'myfile',
// src: 'foo\nbar\nbaz\nbash\nbing',
// source: 'foo\nbar\nbaz\nbash\nbing',
// message: 'myfile:3\n 1| foo\n 2| bar\n > 3| baz\n 4| bash\n 5| bing\n\nMy message' }

throw err;
Expand Down
4 changes: 2 additions & 2 deletions packages/error/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function makeError(code, message, options) {
var line = options.line;
var column = options.column;
var filename = options.filename;
var src = options.src;
var src = options.source;
var fullMessage;
var location = line + (column ? ':' + column : '');
if (src && line >= 1 && line <= src.split('\n').length) {
Expand Down Expand Up @@ -36,7 +36,7 @@ function makeError(code, message, options) {
err.line = line;
err.column = column;
err.filename = filename;
err.src = src;
err.source = src;
err.toJSON = function() {
return {
code: this.code,
Expand Down
26 changes: 13 additions & 13 deletions packages/error/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('with a source', function() {
var err = error('MY_CODE', 'My message', {
line: 3,
filename: 'myfile',
src: 'foo\nbar\nbaz\nbash\nbing',
source: 'foo\nbar\nbaz\nbash\nbing',
});
expect(err.message).toBe(
'myfile:3\n 1| foo\n 2| bar\n > 3| baz\n 4| bash\n 5| bing\n\nMy message'
Expand All @@ -17,12 +17,12 @@ describe('with a source', function() {
expect(err.msg).toBe('My message');
expect(err.line).toBe(3);
expect(err.filename).toBe('myfile');
expect(err.src).toBe('foo\nbar\nbaz\nbash\nbing');
expect(err.source).toBe('foo\nbar\nbaz\nbash\nbing');
});
test('and no filename', function() {
var err = error('MY_CODE', 'My message', {
line: 3,
src: 'foo\nbar\nbaz\nbash\nbing',
source: 'foo\nbar\nbaz\nbash\nbing',
});
expect(err.message).toBe(
'3\n 1| foo\n 2| bar\n > 3| baz\n 4| bash\n 5| bing\n\nMy message'
Expand All @@ -31,7 +31,7 @@ describe('with a source', function() {
expect(err.msg).toBe('My message');
expect(err.line).toBe(3);
expect(err.filename).toBe(undefined);
expect(err.src).toBe('foo\nbar\nbaz\nbash\nbing');
expect(err.source).toBe('foo\nbar\nbaz\nbash\nbing');
});
});

Expand All @@ -43,7 +43,7 @@ describe('without source', function() {
expect(err.msg).toBe('My message');
expect(err.line).toBe(3);
expect(err.filename).toBe('myfile');
expect(err.src).toBe(undefined);
expect(err.source).toBe(undefined);
});
test('and with no filename', function() {
var err = error('MY_CODE', 'My message', {line: 3});
Expand All @@ -52,7 +52,7 @@ describe('without source', function() {
expect(err.msg).toBe('My message');
expect(err.line).toBe(3);
expect(err.filename).toBe(undefined);
expect(err.src).toBe(undefined);
expect(err.source).toBe(undefined);
});
});

Expand All @@ -62,7 +62,7 @@ describe('with column', function() {
line: 3,
column: 2,
filename: 'myfile',
src: 'foo\nbar\nbaz\nbash\nbing',
source: 'foo\nbar\nbaz\nbash\nbing',
});
expect(err.message).toBe(
'myfile:3:2\n 1| foo\n 2| bar\n > 3| baz\n--------^\n 4| bash\n 5| bing\n\nMy message'
Expand All @@ -71,7 +71,7 @@ describe('with column', function() {
expect(err.msg).toBe('My message');
expect(err.line).toBe(3);
expect(err.filename).toBe('myfile');
expect(err.src).toBe('foo\nbar\nbaz\nbash\nbing');
expect(err.source).toBe('foo\nbar\nbaz\nbash\nbing');
});
test('and with no filename', function() {
var err = error('MY_CODE', 'My message', {line: 3, column: 1});
Expand All @@ -80,7 +80,7 @@ describe('with column', function() {
expect(err.msg).toBe('My message');
expect(err.line).toBe(3);
expect(err.filename).toBe(undefined);
expect(err.src).toBe(undefined);
expect(err.source).toBe(undefined);
});
});

Expand All @@ -89,7 +89,7 @@ describe('invalid information', function() {
var err = error('MY_CODE', 'My message', {
line: 3,
column: -1,
src: 'foo\nbar\nbaz\nbash\nbing',
source: 'foo\nbar\nbaz\nbash\nbing',
});
expect(err.message).toBe(
'3:-1\n 1| foo\n 2| bar\n > 3| baz\n 4| bash\n 5| bing\n\nMy message'
Expand All @@ -98,7 +98,7 @@ describe('invalid information', function() {
expect(err.msg).toBe('My message');
expect(err.line).toBe(3);
expect(err.filename).toBe(undefined);
expect(err.src).toBe('foo\nbar\nbaz\nbash\nbing');
expect(err.source).toBe('foo\nbar\nbaz\nbash\nbing');
});
test('out of range line', function() {
check(0);
Expand All @@ -107,14 +107,14 @@ describe('invalid information', function() {
function check(line) {
var err = error('MY_CODE', 'My message', {
line: line,
src: 'foo\nbar\nbaz\nbash\nbing',
source: 'foo\nbar\nbaz\nbash\nbing',
});
expect(err.message).toBe(line + '\n\nMy message');
expect(err.code).toBe('PUGNEUM:MY_CODE');
expect(err.msg).toBe('My message');
expect(err.line).toBe(line);
expect(err.filename).toBe(undefined);
expect(err.src).toBe('foo\nbar\nbaz\nbash\nbing');
expect(err.source).toBe('foo\nbar\nbaz\nbash\nbing');
}
});
});
10 changes: 2 additions & 8 deletions packages/filterer/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ p
Filters can be used.
`;

const ast = parse(lex(source, {filename}), {
filename,
src: source,
});
const ast = parse(lex(source, {filename}), {filename, source});

const output = filter(ast, customFilters);
expect(output).toMatchSnapshot();
Expand All @@ -50,10 +47,7 @@ p
They're just strings.
`;

const ast = parse(lex(source, {filename}), {
filename,
src: source,
});
const ast = parse(lex(source, {filename}), {filename, source});

const output = filter(ast, customFilters);
expect(output).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ p
Will be wrapped in second.
`;

const ast = parse(lex(source, {filename}), {
filename,
src: source,
});
const ast = parse(lex(source, {filename}), {filename, source});

const options = {
second: {wrap: true},
Expand Down
6 changes: 3 additions & 3 deletions packages/filterer/test/resolution.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ const filter = require('../');

test('installed filter packages can be used implicitly', () => {

const src = `
const source = `
pre
code
:'highlight.js'(language=ruby)
puts 'This should be', :syntax_highlighted
`;

const tokens = lex(src, {filename});
const ast = parse(tokens, {filename, src});
const tokens = lex(source, {filename});
const ast = parse(tokens, {filename, source});
const filtered = filter(ast);

expect(filtered).toMatchSnapshot();
Expand Down
2 changes: 1 addition & 1 deletion packages/lexer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Lexer.prototype = {
line: this.lineno,
column: this.colno,
filename: this.filename,
src: this.originalInput,
source: this.originalInput,
});
throw err;
},
Expand Down
6 changes: 3 additions & 3 deletions packages/linker/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function testDir(dir) {
if (!/\.pg$/.test(name)) return;
test(name, function() {
let filename = dir + '/' + name;
let src = fs.readFileSync(filename, 'utf8');
let options = {filename, src, lex, parse, basedir};
let tokens = lex(src, options);
let source = fs.readFileSync(filename, 'utf8');
let options = {filename, source, lex, parse, basedir};
let tokens = lex(source, options);
let ast = parse(tokens, options);
let loaded = load(ast, options);
var actual = link(loaded);
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function load(ast, options) {
file.str = str;
file.raw = raw;
if (node.type === 'Extends' || node.type === 'Include') {
let opts = assign({}, options, {filename: path, src: str});
let opts = assign({}, options, {filename: path, source: str});
let tokens = options.lex(str, opts);
let ast = options.parse(tokens, opts);
file.ast = load(ast, opts);
Expand Down
8 changes: 4 additions & 4 deletions packages/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ Convert pugneum tokens into an abstract syntax tree (AST).

- `filename` (string): pugneum file name; included in AST nodes and used in error handling
- `plugins` (array): array of plugins in the order they should be applied
- `src` (string): pugneum source code before tokenization; used in error handling
- `source` (string): pugneum source code before tokenization; used in error handling

```js
const lex = require('pugneum-lexer');

let filename = 'my-file.pg';
let src = 'div(data-foo="bar")';
let tokens = lex(src, {filename});
let source = 'div(data-foo="bar")';
let tokens = lex(source, {filename});

let ast = parse(tokens, {filename, src});
let ast = parse(tokens, {filename, source});

console.log(JSON.stringify(ast, null, ' '))
```
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function Parser(tokens, options) {
}
this.tokens = new TokenStream(tokens);
this.filename = options.filename;
this.src = options.src;
this.source = options.source;
this.inMixin = 0;
this.plugins = options.plugins || [];
}
Expand All @@ -76,7 +76,7 @@ Parser.prototype = {
line: token.loc.start.line,
column: token.loc.start.column,
filename: this.filename,
src: this.src,
source: this.source,
});
throw err;
},
Expand Down
6 changes: 3 additions & 3 deletions packages/pugneum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const render = require('pugneum-renderer');

function renderPugneum(string, options) {
options ||= {};
options.src = string;
options.source = string;
options.lex = lex;
options.parse = parse;

Expand All @@ -27,10 +27,10 @@ function renderPugneum(string, options) {

function renderPugneumFile(path, options) {
path = resolve(path);
let src = fs.readFileSync(path, 'utf8');
let source = fs.readFileSync(path, 'utf8');
options ||= {};
options.filename = path;
return renderPugneum(src, options);
return renderPugneum(source, options);
};

exports.render = renderPugneum;
Expand Down

0 comments on commit 26f9adf

Please sign in to comment.