mirror of https://github.com/lukechilds/rollup.git
Rich-Harris
8 years ago
22 changed files with 125 additions and 68 deletions
@ -0,0 +1,38 @@ |
|||||
|
function spaces ( i ) { |
||||
|
let result = ''; |
||||
|
while ( i-- ) result += ' '; |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function tabsToSpaces ( str ) { |
||||
|
return str.replace( /^\t+/, match => match.split( '\t' ).join( ' ' ) ); |
||||
|
} |
||||
|
|
||||
|
export default function getCodeFrame ( source, line, column ) { |
||||
|
let lines = source.split( '\n' ); |
||||
|
|
||||
|
const frameStart = Math.max( 0, line - 3 ); |
||||
|
const frameEnd = Math.min( line + 2, lines.length ); |
||||
|
|
||||
|
const digits = String( frameEnd + 1 ).length; |
||||
|
|
||||
|
lines = lines.slice( frameStart, frameEnd ); |
||||
|
while ( !/\S/.test( lines[ lines.length - 1 ] ) ) lines.pop(); |
||||
|
|
||||
|
return lines |
||||
|
.map( ( str, i ) => { |
||||
|
const isErrorLine = frameStart + i + 1 === line; |
||||
|
|
||||
|
let lineNum = String( i + frameStart + 1 ); |
||||
|
while ( lineNum.length < digits ) lineNum = ` ${lineNum}`; |
||||
|
|
||||
|
if ( isErrorLine ) { |
||||
|
const indicator = spaces( digits + 2 + tabsToSpaces( str.slice( 0, column ) ).length ) + '^'; |
||||
|
return `${lineNum}: ${tabsToSpaces( str )}\n${indicator}`; |
||||
|
} |
||||
|
|
||||
|
return `${lineNum}: ${tabsToSpaces( str )}`; |
||||
|
}) |
||||
|
.join( '\n' ); |
||||
|
} |
@ -1,20 +0,0 @@ |
|||||
export default function getLocation ( source, charIndex ) { |
|
||||
const lines = source.split( '\n' ); |
|
||||
const len = lines.length; |
|
||||
|
|
||||
let lineStart = 0; |
|
||||
let i; |
|
||||
|
|
||||
for ( i = 0; i < len; i += 1 ) { |
|
||||
const line = lines[i]; |
|
||||
const lineEnd = lineStart + line.length + 1; // +1 for newline
|
|
||||
|
|
||||
if ( lineEnd > charIndex ) { |
|
||||
return { line: i + 1, column: charIndex - lineStart }; |
|
||||
} |
|
||||
|
|
||||
lineStart = lineEnd; |
|
||||
} |
|
||||
|
|
||||
throw new Error( 'Could not determine location of character' ); |
|
||||
} |
|
@ -1,16 +1,16 @@ |
|||||
var assert = require( 'assert' ); |
|
||||
|
|
||||
var warned = false; |
|
||||
|
|
||||
module.exports = { |
module.exports = { |
||||
description: 'warns about use of eval', |
description: 'warns about use of eval', |
||||
options: { |
warnings: [ |
||||
onwarn: function ( message ) { |
{ |
||||
warned = true; |
code: 'EVAL', |
||||
assert.ok( /Use of `eval` \(in .+?main\.js\) is strongly discouraged, as it poses security risks and may cause issues with minification\. See https:\/\/github.com\/rollup\/rollup\/wiki\/Troubleshooting#avoiding-eval for more details/.test( message ) ); |
message: `Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification`, |
||||
|
pos: 13, |
||||
|
loc: { |
||||
|
column: 13, |
||||
|
file: require( 'path' ).resolve( __dirname, 'main.js' ), |
||||
|
line: 2 |
||||
|
}, |
||||
|
url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval' |
||||
} |
} |
||||
}, |
] |
||||
exports: function () { |
|
||||
assert.ok( warned, 'did not warn' ); |
|
||||
} |
|
||||
}; |
}; |
||||
|
Loading…
Reference in new issue