This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+60
View File
@@ -0,0 +1,60 @@
/**
* Copyright 2020 Google LLC
*
* 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.
*/
import { separatorRegex } from './skip';
import parseNamedImports from './parse-named-imports';
import parseNamespaceImport from './parse-namespace-import';
import parseDefaultImport from './parse-default-import'; // Assumes import clause is syntactically valid
const parseImportClause = importClauseString => {
let defaultImport;
let namespaceImport;
const namedImports = [];
for (let i = 0; i < importClauseString.length; i++) {
if (separatorRegex.test(importClauseString[i])) {
continue;
}
if (importClauseString[i] === `{`) {
let newNamedImports;
({
namedImports: newNamedImports,
i
} = parseNamedImports(importClauseString, i));
namedImports.push(...newNamedImports);
} else if (importClauseString[i] === `*`) {
;
({
namespaceImport,
i
} = parseNamespaceImport(importClauseString, i));
} else {
;
({
defaultImport,
i
} = parseDefaultImport(importClauseString, i));
}
}
return {
default: defaultImport,
namespace: namespaceImport,
named: namedImports
};
};
export default parseImportClause;
@@ -0,0 +1,27 @@
/**
* Copyright 2020 Google LLC
*
* 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.
*/
import { skipNonSeparators } from './skip';
const parseDefaultImport = (importClauseString, i) => {
const startIndex = i;
i = skipNonSeparators(importClauseString, i);
return {
defaultImport: importClauseString.substring(startIndex, i),
i
};
};
export default parseDefaultImport;
@@ -0,0 +1,47 @@
/**
* Copyright 2020 Google LLC
*
* 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.
*/
const parseNamedImports = (importClauseString, i) => {
const startIndex = ++i;
while (i < importClauseString.length && importClauseString[i] !== `}`) {
i++;
}
const namedImports = importClauseString.substring(startIndex, i++).split(`,`).map(namedImport => {
namedImport = namedImport.trim();
if (namedImport.includes(` `)) {
const components = namedImport.split(` `);
return {
specifier: components[0],
binding: components[components.length - 1]
};
}
return {
specifier: namedImport,
binding: namedImport
};
}).filter(({
specifier
}) => specifier.length > 0);
return {
namedImports,
i
};
};
export default parseNamedImports;
@@ -0,0 +1,31 @@
/**
* Copyright 2020 Google LLC
*
* 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.
*/
import { skipNonSeparators, skipSeparators } from './skip';
const parseNamespaceImport = (importClauseString, i) => {
i++;
i = skipSeparators(importClauseString, i);
i += `as`.length;
i = skipSeparators(importClauseString, i);
const startIndex = i;
i = skipNonSeparators(importClauseString, i);
return {
namespaceImport: importClauseString.substring(startIndex, i),
i
};
};
export default parseNamespaceImport;
+30
View File
@@ -0,0 +1,30 @@
/**
* Copyright 2020 Google LLC
*
* 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.
*/
export const separatorRegex = /^(?:\s+|,)$/u;
export const skipSeparators = (imported, i) => {
while (i < imported.length && separatorRegex.test(imported[i])) {
i++;
}
return i;
};
export const skipNonSeparators = (imported, i) => {
while (i < imported.length && !separatorRegex.test(imported[i])) {
i++;
}
return i;
};