CasperJS/PhantomJSでシンタックスエラー行を取得する方法

11月 24, 2013 Computer, Development, Programming
(Last Updated On: 2018年8月13日)

CasperJSやPhantomJSを使っていてシンタックスエラーがあると

[yohgaki@localhost ]$ casperjs test.js
SyntaxError: Parse error

と表示するだけでエラー行を返してくれません。


上記のエラーは以下のスクリプトで起きています。

[yohgaki@localhost]$ cat test.js
var casper = require('casper').create();

casper.start('http://casperjs.org/', function() {
    this.echo(this.getTitle());
});

casper.thenOpen('http://kakaku.com', function() {
    this.echo(this.getTitle();
});

casper.run();

シンタックスエラーはPhantomJSで起きているのでCasperJSでなくPhantomJSを使っても同じです。

[yohgaki@localhost ]$ phantomjs test.js
SyntaxError: Parse error

デバッグオプションなどを設定しても状況は変わりません。

[yohgaki@localhost ]$ phantomjs --debug=yes test.js
2013-11-24T08:27:59 [DEBUG] CookieJar - Created but will not store cookies (use option '--cookies-file=<filename>' to enable persisten cookie storage) 
2013-11-24T08:27:59 [DEBUG] Phantom - execute: Configuration 
2013-11-24T08:27:59 [DEBUG]      0 objectName : "" 
2013-11-24T08:27:59 [DEBUG]      1 cookiesFile : "" 
2013-11-24T08:27:59 [DEBUG]      2 diskCacheEnabled : "false" 
2013-11-24T08:27:59 [DEBUG]      3 maxDiskCacheSize : "-1" 
2013-11-24T08:27:59 [DEBUG]      4 ignoreSslErrors : "false" 
2013-11-24T08:27:59 [DEBUG]      5 localToRemoteUrlAccessEnabled : "false" 
2013-11-24T08:27:59 [DEBUG]      6 outputEncoding : "UTF-8" 
2013-11-24T08:27:59 [DEBUG]      7 proxyType : "http" 
2013-11-24T08:27:59 [DEBUG]      8 proxy : ":1080" 
2013-11-24T08:27:59 [DEBUG]      9 proxyAuth : ":" 
2013-11-24T08:27:59 [DEBUG]      10 scriptEncoding : "UTF-8" 
2013-11-24T08:27:59 [DEBUG]      11 webSecurityEnabled : "true" 
2013-11-24T08:27:59 [DEBUG]      12 offlineStoragePath : "" 
2013-11-24T08:27:59 [DEBUG]      13 offlineStorageDefaultQuota : "-1" 
2013-11-24T08:27:59 [DEBUG]      14 printDebugMessages : "true" 
2013-11-24T08:27:59 [DEBUG]      15 javascriptCanOpenWindows : "true" 
2013-11-24T08:27:59 [DEBUG]      16 javascriptCanCloseWindows : "true" 
2013-11-24T08:27:59 [DEBUG]      17 sslProtocol : "sslv3" 
2013-11-24T08:27:59 [DEBUG]      18 sslCertificatesPath : "" 
2013-11-24T08:27:59 [DEBUG]      19 webdriver : ":" 
2013-11-24T08:27:59 [DEBUG]      20 webdriverLogFile : "" 
2013-11-24T08:27:59 [DEBUG]      21 webdriverLogLevel : "INFO" 
2013-11-24T08:27:59 [DEBUG]      22 webdriverSeleniumGridHub : "" 
2013-11-24T08:27:59 [DEBUG] Phantom - execute: Script & Arguments 
2013-11-24T08:27:59 [DEBUG]      script: "test.js" 
2013-11-24T08:27:59 [DEBUG] Phantom - execute: Starting normal mode 
2013-11-24T08:27:59 [DEBUG] WebPage - setupFrame "" 
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/fs.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/system.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/_coffee-script.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/package.json" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/coffee-script.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/./lexer.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/././rewriter.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/././helpers.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/./parser.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/./helpers.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/./nodes.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/././scope.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/./././helpers.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/././lexer.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/../coffee-script/./lib/coffee-script/./././rewriter.js" QMap(("mode", QVariant(QString, "r") ) )  
2013-11-24T08:27:59 [DEBUG] FileSystem - _open: ":/modules/webpage.js" QMap(("mode", QVariant(QString, "r") ) )  
SyntaxError: Parse error

シンタックスエラーは注意していても書いてしまいます。エラー行がどこなのか分からなければ効率が悪くて仕方ありません。シンタックスエラーだけならNode.jsでも見つけられるのでnodeコマンドで実行してみます。

[yohgaki@localhost ]$ node test.js

/home/yohgaki/test.js:8
    this.echo(this.getTitle();
                             ^
SyntaxError: Unexpected token ;
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

ばっちりエラー行を取得出来ました。

投稿者: yohgaki