Created
November 21, 2016 22:02
-
-
Save gsherwood/f17cfc90f14a9b29eeb6b2e99e6e7f66 to your computer and use it in GitHub Desktop.
Using PHP_CodeSniffer to tokenize a JS file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyCustomProject; | |
use PHP_CodeSniffer\Runner; | |
use PHP_CodeSniffer\Config; | |
use PHP_CodeSniffer\Files\DummyFile; | |
// Include the PHPCS autoloader, however you need to. | |
require_once __DIR__.'/autoload.php'; | |
// Init the PHPCS runner; no settings are required. | |
$runner = new Runner(); | |
$runner->config = new Config(); | |
$runner->init(); | |
// Create a new JS file, but only tokenize it - don't process it. | |
$fileContent = file_get_contents(__DIR__.'/temp.js'); | |
$file = new DummyFile($fileContent, $runner->ruleset, $runner->config); | |
$file->tokenizerType = 'JS'; | |
$file->parse(); | |
// Get the tokens of the file. Note that the first token is a T_OPEN_TAG | |
// token and the last is a T_CLOSE_TAG token, so these can be removed | |
// to make the token array cleaner because they are only needed | |
// by PHP_CodeSniffer sniffs. | |
$tokens = $file->getTokens(); | |
array_pop($tokens); | |
array_shift($tokens); | |
print_r($tokens); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (window.jQuery)(function($) { | |
$.fn.reset = function() { | |
return this.each(function() { | |
try { | |
this.reset(); | |
} catch (e) {} | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output from running the above JSTokenizer.php script is: