-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
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
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**/__tests__/** | ||
yarn.lock |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import codeframe from '../src'; | ||
Check failure on line 1 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (18)
|
||
|
||
const rawLines = `<div class="home"> | ||
<h1 class="title"> | ||
{{ message }} | ||
</h1> | ||
{% for key in [1,2,3,4] %} | ||
<div> | ||
{% mount '../counter' as key with { count: 5 } %} | ||
</div> | ||
{% endfor %} | ||
</div>`; | ||
|
||
describe('codeframe', () => { | ||
Check failure on line 14 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (18)
Check failure on line 14 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (18)
Check failure on line 14 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (20)
|
||
it('should match output with line -1, col -1 and length -1', () => { | ||
Check failure on line 15 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (18)
Check failure on line 15 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (18)
Check failure on line 15 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (20)
|
||
const input = { | ||
rawLines, | ||
lineNumber: -1, | ||
colNumber: -1, | ||
length: -1, | ||
Check failure on line 20 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (18)
|
||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
Check failure on line 22 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (18)
|
||
}); | ||
|
||
it('should match output with line 0, col 1 and length 0', () => { | ||
Check failure on line 25 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (18)
Check failure on line 25 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (18)
Check failure on line 25 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (20)
|
||
const input = { | ||
rawLines, | ||
lineNumber: 0, | ||
colNumber: 1, | ||
length: 0, | ||
Check failure on line 30 in src/melody/melody-code-frame/__tests__/CodeFrameSpec.js GitHub Actions / Test (18)
|
||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match output with line 1, col 1 and length 1', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 1, | ||
colNumber: 1, | ||
length: 1, | ||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
it('should match output with line 2, col 4 and length 2', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 2, | ||
colNumber: 4, | ||
length: 2, | ||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match output with line 5, col 4 and length 1', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 5, | ||
colNumber: 4, | ||
length: 1, | ||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match output with line 5, col 44 and length 2', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 5, | ||
colNumber: 44, | ||
length: 2, | ||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match output with line 5, col 14 and length 4', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 5, | ||
colNumber: 14, | ||
length: 4, | ||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match output with line 5, col -1 and length 1', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 5, | ||
colNumber: -1, | ||
length: 1, | ||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match output with line 10, col 1 and length 1', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 10, | ||
colNumber: 1, | ||
length: 1, | ||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match output with line 10, col 1 and length 0', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 10, | ||
colNumber: 1, | ||
length: 0, | ||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match output with line 10, col 1 and length 2', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 10, | ||
colNumber: 1, | ||
length: 2, | ||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match output with line 15, col 1 and length 1', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 15, | ||
colNumber: 1, | ||
length: 1, | ||
}; | ||
expect(codeframe(input)).toEqual(''); | ||
}); | ||
|
||
it('should match output with line 15, col 1 and length 4', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 15, | ||
colNumber: 1, | ||
length: 4, | ||
}; | ||
expect(codeframe(input)).toEqual(''); | ||
}); | ||
|
||
it('should match output with line 15, col -1 and length 4', () => { | ||
const input = { | ||
rawLines, | ||
lineNumber: 15, | ||
colNumber: -1, | ||
length: 4, | ||
}; | ||
expect(codeframe(input)).toEqual(''); | ||
}); | ||
|
||
it('should match output with line 1, col 1 and length 1 for blank rowLines', () => { | ||
const input = { | ||
rawLines: '', | ||
lineNumber: 1, | ||
colNumber: 1, | ||
length: 1, | ||
}; | ||
expect(codeframe(input)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match output with line 4, col 1 and length 1 for blank rowLines', () => { | ||
const input = { | ||
rawLines: '', | ||
lineNumber: 4, | ||
colNumber: 1, | ||
length: 1, | ||
}; | ||
expect(codeframe(input)).toEqual(''); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`codeframe should match output with line -1, col -1 and length -1 1`] = ` | ||
" 1 | <div class=\\"home\\"> | ||
2 | <h1 class=\\"title\\">" | ||
`; | ||
exports[`codeframe should match output with line 0, col 1 and length 0 1`] = ` | ||
" 1 | <div class=\\"home\\"> | ||
2 | <h1 class=\\"title\\"> | ||
3 | {{ message }}" | ||
`; | ||
exports[`codeframe should match output with line 1, col 1 and length 1 1`] = ` | ||
"> 1 | <div class=\\"home\\"> | ||
| ^ | ||
2 | <h1 class=\\"title\\"> | ||
3 | {{ message }} | ||
4 | </h1>" | ||
`; | ||
exports[`codeframe should match output with line 1, col 1 and length 1 for blank rowLines 1`] = ` | ||
"> 1 | | ||
| ^" | ||
`; | ||
exports[`codeframe should match output with line 2, col 4 and length 2 1`] = ` | ||
" 1 | <div class=\\"home\\"> | ||
> 2 | <h1 class=\\"title\\"> | ||
| ^^ | ||
3 | {{ message }} | ||
4 | </h1> | ||
5 | {% for key in [1,2,3,4] %}" | ||
`; | ||
exports[`codeframe should match output with line 5, col -1 and length 1 1`] = ` | ||
" 3 | {{ message }} | ||
4 | </h1> | ||
> 5 | {% for key in [1,2,3,4] %} | ||
| ^ | ||
6 | <div> | ||
7 | {% mount '../counter' as key with { count: 5 } %} | ||
8 | </div>" | ||
`; | ||
exports[`codeframe should match output with line 5, col 4 and length 1 1`] = ` | ||
" 3 | {{ message }} | ||
4 | </h1> | ||
> 5 | {% for key in [1,2,3,4] %} | ||
| ^ | ||
6 | <div> | ||
7 | {% mount '../counter' as key with { count: 5 } %} | ||
8 | </div>" | ||
`; | ||
exports[`codeframe should match output with line 5, col 14 and length 4 1`] = ` | ||
" 3 | {{ message }} | ||
4 | </h1> | ||
> 5 | {% for key in [1,2,3,4] %} | ||
| ^^^^ | ||
6 | <div> | ||
7 | {% mount '../counter' as key with { count: 5 } %} | ||
8 | </div>" | ||
`; | ||
exports[`codeframe should match output with line 5, col 44 and length 2 1`] = ` | ||
" 3 | {{ message }} | ||
4 | </h1> | ||
> 5 | {% for key in [1,2,3,4] %} | ||
| ^^ | ||
6 | <div> | ||
7 | {% mount '../counter' as key with { count: 5 } %} | ||
8 | </div>" | ||
`; | ||
|
||
exports[`codeframe should match output with line 10, col 1 and length 0 1`] = ` | ||
" 8 | </div> | ||
9 | {% endfor %} | ||
> 10 | </div> | ||
| " | ||
`; | ||
|
||
exports[`codeframe should match output with line 10, col 1 and length 1 1`] = ` | ||
" 8 | </div> | ||
9 | {% endfor %} | ||
> 10 | </div> | ||
| ^" | ||
`; | ||
|
||
exports[`codeframe should match output with line 10, col 1 and length 2 1`] = ` | ||
" 8 | </div> | ||
9 | {% endfor %} | ||
> 10 | </div> | ||
| ^^" | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "melody-code-frame", | ||
"version": "1.7.5", | ||
"description": "", | ||
"main": "./lib/index.js", | ||
"jsnext:main": "./lib/index.esm.js", | ||
"scripts": { | ||
"build": "mkdir lib; rollup -c ../../rollup.config.js -i src/index.js" | ||
}, | ||
"author": "", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"lodash": "^4.15.0" | ||
} | ||
} |
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'; | ||
import { repeat } from 'lodash'; | ||
|
||
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/; | ||
|
||
export default function({ rawLines, lineNumber, colNumber, length }) { | ||
const lines = rawLines.split(NEWLINE), | ||
start = Math.max(lineNumber - 3, 0), | ||
end = Math.min(lineNumber + 3, lines.length); | ||
|
||
return lineNumbers(lines.slice(start, end), { | ||
start: start + 1, | ||
before: ' ', | ||
after: ' | ', | ||
transform(params) { | ||
if (params.number !== lineNumber) { | ||
return; | ||
} | ||
|
||
if (typeof colNumber === 'number') { | ||
params.line += `\n${params.before}${repeat(' ', params.width)}${ | ||
params.after | ||
}${repeat(' ', colNumber)}${repeat('^', length)}`; | ||
} | ||
|
||
params.before = params.before.replace(/^./, '>'); | ||
}, | ||
}).join('\n'); | ||
} |
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**/__tests__/** | ||
yarn.lock |