🌐 AI搜索 & 代理 主页
Skip to content

Commit 90305e0

Browse files
kaicataldoilyavolodin
authored andcommitted
Update: Depcrecate isSpaceBetweenTokens() (#12519)
1 parent 41b1e43 commit 90305e0

File tree

3 files changed

+350
-2
lines changed

3 files changed

+350
-2
lines changed

docs/developer-guide/working-with-rules.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ Once you have an instance of `SourceCode`, you can use the methods on it to work
392392
* `getCommentsAfter(nodeOrToken)` - returns an array of comment tokens that occur directly after the given node or token.
393393
* `getCommentsInside(node)` - returns an array of all comment tokens inside a given node.
394394
* `getJSDocComment(node)` - returns the JSDoc comment for a given node or `null` if there is none.
395-
* `isSpaceBetweenTokens(first, second)` - returns true if there is a whitespace character between the two tokens.
395+
* `isSpaceBetween(nodeOrToken, nodeOrToken)` - returns true if there is a whitespace character between the two tokens or, if given a node, the last token of the first node and the first token of the second node.
396396
* `getFirstToken(node, skipOptions)` - returns the first token representing the given node.
397397
* `getFirstTokens(node, countOptions)` - returns the first `count` tokens representing the given node.
398398
* `getLastToken(node, skipOptions)` - returns the last token representing the given node.
@@ -447,6 +447,7 @@ Please note that the following methods have been deprecated and will be removed
447447
* `getComments()` - replaced by `getCommentsBefore()`, `getCommentsAfter()`, and `getCommentsInside()`
448448
* `getTokenOrCommentBefore()` - replaced by `getTokenBefore()` with the `{ includeComments: true }` option
449449
* `getTokenOrCommentAfter()` - replaced by `getTokenAfter()` with the `{ includeComments: true }` option
450+
* `isSpaceBetweenTokens()` - replaced by `isSpaceBetween()`
450451

451452
### Options Schemas
452453

lib/source-code/source-code.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ class SourceCode extends TokenStore {
432432
* any of the tokens found between the two given nodes or tokens.
433433
* @public
434434
*/
435-
isSpaceBetweenTokens(first, second) {
435+
isSpaceBetween(first, second) {
436436
if (nodesOrTokensOverlap(first, second)) {
437437
return false;
438438
}
@@ -457,6 +457,20 @@ class SourceCode extends TokenStore {
457457
return false;
458458
}
459459

460+
/**
461+
* Determines if two nodes or tokens have at least one whitespace character
462+
* between them. Order does not matter. Returns false if the given nodes or
463+
* tokens overlap.
464+
* @param {...ASTNode|Token} args The nodes or tokens to check between.
465+
* @returns {boolean} True if there is a whitespace character between
466+
* any of the tokens found between the two given nodes or tokens.
467+
* @deprecated in favor of isSpaceBetween().
468+
* @public
469+
*/
470+
isSpaceBetweenTokens(...args) {
471+
return this.isSpaceBetween(...args);
472+
}
473+
460474
/**
461475
* Converts a source text index into a (line, column) pair.
462476
* @param {number} index The index of a character in a file

tests/lib/source-code/source-code.js

Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,339 @@ describe("SourceCode", () => {
17891789
});
17901790
});
17911791

1792+
describe("isSpaceBetween()", () => {
1793+
describe("should return true when there is at least one whitespace character between two tokens", () => {
1794+
leche.withData([
1795+
["let foo", true],
1796+
["let foo", true],
1797+
["let /**/ foo", true],
1798+
["let/**/foo", false],
1799+
["let/*\n*/foo", false]
1800+
], (code, expected) => {
1801+
describe("when the first given is located before the second", () => {
1802+
it(code, () => {
1803+
const ast = espree.parse(code, DEFAULT_CONFIG),
1804+
sourceCode = new SourceCode(code, ast);
1805+
1806+
assert.strictEqual(
1807+
sourceCode.isSpaceBetween(
1808+
sourceCode.ast.tokens[0],
1809+
sourceCode.ast.tokens[sourceCode.ast.tokens.length - 1]
1810+
),
1811+
expected
1812+
);
1813+
});
1814+
});
1815+
1816+
describe("when the first given is located after the second", () => {
1817+
it(code, () => {
1818+
const ast = espree.parse(code, DEFAULT_CONFIG),
1819+
sourceCode = new SourceCode(code, ast);
1820+
1821+
assert.strictEqual(
1822+
sourceCode.isSpaceBetween(
1823+
sourceCode.ast.tokens[sourceCode.ast.tokens.length - 1],
1824+
sourceCode.ast.tokens[0]
1825+
),
1826+
expected
1827+
);
1828+
});
1829+
});
1830+
});
1831+
1832+
leche.withData([
1833+
["a+b", false],
1834+
["a +b", true],
1835+
["a/**/+b", false],
1836+
["a/* */+b", false],
1837+
["a/**/ +b", true],
1838+
["a/**/ /**/+b", true],
1839+
["a/* */ /* */+b", true],
1840+
["a/**/\n/**/+b", true],
1841+
["a/* */\n/* */+b", true],
1842+
["a/**/+b/**/+c", false],
1843+
["a/* */+b/* */+c", false],
1844+
["a/**/+b /**/+c", true],
1845+
["a/* */+b /* */+c", true],
1846+
["a/**/ +b/**/+c", true],
1847+
["a/* */ +b/* */+c", true],
1848+
["a/**/+b\t/**/+c", true],
1849+
["a/* */+b\t/* */+c", true],
1850+
["a/**/\t+b/**/+c", true],
1851+
["a/* */\t+b/* */+c", true],
1852+
["a/**/+b\n/**/+c", true],
1853+
["a/* */+b\n/* */+c", true],
1854+
["a/**/\n+b/**/+c", true],
1855+
["a/* */\n+b/* */+c", true],
1856+
["a/* */+' /**/ '/* */+c", false],
1857+
["a/* */+ ' /**/ '/* */+c", true],
1858+
["a/* */+' /**/ ' /* */+c", true],
1859+
["a/* */+ ' /**/ ' /* */+c", true],
1860+
["a/* */+` /*\n*/ `/* */+c", false],
1861+
["a/* */+ ` /*\n*/ `/* */+c", true],
1862+
["a/* */+` /*\n*/ ` /* */+c", true],
1863+
["a/* */+ ` /*\n*/ ` /* */+c", true]
1864+
], (code, expected) => {
1865+
describe("when the first given is located before the second", () => {
1866+
it(code, () => {
1867+
const ast = espree.parse(code, DEFAULT_CONFIG),
1868+
sourceCode = new SourceCode(code, ast);
1869+
1870+
assert.strictEqual(
1871+
sourceCode.isSpaceBetween(
1872+
sourceCode.ast.tokens[0],
1873+
sourceCode.ast.tokens[sourceCode.ast.tokens.length - 2]
1874+
),
1875+
expected
1876+
);
1877+
});
1878+
});
1879+
1880+
describe("when the first given is located after the second", () => {
1881+
it(code, () => {
1882+
const ast = espree.parse(code, DEFAULT_CONFIG),
1883+
sourceCode = new SourceCode(code, ast);
1884+
1885+
assert.strictEqual(
1886+
sourceCode.isSpaceBetween(
1887+
sourceCode.ast.tokens[sourceCode.ast.tokens.length - 2],
1888+
sourceCode.ast.tokens[0]
1889+
),
1890+
expected
1891+
);
1892+
});
1893+
});
1894+
});
1895+
});
1896+
1897+
describe("should return true when there is at least one whitespace character between a token and a node", () => {
1898+
leche.withData([
1899+
[";let foo = bar", false],
1900+
[";/**/let foo = bar", false],
1901+
[";/* */let foo = bar", false],
1902+
["; let foo = bar", true],
1903+
["; let foo = bar", true],
1904+
["; /**/let foo = bar", true],
1905+
["; /* */let foo = bar", true],
1906+
[";/**/ let foo = bar", true],
1907+
[";/* */ let foo = bar", true],
1908+
["; /**/ let foo = bar", true],
1909+
["; /* */ let foo = bar", true],
1910+
[";\tlet foo = bar", true],
1911+
[";\tlet foo = bar", true],
1912+
[";\t/**/let foo = bar", true],
1913+
[";\t/* */let foo = bar", true],
1914+
[";/**/\tlet foo = bar", true],
1915+
[";/* */\tlet foo = bar", true],
1916+
[";\t/**/\tlet foo = bar", true],
1917+
[";\t/* */\tlet foo = bar", true],
1918+
[";\nlet foo = bar", true],
1919+
[";\nlet foo = bar", true],
1920+
[";\n/**/let foo = bar", true],
1921+
[";\n/* */let foo = bar", true],
1922+
[";/**/\nlet foo = bar", true],
1923+
[";/* */\nlet foo = bar", true],
1924+
[";\n/**/\nlet foo = bar", true],
1925+
[";\n/* */\nlet foo = bar", true]
1926+
], (code, expected) => {
1927+
describe("when the first given is located before the second", () => {
1928+
it(code, () => {
1929+
const ast = espree.parse(code, DEFAULT_CONFIG),
1930+
sourceCode = new SourceCode(code, ast);
1931+
1932+
assert.strictEqual(
1933+
sourceCode.isSpaceBetween(
1934+
sourceCode.ast.tokens[0],
1935+
sourceCode.ast.body[sourceCode.ast.body.length - 1]
1936+
),
1937+
expected
1938+
);
1939+
});
1940+
});
1941+
1942+
describe("when the first given is located after the second", () => {
1943+
it(code, () => {
1944+
const ast = espree.parse(code, DEFAULT_CONFIG),
1945+
sourceCode = new SourceCode(code, ast);
1946+
1947+
assert.strictEqual(
1948+
sourceCode.isSpaceBetween(
1949+
sourceCode.ast.body[sourceCode.ast.body.length - 1],
1950+
sourceCode.ast.tokens[0]
1951+
),
1952+
expected
1953+
);
1954+
});
1955+
});
1956+
});
1957+
});
1958+
1959+
describe("should return true when there is at least one whitespace character between a node and a token", () => {
1960+
leche.withData([
1961+
["let foo = bar;;", false],
1962+
["let foo = bar;;;", false],
1963+
["let foo = 1; let bar = 2;;", true],
1964+
["let foo = bar;/**/;", false],
1965+
["let foo = bar;/* */;", false],
1966+
["let foo = bar;;;", false],
1967+
["let foo = bar; ;", true],
1968+
["let foo = bar; /**/;", true],
1969+
["let foo = bar; /* */;", true],
1970+
["let foo = bar;/**/ ;", true],
1971+
["let foo = bar;/* */ ;", true],
1972+
["let foo = bar; /**/ ;", true],
1973+
["let foo = bar; /* */ ;", true],
1974+
["let foo = bar;\t;", true],
1975+
["let foo = bar;\t/**/;", true],
1976+
["let foo = bar;\t/* */;", true],
1977+
["let foo = bar;/**/\t;", true],
1978+
["let foo = bar;/* */\t;", true],
1979+
["let foo = bar;\t/**/\t;", true],
1980+
["let foo = bar;\t/* */\t;", true],
1981+
["let foo = bar;\n;", true],
1982+
["let foo = bar;\n/**/;", true],
1983+
["let foo = bar;\n/* */;", true],
1984+
["let foo = bar;/**/\n;", true],
1985+
["let foo = bar;/* */\n;", true],
1986+
["let foo = bar;\n/**/\n;", true],
1987+
["let foo = bar;\n/* */\n;", true]
1988+
], (code, expected) => {
1989+
describe("when the first given is located before the second", () => {
1990+
it(code, () => {
1991+
const ast = espree.parse(code, DEFAULT_CONFIG),
1992+
sourceCode = new SourceCode(code, ast);
1993+
1994+
assert.strictEqual(
1995+
sourceCode.isSpaceBetween(
1996+
sourceCode.ast.body[0],
1997+
sourceCode.ast.tokens[sourceCode.ast.tokens.length - 1]
1998+
),
1999+
expected
2000+
);
2001+
});
2002+
});
2003+
2004+
describe("when the first given is located after the second", () => {
2005+
it(code, () => {
2006+
const ast = espree.parse(code, DEFAULT_CONFIG),
2007+
sourceCode = new SourceCode(code, ast);
2008+
2009+
assert.strictEqual(
2010+
sourceCode.isSpaceBetween(
2011+
sourceCode.ast.tokens[sourceCode.ast.tokens.length - 1],
2012+
sourceCode.ast.body[0]
2013+
),
2014+
expected
2015+
);
2016+
});
2017+
});
2018+
});
2019+
});
2020+
2021+
describe("should return true when there is at least one whitespace character between two nodes", () => {
2022+
leche.withData([
2023+
["let foo = bar;let baz = qux;", false],
2024+
["let foo = bar;/**/let baz = qux;", false],
2025+
["let foo = bar;/* */let baz = qux;", false],
2026+
["let foo = bar; let baz = qux;", true],
2027+
["let foo = bar; /**/let baz = qux;", true],
2028+
["let foo = bar; /* */let baz = qux;", true],
2029+
["let foo = bar;/**/ let baz = qux;", true],
2030+
["let foo = bar;/* */ let baz = qux;", true],
2031+
["let foo = bar; /**/ let baz = qux;", true],
2032+
["let foo = bar; /* */ let baz = qux;", true],
2033+
["let foo = bar;\tlet baz = qux;", true],
2034+
["let foo = bar;\t/**/let baz = qux;", true],
2035+
["let foo = bar;\t/* */let baz = qux;", true],
2036+
["let foo = bar;/**/\tlet baz = qux;", true],
2037+
["let foo = bar;/* */\tlet baz = qux;", true],
2038+
["let foo = bar;\t/**/\tlet baz = qux;", true],
2039+
["let foo = bar;\t/* */\tlet baz = qux;", true],
2040+
["let foo = bar;\nlet baz = qux;", true],
2041+
["let foo = bar;\n/**/let baz = qux;", true],
2042+
["let foo = bar;\n/* */let baz = qux;", true],
2043+
["let foo = bar;/**/\nlet baz = qux;", true],
2044+
["let foo = bar;/* */\nlet baz = qux;", true],
2045+
["let foo = bar;\n/**/\nlet baz = qux;", true],
2046+
["let foo = bar;\n/* */\nlet baz = qux;", true],
2047+
["let foo = 1;let foo2 = 2; let foo3 = 3;", true]
2048+
], (code, expected) => {
2049+
describe("when the first given is located before the second", () => {
2050+
it(code, () => {
2051+
const ast = espree.parse(code, DEFAULT_CONFIG),
2052+
sourceCode = new SourceCode(code, ast);
2053+
2054+
assert.strictEqual(
2055+
sourceCode.isSpaceBetween(
2056+
sourceCode.ast.body[0],
2057+
sourceCode.ast.body[sourceCode.ast.body.length - 1]
2058+
),
2059+
expected
2060+
);
2061+
});
2062+
});
2063+
2064+
describe("when the first given is located after the second", () => {
2065+
it(code, () => {
2066+
const ast = espree.parse(code, DEFAULT_CONFIG),
2067+
sourceCode = new SourceCode(code, ast);
2068+
2069+
assert.strictEqual(
2070+
sourceCode.isSpaceBetween(
2071+
sourceCode.ast.body[sourceCode.ast.body.length - 1],
2072+
sourceCode.ast.body[0]
2073+
),
2074+
expected
2075+
);
2076+
});
2077+
});
2078+
});
2079+
});
2080+
2081+
describe("should return false either of the arguments' location is inside the other one", () => {
2082+
leche.withData([
2083+
["let foo = bar;", false]
2084+
], (code, expected) => {
2085+
it(code, () => {
2086+
const ast = espree.parse(code, DEFAULT_CONFIG),
2087+
sourceCode = new SourceCode(code, ast);
2088+
2089+
assert.strictEqual(
2090+
sourceCode.isSpaceBetween(
2091+
sourceCode.ast.tokens[0],
2092+
sourceCode.ast.body[0]
2093+
),
2094+
expected
2095+
);
2096+
2097+
assert.strictEqual(
2098+
sourceCode.isSpaceBetween(
2099+
sourceCode.ast.tokens[sourceCode.ast.tokens.length - 1],
2100+
sourceCode.ast.body[0]
2101+
),
2102+
expected
2103+
);
2104+
2105+
assert.strictEqual(
2106+
sourceCode.isSpaceBetween(
2107+
sourceCode.ast.body[0],
2108+
sourceCode.ast.tokens[0]
2109+
),
2110+
expected
2111+
);
2112+
2113+
assert.strictEqual(
2114+
sourceCode.isSpaceBetween(
2115+
sourceCode.ast.body[0],
2116+
sourceCode.ast.tokens[sourceCode.ast.tokens.length - 1]
2117+
),
2118+
expected
2119+
);
2120+
});
2121+
});
2122+
});
2123+
});
2124+
17922125
describe("isSpaceBetweenTokens()", () => {
17932126
describe("should return true when there is at least one whitespace character between two tokens", () => {
17942127
leche.withData([

0 commit comments

Comments
 (0)