Adding mapbox-gl branch
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
;; Copyright 2013 The Closure Library Authors. All Rights Reserved.
|
||||
;;
|
||||
;; 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.
|
||||
|
||||
;; Closure JS code editing functions for emacs.
|
||||
|
||||
;; Author: nnaze@google.com (Nathan Naze)
|
||||
|
||||
;; Remember the path of this file, as we will base our paths on it.
|
||||
(setq closure-el-path load-file-name)
|
||||
|
||||
(defun closure-el-directory ()
|
||||
"Get the directory the closure.el file lives in."
|
||||
(file-name-directory closure-el-path))
|
||||
|
||||
(defun closure-generate-jsdoc-path()
|
||||
"The path of the generate_jsdoc.py script."
|
||||
(concat (closure-el-directory) "generate_jsdoc.py"))
|
||||
|
||||
(defun closure-insert-jsdoc ()
|
||||
"Insert JSDoc for the next function after the cursor."
|
||||
(interactive)
|
||||
(save-excursion ; Remembers cursor location
|
||||
(call-process-region
|
||||
(point) (point-max)
|
||||
(closure-generate-jsdoc-path)
|
||||
t t)))
|
||||
|
||||
(provide 'closure)
|
||||
@@ -0,0 +1,31 @@
|
||||
;; Copyright 2013 The Closure Library Authors. All Rights Reserved.
|
||||
;;
|
||||
;; 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.
|
||||
|
||||
;; Unit tests for closure_test.el
|
||||
|
||||
;; Author: nnaze@google.com (Nathan Naze)
|
||||
|
||||
(require 'cl)
|
||||
|
||||
(setq closure-test-directory (file-name-directory load-file-name))
|
||||
|
||||
(load-file (concat closure-test-directory "closure.el"))
|
||||
|
||||
(setq closure-el-path "/test/path/closure.el")
|
||||
|
||||
(assert (string= "/test/path/" (closure-el-directory)))
|
||||
|
||||
(assert (string=
|
||||
"/test/path/generate_jsdoc.py"
|
||||
(closure-generate-jsdoc-path)))
|
||||
@@ -0,0 +1,171 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright 2013 The Closure Library Authors. All Rights Reserved.
|
||||
#
|
||||
# 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.
|
||||
|
||||
"""Tool to insert JsDoc before a function.
|
||||
|
||||
This script attempts to find the first function passed in to stdin, generate
|
||||
JSDoc for it (with argument names and possibly return value), and inject
|
||||
it in the string. This is intended to be used as a subprocess by editors
|
||||
such as emacs and vi.
|
||||
"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
|
||||
|
||||
# Matches a typical Closure-style function definition.
|
||||
_FUNCTION_REGEX = re.compile(r"""
|
||||
# Start of line
|
||||
^
|
||||
|
||||
# Indentation
|
||||
(?P<indentation>[ ]*)
|
||||
|
||||
# Identifier (handling split across line)
|
||||
(?P<identifier>\w+(\s*\.\s*\w+)*)
|
||||
|
||||
# "= function"
|
||||
\s* = \s* function \s*
|
||||
|
||||
# opening paren
|
||||
\(
|
||||
|
||||
# Function arguments
|
||||
(?P<arguments>(?:\s|\w+|,)*)
|
||||
|
||||
# closing paren
|
||||
\)
|
||||
|
||||
# opening bracket
|
||||
\s* {
|
||||
|
||||
""", re.MULTILINE | re.VERBOSE)
|
||||
|
||||
|
||||
def _MatchFirstFunction(script):
|
||||
"""Match the first function seen in the script."""
|
||||
return _FUNCTION_REGEX.search(script)
|
||||
|
||||
|
||||
def _ParseArgString(arg_string):
|
||||
"""Parse an argument string (inside parens) into parameter names."""
|
||||
for arg in arg_string.split(','):
|
||||
arg = arg.strip()
|
||||
if arg:
|
||||
yield arg
|
||||
|
||||
|
||||
def _ExtractFunctionBody(script, indentation=0):
|
||||
"""Attempt to return the function body."""
|
||||
|
||||
# Real extraction would require a token parser and state machines.
|
||||
# We look for first bracket at the same level of indentation.
|
||||
regex_str = r'{(.*?)^[ ]{%d}}' % indentation
|
||||
|
||||
function_regex = re.compile(regex_str, re.MULTILINE | re.DOTALL)
|
||||
match = function_regex.search(script)
|
||||
if match:
|
||||
return match.group(1)
|
||||
|
||||
|
||||
def _ContainsReturnValue(function_body):
|
||||
"""Attempt to determine if the function body returns a value."""
|
||||
return_regex = re.compile(r'\breturn\b[^;]')
|
||||
|
||||
# If this matches, we assume they're returning something.
|
||||
return bool(return_regex.search(function_body))
|
||||
|
||||
|
||||
def _InsertString(original_string, inserted_string, index):
|
||||
"""Insert a string into another string at a given index."""
|
||||
return original_string[0:index] + inserted_string + original_string[index:]
|
||||
|
||||
|
||||
def _GenerateJsDoc(args, return_val=False):
|
||||
"""Generate JSDoc for a function.
|
||||
|
||||
Args:
|
||||
args: A list of names of the argument.
|
||||
return_val: Whether the function has a return value.
|
||||
|
||||
Returns:
|
||||
The JSDoc as a string.
|
||||
"""
|
||||
|
||||
lines = []
|
||||
lines.append('/**')
|
||||
|
||||
lines += [' * @param {} %s' % arg for arg in args]
|
||||
|
||||
if return_val:
|
||||
lines.append(' * @return')
|
||||
|
||||
lines.append(' */')
|
||||
|
||||
return '\n'.join(lines) + '\n'
|
||||
|
||||
|
||||
def _IndentString(source_string, indentation):
|
||||
"""Indent string some number of characters."""
|
||||
lines = [(indentation * ' ') + line
|
||||
for line in source_string.splitlines(True)]
|
||||
return ''.join(lines)
|
||||
|
||||
|
||||
def InsertJsDoc(script):
|
||||
"""Attempt to insert JSDoc for the first seen function in the script.
|
||||
|
||||
Args:
|
||||
script: The script, as a string.
|
||||
|
||||
Returns:
|
||||
Returns the new string if function was found and JSDoc inserted. Otherwise
|
||||
returns None.
|
||||
"""
|
||||
|
||||
match = _MatchFirstFunction(script)
|
||||
if not match:
|
||||
return
|
||||
|
||||
# Add argument flags.
|
||||
args_string = match.group('arguments')
|
||||
args = _ParseArgString(args_string)
|
||||
|
||||
start_index = match.start(0)
|
||||
function_to_end = script[start_index:]
|
||||
|
||||
lvalue_indentation = len(match.group('indentation'))
|
||||
|
||||
return_val = False
|
||||
function_body = _ExtractFunctionBody(function_to_end, lvalue_indentation)
|
||||
if function_body:
|
||||
return_val = _ContainsReturnValue(function_body)
|
||||
|
||||
jsdoc = _GenerateJsDoc(args, return_val)
|
||||
if lvalue_indentation:
|
||||
jsdoc = _IndentString(jsdoc, lvalue_indentation)
|
||||
|
||||
return _InsertString(script, jsdoc, start_index)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
stdin_script = sys.stdin.read()
|
||||
result = InsertJsDoc(stdin_script)
|
||||
|
||||
if result:
|
||||
sys.stdout.write(result)
|
||||
else:
|
||||
sys.stdout.write(stdin_script)
|
||||
@@ -0,0 +1,167 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright 2013 The Closure Library Authors. All Rights Reserved.
|
||||
#
|
||||
# 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.
|
||||
|
||||
|
||||
"""Unit test for generate_jsdoc."""
|
||||
|
||||
__author__ = 'nnaze@google.com (Nathan Naze)'
|
||||
|
||||
|
||||
import re
|
||||
import unittest
|
||||
|
||||
import generate_jsdoc
|
||||
|
||||
|
||||
class InsertJsDocTestCase(unittest.TestCase):
|
||||
"""Unit test for source. Tests the parser on a known source input."""
|
||||
|
||||
def testMatchFirstFunction(self):
|
||||
match = generate_jsdoc._MatchFirstFunction(_TEST_SOURCE)
|
||||
self.assertNotEqual(None, match)
|
||||
self.assertEqual('aaa, bbb, ccc', match.group('arguments'))
|
||||
|
||||
match = generate_jsdoc._MatchFirstFunction(_INDENTED_SOURCE)
|
||||
self.assertNotEqual(None, match)
|
||||
self.assertEqual('', match.group('arguments'))
|
||||
|
||||
match = generate_jsdoc._MatchFirstFunction(_ODD_NEWLINES_SOURCE)
|
||||
self.assertEquals('goog.\nfoo.\nbar\n.baz.\nqux',
|
||||
match.group('identifier'))
|
||||
|
||||
def testParseArgString(self):
|
||||
self.assertEquals(
|
||||
['foo', 'bar', 'baz'],
|
||||
list(generate_jsdoc._ParseArgString('foo, bar, baz')))
|
||||
|
||||
def testExtractFunctionBody(self):
|
||||
self.assertEquals(
|
||||
'\n // Function comments.\n return;\n',
|
||||
generate_jsdoc._ExtractFunctionBody(_TEST_SOURCE))
|
||||
|
||||
self.assertEquals(
|
||||
'\n var bar = 3;\n return true;\n',
|
||||
generate_jsdoc._ExtractFunctionBody(_INDENTED_SOURCE, 2))
|
||||
|
||||
def testContainsValueReturn(self):
|
||||
self.assertTrue(generate_jsdoc._ContainsReturnValue(_INDENTED_SOURCE))
|
||||
self.assertFalse(generate_jsdoc._ContainsReturnValue(_TEST_SOURCE))
|
||||
|
||||
def testInsertString(self):
|
||||
self.assertEquals(
|
||||
'abc123def',
|
||||
generate_jsdoc._InsertString('abcdef', '123', 3))
|
||||
|
||||
def testInsertJsDoc(self):
|
||||
self.assertEquals(
|
||||
_EXPECTED_INDENTED_SOURCE,
|
||||
generate_jsdoc.InsertJsDoc(_INDENTED_SOURCE))
|
||||
|
||||
self.assertEquals(
|
||||
_EXPECTED_TEST_SOURCE,
|
||||
generate_jsdoc.InsertJsDoc(_TEST_SOURCE))
|
||||
|
||||
self.assertEquals(
|
||||
_EXPECTED_ODD_NEWLINES_SOURCE,
|
||||
generate_jsdoc.InsertJsDoc(_ODD_NEWLINES_SOURCE))
|
||||
|
||||
|
||||
_INDENTED_SOURCE = """\
|
||||
boo.foo.woo = function() {
|
||||
var bar = 3;
|
||||
return true;
|
||||
};
|
||||
"""
|
||||
|
||||
_EXPECTED_INDENTED_SOURCE = """\
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
boo.foo.woo = function() {
|
||||
var bar = 3;
|
||||
return true;
|
||||
};
|
||||
"""
|
||||
|
||||
|
||||
_TEST_SOURCE = """\
|
||||
|
||||
// Random comment.
|
||||
|
||||
goog.foo.bar = function (aaa, bbb, ccc) {
|
||||
// Function comments.
|
||||
return;
|
||||
};
|
||||
"""
|
||||
|
||||
_EXPECTED_TEST_SOURCE = """\
|
||||
|
||||
// Random comment.
|
||||
|
||||
/**
|
||||
* @param {} aaa
|
||||
* @param {} bbb
|
||||
* @param {} ccc
|
||||
*/
|
||||
goog.foo.bar = function (aaa, bbb, ccc) {
|
||||
// Function comments.
|
||||
return;
|
||||
};
|
||||
"""
|
||||
|
||||
_ODD_NEWLINES_SOURCE = """\
|
||||
goog.
|
||||
foo.
|
||||
bar
|
||||
.baz.
|
||||
qux
|
||||
=
|
||||
|
||||
function
|
||||
|
||||
(aaa,
|
||||
|
||||
bbb, ccc) {
|
||||
// Function comments.
|
||||
return;
|
||||
};
|
||||
"""
|
||||
|
||||
_EXPECTED_ODD_NEWLINES_SOURCE = """\
|
||||
/**
|
||||
* @param {} aaa
|
||||
* @param {} bbb
|
||||
* @param {} ccc
|
||||
*/
|
||||
goog.
|
||||
foo.
|
||||
bar
|
||||
.baz.
|
||||
qux
|
||||
=
|
||||
|
||||
function
|
||||
|
||||
(aaa,
|
||||
|
||||
bbb, ccc) {
|
||||
// Function comments.
|
||||
return;
|
||||
};
|
||||
"""
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,31 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright 2013 The Closure Library Authors
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# Wraps the unit tests not using the Google framework so they may be run
|
||||
# on the continuous build.
|
||||
#
|
||||
# Author: nnaze@google.com (Nathan Naze)
|
||||
#
|
||||
# Wraps the unit tests not using the Google framework so they may be run
|
||||
# run on the continuous build.
|
||||
|
||||
set -e
|
||||
|
||||
source googletest.sh || exit 1
|
||||
|
||||
CLOSURE_SRCDIR=$TEST_SRCDIR/google3/javascript/closure/labs/bin/code/
|
||||
|
||||
emacs --script $CLOSURE_SRCDIR/closure_test.el
|
||||
@@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright 2013 The Closure Library Authors
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# Wraps the unit tests not using the Google framework so they may be run
|
||||
# on the continuous build.
|
||||
|
||||
set -e
|
||||
|
||||
source googletest.sh || exit 1
|
||||
|
||||
CLOSURE_SRCDIR=$TEST_SRCDIR/google3/javascript/closure/labs/bin/code/
|
||||
|
||||
PYTHONPATH=$CLOSURE_SRCDIR
|
||||
|
||||
$CLOSURE_SRCDIR/generate_jsdoc_test.py
|
||||
Reference in New Issue
Block a user