Skip to content

Commit

Permalink
chore(dependency): partially import melody project
Browse files Browse the repository at this point in the history
Related: GH-26, GH-32

This project has external dependencies that no longer updated. Instead
of forking and probably re-releasing it as separate packages it might be
easier if we integrated it directly. This way, we don't have to manage
separate project.
  • Loading branch information
zackad committed Jul 18, 2024
2 parents 790d1d5 + af7a067 commit 0d9e2aa
Show file tree
Hide file tree
Showing 45 changed files with 6,707 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/melody/melody-code-frame/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2017 trivago N.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import lineNumbers from './lineNumbers';

Check failure on line 16 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (18)

Replace `'./lineNumbers'` with `"./lineNumbers"`

Check failure on line 16 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (20)

Replace `'./lineNumbers'` with `"./lineNumbers"`
import { repeat } from 'lodash';

Check failure on line 17 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (18)

'lodash' should be listed in the project's dependencies. Run 'npm i -S lodash' to add it

Check failure on line 17 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (18)

Replace `'lodash'` with `"lodash"`

Check failure on line 17 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (20)

'lodash' should be listed in the project's dependencies. Run 'npm i -S lodash' to add it

Check failure on line 17 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (20)

Replace `'lodash'` with `"lodash"`

const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;

export default function({ rawLines, lineNumber, colNumber, length }) {

Check failure on line 21 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (18)

Insert `·`

Check failure on line 21 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (20)

Insert `·`
const lines = rawLines.split(NEWLINE),

Check failure on line 22 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (18)

Split 'const' declarations into multiple statements

Check failure on line 22 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (20)

Split 'const' declarations into multiple statements
start = Math.max(lineNumber - 3, 0),
end = Math.min(lineNumber + 3, lines.length);

return lineNumbers(lines.slice(start, end), {
start: start + 1,
before: ' ',

Check failure on line 28 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (18)

Replace `'··'` with `"··"`

Check failure on line 28 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (20)

Replace `'··'` with `"··"`
after: ' | ',

Check failure on line 29 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (18)

Replace `'·|·'` with `"·|·"`

Check failure on line 29 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (20)

Replace `'·|·'` with `"·|·"`
transform(params) {
if (params.number !== lineNumber) {
return;
}

if (typeof colNumber === 'number') {

Check failure on line 35 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (18)

Replace `'number'` with `"number"`

Check failure on line 35 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (20)

Replace `'number'` with `"number"`
params.line += `\n${params.before}${repeat(' ', params.width)}${

Check failure on line 36 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (18)

Replace `'·'` with `"·"`

Check failure on line 36 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (20)

Replace `'·'` with `"·"`
params.after
}${repeat(' ', colNumber)}${repeat('^', length)}`;

Check failure on line 38 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (18)

Replace `'·',·colNumber)}${repeat('^'` with `"·",·colNumber)}${repeat("^"`

Check failure on line 38 in src/melody/melody-code-frame/index.js

View workflow job for this annotation

GitHub Actions / Test (20)

Replace `'·',·colNumber)}${repeat('^'` with `"·",·colNumber)}${repeat("^"`
}

params.before = params.before.replace(/^./, '>');
},
}).join('\n');
}
45 changes: 45 additions & 0 deletions src/melody/melody-code-frame/lineNumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2014, 2015 Simon Lydell
// X11 (“MIT”) Licensed. (See LICENSE.)
/**
* Copyright 2017 trivago N.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { padStart } from 'lodash';

const get = options => (key, defaultValue) =>
key in options ? options[key] : defaultValue;

function lineNumbers(lines, options) {
const getOption = get(options);
const transform = getOption('transform', Function.prototype);
const padding = getOption('padding', ' ');
const before = getOption('before', ' ');
const after = getOption('after', ' | ');
const start = getOption('start', 1);
const end = start + lines.length - 1;
const width = String(end).length;
return lines.map(function(line, index) {
const number = start + index;
const params = { before, number, width, after, line };
transform(params);
return (
params.before +
padStart(params.number, width, padding) +
params.after +
params.line
);
});
}

export default lineNumbers;
167 changes: 167 additions & 0 deletions src/melody/melody-extension-core/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* Copyright 2017 trivago N.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { unaryOperators, binaryOperators, tests } from './operators';
import { AutoescapeParser } from './parser/autoescape';
import { BlockParser } from './parser/block';
import { DoParser } from './parser/do';
import { EmbedParser } from './parser/embed';
import { ExtendsParser } from './parser/extends';
import { FilterParser } from './parser/filter';
import { FlushParser } from './parser/flush';
import { ForParser } from './parser/for';
import { FromParser } from './parser/from';
import { IfParser } from './parser/if';
import { ImportParser } from './parser/import';
import { IncludeParser } from './parser/include';
import { MacroParser } from './parser/macro';
import { SetParser } from './parser/set';
import { SpacelessParser } from './parser/spaceless';
import { UseParser } from './parser/use';
import { MountParser } from './parser/mount';

import forVisitor from './visitors/for';
import testVisitor from './visitors/tests';
import filters from './visitors/filters';
import functions from './visitors/functions';

const filterMap = [
'attrs',
'classes',
'styles',
'batch',
'escape',
'format',
'merge',
'nl2br',
'number_format',
'raw',
'replace',
'reverse',
'round',
'striptags',
'title',
'url_encode',
'trim',
].reduce((map, filterName) => {
map[filterName] = 'melody-runtime';
return map;
}, Object.create(null));

Object.assign(filterMap, filters);

const functionMap = [
'attribute',
'constant',
'cycle',
'date',
'max',
'min',
'random',
'range',
'source',
'template_from_string',
].reduce((map, functionName) => {
map[functionName] = 'melody-runtime';
return map;
}, Object.create(null));
Object.assign(functionMap, functions);

export const extension = {
tags: [
AutoescapeParser,
BlockParser,
DoParser,
EmbedParser,
ExtendsParser,
FilterParser,
FlushParser,
ForParser,
FromParser,
IfParser,
ImportParser,
IncludeParser,
MacroParser,
SetParser,
SpacelessParser,
UseParser,
MountParser,
],
unaryOperators,
binaryOperators,
tests,
visitors: [forVisitor, testVisitor],
filterMap,
functionMap,
};

export {
AutoescapeBlock,
BlockStatement,
BlockCallExpression,
MountStatement,
DoStatement,
EmbedStatement,
ExtendsStatement,
FilterBlockStatement,
FlushStatement,
ForStatement,
ImportDeclaration,
FromStatement,
IfStatement,
IncludeStatement,
MacroDeclarationStatement,
VariableDeclarationStatement,
SetStatement,
SpacelessBlock,
AliasExpression,
UseStatement,
UnaryNotExpression,
UnaryNeqExpression,
UnaryPosExpression,
BinaryOrExpression,
BinaryAndExpression,
BitwiseOrExpression,
BitwiseXorExpression,
BitwiseAndExpression,
BinaryEqualsExpression,
BinaryNotEqualsExpression,
BinaryLessThanExpression,
BinaryGreaterThanExpression,
BinaryLessThanOrEqualExpression,
BinaryGreaterThanOrEqualExpression,
BinaryNotInExpression,
BinaryInExpression,
BinaryMatchesExpression,
BinaryStartsWithExpression,
BinaryEndsWithExpression,
BinaryRangeExpression,
BinaryAddExpression,
BinaryMulExpression,
BinaryDivExpression,
BinaryFloorDivExpression,
BinaryModExpression,
BinaryPowerExpression,
BinaryNullCoalesceExpression,
TestEvenExpression,
TestOddExpression,
TestDefinedExpression,
TestSameAsExpression,
TestNullExpression,
TestDivisibleByExpression,
TestConstantExpression,
TestEmptyExpression,
TestIterableExpression,
} from './types';
Loading

0 comments on commit 0d9e2aa

Please sign in to comment.