Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore missing / unavailable files #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"rules": {
"array-bracket-spacing": [ "error", "always" ],
"indent": [ 2, "tab", { "SwitchCase": 1 } ],
"quotes": [ 2, "single" ],
"linebreak-style": [ 2, "unix" ],
"semi": [ 2, "always" ],
"keyword-spacing": [ 2, { "before": true, "after": true } ],
"space-before-blocks": [ 2, "always" ],
"space-in-parens": [ "error", "always", { "exceptions": [ "{}", "[]" ] } ],
"space-before-function-paren": [ 2, "always" ],
"object-curly-spacing": [ "error", "always" ],
"no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
"no-cond-assign": [ 0 ]
},
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"prepare-tests": "node test/samples/prepare-tests.js",
"test": "mocha --compilers js:buble/register",
"prepublish": "npm test",
"lint": "eslint src",
"lint": "eslint src --fix",
"pretest-coverage": "npm run build",
"test-coverage": "rm -rf coverage/* && istanbul cover --report json node_modules/.bin/_mocha -- -u exports -R spec test/test.js",
"posttest-coverage": "remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.json -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.lcov -t lcovonly -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped -t html -b dist",
Expand All @@ -53,4 +53,4 @@
"bin/",
"README.md"
]
}
}
2 changes: 1 addition & 1 deletion src/Chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Chain.prototype = {
};
},

apply ( options = {} ) {
apply ( options = {}) {
let allNames = [];
let allSources = [];

Expand Down
26 changes: 15 additions & 11 deletions src/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function Node ({ file, content }) {
Node.prototype = {
load ( sourcesContentByPath, sourceMapByPath ) {
return getContent( this, sourcesContentByPath ).then( content => {
this.content = sourcesContentByPath[ this.file ] = content;
this.content = sourcesContentByPath[this.file] = content;

return getMap( this, sourceMapByPath ).then( map => {
if ( !map ) return null;
Expand Down Expand Up @@ -60,11 +60,15 @@ Node.prototype = {

loadSync ( sourcesContentByPath, sourceMapByPath ) {
if ( !this.content ) {
if ( !sourcesContentByPath[ this.file ] ) {
sourcesContentByPath[ this.file ] = readFileSync( this.file, { encoding: 'utf-8' });
if ( !sourcesContentByPath[this.file]) {
try {
sourcesContentByPath[this.file] = readFileSync( this.file, { encoding: 'utf-8' });
} catch ( e ) {
sourcesContentByPath[this.file] = null;
}
}

this.content = sourcesContentByPath[ this.file ];
this.content = sourcesContentByPath[this.file];
}

const map = getMap( this, sourceMapByPath, true );
Expand Down Expand Up @@ -121,7 +125,7 @@ Node.prototype = {

// Otherwise, we need to figure out what this position in
// the intermediate file corresponds to in *its* source
const segments = this.mappings[ lineIndex ];
const segments = this.mappings[lineIndex];

if ( !segments || segments.length === 0 ) {
return null;
Expand All @@ -146,8 +150,8 @@ Node.prototype = {
let sourceCodeColumn = segments[i][3];
let nameIndex = segments[i][4];

let parent = this.sources[ sourceFileIndex ];
return parent.trace( sourceCodeLine, sourceCodeColumn, this.map.names[ nameIndex ] || name );
let parent = this.sources[sourceFileIndex];
return parent.trace( sourceCodeLine, sourceCodeColumn, this.map.names[nameIndex] || name );
}
}
}
Expand All @@ -157,18 +161,18 @@ Node.prototype = {
let sourceCodeLine = segments[0][2];
let nameIndex = segments[0][4];

let parent = this.sources[ sourceFileIndex ];
return parent.trace( sourceCodeLine, null, this.map.names[ nameIndex ] || name );
let parent = this.sources[sourceFileIndex];
return parent.trace( sourceCodeLine, null, this.map.names[nameIndex] || name );
}
};

function getContent ( node, sourcesContentByPath ) {
if ( node.file in sourcesContentByPath ) {
node.content = sourcesContentByPath[ node.file ];
node.content = sourcesContentByPath[node.file];
}

if ( !node.content ) {
return readFile( node.file, { encoding: 'utf-8' });
return readFile( node.file, { encoding: 'utf-8' }).catch( () => null );
}

return Promise.resolve( node.content );
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export function load ( file, options ) {
.then( () => node.isOriginalSource ? null : new Chain( node, sourcesContentByPath ) );
}

export function loadSync ( file, options = {} ) {
export function loadSync ( file, options = {}) {
const { node, sourcesContentByPath, sourceMapByPath } = init( file, options );

node.loadSync( sourcesContentByPath, sourceMapByPath );
return node.isOriginalSource ? null : new Chain( node, sourcesContentByPath );
}

function init ( file, options = {} ) {
function init ( file, options = {}) {
const node = new Node({ file });

let sourcesContentByPath = {};
Expand Down
8 changes: 6 additions & 2 deletions src/utils/getMapFromUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ export default function getMapFromUrl ( url, base, sync ) {
url = resolve( dirname( base ), decodeURI( url ) );

if ( sync ) {
return parseJSON( readFileSync( url, { encoding: 'utf-8' }), url );
try {
return parseJSON( readFileSync( url, { encoding: 'utf-8' }), url );
} catch ( e ) {
return null;
}
} else {
return readFile( url, { encoding: 'utf-8' }).then( json => parseJSON( json, url ) );
return readFile( url, { encoding: 'utf-8' }).then( json => parseJSON( json, url ) ).catch( () => null );
}
}
1 change: 1 addition & 0 deletions src/utils/getSourceMappingUrl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SOURCEMAPPING_URL from './sourceMappingURL.js';

export default function getSourceMappingUrl ( str ) {
if ( !str ) return null;
var index, substring, url, match;

// assume we want the last occurence
Expand Down