Skip to content

Commit

Permalink
Merge pull request #130 from asottile/remove_comments_with_sourcemap
Browse files Browse the repository at this point in the history
Remove comments with sourcemap
  • Loading branch information
asottile committed Mar 8, 2016
2 parents c84f25c + 793bc16 commit 5fbf419
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pysass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ PySass_compile_filename(PyObject *self, PyObject *args) {
context = sass_make_file_context(filename);
options = sass_file_context_get_options(context);

if (source_comments && PySass_Bytes_Check(source_map_filename)) {
if (PySass_Bytes_Check(source_map_filename)) {
size_t source_map_file_len = PySass_Bytes_GET_SIZE(source_map_filename);
if (source_map_file_len) {
char *source_map_file = (char *) malloc(source_map_file_len + 1);
Expand Down
9 changes: 4 additions & 5 deletions sass.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,6 @@ def compile(**kwargs):
:param source_map_filename: use source maps and indicate the source map
output filename. :const:`None` means not
using source maps. :const:`None` by default.
note that it implies ``source_comments``
is also :const:`True`
:type source_map_filename: :class:`str`
:param include_paths: an optional list of paths to find ``@import``\ ed
SASS/CSS source files
Expand All @@ -327,7 +325,7 @@ def compile(**kwargs):
<importer-callbacks>`_ description
:type importers: :class:`collections.Callable`
:returns: the compiled CSS string, or a pair of the compiled CSS string
and the source map string if ``source_comments='map'``
and the source map string if ``source_map_filename`` is set
:rtype: :class:`str`, :class:`tuple`
:raises sass.CompileError: when it fails for any reason
(for example the given SASS has broken syntax)
Expand Down Expand Up @@ -480,6 +478,9 @@ def my_importer(path):
.. versionadded:: 0.7.0
Added ``custom_functions`` parameter.
.. versionadded:: 0.11.0
``source_map_filename`` no longer implies ``source_comments``.
"""
modes = set()
for mode_name in MODES:
Expand Down Expand Up @@ -537,8 +538,6 @@ def my_importer(path):
raise CompileError('source_map_filename is only available with '
'filename= keyword argument since it has to be '
'aware of it')
if source_map_filename is not None:
source_comments = True
try:
include_paths = kwargs.pop('include_paths') or b''
except KeyError:
Expand Down
12 changes: 12 additions & 0 deletions sassc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
.. versionadded:: 0.7.0
.. option:: --source-comments
Include debug info in output.
.. versionadded:: 0.11.0
.. option:: -v, --version
Prints the program version.
Expand Down Expand Up @@ -101,6 +107,10 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
'-p', '--precision', action='store', type='int', default=5,
help='Set the precision for numbers. [default: %default]'
)
parser.add_option(
'--source-comments', action='store_true', default=False,
help='Include debug info in output',
)
options, args = parser.parse_args(argv[1:])
error = functools.partial(print,
parser.get_prog_name() + ': error:',
Expand Down Expand Up @@ -134,6 +144,7 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
css, source_map = compile(
filename=filename,
output_style=options.style,
source_comments=options.source_comments,
source_map_filename=source_map_filename,
include_paths=options.include_paths,
precision=options.precision
Expand All @@ -144,6 +155,7 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
css = compile(
filename=filename,
output_style=options.style,
source_comments=options.source_comments,
include_paths=options.include_paths,
precision=options.precision
)
Expand Down
45 changes: 18 additions & 27 deletions sasstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ def normalize_path(path):
'''

A_EXPECTED_CSS_WITH_MAP = '''\
/* line 6, SOURCE */
body {
background-color: green; }
/* line 8, SOURCE */
body a {
color: blue; }
Expand All @@ -60,8 +58,8 @@ def normalize_path(path):
'sources': ['test/a.scss'],
'names': [],
'mappings': (
';AAKA,AAAA,IAAI,CAAC;EAHH,gBAAgB,EAAE,KAAM,GAQzB;;EALD,AAEE,IAFE,'
'CAEF,CAAC,CAAC;IACA,KAAK,EAAE,IAAK,GACb'
'AAKA,AAAA,IAAI,CAAC;EAHH,gBAAgB,EAAE,KAAM,GAQzB;EALD,AAEE,'
'IAFE,CAEF,CAAC,CAAC;IACA,KAAK,EAAE,IAAK,GACb'
),
}

Expand All @@ -71,7 +69,6 @@ def normalize_path(path):
'''

B_EXPECTED_CSS_WITH_MAP = '''\
/* line 2, SOURCE */
b i {
font-size: 20px; }
Expand All @@ -97,10 +94,8 @@ def normalize_path(path):

D_EXPECTED_CSS_WITH_MAP = '''\
@charset "UTF-8";
/* line 6, SOURCE */
body {
background-color: green; }
/* line 8, SOURCE */
body a {
font: '나눔고딕', sans-serif; }
Expand Down Expand Up @@ -446,10 +441,7 @@ def test_compile_source_map(self):
source_map_filename='a.scss.css.map'
)
self.assertEqual(
A_EXPECTED_CSS_WITH_MAP.replace(
'SOURCE',
normalize_path(os.path.abspath(filename))
),
A_EXPECTED_CSS_WITH_MAP,
actual
)
self.assert_source_map_equal(A_EXPECTED_MAP, source_map)
Expand Down Expand Up @@ -612,7 +604,7 @@ def replace_source_path(s, name):
'sources': ['../test/b.scss'],
'names': [],
'mappings': (
';AAAA,AACE,CADD,CACC,CAAC,CAAC;EACA,SAAS,EAAE,IAAK,'
'AAAA,AACE,CADD,CACC,CAAC,CAAC;EACA,SAAS,EAAE,IAAK,'
'GACjB'
),
},
Expand All @@ -632,7 +624,7 @@ def replace_source_path(s, name):
'sources': ['../test/d.scss'],
'names': [],
'mappings': (
';;AAKA,AAAA,IAAI,CAAC;EAHH,gBAAgB,EAAE,KAAM,GAQzB;;'
';AAKA,AAAA,IAAI,CAAC;EAHH,gBAAgB,EAAE,KAAM,GAQzB;'
'EALD,AAEE,IAFE,CAEF,CAAC,CAAC;IACA,IAAI,EAAE,0BAA2B,'
'GAClC'
),
Expand Down Expand Up @@ -665,9 +657,8 @@ def test_wsgi_sass_middleware(self):
self.assertEqual('text/plain', r.mimetype)
r = client.get('/static/a.scss.css')
self.assertEqual(200, r.status_code)
src_path = normalize_path(os.path.join(src_dir, 'a.scss'))
self.assert_bytes_equal(
b(A_EXPECTED_CSS_WITH_MAP.replace('SOURCE', src_path)),
b(A_EXPECTED_CSS_WITH_MAP),
r.data
)
self.assertEqual('text/css', r.mimetype)
Expand Down Expand Up @@ -737,7 +728,7 @@ def setUp(self):
self.err = StringIO()

def test_no_args(self):
exit_code = sassc.main(['sassc', ], self.out, self.err)
exit_code = sassc.main(['sassc'], self.out, self.err)
self.assertEqual(2, exit_code)
err = self.err.getvalue()
assert err.strip().endswith('error: too few arguments'), \
Expand Down Expand Up @@ -803,12 +794,11 @@ def test_sassc_source_map_without_css_filename(self):
self.assertEqual('', self.out.getvalue())

def test_sassc_sourcemap(self):
tmp_dir = tempfile.mkdtemp()
src_dir = os.path.join(tmp_dir, 'test')
shutil.copytree('test', src_dir)
src_filename = os.path.join(src_dir, 'a.scss')
out_filename = os.path.join(tmp_dir, 'a.scss.css')
try:
with tempdir() as tmp_dir:
src_dir = os.path.join(tmp_dir, 'test')
shutil.copytree('test', src_dir)
src_filename = os.path.join(src_dir, 'a.scss')
out_filename = os.path.join(tmp_dir, 'a.scss.css')
exit_code = sassc.main(
['sassc', '-m', src_filename, out_filename],
self.out, self.err
Expand All @@ -818,18 +808,14 @@ def test_sassc_sourcemap(self):
self.assertEqual('', self.out.getvalue())
with open(out_filename) as f:
self.assertEqual(
A_EXPECTED_CSS_WITH_MAP.replace(
'SOURCE', normalize_path(src_filename)
),
A_EXPECTED_CSS_WITH_MAP,
f.read().strip()
)
with open(out_filename + '.map') as f:
self.assert_source_map_equal(
dict(A_EXPECTED_MAP, sources=None),
dict(json.load(f), sources=None)
)
finally:
shutil.rmtree(tmp_dir)


@contextlib.contextmanager
Expand Down Expand Up @@ -1432,3 +1418,8 @@ def test_stack_trace_formatting():
'>> a{☃\n'
' --^\n\n'
)


def test_source_comments():
out = sass.compile(string='a{color: red}', source_comments=True)
assert out == '/* line 1, stdin */\na {\n color: red; }\n'

0 comments on commit 5fbf419

Please sign in to comment.