mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
* Fix #4491: import- and export-specific lexing should stop * split up tests * fixes from code review
This commit is contained in:
committed by
Geoffrey Booth
parent
90ec761f95
commit
fb60070647
@@ -27,6 +27,7 @@
|
||||
this.seenFor = false;
|
||||
this.seenImport = false;
|
||||
this.seenExport = false;
|
||||
this.importSpecifierList = false;
|
||||
this.exportSpecifierList = false;
|
||||
this.chunkLine = opts.line || 0;
|
||||
this.chunkColumn = opts.column || 0;
|
||||
@@ -425,6 +426,12 @@
|
||||
}
|
||||
indent = match[0];
|
||||
this.seenFor = false;
|
||||
if (!this.importSpecifierList) {
|
||||
this.seenImport = false;
|
||||
}
|
||||
if (!this.exportSpecifierList) {
|
||||
this.seenExport = false;
|
||||
}
|
||||
size = indent.length - 1 - indent.lastIndexOf('\n');
|
||||
noNewlines = this.unfinished();
|
||||
if (size - this.indebt === this.indent) {
|
||||
@@ -566,7 +573,11 @@
|
||||
return value.length;
|
||||
}
|
||||
}
|
||||
if (value === '{' && (prev != null ? prev[0] : void 0) === 'EXPORT') {
|
||||
if (value === '{' && this.seenImport) {
|
||||
this.importSpecifierList = true;
|
||||
} else if (this.importSpecifierList && value === '}') {
|
||||
this.importSpecifierList = false;
|
||||
} else if (value === '{' && (prev != null ? prev[0] : void 0) === 'EXPORT') {
|
||||
this.exportSpecifierList = true;
|
||||
} else if (this.exportSpecifierList && value === '}') {
|
||||
this.exportSpecifierList = false;
|
||||
@@ -868,12 +879,12 @@
|
||||
};
|
||||
|
||||
Lexer.prototype.validateEscapes = function(str, options) {
|
||||
var before, hex, invalidEscape, invalid_escape_regex, match, message, octal, ref2, unicode;
|
||||
var before, hex, invalidEscape, invalidEscapeRegex, match, message, octal, ref2, unicode;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
invalid_escape_regex = options.isRegex ? REGEX_INVALID_ESCAPE : STRING_INVALID_ESCAPE;
|
||||
match = invalid_escape_regex.exec(str);
|
||||
invalidEscapeRegex = options.isRegex ? REGEX_INVALID_ESCAPE : STRING_INVALID_ESCAPE;
|
||||
match = invalidEscapeRegex.exec(str);
|
||||
if (!match) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ exports.Lexer = class Lexer
|
||||
@seenFor = no # Used to recognize FORIN, FOROF and FORFROM tokens.
|
||||
@seenImport = no # Used to recognize IMPORT FROM? AS? tokens.
|
||||
@seenExport = no # Used to recognize EXPORT FROM? AS? tokens.
|
||||
@importSpecifierList = no # Used to identify when in an IMPORT {...} FROM? ...
|
||||
@exportSpecifierList = no # Used to identify when in an EXPORT {...} FROM? ...
|
||||
|
||||
@chunkLine =
|
||||
@@ -365,6 +366,8 @@ exports.Lexer = class Lexer
|
||||
indent = match[0]
|
||||
|
||||
@seenFor = no
|
||||
@seenImport = no unless @importSpecifierList
|
||||
@seenExport = no unless @exportSpecifierList
|
||||
|
||||
size = indent.length - 1 - indent.lastIndexOf '\n'
|
||||
noNewlines = @unfinished()
|
||||
@@ -473,7 +476,11 @@ exports.Lexer = class Lexer
|
||||
@error message, origin[2] if message
|
||||
return value.length if skipToken
|
||||
|
||||
if value is '{' and prev?[0] is 'EXPORT'
|
||||
if value is '{' and @seenImport
|
||||
@importSpecifierList = yes
|
||||
else if @importSpecifierList and value is '}'
|
||||
@importSpecifierList = no
|
||||
else if value is '{' and prev?[0] is 'EXPORT'
|
||||
@exportSpecifierList = yes
|
||||
else if @exportSpecifierList and value is '}'
|
||||
@exportSpecifierList = no
|
||||
@@ -763,12 +770,12 @@ exports.Lexer = class Lexer
|
||||
|
||||
# Validates escapes in strings and regexes.
|
||||
validateEscapes: (str, options = {}) ->
|
||||
invalid_escape_regex =
|
||||
invalidEscapeRegex =
|
||||
if options.isRegex
|
||||
REGEX_INVALID_ESCAPE
|
||||
else
|
||||
STRING_INVALID_ESCAPE
|
||||
match = invalid_escape_regex.exec str
|
||||
match = invalidEscapeRegex.exec str
|
||||
return unless match
|
||||
[[], before, octal, hex, unicode] = match
|
||||
message =
|
||||
|
||||
@@ -773,3 +773,110 @@ test "#4451: `default` in an export statement is only treated as a keyword when
|
||||
"default": 1
|
||||
};
|
||||
"""
|
||||
eq toJS(input), output
|
||||
|
||||
test "#4491: import- and export-specific lexing should stop after import/export statement", ->
|
||||
input = """
|
||||
import {
|
||||
foo,
|
||||
bar as baz
|
||||
} from 'lib'
|
||||
|
||||
foo as
|
||||
3 * as 4
|
||||
from 'foo'
|
||||
"""
|
||||
output = """
|
||||
import {
|
||||
foo,
|
||||
bar as baz
|
||||
} from 'lib';
|
||||
|
||||
foo(as);
|
||||
|
||||
3 * as(4);
|
||||
|
||||
from('foo');
|
||||
"""
|
||||
eq toJS(input), output
|
||||
|
||||
input = """
|
||||
import { foo, bar as baz } from 'lib'
|
||||
|
||||
foo as
|
||||
3 * as 4
|
||||
from 'foo'
|
||||
"""
|
||||
output = """
|
||||
import {
|
||||
foo,
|
||||
bar as baz
|
||||
} from 'lib';
|
||||
|
||||
foo(as);
|
||||
|
||||
3 * as(4);
|
||||
|
||||
from('foo');
|
||||
"""
|
||||
eq toJS(input), output
|
||||
|
||||
input = """
|
||||
import * as lib from 'lib'
|
||||
|
||||
foo as
|
||||
3 * as 4
|
||||
from 'foo'
|
||||
"""
|
||||
output = """
|
||||
import * as lib from 'lib';
|
||||
|
||||
foo(as);
|
||||
|
||||
3 * as(4);
|
||||
|
||||
from('foo');
|
||||
"""
|
||||
eq toJS(input), output
|
||||
|
||||
input = """
|
||||
export {
|
||||
foo,
|
||||
bar
|
||||
}
|
||||
|
||||
foo as
|
||||
3 * as 4
|
||||
from 'foo'
|
||||
"""
|
||||
output = """
|
||||
export {
|
||||
foo,
|
||||
bar
|
||||
};
|
||||
|
||||
foo(as);
|
||||
|
||||
3 * as(4);
|
||||
|
||||
from('foo');
|
||||
"""
|
||||
eq toJS(input), output
|
||||
|
||||
input = """
|
||||
export * from 'lib'
|
||||
|
||||
foo as
|
||||
3 * as 4
|
||||
from 'foo'
|
||||
"""
|
||||
output = """
|
||||
export * from 'lib';
|
||||
|
||||
foo(as);
|
||||
|
||||
3 * as(4);
|
||||
|
||||
from('foo');
|
||||
"""
|
||||
eq toJS(input), output
|
||||
|
||||
Reference in New Issue
Block a user